From: Vincent Vanwaelscappel Date: Tue, 20 Aug 2019 14:21:33 +0000 (+0200) Subject: wip #2941 @5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=21ef7871b19bdc6c84f67df2d5e344dc708f75c9;p=cubist_cms-back.git wip #2941 @5 --- diff --git a/src/CubistBackpackServiceProvider.php b/src/CubistBackpackServiceProvider.php index 89678f3..6fb6111 100644 --- a/src/CubistBackpackServiceProvider.php +++ b/src/CubistBackpackServiceProvider.php @@ -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; diff --git a/src/app/Console/Commands/SearchIndexCommand.php b/src/app/Console/Commands/SearchIndexCommand.php index cecef08..b769048 100644 --- a/src/app/Console/Commands/SearchIndexCommand.php +++ b/src/app/Console/Commands/SearchIndexCommand.php @@ -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 index 0000000..1c9fbf4 --- /dev/null +++ b/src/app/Magic/Models/CubistMagicPageModel.php @@ -0,0 +1,72 @@ +_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', + ]); + } +} diff --git a/src/app/Magic/Models/News.php b/src/app/Magic/Models/News.php index 4ddf592..f263550 100644 --- a/src/app/Magic/Models/News.php +++ b/src/app/Magic/Models/News.php @@ -4,7 +4,7 @@ namespace Cubist\Backpack\app\Magic\Models; -class News extends CubistMagicModel +class News extends CubistMagicPageModel { protected $table = 'cubist_news'; diff --git a/src/app/Magic/PageData.php b/src/app/Magic/PageData.php index 9b3579b..584a100 100644 --- a/src/app/Magic/PageData.php +++ b/src/app/Magic/PageData.php @@ -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)) { diff --git a/src/app/Template/TemplatePage.php b/src/app/Template/TemplatePage.php index 7d21107..b900102 100644 --- a/src/app/Template/TemplatePage.php +++ b/src/app/Template/TemplatePage.php @@ -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', ]); } diff --git a/src/resources/config/cubist.php b/src/resources/config/cubist.php index 8299bca..ed5255d 100644 --- a/src/resources/config/cubist.php +++ b/src/resources/config/cubist.php @@ -1,5 +1,6 @@ env('CUBIST_INTERNAL_SEARCH', false), 'page_model' => '\App\Models\Page', 'settings_model' => '\App\Models\Settings', ];