Skip to content

Commit 9dd0656

Browse files
authored
Add diacritics setting (#1636)
Setting to enable further char_filter mappings for diacritics.
1 parent d996e1c commit 9dd0656

File tree

7 files changed

+294
-11
lines changed

7 files changed

+294
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/config/config.php
99
/config/databases.yml
1010
/config/propel.ini
11+
/config/diacritics_mapping.yml
1112

1213
# Internal use
1314
/cache/
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/*
3+
* This file is part of the Access to Memory (AtoM) software.
4+
*
5+
* Access to Memory (AtoM) is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* Access to Memory (AtoM) is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with Access to Memory (AtoM). If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
class SettingsDiacriticsAction extends SettingsEditAction
20+
{
21+
// Arrays not allowed in class constants
22+
public static $NAMES = [
23+
'diacritics',
24+
'mappings',
25+
];
26+
27+
public function earlyExecute()
28+
{
29+
parent::earlyExecute();
30+
31+
$this->updateMessage = $this->i18n->__('Diacritics settings saved.');
32+
33+
$this->settingDefaults = [
34+
'diacritics' => 0,
35+
];
36+
}
37+
38+
public function processForm()
39+
{
40+
foreach ($this->form as $field) {
41+
$this->processField($field);
42+
}
43+
}
44+
45+
protected function addField($name)
46+
{
47+
switch ($name) {
48+
case 'diacritics':
49+
$this->form->setDefault($name, $this->settingDefaults[$name]);
50+
$this->form->setWidget($name, new sfWidgetFormSelectRadio(['choices' => [0 => $this->i18n->__('Disabled'), 1 => $this->i18n->__('Enabled')]], ['class' => 'radio']));
51+
$this->form->setValidator($name, new sfValidatorChoice(['choices' => [1, 0]]));
52+
53+
break;
54+
55+
case 'mappings':
56+
$this->form->setWidget($name, new sfWidgetFormInputFile([], ['accept' => '.yml,.yaml']));
57+
$this->form->setValidator($name, new sfValidatorFile(['mime_types' => ['text/plain']]));
58+
59+
break;
60+
}
61+
}
62+
63+
protected function processField($field)
64+
{
65+
switch ($name = $field->getName()) {
66+
case 'diacritics':
67+
parent::processField($field);
68+
69+
break;
70+
71+
case 'mappings':
72+
$file = $this->form->getValue('mappings');
73+
74+
$diacriticsMappingPath = sfConfig::get('sf_config_dir').DIRECTORY_SEPARATOR.'diacritics_mapping.yml';
75+
76+
if (null !== $file) {
77+
try {
78+
sfYaml::load($file->getTempName());
79+
80+
if (!move_uploaded_file($file->getTempName(), $diacriticsMappingPath)) {
81+
$this->getUser()->setFlash('error', $this->context->i18n->__('Unable to upload diacritics mapping yaml file.'));
82+
unset($this->updateMessage);
83+
84+
return;
85+
}
86+
} catch (Exception $e) {
87+
QubitSetting::findAndSave('diacritics', 0, ['sourceCulture' => true]);
88+
unlink($diacriticsMappingPath);
89+
$this->getUser()->setFlash('error', $this->context->i18n->__('Unable to upload diacritis mapping yaml file.'));
90+
unset($this->updateMessage);
91+
}
92+
} else {
93+
// Reset diacritics settings when uploading yaml fails
94+
QubitSetting::findAndSave('diacritics', 0, ['sourceCulture' => true]);
95+
unlink($diacriticsMappingPath);
96+
$this->getUser()->setFlash('error', $this->context->i18n->__('Unable to upload diacritis mapping yaml file.'));
97+
unset($this->updateMessage);
98+
}
99+
100+
break;
101+
}
102+
}
103+
}

apps/qubit/modules/settings/actions/editAction.class.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/*
43
* This file is part of the Access to Memory (AtoM) software.
54
*
@@ -28,7 +27,7 @@ public function execute($request)
2827

2928
// Handle posted data
3029
if ($request->isMethod('post')) {
31-
$this->form->bind($request->getPostParameters());
30+
$this->form->bind($request->getPostParameters(), $request->getFiles());
3231

3332
if ($this->form->isValid()) {
3433
$this->processForm();

apps/qubit/modules/settings/actions/menuComponent.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function execute($request)
3939
'label' => $i18n->__('Default template'),
4040
'action' => 'template',
4141
],
42+
[
43+
'label' => $i18n->__('Diacritics'),
44+
'action' => 'diacritics',
45+
],
4246
[
4347
'label' => $i18n->__('Digital object derivatives'),
4448
'action' => 'digitalObjectDerivatives',
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php decorate_with('layout_2col.php'); ?>
2+
3+
<?php slot('sidebar'); ?>
4+
5+
<?php echo get_component('settings', 'menu'); ?>
6+
7+
<?php end_slot(); ?>
8+
9+
<?php slot('title'); ?>
10+
11+
<h1>
12+
<?php echo __('Diacritics'); ?>
13+
</h1>
14+
15+
<?php end_slot(); ?>
16+
17+
<?php slot('content'); ?>
18+
<div class="alert alert-info">
19+
<p>
20+
<?php echo __('Please rebuild the search index after uploading diacritics mappings.'); ?>
21+
</p>
22+
<pre>$ php symfony search:populate</pre>
23+
</div>
24+
25+
<div class="alert alert-info">
26+
<p>
27+
<?php echo __('Example CSV:'); ?>
28+
</p>
29+
<pre>type: mapping<br/>mappings:<br/> - À => A<br/> - Á => A</pre>
30+
</div>
31+
32+
<?php echo $form->renderGlobalErrors(); ?>
33+
34+
<?php echo $form->renderFormTag(url_for(['module' => 'settings', 'action' => 'diacritics'])); ?>
35+
36+
<?php echo $form->renderHiddenFields(); ?>
37+
38+
<div id="content">
39+
40+
<fieldset class="collapsible">
41+
<legend>
42+
<?php echo __('Diacritics settings'); ?>
43+
</legend>
44+
45+
<?php echo $form->diacritics->label(__('Diacritics'))->renderRow(); ?>
46+
</fieldset>
47+
48+
<fieldset class="collapsible">
49+
<legend>
50+
<?php echo __('CSV Mapping YAML'); ?>
51+
</legend>
52+
53+
<?php echo $form->mappings->label(__('Mappings YAML'))->renderRow(); ?>
54+
</fieldset>
55+
56+
</div>
57+
58+
<section class="actions">
59+
<ul>
60+
<li><input class="c-btn c-btn-submit" type="submit" value="<?php echo __('Save'); ?>" /></li>
61+
</ul>
62+
</section>
63+
64+
</form>
65+
66+
<?php end_slot(); ?>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php decorate_with('layout_2col.php'); ?>
2+
3+
<?php slot('sidebar'); ?>
4+
5+
<?php echo get_component('settings', 'menu'); ?>
6+
7+
<?php end_slot(); ?>
8+
9+
<?php slot('title'); ?>
10+
<h1>
11+
<?php echo __('Diacritics settings'); ?>
12+
</h1>
13+
<?php end_slot(); ?>
14+
15+
<?php slot('content'); ?>
16+
17+
<div class="alert alert-info">
18+
<p>
19+
<?php echo __('Please rebuild the search index after uploading diacritics mappings.'); ?>
20+
</p>
21+
<pre>$ php symfony search:populate</pre>
22+
</div>
23+
24+
<?php echo $form->renderGlobalErrors(); ?>
25+
26+
<?php echo $form->renderFormTag(url_for(['module' => 'settings', 'action' => 'diacritics'])); ?>
27+
28+
<?php echo $form->renderHiddenFields(); ?>
29+
30+
<div class="accordion mb-3">
31+
<div class="accordion-item">
32+
<h2 class="accordion-header" id="diacritics-heading">
33+
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#diacritics-collapse"
34+
aria-expanded="true" aria-controls="diacritics-collapse">
35+
<?php echo __('Diacritics Settings'); ?>
36+
</button>
37+
</h2>
38+
<div id="diacritics-collapse" class="accordion-collapse collapse show" aria-labelledby="diacritics-heading">
39+
<div class="accordion-body">
40+
<?php echo render_field($form->diacritics->label(__('Diacritics'))); ?>
41+
</div>
42+
</div>
43+
</div>
44+
<div class="accordion-item">
45+
<h2 class="accordion-header" id="mappings-heading">
46+
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#mappings-collapse"
47+
aria-expanded="false" aria-controls="mappings-collapse">
48+
<?php echo __('CSV Mapping YAML'); ?>
49+
</button>
50+
</h2>
51+
52+
<div id="mappings-collapse" class="accordion-collapse collapse show" aria-labelledby="sending-heading">
53+
54+
<div class="alert alert-info m-3 mb-0">
55+
<p>
56+
<?php echo __('Example CSV:'); ?>
57+
</p>
58+
<pre>type: mapping<br/>mappings:<br/> - À => A<br/> - Á => A</pre>
59+
</div>
60+
61+
<div class="accordion-body">
62+
<?php echo render_field($form->mappings->label(__('Mappings YAML'))); ?>
63+
</div>
64+
</div>
65+
</div>
66+
</div>
67+
68+
<section class="actions">
69+
<input class="btn atom-btn-outline-success" type="submit" value="<?php echo __('Save'); ?>" />
70+
</section>
71+
72+
</form>
73+
74+
<?php end_slot(); ?>

plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,12 @@ public static function loadMappings()
119119
{
120120
// Find mapping.yml
121121
$finder = sfFinder::type('file')->name('mapping.yml');
122-
$files = array_unique(array_merge(
123-
$finder->in(sfConfig::get('sf_config_dir')),
124-
$finder->in(ProjectConfiguration::getActive()->getPluginSubPaths('/config'))
125-
));
122+
$files = array_unique(
123+
array_merge(
124+
$finder->in(sfConfig::get('sf_config_dir')),
125+
$finder->in(ProjectConfiguration::getActive()->getPluginSubPaths('/config'))
126+
)
127+
);
126128

127129
if (!count($files)) {
128130
throw new sfException('You must create a mapping.xml file.');
@@ -135,6 +137,24 @@ public static function loadMappings()
135137
return $esMapping;
136138
}
137139

140+
public function loadDiacriticsMappings()
141+
{
142+
// Find diacritics_mapping.yml
143+
$diacriticsFinder = sfFinder::type('file')->name('diacritics_mapping.yml');
144+
$diacriticsFiles = array_unique(
145+
array_merge(
146+
$diacriticsFinder->in(sfConfig::get('sf_config_dir')),
147+
$diacriticsFinder->in(ProjectConfiguration::getActive()->getPluginSubPaths('/config'))
148+
)
149+
);
150+
151+
if (!count($diacriticsFiles)) {
152+
throw new sfException('You must create a diacritics_mapping.yml file.');
153+
}
154+
155+
return sfYaml::load(array_shift($diacriticsFiles));
156+
}
157+
138158
/**
139159
* Optimize index.
140160
*
@@ -272,10 +292,12 @@ public function populate($options = [])
272292
}
273293
}
274294

275-
$this->log(vsprintf(
276-
'Index populated with %s documents in %s seconds.',
277-
[$total, $timer->elapsed()]
278-
));
295+
$this->log(
296+
vsprintf(
297+
'Index populated with %s documents in %s seconds.',
298+
[$total, $timer->elapsed()]
299+
)
300+
);
279301

280302
if (!$showErrors) {
281303
return;
@@ -468,6 +490,10 @@ public static function modelClassFromQubitObjectClass($className)
468490
*/
469491
protected function initialize()
470492
{
493+
if (sfConfig::get('app_diacritics')) {
494+
$this->config['index']['configuration']['analysis']['char_filter']['diacritics_lowercase'] = $this->loadDiacriticsMappings();
495+
}
496+
471497
try {
472498
$this->index->open();
473499
} catch (Exception $e) {
@@ -479,7 +505,17 @@ protected function initialize()
479505
&& isset($this->config['index']['configuration']['analysis']['char_filter']['strip_md'])
480506
) {
481507
foreach ($this->config['index']['configuration']['analysis']['analyzer'] as $key => $analyzer) {
482-
$this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter'] = ['strip_md'];
508+
$filters = ['strip_md'];
509+
510+
if ($this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter']) {
511+
$filters = array_merge($filters, $this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter']);
512+
}
513+
514+
if (sfConfig::get('app_diacritics')) {
515+
$filters = array_merge($filters, ['diacritics_lowercase']);
516+
}
517+
518+
$this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter'] = $filters;
483519
}
484520
}
485521

0 commit comments

Comments
 (0)