13a98524d2237c7b6b6219bc5856ab93e3613d85
[yaffs-website] / web / modules / contrib / search_api_synonym / src / Plugin / search_api_synonym / import / Solr.php
1 <?php
2
3 namespace Drupal\search_api_synonym\Plugin\search_api_synonym\import;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\file\Entity\File;
7 use Drupal\Core\Link;
8 use Drupal\Core\Url;
9 use Drupal\search_api_synonym\Import\ImportPluginBase;
10 use Drupal\search_api_synonym\Import\ImportPluginInterface;
11
12 /**
13  * Import of Solr synonyms.txt files.
14  *
15  * @SearchApiSynonymImport(
16  *   id = "solr",
17  *   label = @Translation("Solr"),
18  *   description = @Translation("Synonym import plugin from Solr synonyms.txt file.")
19  * )
20  */
21 class Solr extends ImportPluginBase implements ImportPluginInterface {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function parseFile(File $file, array $settings = []) {
27     $data = [];
28
29     // Read file.
30     $rows = file($file->getFileUri());
31
32     if (is_array($rows)) {
33       foreach ($rows as $row) {
34         $row = trim($row);
35
36         // Skip comment lines
37         if (empty($row) || substr($row, 0, 1) == '#') {
38           continue;
39         }
40
41         $parts = explode('=>', $row);
42
43         // Spelling error.
44         if (count($parts) == 2) {
45           $data[] = [
46             'word' => trim($parts[0]),
47             'synonym' => trim($parts['1']),
48             'type' => 'spelling_error'
49           ];
50         }
51         // Synonym.
52         else {
53           $data[] = [
54             'word' => trim(substr($row, 0, strpos($row, ','))),
55             'synonym' => trim(substr($row, strpos($row, ',') + 1)),
56             'type' => 'synonym'
57           ];
58         }
59       }
60     }
61
62     return $data;
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
69     $example_url = 'internal:' . base_path() . drupal_get_path('module', 'search_api_synonym') . '/examples/solr_synonyms.txt';
70     $form['template'] = [
71       '#type' => 'item',
72       '#title' => $this->t('Example'),
73       '#markup' => Link::fromTextAndUrl(t('Download example file'), Url::fromUri($example_url))->toString()
74     ];
75     return $form;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function allowedExtensions() {
88     return ['txt'];
89   }
90
91 }