]> _ Git - cubist_cms-back.git/commitdiff
wip #2941 @5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 20 Aug 2019 14:21:33 +0000 (16:21 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 20 Aug 2019 14:21:33 +0000 (16:21 +0200)
src/CubistBackpackServiceProvider.php
src/app/Console/Commands/SearchIndexCommand.php
src/app/Magic/Models/CubistMagicPageModel.php [new file with mode: 0644]
src/app/Magic/Models/News.php
src/app/Magic/PageData.php
src/app/Template/TemplatePage.php
src/resources/config/cubist.php

index 89678f328cf3c003e0442791f8062646ae84d260..6fb611100365caf5d7964c97b5685021c6f46f5c 100644 (file)
@@ -2,7 +2,6 @@
 
 namespace Cubist\Backpack;
 
-use Composer\Command\SearchCommand;
 use Cubist\Backpack\app\Console\Command\MigrateCommand;
 use Cubist\Backpack\app\Console\Commands\GenerateCommand;
 use Cubist\Backpack\app\Console\Commands\InstallCommand;
index cecef08258f073095782a1b3b7ab729530908880..b76904860605527294e748d490d8db44ae822910 100644 (file)
@@ -8,9 +8,9 @@ use Cubist\Backpack\app\Magic\Menu\PageItem;
 use Cubist\Backpack\app\Magic\Menu\VirtualItem;
 use Cubist\Backpack\app\Template\Navigation;
 use Cubist\Backpack\app\Template\Redirection;
-use Cviebrock\LaravelElasticsearch\Facade;
-use DOMDocument;
+use Cviebrock\LaravelElasticsearch\Facade as Elasticsearch;
 use Illuminate\Console\Command;
+use Cubist\Util\XML\DOMSelector;
 
 
 class SearchIndexCommand extends Command
@@ -20,7 +20,26 @@ class SearchIndexCommand extends Command
 
     public function handle()
     {
-        Facade::
+        $index = env('ELASTICSEARCH_INDEX_NAME', 'cubist_elastic_default_1');
+        echo env('ELASTICSEARCH_HOST');
+
+        try {
+            Elasticsearch::indices()->delete(['index' => $index]);
+        } catch (\Exception $e) {
+            echo $e->getMessage();
+        }
+        Elasticsearch::indices()->create(
+            [
+                'index' => $index,
+                'body' => [
+                    'settings' =>
+                        [
+                            'analysis' => $this->_french(),
+                        ],
+                    'mappings' => $this->_typeMapping(true),
+                ]
+            ]);
+
         /** @var Cubist\Backpack\app\Magic\Menu\Item[] $pages */
         $pages = Menu::getNavigation()->findAll();
 
@@ -38,26 +57,129 @@ class SearchIndexCommand extends Command
                 }
             }
 
-            libxml_use_internal_errors(true);
-            $doc = new DOMDocument();
-            $doc->loadHTMLFile($page->getHref());
+            $href = $page->getHref();
+            if ($href == '#') {
+                continue;
+            }
 
-            $title = $doc->getElementsByTagName('title');
-            $body = $doc->getElementsByTagName('body');
+            $url = action("PageController@catchall", ['page' => $href]);
 
-            $data = [
-                'body' => [
-                    'title' => $title->item(0)->nodeValue,
-                    'body' => $body->item(0)->nodeValue,
-                ],
-                'index' => env('ELASTICSEARCH_INDEX_NAME', 'cubist_elastic_default'),
-                'type' => 'cmspage',
-                'id' => $page->getHref(),
+            $html = @file_get_contents($url);
+            if (!$html) {
+                continue;
+            }
+            $doc = new DOMSelector($html);
+
+            /** @var \DOMElement $meta */
+            $meta = $doc->select('meta[data-search]')[0];
+            $enabled = $meta->getAttribute('data-search') == '1';
+            if (!$enabled) {
+                continue;
+            }
+            $short_title = $meta->getAttribute('data-short-title');
+            $keywords = $meta->getAttribute('data-keywords');
+
+            $body = [
+                'long_title' => (string)$doc->select('title')[0],
+                'short_title' => $short_title,
+                'keywords' => $keywords,
+                'main' => $doc->getDOM()->saveHTML($doc->select('main')->item(0)),
             ];
 
-            print_r($data);
+            $data = [
+                'body' => $body,
+                'index' => $index,
+                'type' => '_doc',
+                'id' => $url,
+            ];
 
             Elasticsearch::index($data);
         }
     }
