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;
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);
}
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\>(.*)\<\/title\>/U', '<title>' . htmlspecialchars($this->entry->title) . '</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
*/
protected function compileMultilang($data, $path, $scorm = false)
{
- $ws = $this->_getws2();
$langs = [];
$countries = [];
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 = [];
$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';
}
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)) {
protected function downloadList($data)
{
- $ws = $this->_getws2();
$options = $this->getCollectionGlobalSettings();
$jobs = [];
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)
}
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
*/
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);
}
--- /dev/null
+<?php
+
+namespace App\Jobs;
+
+use App\Fluidbook\Packager\Packager;
+use Illuminate\Support\Facades\Log;
+
+class FluidbookPackage extends Base
+{
+ protected int|string $_bookId;
+ protected string $_destination;
+ protected string $_version;
+ protected array $_options = [];
+ protected mixed $_result;
+ protected bool $_done = false;
+ protected \Exception|null $_exception;
+
+
+ public function handle()
+ {
+ try {
+ $packager = Packager::package($this->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);
+ }
+
+
+}
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);
}
/**
$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
*/
$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);
- }
-
}