From: Vincent Vanwaelscappel Date: Fri, 31 Mar 2023 08:51:53 +0000 (+0200) Subject: wip #5842 @1.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=a1e098178a228e7e61ba4e70d15c5f9309c07a27;p=fluidbook-toolbox.git wip #5842 @1.5 --- diff --git a/app/Jobs/FluidbookCollectionDownload.php b/app/Jobs/FluidbookCollectionDownload.php index 0ad90dde9..281fb602d 100644 --- a/app/Jobs/FluidbookCollectionDownload.php +++ b/app/Jobs/FluidbookCollectionDownload.php @@ -2,10 +2,9 @@ namespace App\Jobs; +use App\Console\Commands\WorkshopMigration; use App\Fluidbook\Packager\Packager; use App\Models\FluidbookPublication; -use App\Models\User; -use App\Services\WorkshopV2; use Cubist\Backpack\Magic\PageData; use Cubist\Util\CommandLine\Rsync; use Cubist\Util\Files\Files; @@ -57,9 +56,9 @@ class FluidbookCollectionDownload extends DownloadBase PHP::neverStop(); if ($data->type === 'scorm_multilang') { - $res = $this->compileSCORMMultilang($data, $path); + $res = $this->compileMultilang($data, $path, true); } else if ($data->type === 'export_multilang') { - $res = $this->compileMultilang($data, $path); + $res = $this->compileMultilang($data, $path, false); } elseif ($data->type === 'export') { $res = $this->compileExport($data, $path); } @@ -79,95 +78,6 @@ class FluidbookCollectionDownload extends DownloadBase return $options; } - /** - * @param $data PageData - * @param $path string - * @return void - * @throws \Exception - */ - - protected function compileSCORMMultilang($data, $path) - { - $ws = $this->_getws2(); - $langs = []; - $countries = []; - - $defaultFlags = ['en' => 'gb', 'sv' => 'se']; - - foreach ($data->publications as $publication) { - $fbid = $publication['fluidbook']; - $metadata = $ws->getMetadata($fbid); - $langs[$fbid] = $metadata->lang; - $countries[$fbid] = $metadata->settings->country; - } - - $options = $this->getCollectionGlobalSettings(); - $options['scorm_enable'] = true; - - - if (count($data->publications) >= 2) { - if ($data->locale_switch) { - $multilang = []; - foreach ($langs as $fbid => $lang) { - if ($countries[$fbid]) { - $flag = $countries[$fbid]; - } else { - if (strlen($lang) === 5) { - $flag = substr($lang, 3, 2); - } else if (isset($defaultFlags[$lang])) { - $flag = $defaultFlags[$lang]; - } else { - $flag = $lang; - } - } - $multilang[] = $lang . ',' . strtolower($flag) . ',../' . $lang . '/index.html'; - } - $options['multilang'] = implode("\n", $multilang); - $options['multilangDisplay'] = 'lang'; - } else { - $options['multilang'] = ''; - } - - foreach ($data->publications as $publication) { - $fbid = $publication['fluidbook']; - $lang = $langs[$fbid]; - $dir = $path . '/' . $lang; - $ws->installBook($fbid, $dir, $options, 'scorm', 3); - } - - if (in_array('en', $langs)) { - $default = 'en'; - } else { - $default = $langs[0]; - } - - $manifestFile = $path . '/imsmanifest.xml'; - foreach ($langs as $lang) { - $manifest = $path . '/' . $lang . '/imsmanifest.xml'; - if ($lang === $default) { - rename($manifest, $manifestFile); - } else if (file_exists($manifest)) { - unlink($manifest); - } - } - - $manifestContent = file_get_contents($manifestFile); - $manifestContent = preg_replace('/\(.*)\<\/title\>/U', '' . htmlspecialchars($this->entry->title) . '', $manifestContent); - file_put_contents($manifestFile, $manifestContent); - - $redirectionScript = self::getRedirectScript($langs, $default); - - file_put_contents($path . '/index.html', $redirectionScript); - } else { - foreach ($data->publications as $publication) { - $fbid = $publication['fluidbook']; - $dir = $path; - $ws->installBook($fbid, $dir, $options, 'scorm', 3); - break; - } - } - } - /** * @param $data PageData * @param $path string @@ -176,7 +86,6 @@ class FluidbookCollectionDownload extends DownloadBase */ protected function compileMultilang($data, $path, $scorm = false) { - $ws = $this->_getws2(); $langs = []; $countries = []; @@ -187,14 +96,12 @@ class FluidbookCollectionDownload extends DownloadBase foreach ($data->publications as $publication) { $fbid = $publication['fluidbook']; $fluidbooks[$fbid] = FluidbookPublication::find($fbid); - $langs[$fbid] = $fluidbooks[$fbid]->lang; + $langs[$fbid] = $fluidbooks[$fbid]->locale; $countries[$fbid] = $fluidbooks[$fbid]->country; } $options = $this->getCollectionGlobalSettings(); - if ($scorm) { - $options['scorm_enable'] = true; - } + $options['scorm_enable'] = $scorm; if ($data->locale_switch) { $multilang = []; @@ -218,9 +125,11 @@ class FluidbookCollectionDownload extends DownloadBase $options['multilang'] = ''; } + $jobs = []; foreach ($fluidbooks as $fbid => $publication) { - $this->package($fbid, $scorm ? 'scorm' : $data->version, $options, $path . '/' . $langs[$fbid]); + $jobs[] = $this->_jobDownloadFluidbook($fbid, $scorm ? 'scorm' : $data->version, $path . $langs[$fbid], $options); } + $this->_waitJobs($jobs); if (in_array('en', $langs)) { $default = 'en'; @@ -229,9 +138,9 @@ class FluidbookCollectionDownload extends DownloadBase } if ($scorm) { - $manifestFile = $path . '/imsmanifest.xml'; + $manifestFile = $path . 'imsmanifest.xml'; foreach ($langs as $lang) { - $manifest = $path . '/' . $lang . '/imsmanifest.xml'; + $manifest = $path . $lang . '/imsmanifest.xml'; if ($lang === $default) { rename($manifest, $manifestFile); } else if (file_exists($manifest)) { @@ -256,7 +165,6 @@ class FluidbookCollectionDownload extends DownloadBase protected function downloadList($data) { - $ws = $this->_getws2(); $options = $this->getCollectionGlobalSettings(); $jobs = []; @@ -268,36 +176,62 @@ class FluidbookCollectionDownload extends DownloadBase if ($publication['export']) { $name = $publication['export']; } else { - $metadata = $ws->getMetadata($fbid); - $name = Str::slug($metadata->title); + $name = Str::slug($this->_getFluidbookTitle($fbid)); } $name .= '.exe'; - $job = new FluidbookWS2Download(); - $job->setBookId($fbid); - $job->setVersion($this->entry->version); - $job->setOptions($options); - $job->setAction('export'); - $job->setJobName('export_' . $fbid); - $job->setDestination($compilepath . '/' . $name); - $u = backpack_user() ?? User::withoutGlobalScopes()->find(5); - $job->setCredentials([$u->email, $u->api_token]); - dispatch($job)->onQueue('ws2'); - //$job->handle(); + $job = $this->_jobDownloadFluidbook($fbid, $this->entry->version, $compilepath . $name, $options); $jobs['Fluidbook #' . $fbid] = $job; $res['Fluidbook #' . $fbid] = url('fluidbook-collection/' . $this->entry->id . '/downloadfile/' . $rand . '/' . $name); } - while (!$this->_checkJobs($jobs)) { - usleep(1000000 * 0.25); - } + $this->_waitJobs($jobs); + return $res; } + protected function _getFluidbookTitle($id) + { + return FluidbookPublication::find($id)->c_title; + } + + protected function _jobDownloadFluidbook($id, $version, $dest, $options = [], $async = true, $compilerVersion = 'auto') + { + if ($compilerVersion === 'auto') { + $compilerVersion = ($id < WorkshopMigration::WS3_BOOK_ID) ? 2 : 3; + } + /** @var $job FluidbookPackage */ + if ($compilerVersion === 2) { + $job = new FluidbookWS2Download(); + $job->setCredentials([$this->user->email, $this->user->api_token]); + } else if ($compilerVersion === 3) { + $job = new FluidbookPackage(); + } + $job->setBookId($id); + $job->setVersion($version); + $job->setOptions($options); + $job->setAction('export'); + $job->setJobName('export_' . $id); + $job->setDestination($dest); + $f = $async ? 'dispatch' : 'dispatchSync'; + $f($job)->onQueue('ws2'); + return $job; + } + + /** + * @param $jobs FluidbookPackage[] + * @return void + */ + protected function _waitJobs($jobs) + { + while (!$this->_checkJobs($jobs)) { + usleep(1000000 * 0.25); + } + } /** - * @param $jobs FluidbookWS2Download[] + * @param $jobs FluidbookPackage[] * @return bool */ protected function _checkJobs($jobs) @@ -311,18 +245,10 @@ class FluidbookCollectionDownload extends DownloadBase } if (rand(1, 10) == 5) { echo $done . '/' . $nbjobs . "\n"; - } return $done === $nbjobs; } - protected function _getws2() - { - $ws = new WorkshopV2(); - $ws->login($this->user->email, $this->user->api_token); - return $ws; - } - /** * @param $data PageData * @param $path string @@ -331,30 +257,24 @@ class FluidbookCollectionDownload extends DownloadBase */ protected function compileExport($data, $path) { - $ws = $this->_getws2(); - $options = $this->getCollectionGlobalSettings(); - $zipmerge = in_array($data->version, ['online', 'scorm', 'sharepoint', 'precompiled', 'win_exe_html', 'win_html', 'win_cd_html', 'mac_exe_html']); + $jobs = []; foreach ($data->publications as $publication) { $fbid = $publication['fluidbook']; - $metadata = $ws->getMetadata($fbid); - - $dir = $path . '/'; + $dest = $path; if ($publication['export']) { - $dir .= $publication['export']; + $dest .= $publication['export']; } else { - $dir .= Str::slug($metadata->title); + $dest .= Str::slug($this->_getFluidbookTitle($fbid)); } if (!$zipmerge) { - $dir .= '.exe'; - } - if ($zipmerge) { - $ws->installBook($fbid, $dir, $options, $data->version, 3); - } else { - $ws->downloadBookExport($fbid, $dir, $options, $data->version, 3); + $dest .= '.exe'; } + + $jobs[] = $this->_jobDownloadFluidbook($fbid, $data->version, $dest, $options); } + $this->_waitJobs($jobs); } diff --git a/app/Jobs/FluidbookPackage.php b/app/Jobs/FluidbookPackage.php new file mode 100644 index 000000000..b2bb5a084 --- /dev/null +++ b/app/Jobs/FluidbookPackage.php @@ -0,0 +1,144 @@ +getBookId(), $this->getVersion(), false, true, $this->getOptions()); + $this->setResult($packager->getFinalPath()); + } catch (\Exception $e) { + $this->setException($e); + } + $this->setDone(true); + } + + /** + * @param int|string $bookId + */ + public function setBookId(int|string $bookId): void + { + $this->_bookId = $bookId; + } + + /** + * @return int|string + */ + public function getBookId(): int|string + { + return $this->_bookId; + } + + /** + * @return string + */ + public function getDestination(): string + { + return $this->_destination; + } + + /** + * @param string $destination + */ + public function setDestination(string $destination): void + { + $this->_destination = $destination; + } + + /** + * @return string + */ + public function getVersion(): string + { + return $this->_version; + } + + /** + * @param string $version + */ + public function setVersion(string $version): void + { + $this->_version = $version; + } + + /** + * @param array $options + */ + public function setOptions(array $options): void + { + $this->_options = $options; + } + + /** + * @return array + */ + public function getOptions(): array + { + return $this->_options; + } + + /** + * @return mixed + */ + public function getResult(): mixed + { + return $this->_getState('result', null); + } + + /** + * @param mixed $result + */ + public function setResult(mixed $result): void + { + $this->_setState('result', $result); + } + + /** + * @return bool + */ + public function isDone(): bool + { + return $this->_getState('done', false); + } + + /** + * @param bool $done + */ + public function setDone(bool $done): void + { + $this->_setState('done', $done); + } + + /** + * @return \Exception|null + */ + public function getException(): \Exception|null + { + return $this->_getState('exception', null); + } + + /** + * @param \Exception|null $exception + */ + public function setException(\Exception|null $exception): void + { + Log::error($exception); + $this->_setState('exception', $exception); + } + + +} diff --git a/app/Jobs/FluidbookWS2Download.php b/app/Jobs/FluidbookWS2Download.php index 9891ef238..41e90845d 100644 --- a/app/Jobs/FluidbookWS2Download.php +++ b/app/Jobs/FluidbookWS2Download.php @@ -2,70 +2,29 @@ namespace App\Jobs; -use App\Models\User; use App\Services\WorkshopV2; -use Illuminate\Support\Facades\Cache; -class FluidbookWS2Download extends Base +class FluidbookWS2Download extends FluidbookPackage { - protected int|string $_bookId; - protected string $_destination; - protected string $_version; - protected int $_tries = 1; - protected string $_action; - protected array $_options = []; - protected mixed $_result; - protected bool $_done = false; - protected \Exception|null $_exception; - protected array $_credentials; - - - /** - * @param int|string $bookId - */ - public function setBookId(int|string $bookId): void - { - $this->_bookId = $bookId; - } - /** - * @return int|string - */ - public function getBookId(): int|string - { - return $this->_bookId; - } - - /** - * @return string - */ - public function getDestination(): string - { - return $this->_destination; - } - - /** - * @param string $destination - */ - public function setDestination(string $destination): void - { - $this->_destination = $destination; - } - - /** - * @return string - */ - public function getVersion(): string - { - return $this->_version; - } + protected array $_credentials; + protected int $_tries = 1; - /** - * @param string $version - */ - public function setVersion(string $version): void + public function handle() { - $this->_version = $version; + try { + $ws = new WorkshopV2(); + $ws->login($this->getCredentials()[0], $this->getCredentials()[1]); + if (in_array($this->getVersion(), ['win_ins_html', 'win_inss_html'])) { + $res = $ws->downloadBookExport($this->getBookId(), $this->getDestination(), $this->getOptions(), $this->getVersion(), $this->getTries()); + } else { + $res = $ws->installBook($this->getBookId(), $this->getDestination(), $this->getOptions(), $this->getVersion(), $this->getTries()); + } + $this->setResult($res); + } catch (\Exception $e) { + $this->setException($e); + } + $this->setDone(true); } /** @@ -84,86 +43,6 @@ class FluidbookWS2Download extends Base $this->_tries = $tries; } - /** - * @return string - */ - public function getAction(): string - { - return $this->_action; - } - - /** - * @param string $action - */ - public function setAction(string $action): void - { - $this->_action = $action; - } - - /** - * @param array $options - */ - public function setOptions(array $options): void - { - $this->_options = $options; - } - - /** - * @return array - */ - public function getOptions(): array - { - return $this->_options; - } - - /** - * @return mixed - */ - public function getResult(): mixed - { - return $this->_getState('result', null); - } - - /** - * @param mixed $result - */ - public function setResult(mixed $result): void - { - $this->_setState('result', $result); - } - - /** - * @return bool - */ - public function isDone(): bool - { - return $this->_getState('done', false); - } - - /** - * @param bool $done - */ - public function setDone(bool $done): void - { - $this->_setState('done', $done); - } - - /** - * @return \Exception|null - */ - public function getException(): \Exception|null - { - return $this->_getState('exception', null); - } - - /** - * @param \Exception|null $exception - */ - public function setException(\Exception|null $exception): void - { - $this->_setState('exception', $exception); - } - /** * @return array */ @@ -180,24 +59,4 @@ class FluidbookWS2Download extends Base $this->_credentials = $credentials; } - - public function handle() - { - try { - $ws = new WorkshopV2(); - $ws->login($this->getCredentials()[0], $this->getCredentials()[1]); - if ($this->getAction() === 'install_hosting') { - $res = $ws->installBookOnHosting($this->getBookId(), $this->getOptions(), 'online', $this->getTries()); - } else if ($this->getAction() === 'export') { - if (in_array($this->getVersion(), ['win_ins_html', 'win_inss_html'])) { - $res = $ws->downloadBookExport($this->getBookId(), $this->getDestination(), $this->getOptions(), $this->getVersion(), $this->getTries()); - } - } - $this->setResult($res); - } catch (\Exception $e) { - $this->setException($e); - } - $this->setDone(true); - } - } diff --git a/app/Models/User.php b/app/Models/User.php index 909a9b8a2..baf979eab 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -16,5 +16,4 @@ class User extends \Cubedesigners\UserDatabase\Models\User $query->orderBy('id', 'ASC'); } - } diff --git a/scripts/restartworkers b/scripts/restartworkers index fcf8b9b61..5153c7170 100644 --- a/scripts/restartworkers +++ b/scripts/restartworkers @@ -2,3 +2,5 @@ sudo /usr/sbin/service supervisor stop sudo pkill -9 -f queue:work sudo /usr/sbin/service supervisor start +sleep 2 +sudo /usr/sbin/service supervisor restart