+
+    protected function _typeMapping($source = true)
+    {
+        $res = [];
+        if ($source) {
+            $res = ['_source' => [
+                'enabled' => true,
+            ]
+            ];
+        }
+
+
+        $res['properties'] = [
+            'short_title' => $this->_frenchMapping(),
+            'long_title' => $this->_frenchMapping(),
+            'description' => $this->_frenchMapping(),
+            'keywords' => $this->_frenchMapping(),
+            'main' => $this->_frenchMapping(),
+        ];
+
+        return $res;
+
+    }
+
+    protected function _frenchMapping()
+    {
+        return [
+            'type' => 'text',
+            'analyzer' => 'french_light',
+            'fields' => [
+                'stemmed' => [
+                    'type' => 'text',
+                    'analyzer' => 'french_heavy'
+                ]
+            ]
+        ];
+    }
+
+    protected function _french()
+    {
+        return [
+            "filter" => [
+                "french_elision" => [
+                    "type" => "elision",
+                    "articles_case" => true,
+                    "articles" => ["l", "m", "t", "qu", "n", "s", "j", "d", "c", "jusqu", "quoiqu", "lorsqu", "puisqu"]
+                ],
+                "french_synonym" => [
+                    "type" => "synonym",
+                    "ignore_case" => true,
+                    "expand" => true,
+                    "synonyms" => [
+                        "salade, laitue",
+                        "mayo, mayonnaise",
+                        "grille, toaste",
+                        'pmi, pm instrumentation',
+                    ]
+                ],
+                "french_stemmer" => [
+                    "type" => "stemmer",
+                    "language" => "light_french"
+                ]
+            ],
+            "analyzer" => [
+                "french_heavy" => [
+                    "tokenizer" => "icu_tokenizer",
+                    "char_filter" => ["html_strip"],
+                    "filter" => [
+                        "french_elision",
+                        "icu_folding",
+                        "french_synonym",
+                        "french_stemmer"
+                    ]
+                ],
+                "french_light" => [
+                    "tokenizer" => "icu_tokenizer",
+                    "char_filter" => ["html_strip"],
+                    "filter" => [
+                        "french_elision",
+                        "icu_folding"
+                    ]
+                ]
+            ]
+        ];
+    }
+
 }
diff --git a/src/app/Magic/Models/CubistMagicPageModel.php b/src/app/Magic/Models/CubistMagicPageModel.php
new file mode 100644 (file)
index 0000000..1c9fbf4
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Models;
+
+
+class CubistMagicPageModel extends CubistMagicModel
+{
+    public function setFields()
+    {
+        parent::setFields();
+        $this->_seo();
+        if (config('cubist.internal_search', false)) {
+            $this->_internalSearch();
+        }
+    }
+
+    protected function _internalSearch()
+    {
+        $tab = 'Recherche';
+        $this->addField(['name' => 'search_internal_enabled',
+            'type' => 'Checkbox',
+            'label' => 'Activer',
+            'default' => true,
+            'hint' => 'Référencer cette page dans le moteur de recherche interne',
+            'tab' => $tab]);
+
+        $this->addField(['name' => 'search_internal_keywords',
+            'type' => 'Tags',
+            'label' => 'Mots clés',
+            'hint' => 'Mots supplémentaires à utiliser par le moteur de recherche',
+            'tab' => $tab]);
+    }
+
+    protected function _seo()
+    {
+        $tab = 'SEO // Meta';
+
+        $this->addField(['name' => 'slug',
+            'type' => 'Slug',
+            'label' => 'Slug (URL)',
+            'tab' => $tab,
+        ]);
+
+        $this->addField([
+            'name' => 'meta_title',
+            'label' => trans('backpack::pagemanager.meta_title'),
+            'type' => 'Text',
+            'hint' => trans('If empty, page title is used.') . ' ' . __('Recommended length: 60 chars'),
+            'tab' => $tab,
+            'store_in' => 'seo',
+        ]);
+
+        $this->addField([
+            'name' => 'meta_description',
+            'label' => trans('backpack::pagemanager.meta_description'),
+            'type' => 'Textarea',
+            'hint' => __('Recommended length: 160 chars'),
+            'tab' => $tab,
+            'store_in' => 'seo',
+        ]);
+
+        $this->addField([
+            'name' => 'robots',
+            'label' => __('Allow page index by search engines'),
+            'type' => 'Checkbox',
+            'default' => true,
+            'tab' => $tab,
+            'store_in' => 'seo',
+        ]);
+    }
+}
index 4ddf5924e960283ef26eeabdff4cbe8eb92be4c3..f2635504c529cea83cbb4d7a7f4fbf41b0367771 100644 (file)
@@ -4,7 +4,7 @@
 namespace Cubist\Backpack\app\Magic\Models;
 
 
