]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5412 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 23 Aug 2022 20:58:54 +0000 (22:58 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 23 Aug 2022 20:58:54 +0000 (22:58 +0200)
app/Console/Commands/WorkshopMigration.php
app/Fluidbook/PDF.php
app/Fluidbook/SEO/Page.php
app/Jobs/FluidbookCompiler.php
app/Models/FluidbookDocument.php
app/Models/FluidbookPublication.php
composer.lock

index 5130038d3d0ff59744c781b8a71f994a4c49283f..462daf14c54c8fc53f419069ab74aeb0e717c307 100644 (file)
@@ -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',
         ];
 
index 40e981ff3af23e481509aff114c8bf1b7a058dd9..36a1bc2b0846a16663838e97c95a26b77b0d6d07 100644 (file)
@@ -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);
+    }
 }
index 8b93cd7e1baf6c334424f3f8b0ee8307d4e8f7e4..fa679e6530ad0cef610a300ab4f7ccfa5a775abb 100644 (file)
@@ -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' => '<script type="text/javascript">var FLUIDBOOK_START_PAGE="' . $this->page . '";</script>' . "\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 -->', $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';
index 93836bb86f5dd96a4fd3b45594f7497785d8f7c4..d3e07acc4ac3ce3ffe8e35cfaf31847d2d4e4c69 100644 (file)
@@ -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"></iframe></noscript>
             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"></iframe></noscript>
         $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"></iframe></noscript>
 
     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';
index 878d490ce15f0f6b5c5650ab7e173153d00efd5c..6e749f929b4304a2a502e0fea61003ad30aaa744 100644 (file)
@@ -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');
+    }
+
 }
index ff6f166ce003546b328490134dc6915c288e98b0..4e6025e339dd328a380558977444c5cf2e54f230 100644 (file)
@@ -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)
     {
 
index c697cd5f96f56c6684474034a16622f51f234452..b9ba2271aca2bf8e5092499c4686bf8a0a63b557 100644 (file)
             "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",
                 "cubist",
                 "pdf"
             ],
-            "time": "2022-08-23T07:38:41+00:00"
+            "time": "2022-08-23T18:14:00+00:00"
         },
         {
             "name": "cubist/scorm",