]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6775 @6
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 19 Jul 2024 08:49:16 +0000 (10:49 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 19 Jul 2024 08:49:16 +0000 (10:49 +0200)
app/Console/Commands/Base/ToolboxCommand.php
app/Fields/Webflow/WebflowPage.php
app/Http/Controllers/Admin/Operations/Tools/StaticSiteUploader.php
app/Jobs/WebflowPublish.php
app/Models/ToolWebflow.php
app/Services/Webflow.php

index 85524cf223f1640c0f2efc30cc72a38804ada3e1..c7c0ad3e26b0c6d652a9d8854a5c23ee30dbf3f3 100644 (file)
@@ -10,7 +10,7 @@ class ToolboxCommand extends CubistCommand
     protected $signature = 'toolbox';
     protected $description = 'Base command for Toolbox';
 
-    protected function getUser($id = 5)
+    protected function getUser($id = 5963)
     {
         return User::withoutGlobalScope('ownerclause')->findOrFail($id);
     }
index b233c362e8c83c9d72c6eb7ba5ca0ef4474d6f7e..2f5bfd49ac0a852f878e411eaa1f291680474d79 100644 (file)
@@ -26,7 +26,10 @@ class WebflowPage extends SelectFromArray
 
         if (!isset(static::$_opts[$id])) {
             static::$_opts[$id] = [];
-            static::$_opts[$id] = ToolWebflow::find($id)->listPages();
+            $wf = ToolWebflow::find($id);
+            if ($wf) {
+                static::$_opts[$id] = $wf->listPages();
+            }
         }
 
         return static::$_opts[$id];
index e3496b4b8d92715363709ea0dd145f5a5f3dfc40..497ab47fbb1168ddb123bffb78a67e14e8762512 100644 (file)
@@ -15,7 +15,7 @@ trait StaticSiteUploader
     public static function getSites()
     {
         return [
-            'cubedesigners-v8' => ['id' => 'cubedesigners-v8', 'label' => __('Cubedesigners V8'), 'server' => 'godzilla.cubedesigners.com', 'url' => 'https://v8.cubedesigners.com/']
+            'cubedesigners-v8' => ['id' => 'cubedesigners-v8', 'label' => __('Cubedesigners V8'), 'server' => 'cloudatlas.cubedesigners.com', 'url' => 'https://v8.cubedesigners.com/']
         ];
     }
 
@@ -67,6 +67,7 @@ trait StaticSiteUploader
 
         $dest = 'staticupload@' . $site['server'] . ':/home/staticupload/' . $site['id'];
         $rsync = new Rsync($from, $dest);
+        $rsync->setMirror(true);
         $rsync->execute();
     }
 }
