From 2b0264a8f8b4592899e6da54738cf4dd700c7e08 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 30 Aug 2023 17:25:53 +0200 Subject: [PATCH] wait #6203 @4 --- app/Jobs/FluidbookCollectionDownload.php | 164 ++++++++++++++++------- app/Services/WorkshopV2.php | 12 +- 2 files changed, 126 insertions(+), 50 deletions(-) diff --git a/app/Jobs/FluidbookCollectionDownload.php b/app/Jobs/FluidbookCollectionDownload.php index 31d7d3fe7..943abbe5b 100644 --- a/app/Jobs/FluidbookCollectionDownload.php +++ b/app/Jobs/FluidbookCollectionDownload.php @@ -24,6 +24,11 @@ class FluidbookCollectionDownload extends DownloadBase // __('La collection ":title" est prêt au téléchargement'); protected $_text = 'La collection ":title" est prêt au téléchargement'; + protected $fluidbooks = []; + protected $dirs = []; + protected $langs = []; + protected $countries = []; + public function handle() { @@ -125,81 +130,82 @@ class FluidbookCollectionDownload extends DownloadBase */ protected function compileMultilang($data, $path, $scorm = false) { - $langs = []; - $countries = []; - - $defaultFlags = ['en' => 'gb', 'sv' => 'se', 'zh' => 'cn', 'el' => 'gr']; - - $fluidbooks = []; + $optionsFluidbooks = []; foreach ($data->publications as $publication) { $fbid = $publication['fluidbook']; - $fluidbooks[$fbid] = FluidbookPublication::syncPublicationFromWorkshop($fbid); - $langs[$fbid] = $fluidbooks[$fbid]->locale; - $countries[$fbid] = $fluidbooks[$fbid]->country; - $devices[$fbid] = $fluidbooks[$fbid]->mobileNavigationType === 'mobilefirst' ? 'mf' : 'd'; + $this->fluidbooks[$fbid] = FluidbookPublication::syncPublicationFromWorkshop($fbid); + $this->langs[$fbid] = $this->fluidbooks[$fbid]->locale; + $this->countries[$fbid] = $this->fluidbooks[$fbid]->country; + $this->devices[$fbid] = $this->fluidbooks[$fbid]->mobileNavigationType === 'mobilefirst' ? 'mf' : 'd'; + $optionsFluidbooks[$fbid] = []; } // We have at least one desktop and one mobile first - $multipleDevices = count(array_unique($devices)) > 1; + $multipleDevices = count(array_unique($this->devices)) > 1; $options = $this->getCollectionGlobalSettings(); $options['scorm_enable'] = $scorm; // We don't want an hybrid fluidbook in a collection $options['mobilefirstFluidbookId'] = ''; - // Switch is disabled for multidevices packages - if (!$multipleDevices && $data->locale_switch) { - $multilang = []; - foreach ($langs as $fbid => $lang) { - if ($countries[$fbid]) { - $flag = $countries[$fbid]; + // Get all available version and install directories + $versionsByLanguage = []; + $versionsByDevice = []; + $allVersions = []; + foreach ($this->fluidbooks as $fbid => $publication) { + $dir = $this->langs[$fbid]; + if ($multipleDevices) { + $dir .= '_' . $this->devices[$fbid]; + if (!isset($versionsByDevice[$this->devices[$fbid]])) { + $versionsByDevice[$this->devices[$fbid]] = []; + } + if (!isset($versionsByLanguage[$this->langs[$fbid]])) { + $versionsByLanguage[$this->langs[$fbid]] = []; + } + $versionsByDevice[$this->devices[$fbid]][$this->langs[$fbid]] = $dir; + $versionsByLanguage[$this->langs[$fbid]][$this->devices[$fbid]] = $dir; + } else { + $versionsByLanguage[$this->langs[$fbid]] = $dir; + } + $allVersions[] = $dir; + $this->dirs[$fbid] = $dir; + } + + if ($data->locale_switch) { + if (!$multipleDevices) { + $options['multilang'] = $this->_getMultilangOptions($this->fluidbooks); + } else { + if ($data->redirection_algorithm === 'language') { + foreach ($this->fluidbooks as $fbid => $publication) { + $optionsFluidbooks[$fbid]['multilang'] = $this->getLanguageMultilangOptions($fbid); + } } else { - if (strlen($lang) === 5) { - $flag = substr($lang, 3, 2); - } else if (isset($defaultFlags[$lang])) { - $flag = $defaultFlags[$lang]; - } else { - $flag = $lang; + foreach ($this->fluidbooks as $fbid => $publication) { + $optionsFluidbooks[$fbid]['multilang'] = $this->getDeviceMultilangOptions($fbid); } } - $multilang[] = $lang . ',' . strtolower($flag) . ',../' . $lang . '/index.html'; } - $options['multilang'] = implode("\n", $multilang); $options['multilangDisplay'] = 'lang'; } else { $options['multilang'] = ''; } $jobs = []; - $versionsByLanguage = []; - $versionsByDevice = []; - $allVersions = []; - foreach ($fluidbooks as $fbid => $publication) { - $dir = $langs[$fbid]; - if ($multipleDevices) { - $dir .= '_' . $devices[$fbid]; - if (!isset($versionsByDevice[$devices[$fbid]])) { - $versionsByDevice[$devices[$fbid]] = []; - } - if (!isset($versionsByLanguage[$langs[$fbid]])) { - $versionsByLanguage[$langs[$fbid]] = []; - } - $versionsByDevice[$devices[$fbid]][$langs[$fbid]] = $dir; - $versionsByLanguage[$langs[$fbid]][$devices[$fbid]] = $dir; - } else { - $versionsByLanguage[$langs[$fbid]] = $dir; + foreach ($this->fluidbooks as $fbid => $publication) { + $fbOptions = $options; + if (isset($optionsFluidbooks[$fbid])) { + $fbOptions += $optionsFluidbooks[$fbid]; } - $allVersions[] = $dir; - $jobs[] = $this->_jobDownloadFluidbook($fbid, $scorm ? 'scorm' : 'online', $path . $dir, $options); + $jobs[] = $this->_jobDownloadFluidbook($fbid, $scorm ? 'scorm' : 'online', $path . $this->dirs[$fbid], $fbOptions); } $this->_waitJobs($jobs); - if (in_array('en', $langs)) { + if (in_array('en', $this->langs)) { $default = 'en'; } else { - $default = $langs[0]; + $default = $this->langs[0]; } if ($scorm) { @@ -231,6 +237,70 @@ class FluidbookCollectionDownload extends DownloadBase file_put_contents($path . '/index.html', $redirectScript); } + + protected function getLanguageMultilangOptions($fbid) + { + $device = $this->devices[$fbid]; + $sameDevice = []; + $otherDevice = []; + foreach ($this->fluidbooks as $k => $v) { + if ($this->devices[$k] === $device) { + $sameDevice[$this->langs[$k]] = $k; + } else { + $otherDevice[$this->langs[$k]] = $k; + } + } + foreach ($otherDevice as $lang => $fbid) { + if (isset($sameDevice[$lang])) { + continue; + } + $sameDevice[$lang] = $fbid; + } + $fluidbooks = []; + foreach ($sameDevice as $lang => $fbid) { + $fluidbooks[$fbid] = $this->fluidbooks[$fbid]; + } + return $this->_getMultilangOptions($fluidbooks); + } + + protected function getDeviceMultilangOptions($fbid) + { + $device = $this->devices[$fbid]; + $fluidbooks = []; + foreach ($this->fluidbooks as $k => $v) { + if ($this->devices[$k] === $device) { + $fluidbooks[$k] = $v; + } + } + return $this->_getMultilangOptions($fluidbooks); + } + + protected function _getMultilangOptions($fluidbooks) + { + $defaultFlags = ['en' => 'gb', 'sv' => 'se', 'zh' => 'cn', 'el' => 'gr']; + + $multilang = []; + foreach ($fluidbooks as $fbid => $publication) { + $lang = $this->langs[$fbid]; + if ($this->countries[$fbid]) { + $flag = $this->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) . ',../' . $this->dirs[$fbid] . '/index.html'; + } + if (count($multilang) <= 1) { + return ''; + } + return implode("\n", $multilang); + } + protected function package($fbid, $version, $options, $path) { $packager = Packager::package($fbid, $version, false, true, $options); @@ -255,7 +325,7 @@ class FluidbookCollectionDownload extends DownloadBase } $name .= '.exe'; - $job = $this->_jobDownloadFluidbook($fbid, $this->entry->version, $compilepath . $name, $options); + $job = $this->_jobDownloadFluidbook($fbid, $this->entry->version, $compilepath . $name, $options, false); $jobs['Fluidbook #' . $fbid] = $job; $res['Fluidbook #' . $fbid] = url('fluidbook-collection/' . $this->entry->id . '/downloadfile/' . $rand . '/' . $name); diff --git a/app/Services/WorkshopV2.php b/app/Services/WorkshopV2.php index d927c1e4d..480c1be26 100644 --- a/app/Services/WorkshopV2.php +++ b/app/Services/WorkshopV2.php @@ -101,6 +101,9 @@ class WorkshopV2 return $res; } + /** + * @throws Exception + */ public function installBook($id, $dir, $options = [], $version = 'online', $tries = 3, $beforeInstallCallback = null, $function = 'downloadBookAndInstall') { $action = 'download'; @@ -116,12 +119,11 @@ class WorkshopV2 } if ($xml !== false && isset($xml->redirection) && !is_null($xml->redirection)) { - $this->$function((string)$xml->redirection, $dir, $beforeInstallCallback); return true; } else { if ($tries <= 0) { - throw new Exception('Unable to download book'); + throw new Exception('Unable to download book ' . $id); } $this->validDownloadBook($id); $this->installBook($id, $dir, $options, $version, $tries - 1, $beforeInstallCallback, $function); @@ -237,7 +239,11 @@ class WorkshopV2 protected function _getXMLFromResponse($response) { $body = $response->getBody(); - return @simplexml_load_string($body); + $res = @simplexml_load_string($body); + if (!$res) { + Log::warning($body); + } + return $res; } /** -- 2.39.5