5 * Drush commands for Search API Synonym.
9 * Implements hook_drush_command().
11 function search_api_synonym_drush_command() {
14 $items['search-api-synonym-export'] = [
15 'description' => 'Export search synonyms to a specific format.',
17 'drush search-api-synonym-export --plugin=solr langcode=da type=spelling_error filter=all' => dt('Export all Danish spelling errors in the Solr format.'),
18 'drush sapi-syn-ex --plugin=solr langcode=da type=spelling_error filter=all' => dt('Export all Danish spelling errors in the Solr format.'),
21 'plugin' => dt('Machine name of the export plugin. E.g. solr.'),
22 'langcode' => dt('Language being exported. Use the language code. E.g. en or da.'),
23 'type' => dt('Synonym type. Allowed values: synonym = Synomyms, spelling_error = Spelling errors, all = All types (synonyms and spelling errors). Defaults to "alL".'),
24 'filter' => dt('Export filter. Allowed values: nospace = Skip all words containing a space, onlyspace = Skip all words without a space. Defaults to "all".'),
25 'incremental' => dt('Incremental export - use Unix timestamp. Only export synonyms changed after the provided timestamp.'),
26 'file' => dt('File name used when saving the exported file. Include extension but not folder name!.'),
28 'aliases' => ['sapi-syn-ex'],
35 * Export synonyms to a flat file.
37 function drush_search_api_synonym_export() {
39 $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');
42 $plugin = drush_get_option('plugin');
43 $langcode = drush_get_option('langcode');
44 $type = drush_get_option('type', 'all');
45 $filter = drush_get_option('filter', 'all');
46 $file = drush_get_option('file');
47 $incremental = drush_get_option('incremental');
49 // Validate option: plugin
50 if (!$pluginManager->validatePlugin($plugin)) {
52 drush_set_error(dt('--plugin is not valid. Please, use an existing plugin machine name.'));
55 // Validate option: langcode
56 if (empty($langcode)) {
58 drush_set_error(dt('--langcode is not valid. Please, use an existing language code.'));
61 // Validate option: type
62 if (!empty($type) && !search_api_synonym_drush_validate_option_type($type)) {
64 drush_set_error(dt('--type option is not valid. The only allowed values are "synonym", "spelling_error", "all".'));
67 // Validate option: filter
68 if (!empty($filter) && !search_api_synonym_drush_validate_option_filter($filter)) {
70 drush_set_error(dt('--filter option is not valid. The only allowed values are "nospace", "onlyspace", "all".'));
75 drush_log(dt('Starting synonym export....'), 'ok');
78 'langcode' => $langcode,
82 'incremental' => (int) $incremental,
84 $pluginManager->setPluginId($plugin);
85 $pluginManager->setExportOptions($options);
88 if ($result = $pluginManager->executeExport()) {
91 drush_log(dt('Synonyms export and saved in the file @file', ['@file' => $result]), 'ok');
97 * Validate that the type option is valid.
100 * Type value from --type command option.
103 * TRUE if valid, FALSE if invalid.
105 function search_api_synonym_drush_validate_option_type($type) {
106 $types = ['synonym', 'spelling_error', 'all'];
107 return in_array($type, $types);
111 * Validate that the filter option is valid.
113 * @param string $filter
114 * Type value from --filter command option.
117 * TRUE if valid, FALSE if invalid.
119 function search_api_synonym_drush_validate_option_filter($filter) {
120 $filters = ['nospace', 'onlyspace', 'all'];
121 return in_array($filter, $filters);