Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / web / modules / contrib / search_api_synonym / src / Export / ExportPluginManager.php
1 <?php
2
3 namespace Drupal\search_api_synonym\Export;
4
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Plugin\DefaultPluginManager;
8
9 /**
10  * Base class for search api synonym export plugin managers.
11  *
12  * @ingroup plugin_api
13  */
14 class ExportPluginManager extends DefaultPluginManager {
15
16   /**
17    * Active plugin id
18    *
19    * @var string
20    */
21   protected $pluginId;
22
23   /**
24    * Export options.
25    *
26    * @var array
27    */
28   protected $exportOptions;
29
30   /**
31    * {@inheritdoc}
32    */
33   public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
34     parent::__construct('Plugin/search_api_synonym/export', $namespaces, $module_handler, 'Drupal\search_api_synonym\Export\ExportPluginInterface', 'Drupal\search_api_synonym\Annotation\SearchApiSynonymExport');
35     $this->alterInfo('search_api_synonym_export_info');
36     $this->setCacheBackend($cache_backend, 'search_api_synonym_export_info_plugins');
37   }
38
39   /**
40    * Set active plugin.
41    *
42    * @param string $plugin_id
43    *   The active plugin.
44    */
45   public function setPluginId($plugin_id) {
46     $this->pluginId = $plugin_id;
47   }
48
49   /**
50    * Get active plugin.
51    *
52    * @return string
53    *   The active plugin.
54    */
55   public function getPluginId() {
56     return $this->pluginId;
57   }
58
59   /**
60    * Set export options.
61    *
62    * @param array $export_options
63    *   Array with export options
64    */
65   public function setExportOptions(array $export_options) {
66     $this->exportOptions = $export_options;
67   }
68
69   /**
70    * Get export options.
71    *
72    * @return array
73    *   Array with export options
74    */
75   public function getExportOptions() {
76     return $this->exportOptions;
77   }
78
79   /**
80    * Get single export option.
81    *
82    * @param string $key
83    *   Option key
84    *
85    * @return string
86    *   Option value
87    */
88   public function getExportOption($key) {
89     return isset($this->exportOptions[$key]) ? $this->exportOptions[$key] : '';
90   }
91
92   /**
93    * Gets a list of available export plugins.
94    *
95    * @return array
96    *   An array with the plugin names as keys and the descriptions as values.
97    */
98   public function getAvailableExportPlugins() {
99     // Use plugin system to get list of available export plugins.
100     $plugins = $this->getDefinitions();
101
102     $output = [];
103     foreach ($plugins as $id => $definition) {
104       $output[$id] = $definition;
105     }
106
107     return $output;
108   }
109
110   /**
111    * Validate that a specific export plugin exists.
112    *
113    * @param string $plugin
114    *   The plugin machine name.
115    *
116    * @return boolean
117    *   TRUE if the plugin exists.
118    */
119   public function validatePlugin($plugin) {
120     if ($this->getDefinition($plugin, FALSE)) {
121       return TRUE;
122     }
123     else {
124       return FALSE;
125     }
126   }
127
128   /**
129    * Execute the synonym export.
130    *
131    * @return mixed
132    *   Export result
133    */
134   public function executeExport() {
135     // Export plugin instance
136     $instance = $this->createInstance($this->getPluginId(), []);
137
138     // Get synonyms data matching the options.
139     $synonyms = $this->getSynonymsData();
140
141     // We only export if full export or if their is new synonyms.
142     if (!($this->getExportOption('incremental') && empty($synonyms))) {
143       // Get data in the plugin instance format
144       $data = $instance->getFormattedSynonyms($synonyms);
145
146       return $this->saveSynonymsFile($data);
147     }
148     else {
149       return FALSE;
150     }
151   }
152
153   /**
154    * Get synonyms  matching the export options.
155    *
156    * @return array
157    *   Array with synonyms
158    */
159   private function getSynonymsData() {
160     // Create the db query.
161     $query = \Drupal::database()->select('search_api_synonym', 's');
162     $query->fields('s', ['sid', 'type', 'word', 'synonyms']);
163     $query->condition('s.status', 1);
164     $query->condition('s.langcode', $this->getExportOption('langcode'));
165     $query->orderBy('s.word');
166
167     // Add type condition if it is set and different from all.
168     $type = $this->getExportOption('type');
169     if ($type && $type != 'all') {
170       $query->condition('s.type', $type);
171     }
172
173     // Add filter condition if it is set and different from all.
174     $filter = $this->getExportOption('filter');
175     if ($filter && $filter != 'all') {
176       switch ($filter) {
177         case 'nospace':
178           $query->condition('s.word', '% %', 'NOT LIKE');
179           $query->condition('s.synonyms', '% %', 'NOT LIKE');
180           break;
181         case 'onlyspace':
182           $group = $query->orConditionGroup()
183             ->condition('s.word', '% %', 'LIKE')
184             ->condition('s.synonyms', '% %', 'LIKE');
185           $query = $query->condition($group);
186           break;
187       }
188     }
189
190     // Add changed condition if incremental option is set.
191     if ($incremental = $this->getExportOption('incremental')) {
192       $query->condition('s.changed', $incremental, '>=');
193     }
194
195     // Fetch the result.
196     return $query->execute()->fetchAllAssoc('sid');
197   }
198
199   /**
200    * Save synonyms data to a file.
201    *
202    * @param string $data
203    *   String with the synonyms data being written to a file.
204    *
205    * @return string
206    *   Return path to the saved synonyms file.
207    */
208   private function saveSynonymsFile($data) {
209     if ($file = $this->getExportOption('file')) {
210       $filename = $file;
211     }
212     else {
213       $filename = $this->generateFileName();
214     }
215
216     // Create folder if it does not exist.
217     $folder = 'public://synonyms';
218     file_prepare_directory($folder, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
219
220     // Save file and return result.
221     $path = $folder . '/'. $filename;
222     return file_unmanaged_save_data($data, $path, FILE_EXISTS_REPLACE);
223   }
224
225   /**
226    * Generate an export file name based on export options.
227    *
228    * @return string
229    *   The generated file name.
230    */
231   private function generateFileName() {
232     $options = $this->getExportOptions();
233
234     // Add benning of file name
235     $name[] = 'synonyms';
236
237     // Add language code as the first part of the file name.
238     $name[] = "lang_{$options['langcode']}";
239
240     // Add type option to file name
241     if (!empty($options['type'])) {
242       $name[] = "type_{$options['type']}";
243     }
244
245     // Add filter option to file name
246     if (!empty($options['filter'])) {
247       $name[] = "filter_{$options['filter']}";
248     }
249
250     // Implode the name parts.
251     return implode('__', $name) . '.txt';
252   }
253
254 }