]> _ Git - fluidbook-toolbox.git/commitdiff
wip #7894 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 16 Jan 2026 17:21:00 +0000 (18:21 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 16 Jan 2026 17:21:00 +0000 (18:21 +0100)
app/Fluidbook/Compiler/Compiler.php
app/Fluidbook/Link/LinksData.php
app/Jobs/FluidbookImagesPreprocess.php
app/Models/FluidbookPublication.php
app/SubForms/Link/Base.php

index 3800bca6cb4e852c9a76e88cad7bfdbc5bb87108..5c3929651ecbe0a8a45ec8c180c1f273bf6eb7c2 100644 (file)
@@ -313,7 +313,7 @@ class Compiler extends Base implements CompilerInterface, IVirtualDirectoryError
      * @throws \Exception
      */
 
-    function __construct(FluidbookPublication $book, $variant = 'online', $phonegap = false, $phonegapVersion = 'latest', $dir = null, $standalone = false, $appcache = false, $home = false, FluidbookTheme $theme = null, $hybrid = false, Command $command = null)
+    function __construct(FluidbookPublication $book, $variant = 'online', $phonegap = false, $phonegapVersion = 'latest', $dir = null, $standalone = false, $appcache = false, $home = false, ?FluidbookTheme $theme, $hybrid = false, ?Command $command)
     {
         ExcelToArray::setCache($book->protected_path('fluidbookpublication/cache/exceltoarray'));
         YoutubeDL::setCookiesFile(resource_path('yt-dlp/cookies.txt'), 'firefox');
index a87bea79c27419aa8f43baaa9b61f952658faf25..6142c98d17511df573d4e030a3f03a88f618fafa 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace App\Fluidbook\Link;
 
+use App\Fluidbook\Compiler\Compiler;
+use App\Jobs\OCR;
 use App\Models\FluidbookPublication;
 use App\Models\User;
 use App\SubForms\Link\Base;
@@ -624,9 +626,10 @@ class LinksData
             }
         }
         if (!isset(static::$_names[$u])) {
-            try {
-                static::$_names[$u] = User::find($u)->name;
-            } catch (\Exception $e) {
+            $u=User::find($u);
+            if($u){
+                static::$_names[$u] = $u->name;
+            } else {
                 static::$_names[$u] = '-';
             }
         }
@@ -789,4 +792,63 @@ class LinksData
         self::getLinksAndRulers($source, $links, $rulers, $sourceVersion);
         self::saveLinksInFile($dest, backpack_user()->id, __('Copier les liens à partir du fluidbook :fluidbook', ['fluidbook' => $source]), $links, $rulers);
     }
+
+    /**
+     * @param $book FluidbookPublication
+     * @param $sync bool
+     * @param bool $updateLinks
+     * @return OCR[]
+     * @throws \Exception
+     */
+    public static function ocrLinksImages($book, $sync = false, $updateLinks = false)
+    {
+        $res = [];
+        if (!($book instanceof FluidbookPublication)) {
+            $book = FluidbookPublication::withoutGlobalScopes()->find($book);
+        }
+        $book->getLinksAndRulers($links, $rulers);
+        $compiler = new Compiler($book);
+        $i = 0;
+        $linksToUpdate = [];
+        foreach ($links as $link) {
+            $l = Link::getInstance($i, $link, $compiler);
+            if (null === $l) {
+                continue;
+            }
+            $jobs = $compiler->getLinkAlternativeText($l, true);
+            if (!$jobs) {
+                continue;
+            }
+            $linksToUpdate[] = ['data' => $link, 'object' => $l];
+            $res = array_merge($res, $jobs);
+
+            foreach ($jobs as $job) {
+                if ($sync) {
+                    $job->handle();
+                } else {
+                    dispatch($job);
+                }
+            }
+            $i++;
+        }
+
+        if ($sync && $updateLinks) {
+            $change = false;
+            foreach ($linksToUpdate as $link) {
+                $d = $link['data'];
+                $o = $link['object'];
+                if ($d['alt'] !== '') {
+                    continue;
+                }
+                $alt = $compiler->getLinkAlternativeText($o);
+                if (!$alt) {
+                    continue;
+                }
+                $change = true;
+                $links[$d['id']]['alt'] = $alt;
+            }
+            self::saveLinksInFile($book->id, backpack_user() ?? backpack_user()->id, 'Alternative contents automatically ocerized', $links, $rulers);
+        }
+        return $res;
+    }
 }
index 9b8cb5fc3f445a0a8b4f9c482a9fd6fa44b96cf6..05378c8f7ae74312f7173e8517ea2b6a949c77b0 100644 (file)
@@ -3,6 +3,7 @@
 namespace App\Jobs;
 
 use App\Fluidbook\Compiler\Compiler;
+use App\Fluidbook\Link\LinksData;
 use App\Models\FluidbookPublication;
 use Cubist\Util\ArrayUtil;
 use Fluidbook\Tools\Links\Link;
@@ -36,6 +37,9 @@ class FluidbookImagesPreprocess extends Base
         parent::__construct();
     }
 
