use App\Models\FluidbookDocument;
use App\Models\FluidbookPublication;
use App\Models\FluidbookTheme;
+use App\Models\FluidbookTranslate;
use Cubist\Backpack\Console\Commands\CubistCommand;
use Cubist\Backpack\Magic\Fields\Color;
use Cubist\Backpack\Magic\Fields\Datetime;
use Cubist\Util\ArrayUtil;
+use Cubist\Util\Json;
use Cubist\Util\ObjectUtil;
use Cubist\Util\PHP;
use Illuminate\Contracts\Cache\Lock;
class WorkshopMigration extends CubistCommand
{
- protected $signature = 'ws:migrate {--publications=v2} {--documents=missing} {--quick}';
+ protected $signature = 'ws:migrate {--publications=v2} {--documents=missing} {--quick} {--force}';
protected $description = 'Migrate data from Workshop V2';
protected $_wsRanks = [];
const OLD_DB = 'extranet_clean';
public function handle()
{
$this->_lock = Cache::lock('ws2_migrate', 3600);
- if (!$this->_lock->get()) {
+
+ if (!$this->option('force') && !$this->_lock->get()) {
+ $this->line('Migration is locked. Use --force to remove lock');
return;
}
DB::update('DELETE FROM fluidbook_document WHERE id<' . self::WS3_DOC_ID);
} else {
$range = ArrayUtil::parseRange($this->option('documents'));
- FluidbookDocument::withoutGlobalScopes()->whereIn('id', $range)->delete();
+ FluidbookDocument::withoutGlobalScopes()->whereIn('id', $range)->forceDelete();
$q->whereIn('document_id', $range);
}
$q->where('book_id', '>', $f->id);
} else {
$range = ArrayUtil::parseRange($this->option('publications'));
- FluidbookPublication::withoutGlobalScopes()->where('version', '<=', 2)->whereIn('id', $range)->delete();
+ FluidbookPublication::withoutGlobalScopes()->where('version', '<=', 2)->whereIn('id', $range)->forceDelete();
$q->whereIn('book_id', $range);
}
$v = $date;
} else if ($k == 'chapters') {
$v = $this->_normalizeChapters($v);
+ } else if ($new == 'translations') {
+ $v = $this->_normalizeTranslations($v, $e->lang);
+ } else if (Json::isJson($v)) {
+ $v = json_decode($v, true);
}
$c->setAttribute($new, $v);
}
$date = new \DateTime();
$date->setTimestamp($data);
$data = $date;
+ } else if (Json::isJson($data)) {
+ $data = json_decode($data, true);
}
if (isset($mapValues[$data])) {
$data = $mapValues[$data];
}
}
+ protected function _normalizeTranslations($value, $lang)
+ {
+ if (!is_string($value) || !Json::isJson($value)) {
+ return [];
+ }
+ $translations = json_decode($value);
+ $res = [];
+
+ $l10n = FluidbookTranslate::getCompiledTranslations()[$lang];
+
+ foreach ($translations as $base => $trans) {
+ if (!isset($l10n[$base]) || !$trans || $l10n[$base] === $trans) {
+
+ } else {
+ $res[$base] = $trans;
+ }
+ }
+
+ return $res;
+ }
+
public function _normalizeChapters($value)
{
if (!$value) {