3 namespace Drupal\search_api_synonym\Export;
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Plugin\DefaultPluginManager;
10 * Base class for search api synonym export plugin managers.
14 class ExportPluginManager extends DefaultPluginManager {
28 protected $exportOptions;
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');
42 * @param string $plugin_id
45 public function setPluginId($plugin_id) {
46 $this->pluginId = $plugin_id;
55 public function getPluginId() {
56 return $this->pluginId;
62 * @param array $export_options
63 * Array with export options
65 public function setExportOptions(array $export_options) {
66 $this->exportOptions = $export_options;
73 * Array with export options
75 public function getExportOptions() {
76 return $this->exportOptions;
80 * Get single export option.
88 public function getExportOption($key) {
89 return isset($this->exportOptions[$key]) ? $this->exportOptions[$key] : '';
93 * Gets a list of available export plugins.
96 * An array with the plugin names as keys and the descriptions as values.
98 public function getAvailableExportPlugins() {
99 // Use plugin system to get list of available export plugins.
100 $plugins = $this->getDefinitions();
103 foreach ($plugins as $id => $definition) {
104 $output[$id] = $definition;
111 * Validate that a specific export plugin exists.
113 * @param string $plugin
114 * The plugin machine name.
117 * TRUE if the plugin exists.
119 public function validatePlugin($plugin) {
120 if ($this->getDefinition($plugin, FALSE)) {
129 * Execute the synonym export.
134 public function executeExport() {
135 // Export plugin instance
136 $instance = $this->createInstance($this->getPluginId(), []);
138 // Get synonyms data matching the options.
139 $synonyms = $this->getSynonymsData();
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);
146 return $this->saveSynonymsFile($data);
154 * Get synonyms matching the export options.
157 * Array with synonyms
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');
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);
173 // Add filter condition if it is set and different from all.
174 $filter = $this->getExportOption('filter');
175 if ($filter && $filter != 'all') {
178 $query->condition('s.word', '% %', 'NOT LIKE');
179 $query->condition('s.synonyms', '% %', 'NOT LIKE');
182 $group = $query->orConditionGroup()
183 ->condition('s.word', '% %', 'LIKE')
184 ->condition('s.synonyms', '% %', 'LIKE');
185 $query = $query->condition($group);
190 // Add changed condition if incremental option is set.
191 if ($incremental = $this->getExportOption('incremental')) {
192 $query->condition('s.changed', $incremental, '>=');
196 return $query->execute()->fetchAllAssoc('sid');
200 * Save synonyms data to a file.
202 * @param string $data
203 * String with the synonyms data being written to a file.
206 * Return path to the saved synonyms file.
208 private function saveSynonymsFile($data) {
209 if ($file = $this->getExportOption('file')) {
213 $filename = $this->generateFileName();
216 // Create folder if it does not exist.
217 $folder = 'public://synonyms';
218 file_prepare_directory($folder, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
220 // Save file and return result.
221 $path = $folder . '/'. $filename;
222 return file_unmanaged_save_data($data, $path, FILE_EXISTS_REPLACE);
226 * Generate an export file name based on export options.
229 * The generated file name.
231 private function generateFileName() {
232 $options = $this->getExportOptions();
234 // Add benning of file name
235 $name[] = 'synonyms';
237 // Add language code as the first part of the file name.
238 $name[] = "lang_{$options['langcode']}";
240 // Add type option to file name
241 if (!empty($options['type'])) {
242 $name[] = "type_{$options['type']}";
245 // Add filter option to file name
246 if (!empty($options['filter'])) {
247 $name[] = "filter_{$options['filter']}";
250 // Implode the name parts.
251 return implode('__', $name) . '.txt';