-class News extends CubistMagicModel
+class News extends CubistMagicPageModel
 {
 
     protected $table = 'cubist_news';
index 9b3579b73def990b00b1156a993923ec6a8d8309..584a100153722a49e1dbb999c72c7e6bef310422 100644 (file)
@@ -185,6 +185,11 @@ class PageData implements \ArrayAccess
         $this->unset($name);
     }
 
+    public function __isset($name)
+    {
+        return $this->has($name);
+    }
+
     public function getMediaCollection($offset)
     {
         if (!$this->exists($offset)) {
index 7d21107ce84ece8eab1419d04f4f80148df855bf..b90010242b547c25cc664a5de0a56653d622e988 100644 (file)
@@ -9,14 +9,36 @@ class TemplatePage extends TemplateAbstract
     {
         parent::init();
         $this->_seo();
+        if (config('cubist.internal_search', false)) {
+            $this->_internalSearch();
+        }
+    }
+
+    protected function _internalSearch()
+    {
+        $tab = 'Recherche';
+        $this->addField(['name' => 'search_internal_enabled',
+            'type' => 'Checkbox',
+            'label' => 'Activer',
+            'default' => true,
+            'hint' => 'Référencer cette page dans le moteur de recherche interne',
+            'tab' => $tab]);
+
+        $this->addField(['name' => 'search_internal_keywords',
+            'type' => 'Tags',
+            'label' => 'Mots clés',
+            'hint' => 'Mots supplémentaires à utiliser par le moteur de recherche',
+            'tab' => $tab]);
     }
 
     protected function _seo()
     {
+        $tab = 'SEO // Meta';
+
         $this->addField(['name' => 'slug',
             'type' => 'Slug',
             'label' => 'Slug (URL)',
-            'tab' => 'SEO // Meta',
+            'tab' => $tab,
         ]);
 
         $this->addField([
@@ -24,7 +46,7 @@ class TemplatePage extends TemplateAbstract
             'label' => trans('backpack::pagemanager.meta_title'),
             'type' => 'Text',
             'hint' => trans('If empty, page title is used.') . ' ' . __('Recommended length: 60 chars'),
-            'tab' => 'SEO // Meta',
+            'tab' => $tab,
             'store_in' => 'seo',
         ]);
 
@@ -33,7 +55,7 @@ class TemplatePage extends TemplateAbstract
             'label' => trans('backpack::pagemanager.meta_description'),
             'type' => 'Textarea',
             'hint' => __('Recommended length: 160 chars'),
-            'tab' => 'SEO // Meta',
+            'tab' => $tab,
             'store_in' => 'seo',
         ]);
 
@@ -42,7 +64,7 @@ class TemplatePage extends TemplateAbstract
             'label' => __('Allow page index by search engines'),
             'type' => 'Checkbox',
             'default' => true,
-            'tab' => 'SEO // Meta',
+            'tab' => $tab,
             'store_in' => 'seo',
         ]);
     }
index 8299bca8075bcc12883262579d5210abe7adde1c..ed5255db70f91cc438edb6337db07b1b63cd0669 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 return [
+    'internal_search' => env('CUBIST_INTERNAL_SEARCH', false),
     'page_model' => '\App\Models\Page',
     'settings_model' => '\App\Models\Settings',
 ];