]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5410 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 22 Aug 2022 20:13:47 +0000 (22:13 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 22 Aug 2022 20:13:47 +0000 (22:13 +0200)
app/Fluidbook/SEO/Document.php
app/Jobs/FluidbookCompiler.php
app/Models/FluidbookDocument.php
app/Models/FluidbookPublication.php
composer.lock

index b99a7d6768815f79f98749cd3cb0f87cf9e88aa3..a6c274cc6ec1bf3b868a2c11cea3cb69ef3f0f78 100644 (file)
@@ -2,15 +2,16 @@
 
 namespace App\Fluidbook\SEO;
 
+use App\Jobs\FluidbookCompiler;
+use App\Models\FluidbookDocument;
 use App\Util\Excel;
 use Cubist\Util\Text;
-use Fluidbook\Tools\Compiler\CompilerInterface;
 
 class Document
 {
 
     /**
-     * @var CompilerInterface
+     * @var FluidbookCompiler
      */
     public $compiler;
     /**
@@ -22,7 +23,7 @@ class Document
 
 
     /**
-     * @param $compiler CompilerInterface
+     * @param $compiler FluidbookCompiler
      */
     public function __construct($compiler)
     {
@@ -30,7 +31,7 @@ class Document
         // Define default seo contents
         $this->createPages();
 
-        $file=$this->compiler->getSetting('seoAdvanced','');
+        $file = $this->compiler->getSetting('seoAdvanced', '');
         if ($file) {
             $sheets = Excel::excelToArray($this->compiler->working_path($file), true);
             foreach ($sheets as $sheet) {
@@ -55,7 +56,7 @@ class Document
             }
         }
 
-        $this->html = file_get_contents($this->compiler->assets . '/_index.html');
+        $this->html = file_get_contents($this->compiler->source_path('/_index.html'));
     }
 
     public function createPages()
@@ -74,70 +75,59 @@ class Document
             return;
         }
         foreach ($this->compiler->pages as $page => $infos) {
-            $this->createPage($page, $this->getTextContent($infos));
+            $this->createPage($page, $this->getTextContent($page));
         }
     }
 
     public function createPage($page, $content)
     {
+        $title = $this->compiler->getSetting('title');
+        $seoDescription = $this->compiler->getSetting('seoDescription', '');
+
         $p = new Page($this);
         $p->page = $page;
-        $p->title = ($page == 1) ? $this->compiler->book->parametres->title : $this->_getPageLabel($page);
+        $p->title = ($page == 1) ? $title : $this->_getPageLabel($page);
         $p->text = $content;
-        $p->description = $this->compiler->book->parametres->seoDescription ? $this->compiler->book->parametres->seoDescription : $this->compiler->book->parametres->title . ' - Powered by Fluidbook';
-        $p->socialDescription = $this->compiler->book->parametres->facebook_description || $this->compiler->book->parametres->seoDescription;
-        $p->keywords = $this->compiler->book->parametres->seoKeywords;
-        $p->robots = $this->compiler->book->parametres->seoRobots ? 'index,follow' : 'noindex,nofollow';
+        $p->description = $seoDescription ?: $title . ' - Powered by Fluidbook';
+        $p->socialDescription = $this->compiler->getSetting('facebook_description', '') || $seoDescription;
+        $p->keywords = $this->compiler->getSetting('seoKeywords', '');
+        $p->robots = $this->compiler->getSetting('seoRobots', true) ? 'index,follow' : 'noindex,nofollow';
 
         // Google analytics
+        $ga = $this->compiler->getSetting('googleAnalytics', '');
+        $gacustom = $this->compiler->getSetting('googleAnalyticsCustom', '');
+        $statsCustom = $this->compiler->getSetting('statsCustom', '');
+
         $p->ua = '';
-        if ($this->compiler->book->parametres->googleAnalytics != '') {
-            $codes = explode(',', $this->compiler->book->parametres->googleAnalytics);
+        if ($ga != '') {
+            $codes = explode(',', $ga);
             $p->ua .= "<script async src=\"https://www.googletagmanager.com/gtag/js?id=" . $codes[0] . "\"></script><script>window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}</script>";
         }
-        if ($this->compiler->book->parametres->googleAnalyticsCustom != '') {
-            $p->ua .= $this->compiler->book->parametres->googleAnalyticsCustom;
+        if ($gacustom) {
+            $p->ua .= $gacustom;
         }
-
-        if ($this->compiler->book->parametres->statsCustom != '') {
-            $p->footer = $this->compiler->book->parametres->statsCustom;
+        if ($statsCustom) {
+            $p->footer = $statsCustom;
         }
-
         $this->pages[$page] = $p;
     }
 
-    public function getTextContent($infos)
+    public function getTextContent($page)
     {
-        switch ($this->compiler->book->parametres->textExtraction) {
-            case 'poppler':
-                $prefix = 'p';
-                break;
-            case 'fluidbook':
-                $prefix = 'f';
-                break;
-            default:
-                $prefix = '';
-                break;
-        }
+        $f = $this->compiler->getFluidbook()->getTextFile($page, FluidbookDocument::TEXT_HTML);
+        $res = trim(file_get_contents($f));
 
-        $f = wsDocument::getDir($infos['document_id']) . $prefix . 'h' . $infos['document_page'] . '.txt';
-        if (file_exists($f)) {
-            $res = trim(file_get_contents($f));
-        } else {
-            $res = '';
-        }
-
-        if (strpos($res, '<div>') !== 0) {
+        if (!str_starts_with($res, '<div>')) {
             $res = preg_replace('|\<\/div\>$|', '', $res);
         }
-
         return $res;
     }
 
     public function _getPageLabel($page)
     {
-        $res = $this->compiler->book->parametres->title;
-        foreach ($this->compiler->book->chapters as $chapter) {
+        $res = $this->compiler->getSetting('title');
+        dd($this->compiler->getSetting('chapters'));
+        foreach ($this->compiler->getSetting('chapters') as $chapter) {
             if (trim($chapter->label, "\t\r\n\0\x0B-+") == '') {
                 continue;
             }
index 423f260ec78079e51b2dd1c494080fb83a2c0c04..d5baadb095501edca8a74b565e9855b44fbca22c 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace App\Jobs;
 
+use App\Fluidbook\SEO\Document;
 use App\Models\FluidbookPublication;
 use App\Models\FluidbookTheme;
 use App\Models\FluidbookTranslate;
@@ -17,7 +18,6 @@ use Cubist\Util\Files\VirtualDirectory;
 use Cubist\Util\Graphics\Color;
 use Cubist\Util\Graphics\Image;
 use Cubist\Util\Graphics\Resizer;
-use Cubist\Util\ObjectUtil;
 use Cubist\Util\PHP;
 use Cubist\Util\Text;
 use Cubist\Util\Url;
@@ -1137,7 +1137,7 @@ class FluidbookCompiler extends Base implements CompilerInterface
         return $this->linkScale;
     }
 
-    public function virtualToPhysical($virtual)
+    public function virtualToPhysical($virtual): string|int
     {
         if (isset($this->pageLabels[$virtual])) {
             return $virtual;
@@ -1943,7 +1943,7 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
             }
             $this->vdir->file_put_contents('p/' . $seoArticle['url'], $html);
         }
-        $this->seo = new wsHTML5Seo($this);
+        $this->seo = new Document($this);
     }
 
     public function addContentLock($page, $unlockConditions = '')
@@ -3998,4 +3998,17 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
         }
     }
 
+    public function source_path($path = ''): string
+    {
+        $res = $this->assets;
+        if ($path) {
+            $res .= '/' . $path;
+        }
+        return $res;
+    }
+
+    public function page_path($page, $path = ''): string
+    {
+        // TODO: Implement page_path() method.
+    }
 }
index f95777fa7df0d4e2939c449516bacab2d59acfb5..6d0188ba129091813f085ba0d1dce01da705781b 100644 (file)
@@ -9,6 +9,7 @@ use App\Util\FluidbookFarm;
 use Cubist\Backpack\Magic\Fields\Integer;
 use Cubist\Backpack\Magic\Fields\Text;
 use Cubist\Backpack\Magic\Fields\Textarea;
+use Cubist\PDF\CommandLine\FWSTK;
 use Cubist\PDF\PDFTools;
 use Illuminate\Support\Facades\Cache;
 
@@ -21,6 +22,9 @@ class FluidbookDocument extends ToolboxModel
         'plural' => 'documents'];
     protected static $_permissionBase = 'fluidbook-document';
     protected $casts = ['bookmarks' => 'array', 'pdf_data' => 'array', 'file_data' => 'array'];
+    const TEXT_PLAIN = 'p';
+    const TEXT_INDEX = 'i';
+    const TEXT_HTML = 'h';
 
     public function setFields()
     {
@@ -87,7 +91,7 @@ class FluidbookDocument extends ToolboxModel
 
     public function extractTexts()
     {
-        PDFTools::extractTexts($this->path('original.pdf'), $this->path(), '', false, true);
+        PDFTools::extractTexts($this->path('original.pdf'), $this->path(), 'fluidbook', '');
         PDFTools::extractHighlightsData($this->path('original.pdf'), $this->path());
     }
 
@@ -284,4 +288,36 @@ class FluidbookDocument extends ToolboxModel
         return $path;
     }
 
+    /**
+     * @throws \Exception
+     */
+    public function getTextFile($page, $type = self::TEXT_PLAIN, $extractionMethod = 'fluidbook', $ignoreSeparators = '')
+    {
+        $path = $this->_getTextPath($page, $type, $extractionMethod, $ignoreSeparators);
+        if (!file_exists($path)) {
+            PDFTools::extractTexts($this->path('original.pdf'), $this->path(), $extractionMethod, $ignoreSeparators);
+            if (!file_exists($path)) {
+                throw new \Exception('An error occured while producing file ' . $path);
+            }
+        }
+        return $path;
+    }
+
+    protected static function _getTextFilePrefix($textExtraction)
+    {
+        $map = ['poppler' => 'p', 'fluidbook' => 'f', 'pdfbox' => ''];
+        return $map[$textExtraction];
+    }
+
+    public function _getTextPath($page, $type = self::TEXT_PLAIN, $extractionMethod = 'fluidbook', $ignoreSeparators = '')
+    {
+        $sepFolder = '';
+        if ($ignoreSeparators) {
+            $sepFolder = 'sep_' . md5($ignoreSeparators) . '/';
+        }
+
+        $prefix = self::_getTextFilePrefix($extractionMethod);
+        return $this->path('texts/' . $sepFolder . $prefix . $type . $page . '.txt');
+    }
+
 }
