From d62091fdc951d0db9a2106b8e8f6221a99307dbc Mon Sep 17 00:00:00 2001 From: Florent Torregrosa Date: Sat, 9 May 2015 18:36:03 +0200 Subject: [PATCH] Issue #2470553: Fix some typo, coding standard, IDE warnings, and missing variable. --- src/Form/TextExtractorFormSettings.php | 41 ++++++++++++---------- .../SolrExtractor.php | 11 +++--- .../TikaExtractor.php | 3 +- .../processor/FilesFieldsProcessorPlugin.php | 13 ++++--- src/TextExtractorPluginInterface.php | 5 ++- 5 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/Form/TextExtractorFormSettings.php b/src/Form/TextExtractorFormSettings.php index fad5c81..613e858 100644 --- a/src/Form/TextExtractorFormSettings.php +++ b/src/Form/TextExtractorFormSettings.php @@ -6,7 +6,6 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\search_api_attachments\TextExtractorPluginBase; use Drupal\search_api_attachments\TextExtractorPluginManager; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -89,7 +88,7 @@ class TextExtractorFormSettings extends ConfigFormBase { */ public function validateForm(array &$form, FormStateInterface $form_state) { $config = $this->config(static::CONFIGNAME); - // if it is from the configuration + // If it is from the configuration. $extractor_plugin_id = $form_state->getValue('extraction_method'); if ($extractor_plugin_id) { $configuration = $config->get($extractor_plugin_id . '_configuration'); @@ -103,23 +102,26 @@ class TextExtractorFormSettings extends ConfigFormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { $config = $this->config(static::CONFIGNAME); - // it is due to the ajax. - $extractor_plugin_id = $form_state->getValue('extraction_method'); + // It is due to the ajax. + $extractor_plugin_id = $form_state->getValue('extraction_method'); if ($extractor_plugin_id) { $configuration = $config->get($extractor_plugin_id . '_configuration'); $extractor_plugin = $this->textExtractorPluginManager->createInstance($extractor_plugin_id, $configuration); $extractor_plugin->submitConfigurationForm($form, $form_state); } - $config = \Drupal::configFactory() - ->getEditable(static::CONFIGNAME); + + // Set the extraction method variable. + $config = \Drupal::configFactory()->getEditable(static::CONFIGNAME); $config->set('extraction_method', $extractor_plugin_id); $config->save(); + + // Test the extraction. $file = $this->getTestFile(); $extracted_data = $extractor_plugin->extract($file); $file->delete(); if ($extracted_data == '') { - $data = $this->t("Unfortunately, the extraction doesn't seem to work with this configuration !"); + $data = $this->t("Unfortunately, the extraction doesn't seem to work with this configuration!"); } else { $data = $this->t('Extracted data: %extracted_data', array('%extracted_data' => $extracted_data)); @@ -136,7 +138,7 @@ class TextExtractorFormSettings extends ConfigFormBase { * Get definition of Extraction plugins from their annotation definition. * * @return array - * Array with 'labels' and 'descriptions' as keys contaigning plugin ids + * Array with 'labels' and 'descriptions' as keys containing plugin ids * and their labels or descriptions. */ public function getExtractionPluginInformations() { @@ -168,7 +170,7 @@ class TextExtractorFormSettings extends ConfigFormBase { ); $config = $this->config(static::CONFIGNAME); if ($form_state->getValue('extraction_method') != '') { - // it is due to the ajax. + // It is due to the ajax. $extractor_plugin_id = $form_state->getValue('extraction_method'); } else { @@ -179,8 +181,9 @@ class TextExtractorFormSettings extends ConfigFormBase { $form['text_extractor_config']['#title'] = $this->t('Configure extractor %plugin', array('%plugin' => $this->getExtractionPluginInformations()['labels'][$extractor_plugin_id])); $form['text_extractor_config']['#description'] = $this->getExtractionPluginInformations()['descriptions'][$extractor_plugin_id]; $form['text_extractor_config']['#open'] = TRUE; - // If the form is submitted with ajax and the empty value chosed or if there - // is no configuration yet and no extraction method was chosen in the form. + // If the form is submitted with ajax and the empty value is chosen or if + // there is no configuration yet and no extraction method was chosen in the + // form. if ($ajax_submitted_empty_value || !$extractor_plugin_id) { $form['text_extractor_config']['#title'] = $this->t('Please make a choice'); $form['text_extractor_config']['#description'] = $this->t('Please choose an extraction method in the list above.'); @@ -204,15 +207,16 @@ class TextExtractorFormSettings extends ConfigFormBase { */ public function buildTextExtractorTestResultForm(array &$form, FormStateInterface $form_state) { if (isset($form['text_extractor_config'])) { + $extractor_plugin_id = $form_state->getValue('extraction_method'); $form['text_extractor_config']['test_result']['#type'] = 'details'; $form['text_extractor_config']['test_result']['#title'] = $this->t('Test extractor %plugin', array('%plugin' => $this->getExtractionPluginInformations()['labels'][$extractor_plugin_id])); $form['text_extractor_config']['test_result']['#open'] = TRUE; $storage = $form_state->getStorage(); if (empty($storage)) { - // Put the initial thing into the storage + // Put the initial thing into the storage. $storage = array( - 'extracted_test_text' => $this->t("Extraction doesn't seem to work"), + 'extracted_test_text' => $this->t("Extraction doesn't seem to work."), ); $form_state->setStorage($storage); } @@ -228,13 +232,14 @@ class TextExtractorFormSettings extends ConfigFormBase { * @return array */ public static function buildAjaxTextExtractorConfigForm(array $form, FormStateInterface $form_state) { - //We just need to return the relevant part of the form here. + // We just need to return the relevant part of the form here. return $form['text_extractor_config']; } /** * Helper method to get/create a html test file and extract its data. * The file created is then deleted after successful extraction. + * * @return object $file */ public function getTestFile() { @@ -244,19 +249,19 @@ class TextExtractorFormSettings extends ConfigFormBase { -Congratulations ! +Congratulations!
-

