Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / web / modules / contrib / search_api_synonym / search_api_synonym.drush.inc
1 <?php
2
3 /**
4  * @file
5  * Drush commands for Search API Synonym.
6  */
7
8 /**
9  * Implements hook_drush_command().
10  */
11 function search_api_synonym_drush_command() {
12   $items = [];
13
14   $items['search-api-synonym-export'] = [
15     'description' => 'Export search synonyms to a specific format.',
16     'examples' => [
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.'),
19     ],
20     'options' => [
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!.'),
27     ],
28     'aliases' => ['sapi-syn-ex'],
29   ];
30
31   return $items;
32 }
33
34 /**
35  * Export synonyms to a flat file.
36  */
37 function drush_search_api_synonym_export() {
38   // Plugin manager
39   $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');
40
41   // Options
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');
48
49   // Validate option: plugin
50   if (!$pluginManager->validatePlugin($plugin)) {
51     $error = TRUE;
52     drush_set_error(dt('--plugin is not valid. Please, use an existing plugin machine name.'));
53   }
54
55   // Validate option: langcode
56   if (empty($langcode)) {
57     $error = TRUE;
58     drush_set_error(dt('--langcode is not valid. Please, use an existing language code.'));
59   }
60
61   // Validate option: type
62   if (!empty($type) && !search_api_synonym_drush_validate_option_type($type)) {
63     $error = TRUE;
64     drush_set_error(dt('--type option is not valid. The only allowed values are "synonym", "spelling_error", "all".'));
65   }
66
67   // Validate option: filter
68   if (!empty($filter) && !search_api_synonym_drush_validate_option_filter($filter)) {
69     $error = TRUE;
70     drush_set_error(dt('--filter option is not valid. The only allowed values are "nospace", "onlyspace", "all".'));
71   }
72
73   // Prepare export
74   if (!isset($error)) {
75     drush_log(dt('Starting synonym export....'), 'ok');
76
77     $options = [
78       'langcode' => $langcode,
79       'type' => $type,
80       'filter' => $filter,
81       'file' => $file,
82       'incremental' => (int) $incremental,
83     ];
84     $pluginManager->setPluginId($plugin);
85     $pluginManager->setExportOptions($options);
86
87     // Execute export
88     if ($result = $pluginManager->executeExport()) {
89
90       // Output result
91       drush_log(dt('Synonyms export and saved in the file @file', ['@file' => $result]), 'ok');
92     }
93   }
94 }
95
96 /**
97  * Validate that the type option is valid.
98  *
99  * @param string $type
100  *   Type value from --type command option.
101  *
102  * @return boolean
103  *   TRUE if valid, FALSE if invalid.
104  */
105 function search_api_synonym_drush_validate_option_type($type) {
106   $types = ['synonym', 'spelling_error', 'all'];
107   return in_array($type, $types);
108 }
109
110 /**
111  * Validate that the filter option is valid.
112  *
113  * @param string $filter
114  *   Type value from --filter command option.
115  *
116  * @return boolean
117  *   TRUE if valid, FALSE if invalid.
118  */
119 function search_api_synonym_drush_validate_option_filter($filter) {
120   $filters = ['nospace', 'onlyspace', 'all'];
121   return in_array($filter, $filters);
122 }