diff --git a/.gitignore b/.gitignore index bfeb83b739..352a15debb 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ /config/config.php /config/databases.yml /config/propel.ini +/config/diacritics_mapping.yml # Internal use /cache/ diff --git a/apps/qubit/modules/settings/actions/diacriticsAction.class.php b/apps/qubit/modules/settings/actions/diacriticsAction.class.php new file mode 100644 index 0000000000..feb325dd37 --- /dev/null +++ b/apps/qubit/modules/settings/actions/diacriticsAction.class.php @@ -0,0 +1,103 @@ +. + */ + +class SettingsDiacriticsAction extends SettingsEditAction +{ + // Arrays not allowed in class constants + public static $NAMES = [ + 'diacritics', + 'mappings', + ]; + + public function earlyExecute() + { + parent::earlyExecute(); + + $this->updateMessage = $this->i18n->__('Diacritics settings saved.'); + + $this->settingDefaults = [ + 'diacritics' => 0, + ]; + } + + public function processForm() + { + foreach ($this->form as $field) { + $this->processField($field); + } + } + + protected function addField($name) + { + switch ($name) { + case 'diacritics': + $this->form->setDefault($name, $this->settingDefaults[$name]); + $this->form->setWidget($name, new sfWidgetFormSelectRadio(['choices' => [0 => $this->i18n->__('Disabled'), 1 => $this->i18n->__('Enabled')]], ['class' => 'radio'])); + $this->form->setValidator($name, new sfValidatorChoice(['choices' => [1, 0]])); + + break; + + case 'mappings': + $this->form->setWidget($name, new sfWidgetFormInputFile([], ['accept' => '.yml,.yaml'])); + $this->form->setValidator($name, new sfValidatorFile(['mime_types' => ['text/plain']])); + + break; + } + } + + protected function processField($field) + { + switch ($name = $field->getName()) { + case 'diacritics': + parent::processField($field); + + break; + + case 'mappings': + $file = $this->form->getValue('mappings'); + + $diacriticsMappingPath = sfConfig::get('sf_config_dir').DIRECTORY_SEPARATOR.'diacritics_mapping.yml'; + + if (null !== $file) { + try { + sfYaml::load($file->getTempName()); + + if (!move_uploaded_file($file->getTempName(), $diacriticsMappingPath)) { + $this->getUser()->setFlash('error', $this->context->i18n->__('Unable to upload diacritics mapping yaml file.')); + unset($this->updateMessage); + + return; + } + } catch (Exception $e) { + QubitSetting::findAndSave('diacritics', 0, ['sourceCulture' => true]); + unlink($diacriticsMappingPath); + $this->getUser()->setFlash('error', $this->context->i18n->__('Unable to upload diacritis mapping yaml file.')); + unset($this->updateMessage); + } + } else { + // Reset diacritics settings when uploading yaml fails + QubitSetting::findAndSave('diacritics', 0, ['sourceCulture' => true]); + unlink($diacriticsMappingPath); + $this->getUser()->setFlash('error', $this->context->i18n->__('Unable to upload diacritis mapping yaml file.')); + unset($this->updateMessage); + } + + break; + } + } +} diff --git a/apps/qubit/modules/settings/actions/editAction.class.php b/apps/qubit/modules/settings/actions/editAction.class.php index e868af28d7..b07169c870 100644 --- a/apps/qubit/modules/settings/actions/editAction.class.php +++ b/apps/qubit/modules/settings/actions/editAction.class.php @@ -1,5 +1,4 @@ isMethod('post')) { - $this->form->bind($request->getPostParameters()); + $this->form->bind($request->getPostParameters(), $request->getFiles()); if ($this->form->isValid()) { $this->processForm(); diff --git a/apps/qubit/modules/settings/actions/menuComponent.class.php b/apps/qubit/modules/settings/actions/menuComponent.class.php index 2948c80290..621f2ae063 100644 --- a/apps/qubit/modules/settings/actions/menuComponent.class.php +++ b/apps/qubit/modules/settings/actions/menuComponent.class.php @@ -39,6 +39,10 @@ public function execute($request) 'label' => $i18n->__('Default template'), 'action' => 'template', ], + [ + 'label' => $i18n->__('Diacritics'), + 'action' => 'diacritics', + ], [ 'label' => $i18n->__('Digital object derivatives'), 'action' => 'digitalObjectDerivatives', diff --git a/apps/qubit/modules/settings/templates/diacriticsSuccess.php b/apps/qubit/modules/settings/templates/diacriticsSuccess.php new file mode 100644 index 0000000000..add73575c6 --- /dev/null +++ b/apps/qubit/modules/settings/templates/diacriticsSuccess.php @@ -0,0 +1,66 @@ + + + + + + + + + + +

