From 078daebc29aabafb0c514baab2aaa727504e936d Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 23 Aug 2022 22:58:54 +0200 Subject: [PATCH] wip #5412 @1.5 --- app/Console/Commands/WorkshopMigration.php | 2 +- app/Fluidbook/PDF.php | 69 ++++++++++++---------- app/Fluidbook/SEO/Page.php | 21 +++---- app/Jobs/FluidbookCompiler.php | 13 ++-- app/Models/FluidbookDocument.php | 9 +++ app/Models/FluidbookPublication.php | 25 ++++++++ composer.lock | 10 ++-- 7 files changed, 96 insertions(+), 53 deletions(-) diff --git a/app/Console/Commands/WorkshopMigration.php b/app/Console/Commands/WorkshopMigration.php index 5130038d3..462daf14c 100644 --- a/app/Console/Commands/WorkshopMigration.php +++ b/app/Console/Commands/WorkshopMigration.php @@ -164,7 +164,7 @@ class WorkshopMigration extends CubistCommand 'tache' => 'extranet_task', 'changedate' => 'updated_at', 'compiledate' => 'compilation_date', - 'composition_update' => 'composition_update_at', + 'composition_update' => 'composition_updated_at', 'exportdatas' => 'export_data', ]; diff --git a/app/Fluidbook/PDF.php b/app/Fluidbook/PDF.php index 40e981ff3..36a1bc2b0 100644 --- a/app/Fluidbook/PDF.php +++ b/app/Fluidbook/PDF.php @@ -3,6 +3,10 @@ namespace App\Fluidbook; use App\Models\FluidbookPublication; +use Cubist\PDF\PDFTools; +use Cubist\Util\CommandLine; +use Cubist\Util\Files\Files; +use Cubist\Util\Math; class PDF { @@ -11,21 +15,19 @@ class PDF * @param $log * @return false|string|void */ - public static function compilePDF($fluidbook,$log = null) + public static function compilePDF($fluidbook, $log = null) { - global $core; - if (substr($book->parametres->pdfName, 0, 4) == 'http') { + + $pdfName = $fluidbook->getAttribute('pdfName'); + if (str_starts_with($pdfName, 'http')) { return false; } - $cacheDir = WS_BOOKS . '/pdf/' . $book->book_id; - if (!file_exists($cacheDir)) { - mkdir($cacheDir, 0777, true); - } + $cacheDir = Files::mkdir(protected_path('fluidbookpublication/pdf/' . $fluidbook->id)); - $normalPDF = $cacheDir . '/normal.pdf'; - $originalPDF = $cacheDir . '/original.pdf'; - $compressedPDF = $cacheDir . '/compressed.pdf'; + $normalPDF = $cacheDir . 'normal.pdf'; + $originalPDF = $cacheDir . 'original.pdf'; + $compressedPDF = $cacheDir . 'compressed.pdf'; $files = [$normalPDF, $originalPDF, $compressedPDF]; @@ -37,10 +39,10 @@ class PDF if (file_exists($originalPDF)) { $fmtime = filemtime($originalPDF); - if ($fmtime >= $book->composition_update) { + if ($fmtime >= $fluidbook->getCompositionUpdate()) { $invalid = false; - foreach ($pages as $i => $infos) { - $doc = wsDocument::getDir($infos['document_id']) . 'crop.pdf'; + foreach ($fluidbook->composition as $i => $infos) { + $doc = $fluidbook->getPDFSource($i); if (file_exists($doc) && filemtime($doc) > $fmtime) { if (null !== $log) { $log->log('PDF File invalide : one most recent ' . $infos['document_id']); @@ -67,9 +69,8 @@ class PDF } if ($invalid) { - if (file_exists($originalPDF)) { - self::copy($originalPDF, $cacheDir . '/original.' . TIME . '.pdf', false); + self::copy($originalPDF, $cacheDir . '/original.' . time() . '.pdf', false); } $pdfList = array(); @@ -79,33 +80,34 @@ class PDF $k = 0; $original = true; - foreach ($pages as $i => $infos) { + foreach ($fluidbook->composition as $i => $infos) { if (!isset($firstDoc)) { - $firstDoc = $infos['document_id']; + $firstDoc = $infos[0]; } - $doc = wsDocument::getDir($infos['document_id']) . 'crop.pdf'; + $doc = $fluidbook->getPDFSource($i); if (!isset($pdfList[$doc])) { $pdfList[$doc] = $j; - $nb_pages[$doc] = $infos['nb_pages']; + $nb_pages[$doc] = $fluidbook->getDocument($i)->pages; $k = $j; $j++; } else { $k = $pdfList[$doc]; } - $pagesList[$i] = array($k, $infos['document_page']); + $pagesList[$i] = array($k, $infos[1]); - if ($i != $infos['document_page'] || $infos['document_id'] != $firstDoc) { + if ($i != $infos[1] || $infos[0] != $firstDoc) { $original = false; } } if ($original) { - self::copy(wsDocument::getDir($firstDoc) . 'crop.pdf', $originalPDF, false); + // Si on a un seul document source utilisé pour le fluidbook, on le copie directement + self::copy($fluidbook->getPDFSource(1), $originalPDF, false); } else { $args = ''; foreach ($pdfList as $doc => $index) { - $lettre = cubeMath::toPDFLetter($index, true); + $lettre = Math::toPDFLetter($index, true); $args .= $lettre . '=' . $doc . ' '; } @@ -115,7 +117,7 @@ class PDF $currentRange = null; foreach ($pagesList as $p) { - $lettre = cubeMath::toPDFLetter($p[0], true); + $lettre = Math::toPDFLetter($p[0], true); $page = $p[1]; // Initialise l'intervale @@ -162,14 +164,12 @@ class PDF $hash = sha1($args); $args .= ' output ' . $originalPDF; - - $cached = WS_BOOKS . '/pdf/' . $hash . '.pdf'; + $cached = $cacheDir . '/' . $hash . '.pdf'; if (file_exists($cached) && filesize($cached) > 0) { self::copy($cached, $originalPDF, false); } else { - $pdftk = new cubeCommandLine('pdftk'); - $pdftk->setPath(CONVERTER_PATH); + $pdftk = new CommandLine('pdftk'); $pdftk->setManualArg($args); $pdftk->execute(); self::copy($normalPDF, $cached, false); @@ -178,8 +178,8 @@ class PDF } - if ($book->parametres->pdfReplace) { - $replace = $book->getAssetDir() . $book->parametres->pdfReplace; + if ($fluidbook->pdfReplace) { + $replace = $fluidbook->getAssetDir() . $fluidbook->pdfReplace; if (file_exists($replace) && filesize($replace) > 0) { if (!file_exists($normalPDF) || filemtime($normalPDF) < filemtime($replace) || filesize($normalPDF) != filesize($replace)) { self::copy($replace, $normalPDF, false); @@ -189,12 +189,17 @@ class PDF self::copy($originalPDF, $normalPDF, false); } - if ($book->parametres->pdfCompress) { + if ($fluidbook->pdfCompress) { if (!file_exists($compressedPDF) || filemtime($compressedPDF) < filemtime($normalPDF)) { - + PDFTools::compressPDF($normalPDF, $compressedPDF); } return $compressedPDF; } return $normalPDF; } + + protected static function copy($from, $to, $touch = true) + { + return Files::copyFile($from, $to, false, $touch); + } } diff --git a/app/Fluidbook/SEO/Page.php b/app/Fluidbook/SEO/Page.php index 8b93cd7e1..fa679e653 100644 --- a/app/Fluidbook/SEO/Page.php +++ b/app/Fluidbook/SEO/Page.php @@ -2,6 +2,10 @@ namespace App\Fluidbook\SEO; +use Cubist\Util\Files\Files; +use Cubist\Util\Files\VirtualDirectory; +use Cubist\Util\Text; + class Page { public $page; @@ -19,7 +23,7 @@ class Page public $footer; /** - * @var wsHTML5Seo + * @var Document */ public $_container; @@ -55,7 +59,7 @@ class Page 'seoContent' => $this->getSEOContent(), 'startpage' => '' . "\n"]; - if (!$this->_container->compiler->book->parametres->seoVersion) { + if (!$this->_container->compiler->fluidbookSettings->seoVersion) { $vars['canonical'] = $vars['next'] = $vars['prev'] = ''; } @@ -80,7 +84,7 @@ class Page public function getURL() { if (null === $this->url) { - return $this->page . '-' . CubeIT_Text::str2URL(CubeIT_Text::removeAccents($this->title)) . '.html'; + return $this->page . '-' . Text::str2URL(Text::removeAccents($this->title)) . '.html'; } return $this->url; } @@ -92,10 +96,10 @@ class Page $res = 'p/' . $url; } - if ($this->_container->compiler->book->parametres->seoBaseURL == '') { + if($this->_container->compiler->fluidbookSettings->seoBaseURL == '') { $res = $url; } else { - $res = str_replace($this->_container->compiler->book->parametres->seoBaseURL, '', $url); + $res = str_replace($this->_container->compiler->fluidbookSettings->seoBaseURL, '', $url); } return ltrim($res, '/'); @@ -103,7 +107,7 @@ class Page /** * @param string $html - * @param CubeIT_Files_VirtualDirectory $vdir + * @param VirtualDirectory $vdir * @param null|string $path */ public function writePage($html, $vdir, $path = null) @@ -118,10 +122,7 @@ class Page } $html = str_replace('', $base, $html); - $dir = WS_BOOKS . '/seo/' . $this->_container->compiler->book_id . '/'; - if (!file_exists($dir)) { - mkdir($dir, 0777, true); - } + $dir = Files::mkdir(protected_path('fluidbookpublication/seo/' . $this->_container->compiler->getFluidbook()->id)); $file = $dir . $this->page . '.html'; $hash = sha1($html); $hashfile = $dir . $this->page . '.hash'; diff --git a/app/Jobs/FluidbookCompiler.php b/app/Jobs/FluidbookCompiler.php index 93836bb86..d3e07acc4 100644 --- a/app/Jobs/FluidbookCompiler.php +++ b/app/Jobs/FluidbookCompiler.php @@ -2,6 +2,7 @@ namespace App\Jobs; +use App\Fluidbook\PDF; use App\Http\Controllers\Admin\Operations\FluidbookPublication\Services\SocialImageOperation; use App\Models\Signature; use App\Fluidbook\SearchIndex; @@ -1198,7 +1199,7 @@ class FluidbookCompiler extends Base implements CompilerInterface } $this->writeJs(); $this->log('Js written'); - $this->vdir->sync($delete, $this); + $this->vdir->sync(true, $this); $this->log('Files Synced'); // $f=rtrim(str_replace('/html5/', '/compiletime/', $this->dir)); // touch($f); @@ -1999,7 +2000,7 @@ height="0" width="0" style="display:none;visibility:hidden"> return; } - $res = wsUtil::compilePDF($this->book, $this->pages, $this); + $res = PDF::compilePDF($this->getFluidbook()); if (!$this->config->pdfName) { $this->config->pdfName = 'document.pdf'; } @@ -2045,8 +2046,10 @@ height="0" width="0" style="display:none;visibility:hidden"> $this->config->defaultLang = $this->getFluidbook()->locale; $l10n = FluidbookTranslate::getCompiledTranslations(); $l10n['default'] = $l10n[$this->config->defaultLang]; - foreach ($this->getFluidbook()->translations as $k => $v) { - $l10n['default'][$k] = $v; + if($this->getFluidbook()->translations) { + foreach ($this->getFluidbook()->translations as $k => $v) { + $l10n['default'][$k] = $v; + } } $this->config->setRaw('l10n', $l10n); @@ -2779,7 +2782,7 @@ height="0" width="0" style="display:none;visibility:hidden"> public function writeTexts() { - $cache = sha1($this->fluidbookSettings->highlightResults . '/--/' . $this->fluidbookSettings->searchWordSelectionAlgorithm . '///' . $this->fluidbookSettings->textExtraction . '|--|' . $this->fluidbookSettings->ignoreSearchSeparators . '|||' . $this->getFluidbook()->composition_update . '()()()' . FWSTK::lastUpdate()); + $cache = sha1($this->fluidbookSettings->highlightResults . '/--/' . $this->fluidbookSettings->searchWordSelectionAlgorithm . '///' . $this->fluidbookSettings->textExtraction . '|--|' . $this->fluidbookSettings->ignoreSearchSeparators . '|||' . $this->getFluidbook()->getCompositionUpdate(). '()()()' . FWSTK::lastUpdate()); $cacheDir = Files::mkdir(protected_path('fluidbookpublication/index/' . $this->book_id . '/' . $cache . '/')); $indexFile = $cacheDir . '/search.index.js'; $textFile = $cacheDir . '/search.texts.js'; diff --git a/app/Models/FluidbookDocument.php b/app/Models/FluidbookDocument.php index 878d490ce..6e749f929 100644 --- a/app/Models/FluidbookDocument.php +++ b/app/Models/FluidbookDocument.php @@ -142,9 +142,13 @@ class FluidbookDocument extends ToolboxModel { $fixed = $this->path('fixed.pdf'); $original = $this->path('original.pdf'); + $crop = $this->path('crop.pdf'); if (!file_exists($fixed) || filesize($fixed) === 0 || filemtime($fixed) < filemtime($original)) { PDFTools::fixPDF($original, $fixed); } + if (!file_exists($crop)) { + `ln -s $fixed $crop`; + } return $fixed; } @@ -343,4 +347,9 @@ class FluidbookDocument extends ToolboxModel return $this->path('texts/' . $sepFolder . $prefix . $type . $page . '.txt'); } + public function getPDFSource($type = 'crop'): string + { + return $this->path($type . '.pdf'); + } + } diff --git a/app/Models/FluidbookPublication.php b/app/Models/FluidbookPublication.php index ff6f166ce..4e6025e33 100644 --- a/app/Models/FluidbookPublication.php +++ b/app/Models/FluidbookPublication.php @@ -26,6 +26,7 @@ use Cubist\Util\Files\Files; class FluidbookPublication extends ToolboxSettingsModel { + public mixed $pdfName; protected $table = 'fluidbook_publication'; protected $_options = ['name' => 'fluidbook-publication', 'singular' => 'publication', @@ -229,6 +230,15 @@ class FluidbookPublication extends ToolboxSettingsModel } + /** + * @param $page + * @return FluidbookDocument + */ + public function getDocument($page) + { + return self::_getDocument($this->composition[$page][0]); + } + public function getDocumentSize($page = 1) { $sizes = $this->getDocumentSizes(); @@ -255,6 +265,21 @@ class FluidbookPublication extends ToolboxSettingsModel return Files::mkdir('/data1/extranet/www/fluidbook/books/working/' . $this->id); } + public function getCompositionUpdate(): int + { + if (null === $this->composition_updated_at) { + $this->composition_updated_at = new Datetime(); + $this->saveQuietly(); + } + return $this->composition_updated_at->getTimestamp(); + } + + public function getPDFSource($page, $type = 'crop'): string + { + $compo = $this->composition[$page]; + return self::_getDocument($compo[0])->getPDFSource($type); + } + public function compile($dest) { diff --git a/composer.lock b/composer.lock index c697cd5f9..b9ba2271a 100644 --- a/composer.lock +++ b/composer.lock @@ -2039,13 +2039,13 @@ "source": { "type": "git", "url": "git://git.cubedesigners.com/cubist_pdf.git", - "reference": "a3712f58ac4d727c1a4c2e7263d39f6b7dce5b3a" + "reference": "64dd253b4d64f65b5700e15997b76bd0a9a6a584" }, "dist": { "type": "tar", - "url": "https://composer.cubedesigners.com/dist/cubist/pdf/cubist-pdf-dev-master-b055ea.tar", - "reference": "a3712f58ac4d727c1a4c2e7263d39f6b7dce5b3a", - "shasum": "67b078a6c6a157a4f32eacedc086cdb7e2bf9f88" + "url": "https://composer.cubedesigners.com/dist/cubist/pdf/cubist-pdf-dev-master-71a4c6.tar", + "reference": "64dd253b4d64f65b5700e15997b76bd0a9a6a584", + "shasum": "887dc51cca2c64c9cbd7cca9273d0e8f77428fae" }, "require": { "cubist/util": "dev-master", @@ -2081,7 +2081,7 @@ "cubist", "pdf" ], - "time": "2022-08-23T07:38:41+00:00" + "time": "2022-08-23T18:14:00+00:00" }, { "name": "cubist/scorm", -- 2.39.5