index 666e7a269f9d62cfd28fc14ed12e6e9edc2f80c8..8b087054812cd0e86558302bc88c587466ae0fdb 100644 (file)
@@ -174,6 +174,12 @@ class FluidbookPublication extends ToolboxSettingsModel
         return self::$_docs[$documentID];
     }
 
+    public function getTextFile($page, $type = FluidbookDocument::TEXT_PLAIN)
+    {
+        $compo = $this->composition[$page];
+        return self::_getDocument($compo[0])->getTextFile($compo[1], $type, $this->textExtraction, $this->ignoreSearchSeparators);
+    }
+
 
     public function getFile($page, $format = 'jpg', $resolution = 150, $withText = true, $withGraphics = true, $version = 'html', $force = false)
     {
index ec420ff48e83df2d02959c52410fde6f5f5c28a4..95b33fbfaf699c4d92eb208a6168696869a0a55f 100644 (file)
             "source": {
                 "type": "git",
                 "url": "git://git.cubedesigners.com/cubist_pdf.git",
-                "reference": "a9776fd1cf4698352dfe7421aafc53ed0b4c1d6e"
+                "reference": "3de1229df9751a411d0c5cb5d85fcd4889a85826"
             },
             "dist": {
                 "type": "tar",
-                "url": "https://composer.cubedesigners.com/dist/cubist/pdf/cubist-pdf-dev-master-7c7ca6.tar",
-                "reference": "a9776fd1cf4698352dfe7421aafc53ed0b4c1d6e",
-                "shasum": "af35bdf43a35c2b9ca7420d0672289b586f5593b"
+                "url": "https://composer.cubedesigners.com/dist/cubist/pdf/cubist-pdf-dev-master-ba9dfa.tar",
+                "reference": "3de1229df9751a411d0c5cb5d85fcd4889a85826",
+                "shasum": "8ae87c50b61ad69fe6d61390a2280823edccb6b0"
             },
             "require": {
                 "cubist/util": "dev-master",
                 "cubist",
                 "pdf"
             ],
-            "time": "2022-08-22T14:43:58+00:00"
+            "time": "2022-08-22T20:03:01+00:00"
         },
         {
             "name": "cubist/scorm",
             "source": {
                 "type": "git",
                 "url": "git://git.cubedesigners.com/cubist_util.git",
-                "reference": "5f746d46b5892cf178f25ad8a2ba1e3d2cba0568"
+                "reference": "bbc6a1884edcdc5a700a3686ca719adfcb0d7d11"
             },
             "dist": {
                 "type": "tar",
-                "url": "https://composer.cubedesigners.com/dist/cubist/util/cubist-util-dev-master-19118f.tar",
-                "reference": "5f746d46b5892cf178f25ad8a2ba1e3d2cba0568",
-                "shasum": "930fe6d64099ab08dd7a1950d2c1ec20647ef9fb"
+                "url": "https://composer.cubedesigners.com/dist/cubist/util/cubist-util-dev-master-655736.tar",
+                "reference": "bbc6a1884edcdc5a700a3686ca719adfcb0d7d11",
+                "shasum": "b64ade6c61fcb2adb718dae612333897de215ce9"
             },
             "require": {
                 "cubist/net": "dev-master",
                 }
             ],
             "description": "Utilities class",
