Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add diacritics setting #1636

Merged
merged 1 commit into from
Aug 14, 2023
Merged

Add diacritics setting #1636

merged 1 commit into from
Aug 14, 2023

Conversation

melaniekung
Copy link
Contributor

@melaniekung melaniekung commented Jul 12, 2023

New setting for users to enable further char_filter mappings for diacritics, assigned to the default, autocomplete, english and french analyzers. Requires a yaml file to enable the setting, example attached to this gist for reference: https://gist.github.com/melaniekung/6014e100767019b1f85637e5a7cdf9df. Commit includes an empty diacritics_mapping.yml (default) which will be replaced with the submitted yaml to map out the required diacritics.

Copy link
Member

@mcantelon mcantelon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some feedback, but looks good overall!

));

if (!count($diacriticsFiles)) {
throw new sfException('You must create a diacritics_mapping.xml file.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the "xml" in this line be "yml"?

$this->form->bind($request->getPostParameters(), $request->getFiles());

if ($this->form->isValid()) {
if (0 === intval($request->getPostParameters()['diacritics']) || (1 === intval($request->getPostParameters()['diacritics']) && 0 !== $request->getFiles()['mappings']['size'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Instead of 0 === intval you could also use empty"for the check (and insted of 1 === intval you could use empty.


if (1 === (int) $request->getPostParameters()['diacritics']) {
if ('application/x-yaml' === $request->getFiles()['mappings']['type']) {
// Move uploaded file to new location to pass off to background arFileImportJob.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this handled by a background job? I don't see that in the cde.

}

// Replace /config/diacritics_mapping.yml with uploaded file
$diacriticsMappingPath = '/atom/src/plugins/arElasticSearchPlugin/config/diacritics_mapping.yml';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be an absolute path given that where AtoM's code lives may vary? Also I'm wondering if the config dir in the root of the AtoM installation might be better given that files in plugin conf directories usually don't get changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to get AtoM's base code location instead of using /atom/src/? i chose to keep it in arElasticSearchPlugin/config since that's where mapping.yml is located and I reused some of the code for that in arElasticSearchPlugin.class.php, but could change that if you think it's better.

Copy link
Member

@mcantelon mcantelon Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you might be right about putting it in the plugin directory so I'd say go with that (I'm a bit fuzzy on Symfony's config best practices, tbh).

To get the plugin's config directory path dynamically you can do:

sfConfig::get('sf_plugins_dir').DIRECTORY_SEPARATOR.'arElasticSearchPlugin'.DIRECTORY_SEPARATOR.'config';

}
} else {
// Reset diacritics yaml when disabling the setting
$diacriticsMappingPath = '/atom/src/plugins/arElasticSearchPlugin/config/diacritics_mapping.yml';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable could maybe be set earlier in code as it ends up being set to the same thing twice.

<?php slot('content'); ?>

<div class="alert alert-info">
<p><?php echo __('Please rebuild the search index if you are using diacritics mappings.'); ?></p>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thinking to add this as a reminder given that folks may miss the flash message about it. Maybe specify that this needs to be done after the mapping is uploaded.

<tr>
<td><?php echo __('Mappings YAML'); ?></td>
<td>
<span><?php echo 'YAML file containing char_filter mappings.'; ?></span></br>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the text being echoed here be wrapped in a call to the __ function?

@@ -3,7 +3,7 @@ QubitSetting:
name: version
editable: 0
deleteable: 0
value: 193
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if a migration's actually needed for this as I think your call to parent::earlyExecute(); will end up creating the new setting in the DB.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initially, we started out with a global setting so we created a migration. if it's not needed, what changes do i need to make?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd try using the setting in a fresh install of AtoM without running the migration and see if it works. If it works without the migration then all you need to do is remove the changes settings.yml from your dev change (and remove the actual migration file).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, thanks mike! i think all the mentioned issues have been resolved now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done! Looks good!

@@ -117,6 +123,8 @@ public function __destruct()

public static function loadMappings()
{
global $globalDiacritics;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you just use the protected $globalDiacritics property declared earlier?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it's a static function, i can't use $this so i'm not sure how else to access $globalDiacritics, suggestions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like something like this could work:

  1. Move "Check for diacritics_mapping.yml" code into a new method (called loadCharMapping or loadCharFilter whatever makes sense)
  2. Have this method return the results of sfYaml::load
  3. In the initialize method set $this->config['index']['configuration']['analysis']['char_filter']['diacritics_lowercase'] to the results of a call to this method

@melaniekung melaniekung force-pushed the dev/diacritics branch 3 times, most recently from 35c1e92 to a20cde0 Compare July 13, 2023 21:32
Copy link
Member

@mcantelon mcantelon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! I've added a bit of feedback, but nothing major!

$this->uploadDiacritics($request);

$this->getUser()->setFlash('notice', $this->context->i18n->__('Diacritics setting updated. Rebuild the search index for the changes to be applied.'));
$this->redirect(['module' => 'settings', 'action' => 'diacritics']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same redirect is done in both if conditions. You could do it after the if statement.

}
} else {
// Reset diacritics yaml when disabling the setting
file_put_contents($diacriticsMappingPath, '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just delete the file via unlink?

try {
$file = Qubit::moveUploadFile($file);
} catch (sfException $e) {
$this->getUser()->setFlash('error', $e->getMessage());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better, for security reasons, to provide a generic error like "Error: unable to upload" or some such thing. Otherwise the error message might divulge system information.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should also return after setting the flash, right?

@@ -1351,6 +1351,11 @@ QubitSetting:
Qubit_Settings_defaultArchivalDescriptionBrowseView:
name: default_archival_description_browse_view
value: table
Qubit_Settings_diacritics:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to add anything to this file. I think the setting will be dynamically created no matter what.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if i remove these lines, i get a 500 error when i try to open up the diacritics settings page.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah weird... I'll take a look.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try just changing line 33 to "diacritics" => "0"?

Copy link
Member

@mcantelon mcantelon Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Hmm that doesn't seem to work - gets rid of 500 error but causes other issue. I'll poke around more.)

Copy link
Contributor Author

@melaniekung melaniekung Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm were you able to upload a yaml file with those changes? i get the following error when uploading:

atom_1           | [18-Jul-2023 17:25:16] WARNING: [pool atom] child 15 said into stderr: "NOTICE: PHP message: This form is multipart, which means you need to supply a files array as the bind() method second argument."

and this error was the reason why I initially used DefaultEditAction::execute($request); as opposed to parent::execute($request);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to upload... I did run into that error but there was a change I made that resolved that. It's entirely possible there was a change I made that I forgot to add to the list, though, so I'll re-create the changes to see if I missed something!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think I tweaked apps/qubit/modules/settings/actions/editAction.class.php to get it to work and forgot about it. Changing line 31 of that file to $this->form->bind($request->getPostParameters(), $request->getFiles()); fixes things (basically just adding the second argument in the function call so the SettingsEditAction can work with multipart form data). So I'd say include that fix in your PR as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah right, i've tried that before but then it runs the parent functions and skips uploadDiaricits(), which is where all my error handling is, i'll play around more and see if there's another way for it to run the function! thanks mike!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay okay, i found a solution to that! lmk what you think of this solution!

Copy link
Member

@mcantelon mcantelon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting close! Thanks for your patience!

$uploadMappingPath = $file['tmp_name'];
$diacriticsMappings = file_get_contents($uploadMappingPath);
file_put_contents($diacriticsMappingPath, $diacriticsMappings);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to put an else in this logic to flash an error at the user if file isn't "application/x-yaml".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for your patience with my work XD i also updated diacriticsSuccess.php to make the save button match those in BS5, but that changed the look on BS2 theme. any suggestions on making them match other save buttons in both themes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too up-to-speed on BS5 but I'll take a look!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i added a success page to the BS5 theme plugin to resolve this issue!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to put an else in this logic to flash an error at the user if file isn't "application/x-yaml".

If setting the mine type in the sfValidatorFile, it will work like a normal form error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call!

@melaniekung melaniekung force-pushed the dev/diacritics branch 8 times, most recently from d879e36 to 0110b3c Compare July 24, 2023 15:49
public function uploadDiacritics($request)
{
$file = $request->getFiles('mappings');
$diacriticsMappingPath = sfConfig::get('sf_plugins_dir').DIRECTORY_SEPARATOR.'arElasticSearchPlugin'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'diacritics_mapping.yml';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be good to make this multi-line... something like:

    arElasticSearchPlugin' . DIRECTORY_SEPARATOR . 
    'config' . DIRECTORY_SEPARATOR .
    'diacritics_mapping.yml';```

@mcantelon
Copy link
Member

mcantelon commented Jul 24, 2023

It's looking great... on the home stretch!

Sorry for not looking at the BS5 earlier, but seems you worked out a nice fix.

There's a couple minor issues left (including one nitpick I just added) but seems, unless @jraddaoui spots anything, like it should be good to go after that:

@melaniekung
Copy link
Contributor Author

It's looking great... on the home stretch!

Sorry for not looking at the BS5 earlier, but seems you worked out a nice fix.

There's a couple minor issues left (including one nitpick I just added) but seems, unless @jraddaoui spots anything, like it should be good to go after that:

@melaniekung melaniekung reopened this Jul 25, 2023
Copy link
Contributor

@jraddaoui jraddaoui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, a bit late to the review of the form part and I still couldn't check the templates, but I have added a few comments.

try {
$file = Qubit::moveUploadFile($file);
} catch (sfException $e) {
$this->getUser()->setFlash('error', $e->getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should also return after setting the flash, right?

$uploadMappingPath = $file['tmp_name'];
$diacriticsMappings = file_get_contents($uploadMappingPath);
file_put_contents($diacriticsMappingPath, $diacriticsMappings);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to put an else in this logic to flash an error at the user if file isn't "application/x-yaml".

If setting the mine type in the sfValidatorFile, it will work like a normal form error.

apps/qubit/modules/settings/actions/editAction.class.php Outdated Show resolved Hide resolved
@melaniekung melaniekung force-pushed the dev/diacritics branch 2 times, most recently from a2552d5 to ee4095c Compare July 28, 2023 00:07
Copy link
Member

@mcantelon mcantelon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there!

$uploadMappingPath = $file['tmp_name'];
$diacriticsMappings = file_get_contents($uploadMappingPath);
file_put_contents($diacriticsMappingPath, $diacriticsMappings);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call!

// Reset diacritics settings when uploading yaml fails
QubitSetting::findAndSave('diacritics', 0, ['sourceCulture' => true]);
unlink($diacriticsMappingPath);
$this->getUser()->setFlash('error', $this->context->i18n->__('Error: Unable to upload diacritics mapping.'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add a line unset($this->updateMessage); after the setFlash line then you should be good!

Copy link
Member

@mcantelon mcantelon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@mcantelon
Copy link
Member

@jraddaoui How's this looking to you? I've approved but needs your okay to merge.

@melaniekung melaniekung force-pushed the dev/diacritics branch 2 times, most recently from 2f15fc1 to 35d1a58 Compare August 11, 2023 16:35
Copy link
Contributor

@jraddaoui jraddaoui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting close! Some tweaks for the validation below, still looking at the B5 template.

$this->config['index']['configuration']['analysis']['char_filter']['diacritics_lowercase'] = $this->loadDiacriticsMappings();
}

$this->loadMappings();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this extra mapping load?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this second mapping is for the mapping.yml, separate function than loadDiacriticsMapping,

public static function loadMappings()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why do we need to call it here? It's not assigning the returned value to anything and it will be called below through $this->loadAndNormalizeMappings(); before the mappings are really needed.

Copy link
Contributor

@jraddaoui jraddaoui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the B5 template:

Copy link
Contributor

@jraddaoui jraddaoui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @melaniekung! Only the comment about $this->loadMappings(); left, and added a few nitpicks.

$this->getUser()->setFlash('error', $this->context->i18n->__('Unable to upload diacritis mapping yaml file.'));
unset($this->updateMessage);

return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: this return is no longer needed.

Comment on lines 40 to 43
<?php echo render_field(
$form->diacritics
->label(__('Diacritics'))
); ?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: it could fit in one line.

Comment on lines 65 to 66
<?php echo render_field($form->mappings
->label(__('Mappings YAML'))); ?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: it could fit in one line.


</form>

<?php end_slot(); ?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: missing empty line at the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jraddaoui changes made :)

Copy link
Contributor

@jraddaoui jraddaoui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @melaniekung, nice work!

Code looks good to me but now that I check I don't see a ticket/issue related to it. Make sure the @artefactual/atom-maintainers know about this feature before merging, as these changes will require documentation and deployment considerations (for the config/diacritics_mapping.yml file). As we chatted, a next iteration of this feature could involve moving the actual mapping into the DB (now that it even has a settings page), to avoid the use of the filesystem and configuration files.

@melaniekung melaniekung merged commit 9dd0656 into qa/2.x Aug 14, 2023
6 checks passed
@melaniekung melaniekung deleted the dev/diacritics branch August 14, 2023 18:54
@anvit anvit added this to the 2.8.0 milestone Sep 15, 2023
@melaniekung melaniekung linked an issue Oct 20, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Diacritics setting funtionality
4 participants