+    /**
+     * @throws \Exception
+     */
     public function handle()
     {
         $this->book = FluidbookPublication::withoutGlobalScopes()->find($this->book_id);
@@ -66,51 +70,34 @@ class FluidbookImagesPreprocess extends Base
             $imdir = 'html';
         }
 
-//        for ($page = 1; $page <= $this->book->getPagesNumber(); $page++) {
-//            $thisrasterize = in_array($page, $rasterizePages);
-//            $thisimagesvg = !$thisrasterize && $svg;
-//            $pageBackgroundsToGenerate = $thisrasterize ? [true] : $globalBackgroundToGenerate;
-//            foreach ($resolutions as $r) {
-//                foreach ($pageBackgroundsToGenerate as $withText) {
-//                    $this->getFile($page, $settings->imageFormat, $r, $withText, true, $transparent, $imdir);
-//                }
-//            }
-//            if ($thisimagesvg) {
-//                $this->getFile($page, 'svg', 150, true, in_array($page, $vectorPages), false, 'html');
-//            }
-//            $this->getFile($page, $settings->imageFormat, 'thumb');
-//        }
-
-        $this->book->getLinksAndRulers($links, $rulers);
-        $compiler = new Compiler($this->book);
-        $i = 0;
-        foreach ($links as $link) {
-            $l = Link::getInstance($i, $link, $compiler);
-            if (null === $l) {
-                continue;
-            }
-            $jobs = $compiler->getLinkAlternativeText($l, true);
-            if (!$jobs) {
-                continue;
-            }
-            $this->jobs = array_merge($this->jobs, $jobs);
-
-            foreach ($jobs as $job) {
-                if ($this->_sync) {
-                    $job->handle();
-                } else {
-                    dispatch($job);
+        for ($page = 1; $page <= $this->book->getPagesNumber(); $page++) {
+            $thisrasterize = in_array($page, $rasterizePages);
+            $thisimagesvg = !$thisrasterize && $svg;
+            $pageBackgroundsToGenerate = $thisrasterize ? [true] : $globalBackgroundToGenerate;
+            foreach ($resolutions as $r) {
+                foreach ($pageBackgroundsToGenerate as $withText) {
+                    $this->getFile($page, $settings->imageFormat, $r, $withText, true, $transparent, $imdir);
                 }
             }
-            $i++;
+            if ($thisimagesvg) {
+                $this->getFile($page, 'svg', 150, true, in_array($page, $vectorPages), false, 'html');
+            }
+            $this->getFile($page, $settings->imageFormat, 'thumb');
         }
 
+        $this->jobs = array_merge($this->jobs, LinksData::ocrLinksImages($this->book, $this->_sync, false));
+
         while (true) {
             if ($this->_checkJobs()) {
+                dispatch(function () {
+                    LinksData::ocrLinksImages($this->book, true, true);
+                });
                 return;
             }
             usleep(0.25 * 1000000);
         }
+
+
     }
 
     protected function _checkJobs()
@@ -119,7 +106,6 @@ class FluidbookImagesPreprocess extends Base
         $done = 0;
         $error = 0;
 
-
         foreach ($this->jobs as $job) {
             if ($job->isDone()) {
                 $done++;
@@ -127,6 +113,7 @@ class FluidbookImagesPreprocess extends Base
                 $error++;
             }
         }
+
         $finished = $done + $error;
 
         $state = $finished . '(--' . $error . ') /' . $nbjobs;
@@ -146,7 +133,6 @@ class FluidbookImagesPreprocess extends Base
                 if ($job->isError()) {
                     Log::error('This file failed ' . $job->getPath());
                 }
-                //Log::error('This file is stuck ' . $job->getPath());
             }
             return true;
         }
index a5e380eb83b0fa5c4556adaf17c209b9cc86d4ae..16e95210b847d0e5b9e42340e2c64a106742bc7d 100644 (file)
@@ -951,7 +951,7 @@ class FluidbookPublication extends ToolboxStatusModel
         return true;
     }
 
-    protected function _baseReplicate(array $except = null)
+    protected function _baseReplicate(?array $except)
     {
         $current = $this;
         if ($this->syncFromWorkshop()) {
@@ -971,7 +971,7 @@ class FluidbookPublication extends ToolboxStatusModel
         return $new;
     }
 
-    public function replicateFull(array $except = null)
+    public function replicateFull(?array $except)
     {
         $new = $this->_baseReplicate($except);
         if (!$this->composition_fluidbook && $this->version >= 3) {
@@ -988,7 +988,7 @@ class FluidbookPublication extends ToolboxStatusModel
         return $new;
     }
 
-    public function replicateSettings(array $except = null)
+    public function replicateSettings(?array $except)
     {
         $new = $this->_baseReplicate(array_merge($except ?? [], ['composition']));
         $new->composition = [];
index fb5869286e1a21d6f25d19f40aabe7c6d939baaa..d3c27470200e9da314ecbfbfafe0f110f1e43bcf 100644 (file)
@@ -355,7 +355,7 @@ class Base extends Form
      * @return string
      */
 
-    public function render(callable $callback = null)
+    public function render(?callable $callback)
     {
         return view('fluidbook_publication.link_editor_form', ['form' => $this, 'crud' => $this->crud])->render($callback);
     }