-            "time": "2022-08-22T14:49:19+00:00"
+            "time": "2022-08-22T18:08:07+00:00"
         },
         {
             "name": "cviebrock/eloquent-sluggable",
             "source": {
                 "type": "git",
                 "url": "git://git.cubedesigners.com/fluidbook_tools.git",
-                "reference": "d5e3e0fc0176fdb7764669032f02ad1bf638a9d2"
+                "reference": "d147c7ef909706a9eb94281407fb80064a356e3f"
             },
             "dist": {
                 "type": "tar",
-                "url": "https://composer.cubedesigners.com/dist/fluidbook/tools/fluidbook-tools-dev-master-1651ab.tar",
-                "reference": "d5e3e0fc0176fdb7764669032f02ad1bf638a9d2",
-                "shasum": "82602466fb73e0eb1ab91a3a70e44c62c0bb6ef8"
+                "url": "https://composer.cubedesigners.com/dist/fluidbook/tools/fluidbook-tools-dev-master-a0d84e.tar",
+                "reference": "d147c7ef909706a9eb94281407fb80064a356e3f",
+                "shasum": "95e4c9b5142c9ea48451a1c13e768083ceb9bd39"
             },
             "require": {
                 "barryvdh/laravel-debugbar": "^3.6",
                 }
             ],
             "description": "Fluidbook Tools",
-            "time": "2022-08-22T15:58:45+00:00"
+            "time": "2022-08-22T19:31:13+00:00"
         },
         {
             "name": "genealabs/laravel-model-caching",