index 19bc02f0f3290c3aeef6de8b7b1ace220529ace7..f1f9505d1faff8193aedfa40737df5de179f3f41 100644 (file)
@@ -42,9 +42,10 @@ class WebflowPublish extends Base
             $notify = false;
         } else if ($this->mode === 'mirror') {
             $wf->mirror(false);
+            $notify = false;
         } else if ($this->mode === 'webflow' && $wf->webflow_refresh_on_publish) {
-            $wf->mirror(false, true);
             $wf->refreshFormDataFromAPI();
+            $wf->mirror(false, true);
             $text = __('Le site vient d\'être républié suite à une mise à jour de webflow');
         } else if ($this->mode === 'auto') {
             $text = __('Le site vient d\'être républié suite à une mise à jour des contenus');
@@ -59,7 +60,7 @@ class WebflowPublish extends Base
         foreach ($wf->getLocales() as $locale) {
             $actions[__('Visiter [:locale]', ['locale' => $locale['locale']])] = 'https://' . $locale['url'];
         }
-        if ($notify) {
+        if ($notify && isset($text)) {
             if ($this->user === null) {
                 Slack::send($wf->slack, $subject, $text, $actions, false);
             } else {
index 02d814a9d07bf3e8e416e66eca1e0d830d577dae..35c02a545ff63dc6d23abb90a278c875bc107516 100644 (file)
@@ -338,17 +338,15 @@ class ToolWebflow extends ToolboxTranslatableModel
 
         Webflow::setToken($this->webflow_api_token);
 
-        $path = $this->getMirrorPath();
-        if ($force) {
-            $path = Files::emptyDir($path);
-        }
+        $tmp = Files::tmpdir(null, 'webflow_mirror');
+
         $wget = new CommandLine('wget');
         $wget->setArg('mirror');
         $wget->setArg('convert-links');
         $wget->setArg('adjust-extension');
         $wget->setArg('page-requisites');
         $wget->setArg('user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0');
-        $wget->setArg("directory-prefix", $path);
+        $wget->setArg("directory-prefix", $tmp);
         $wget->setArg("span-hosts");
         $wget->setArg("no-host-directories");
         if ($slow) {
@@ -378,6 +376,11 @@ class ToolWebflow extends ToolboxTranslatableModel
         $wget->setArg(null, 'https://' . $this->webflow . '.webflow.io');
         $wget->execute();
 
+
+        $path = $this->getMirrorPath();
+        Files::rmdir($path);
+        `mv $tmp $path`;
+
         stop_measure('Webflow mirror');
         return $wget;
 
@@ -430,9 +433,12 @@ class ToolWebflow extends ToolboxTranslatableModel
     {
         $mirror = $this->getMirrorPath();
         $path = Files::emptyDir(protected_path('webflow/final/' . $this->id . '/' . $locale));
-        $rsync = new CommandLine\Rsync($mirror, $path, true);
+        $rsync = new CommandLine\Rsync($mirror, $path, false);
+        $rsync->addExclude('*.html');
         $rsync->execute();
 
+        $urlmap = $this->getURLMap($locale);
+
         file_put_contents(Files::mkdir($path . '/css') . 'custom.css', $this->getCustomCSS());
         file_put_contents(Files::mkdir($path . '/js') . 'custom.js', $this->getCustomJS());
 
@@ -441,13 +447,31 @@ class ToolWebflow extends ToolboxTranslatableModel
         $images = $this->getTranslation('images', $locale);
         $seo = $this->getTranslation('seo', $locale);
 
-        foreach (Files::getRecursiveDirectoryIterator($path) as $f) {
+        foreach (Files::getRecursiveDirectoryIterator($mirror) as $f) {
             /** @var $f \SplFileInfo */
             if ($f->isDir() || $f->getExtension() !== 'html') {
                 continue;
             }
-            $this->compileHTMLFile($f, $isMainLocale, $locale, $texts, $images, $seo);
+            $relative = '/' . str_replace($mirror, '', $f->getPathname());
+            $this->compileHTMLFile($f, $relative, $path, $isMainLocale, $locale, $texts, $images, $seo, $urlmap);
+        }
+
+        // Redirections
+        $htaccess = '<IfModule mod_rewrite.c>
+    RewriteEngine On
+    RewriteBase /
+';
+        foreach ($this->redirections as $redirection) {
+            foreach ($seo as $s) {
+                if ($s['id'] == $redirection['to']) {
+
+                    $htaccess .= "    " . 'RewriteRule ^' . $redirection['from'] . '$ ' . ($urlmap[$s['url']] ?? $s['url']) . ' [R=308]' . "\n";
+                    break;
+                }
+            }
         }
+        $htaccess .= "</IfModule>\n";
+        file_put_contents($path . '/.htaccess', $htaccess);
     }
 
     /**
@@ -455,7 +479,7 @@ class ToolWebflow extends ToolboxTranslatableModel
      * @param $locale string
      * @return void
      */
-    protected function compileHTMLFile($f, $isMainLocale, $locale, $texts, $images, $seo)
+    protected function compileHTMLFile($f, $relative, $dest, $isMainLocale, $locale, $texts, $images, $seo, $urlmap)
     {
 
         $html = file_get_contents($f->getPathname());
@@ -467,12 +491,19 @@ class ToolWebflow extends ToolboxTranslatableModel
         }
         $pageId = $m[1];
 
+        $htmlPage = false;
+        foreach ($seo as $s) {
+            if ($s['id'] === $relative) {
+                $pageId = $s['id'];
+                $htmlPage = true;
+                break;
+            }
+        }
 
         $html = str_replace('</head>', '<link href="/css/custom.css" rel="stylesheet">' . "\n" . '</head>', $html);
         $html = str_replace('</body>', '<script src="/js/custom.js"></script>' . "\n" . '</body>', $html);
 
-        $html = preg_replace('/lang=\"[a-zA-Z\-_]{2,6}\"/', 'lang="' . $locale . '"', $html);
-
+        $html = preg_replace('/\s+lang=\"[a-zA-Z\-_]{2,6}\"/', ' lang="' . $locale . '"', $html);
 
         // Texts
         foreach ($texts as $text => $translation) {
@@ -481,8 +512,13 @@ class ToolWebflow extends ToolboxTranslatableModel
         // Images
 
 
+        // Replace URL
+        foreach ($urlmap as $k => $v) {
+            $html = str_replace(ltrim($k, '/'), ltrim($v, '/'), $html);
+        }
+
         // SEO
-        if (!$isMainLocale) {
+        if (!$isMainLocale || $htmlPage) {
             foreach ($seo as $s) {
                 if ($s['id'] === $pageId) {
                     $currentPage = $s;
@@ -490,6 +526,7 @@ class ToolWebflow extends ToolboxTranslatableModel
                 }
             }
 
+
             if (isset($currentPage)) {
                 $og_title = e($currentPage['og_title_copied'] ? $currentPage['seo_title'] : $currentPage['og_title']);
                 $og_desc = e($currentPage['og_description_copied'] ? $currentPage['seo_description'] : $currentPage['og_description']);
@@ -502,10 +539,36 @@ class ToolWebflow extends ToolboxTranslatableModel
     <meta content="' . $og_title . '" property="twitter:title" />
     <meta content="' . $og_desc . '" property="twitter:description" />';
                 $html = preg_replace('/<title>[^<]*<\/title>/', '<title>' . e($currentPage['seo_title']) . '</title>' . $meta, $html);
+
+            }
+        }
+
+        file_put_contents($dest . ($urlmap[$relative] ?? $relative), $html);
+    }
+
+    public function getURLMap($locale)
+    {
+        if ($locale === $this->getMainLocale()) {
+            return [];
+        }
+
+        $seoTranslations = $this->getTranslations('seo');
+        $seo = $seoTranslations[$locale];
+        $main = $seoTranslations[$this->getMainLocale()];
+
+        $res = [];
+        foreach ($seo as $item) {
+            foreach ($main as $m) {
+                if ($item['id'] == $m['id']) {
+                    if ($m['url'] !== $item['url']) {
+                        $res[$m['url']] = $item['url'];
+                    }
+                    break;
+                }
             }
         }
 
-        file_put_contents($f->getPathname(), $html);
+        return $res;
     }
 
     protected function getTextTranslationsForCompilation($locale)
@@ -572,6 +635,15 @@ class ToolWebflow extends ToolboxTranslatableModel
                 if (!isset($pages[$item['id']])) {
                     continue;
                 }
+                if (!$item['slug']) {
+                    $item['url'] = '/index.html';
+                } else {
+                    $e = explode('/', $item['url']);
+                    $p = array_pop($e);
+                    $p = $item['slug'] . '.html';
+                    array_push($e, $p);
+                    $item['url'] = implode('/', $e);
+                }
                 $translation[] = $item;
             }
 
@@ -585,7 +657,7 @@ class ToolWebflow extends ToolboxTranslatableModel
                 }
                 return strcmp($a['url'], $b['url']);
             });
-            
+
             $this->setTranslation('seo', $locale, $translation);
         }
 
index 90176f4daa8667a02ebf38d8af69a8ff75136f83..a602428fe0bf002bb09d4e602ec3c2dd74d09a3c 100644 (file)
@@ -264,7 +264,7 @@ class Webflow
             foreach ($data['pages'] as $pageID => $page) {
                 $details = $page['details'];
 
-                $url = trim($details['slug']);
+                $url = trim($details['slug']??'');
                 $c = $page;
                 while (true) {
                     if (null === $c['details']['parentId']) {
@@ -306,7 +306,7 @@ class Webflow
 
                 foreach ($page['contents'] as $node) {
                     if ($node['type'] === 'text') {
-                        if (!$node['text']['text']) {
+                        if (!$node['text']['text'] || !$node['text']['html']) {
                             continue;
                         }
                         $html = str_replace('<br />', "\n", $node['text']['html']);