+ +

+ + + + +
+

+ +

+
$ php symfony search:populate
+
+ +
+

+ +

+
type: mapping
mappings:
- À => A
- Á => A
+
+ +renderGlobalErrors(); ?> + +renderFormTag(url_for(['module' => 'settings', 'action' => 'diacritics'])); ?> + +renderHiddenFields(); ?> + +
+ +
+ + + + + diacritics->label(__('Diacritics'))->renderRow(); ?> +
+ +
+ + + + + mappings->label(__('Mappings YAML'))->renderRow(); ?> +
+ +
+ +
+ +
+ + + + \ No newline at end of file diff --git a/plugins/arDominionB5Plugin/modules/settings/templates/diacriticsSuccess.php b/plugins/arDominionB5Plugin/modules/settings/templates/diacriticsSuccess.php new file mode 100644 index 0000000000..418620a58f --- /dev/null +++ b/plugins/arDominionB5Plugin/modules/settings/templates/diacriticsSuccess.php @@ -0,0 +1,74 @@ + + + + + + + + + +

+ +

+ + + + +
+

+ +

+
$ php symfony search:populate
+
+ +renderGlobalErrors(); ?> + +renderFormTag(url_for(['module' => 'settings', 'action' => 'diacritics'])); ?> + +renderHiddenFields(); ?> + +
+
+

+ +

+
+
+ diacritics->label(__('Diacritics'))); ?> +
+
+
+
+

+ +

+ +
+ +
+

+ +

+
type: mapping
mappings:
- À => A
- Á => A
+
+ +
+ mappings->label(__('Mappings YAML'))); ?> +
+
+
+
+ +
+ +
+ + + + diff --git a/plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php b/plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php index 39862e184d..c315d440e1 100644 --- a/plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php +++ b/plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php @@ -119,10 +119,12 @@ public static function loadMappings() { // Find mapping.yml $finder = sfFinder::type('file')->name('mapping.yml'); - $files = array_unique(array_merge( - $finder->in(sfConfig::get('sf_config_dir')), - $finder->in(ProjectConfiguration::getActive()->getPluginSubPaths('/config')) - )); + $files = array_unique( + array_merge( + $finder->in(sfConfig::get('sf_config_dir')), + $finder->in(ProjectConfiguration::getActive()->getPluginSubPaths('/config')) + ) + ); if (!count($files)) { throw new sfException('You must create a mapping.xml file.'); @@ -135,6 +137,24 @@ public static function loadMappings() return $esMapping; } + public function loadDiacriticsMappings() + { + // Find diacritics_mapping.yml + $diacriticsFinder = sfFinder::type('file')->name('diacritics_mapping.yml'); + $diacriticsFiles = array_unique( + array_merge( + $diacriticsFinder->in(sfConfig::get('sf_config_dir')), + $diacriticsFinder->in(ProjectConfiguration::getActive()->getPluginSubPaths('/config')) + ) + ); + + if (!count($diacriticsFiles)) { + throw new sfException('You must create a diacritics_mapping.yml file.'); + } + + return sfYaml::load(array_shift($diacriticsFiles)); + } + /** * Optimize index. * @@ -272,10 +292,12 @@ public function populate($options = []) } } - $this->log(vsprintf( - 'Index populated with %s documents in %s seconds.', - [$total, $timer->elapsed()] - )); + $this->log( + vsprintf( + 'Index populated with %s documents in %s seconds.', + [$total, $timer->elapsed()] + ) + ); if (!$showErrors) { return; @@ -468,6 +490,10 @@ public static function modelClassFromQubitObjectClass($className) */ protected function initialize() { + if (sfConfig::get('app_diacritics')) { + $this->config['index']['configuration']['analysis']['char_filter']['diacritics_lowercase'] = $this->loadDiacriticsMappings(); + } + try { $this->index->open(); } catch (Exception $e) { @@ -479,7 +505,17 @@ protected function initialize() && isset($this->config['index']['configuration']['analysis']['char_filter']['strip_md']) ) { foreach ($this->config['index']['configuration']['analysis']['analyzer'] as $key => $analyzer) { - $this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter'] = ['strip_md']; + $filters = ['strip_md']; + + if ($this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter']) { + $filters = array_merge($filters, $this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter']); + } + + if (sfConfig::get('app_diacritics')) { + $filters = array_merge($filters, ['diacritics_lowercase']); + } + + $this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter'] = $filters; } }