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
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();
}
}
- 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"
+ ]
+ ]
+ ]
+ ];
+ }
+
}
--- /dev/null
+<?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',
+ ]);
+ }
+}
{
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([
'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',
]);
'label' => trans('backpack::pagemanager.meta_description'),
'type' => 'Textarea',
'hint' => __('Recommended length: 160 chars'),
- 'tab' => 'SEO // Meta',
+ 'tab' => $tab,
'store_in' => 'seo',
]);
'label' => __('Allow page index by search engines'),
'type' => 'Checkbox',
'default' => true,
- 'tab' => 'SEO // Meta',
+ 'tab' => $tab,
'store_in' => 'seo',
]);
}