Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / web / modules / contrib / search_api_synonym / search_api_synonym.module
diff --git a/web/modules/contrib/search_api_synonym/search_api_synonym.module b/web/modules/contrib/search_api_synonym/search_api_synonym.module
new file mode 100644 (file)
index 0000000..e8ab743
--- /dev/null
@@ -0,0 +1,122 @@
+<?php
+
+/**
+ * @file
+ * Contains search_api_synonym.module.
+ */
+
+use Drupal\Core\Routing\RouteMatchInterface;
+
+/**
+ * Denotes that the synonym is not active.
+ */
+const SYNONYM_NOT_ACTIVE = 0;
+
+/**
+ * Denotes that the synonym is active.
+ */
+const SYNONYM_ACTIVE = 1;
+
+/**
+ * Implements hook_help().
+ */
+function search_api_synonym_help($route_name, RouteMatchInterface $route_match) {
+  switch ($route_name) {
+    // Main module help for the search_api_synonym module.
+    case 'help.page.search_api_synonym':
+      $output = '';
+      $output .= '<h3>' . t('About') . '</h3>';
+      $output .= '<p>' . t('Managing of search synonyms in Drupal.') . '</p>';
+      return $output;
+
+    default:
+      return '';
+  }
+}
+
+/**
+ * Implements hook_cron().
+ */
+function search_api_synonym_cron() {
+  $request_time = \Drupal::time()->getRequestTime();
+
+  // Export synonyms to files.
+  $conf = \Drupal::configFactory()->getEditable('search_api_synonym.settings')->get('cron');
+  $interval = !empty($conf['interval']) ? $conf['interval'] : 86400;
+  $next_execution = \Drupal::state()->get('search_api_synonym.export.next_execution', 0);
+
+  if ($request_time >= $next_execution) {
+    $logger = \Drupal::logger('search_api_synonym');
+    // Execute export
+    $logger->notice('Executing export');
+
+    // Plugin manager
+    $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');
+
+    // Validate option: plugin
+    $plugin = $conf['plugin'];
+    if (!$pluginManager->validatePlugin($plugin)) {
+      $logger->warning('Export plugin not found');
+      return;
+    }
+
+    // Setting non language specific export options
+    $options = [
+      'type' => $conf['type'],
+      'filter' => $conf['filter'],
+      'file' => '',
+      'incremental' => $conf['export_if_changed'] ? $next_execution : 0
+    ];
+
+    // Get all languages in the system
+    $languages = \Drupal::languageManager()->getLanguages();
+
+    foreach ($languages as $language) {
+      $options['langcode'] = $language->getId();
+
+      // Export synonyms with and without spaces into separate files
+      if ($conf['separate_files'] && ($conf['filter'] == 'none' || !$conf['filter'])) {
+        // Without spaces
+        $options['filter'] = 'nospace';
+        search_api_synonym_execute_single_import($plugin, $options);
+
+        // With spaces
+        $options['filter'] = 'onlyspace';
+        search_api_synonym_execute_single_import($plugin, $options);
+
+      }
+      else {
+        search_api_synonym_execute_single_import($plugin, $options);
+      }
+    }
+
+    $logger->info('Export done');
+
+    \Drupal::state()->set('search_api_synonym.export.next_execution', $request_time + $interval);
+  }
+
+}
+
+/**
+ * Execute single export.
+ *
+ * @param string $plugin
+ *   Plugin name
+ *
+ * @param array $options
+ *   Array of export options
+ */
+function search_api_synonym_execute_single_import($plugin, $options) {
+  // Plugin manager
+  $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');
+
+  // Logger
+  $logger = \Drupal::logger('search_api_synonym');
+
+  $pluginManager->setPluginId($plugin);
+  $pluginManager->setExportOptions($options);
+  if ($result = $pluginManager->executeExport()) {
+    $logger->info('Synonyms export to {filename}', ['filename' => $result]);
+  }
+
+}