' . $this->t('The extraction seems working !') . '

-

' . $this->t('Yay !') . '

+

' . $this->t('The extraction seems working!') . '

+

' . $this->t('Yay!') . '

'; - $filepath = 'public://search_api_attachments_test_extraction.html'; $values = array('uri' => $filepath); $file = entity_load_multiple_by_properties('file', $values); if (empty($file)) { + // Create the file. file_put_contents($filepath, $contents); $file = entity_create('file', array( diff --git a/src/Plugin/SearchApiAttachmentsTextExtractor/SolrExtractor.php b/src/Plugin/SearchApiAttachmentsTextExtractor/SolrExtractor.php index 6eb75af..130f3aa 100644 --- a/src/Plugin/SearchApiAttachmentsTextExtractor/SolrExtractor.php +++ b/src/Plugin/SearchApiAttachmentsTextExtractor/SolrExtractor.php @@ -4,7 +4,6 @@ namespace Drupal\search_api_attachments\Plugin\SearchApiAttachmentsTextExtractor use Drupal\Component\Serialization\Json; use Drupal\Core\Form\FormStateInterface; -use Drupal\search_api\Entity\Server; use Drupal\search_api_attachments\TextExtractorPluginBase; use Symfony\Component\Serializer\Encoder\XmlEncoder; @@ -35,7 +34,7 @@ class SolrExtractor extends TextExtractorPluginBase { $client = $backend->getSolrConnection(); // Create the Query. $query = $client->createExtract(); - //setExtractOnly is only available in solarium 3.3.0 and up. + // setExtractOnly is only available in solarium 3.3.0 and up. $query->setExtractOnly(TRUE); $query->setFile($filepath); // Execute the query. @@ -43,13 +42,13 @@ class SolrExtractor extends TextExtractorPluginBase { $response = $result->getResponse(); $json_data = $response->getBody(); $array_data = Json::decode($json_data); - // $array_data contains json array with two keys : [filename] that contains the - // extracted text we need and [filename]_metadata that contains some extra - // metadata. + // $array_data contains json array with two keys : [filename] that contains + // the extracted text we need and [filename]_metadata that contains some + // extra metadata. $xml_data = $array_data[$filepath]; // We need to get only what is in body tag. $xmlencoder = new XmlEncoder(); - $dom_data = $xmlencoder->decode($xml_data); + $dom_data = $xmlencoder->decode($xml_data, 'xml'); $dom_data = $dom_data['body']; $htmlencoder = new XmlEncoder(); diff --git a/src/Plugin/SearchApiAttachmentsTextExtractor/TikaExtractor.php b/src/Plugin/SearchApiAttachmentsTextExtractor/TikaExtractor.php index 88f58e1..1c035d5 100644 --- a/src/Plugin/SearchApiAttachmentsTextExtractor/TikaExtractor.php +++ b/src/Plugin/SearchApiAttachmentsTextExtractor/TikaExtractor.php @@ -30,7 +30,8 @@ class TikaExtractor extends TextExtractorPluginBase { $tika = realpath($this->configuration['tika_path']); // UTF-8 multibyte characters will be stripped by escapeshellargs() for the // default C-locale. - // So temporarily set the locale to UTF-8 so that the filepath remains valid. + // So temporarily set the locale to UTF-8 so that the filepath remains + // valid. $backup_locale = setlocale(LC_CTYPE, '0'); setlocale(LC_CTYPE, 'en_US.UTF-8'); $param = ''; diff --git a/src/Plugin/search_api/processor/FilesFieldsProcessorPlugin.php b/src/Plugin/search_api/processor/FilesFieldsProcessorPlugin.php index 830a192..a5d46f7 100644 --- a/src/Plugin/search_api/processor/FilesFieldsProcessorPlugin.php +++ b/src/Plugin/search_api/processor/FilesFieldsProcessorPlugin.php @@ -75,8 +75,7 @@ class FilesFieldsProcessorPlugin extends ProcessorPluginBase { if (!($field = $item->getField('search_api_attachments_' . $field_name))) { continue; } - $config = Drupal::configFactory() - ->getEditable(static::CONFIGNAME); + $config = Drupal::configFactory()->getEditable(static::CONFIGNAME); $extractor_plugin_id = $config->get('extraction_method'); $configuration = $config->get($extractor_plugin_id . '_configuration'); if ($extractor_plugin_id) { @@ -104,14 +103,20 @@ class FilesFieldsProcessorPlugin extends ProcessorPluginBase { } } + /** + * Helper method to get the file fields of indexed bundles. + * + * @return array + * An array of file fields. With field name as key and label as value. + */ protected function getFileFields() { $file_fields = array(); // Retrieve file fields of indexed bundles. foreach ($this->getIndex()->getDatasources() as $datasource) { foreach ($datasource->getPropertyDefinitions() as $property) { if ($property instanceof FieldConfig) { - if ($property->field_type == 'file') { - $file_fields[$property->field_name] = $property->label; + if ($property->get('field_type') == 'file') { + $file_fields[$property->get('field_name')] = $property->get('label'); } } } diff --git a/src/TextExtractorPluginInterface.php b/src/TextExtractorPluginInterface.php index 1728d47..7aadf94 100644 --- a/src/TextExtractorPluginInterface.php +++ b/src/TextExtractorPluginInterface.php @@ -2,7 +2,6 @@ namespace Drupal\search_api_attachments; -use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\PluginFormInterface; use Drupal\Component\Plugin\ConfigurablePluginInterface; @@ -16,11 +15,11 @@ interface TextExtractorPluginInterface extends PluginFormInterface, Configurable /** * Extract method. * - * @param type $file + * @param $file * The file object. * * @return string - * Teh file extracted content. + * The file extracted content. */ public function extract($file); -- 2.1.4