class TikaExtractor in Search API attachments 9.0.x
Same name and namespace in other branches
- 8 src/Plugin/search_api_attachments/TikaExtractor.php \Drupal\search_api_attachments\Plugin\search_api_attachments\TikaExtractor
Provides tika extractor.
Plugin annotation
@SearchApiAttachmentsTextExtractor(
id = "tika_extractor",
label = @Translation("Tika Extractor"),
description = @Translation("Adds Tika extractor support."),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\search_api_attachments\TextExtractorPluginBase implements ContainerFactoryPluginInterface, TextExtractorPluginInterface
- class \Drupal\search_api_attachments\Plugin\search_api_attachments\TikaExtractor
- class \Drupal\search_api_attachments\TextExtractorPluginBase implements ContainerFactoryPluginInterface, TextExtractorPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of TikaExtractor
File
- src/
Plugin/ search_api_attachments/ TikaExtractor.php, line 18
Namespace
Drupal\search_api_attachments\Plugin\search_api_attachmentsView source
class TikaExtractor extends TextExtractorPluginBase {
/**
* Extract file with Tika library.
*
* @param \Drupal\file\Entity\File $file
* A file object.
*
* @return string
* The text extracted from the file.
*/
public function extract(File $file) {
$output = '';
$filepath = $this
->getRealpath($file
->getFileUri());
$tika = realpath($this->configuration['tika_path']);
$java = $this->configuration['java_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.
$backup_locale = setlocale(LC_CTYPE, '0');
setlocale(LC_CTYPE, 'en_US.UTF-8');
$param = '';
if ($file
->getMimeType() != 'audio/mpeg') {
$param = ' -Dfile.encoding=UTF8 -cp ' . escapeshellarg($tika);
}
// Force running the Tika jar headless.
$param = ' -Djava.awt.headless=true ' . $param;
$cmd = $java . $param . ' -jar ' . escapeshellarg($tika) . ' -t ' . escapeshellarg($filepath);
if (strpos(ini_get('extension_dir'), 'MAMP/')) {
$cmd = 'export DYLD_LIBRARY_PATH=""; ' . $cmd;
}
// Restore the locale.
setlocale(LC_CTYPE, $backup_locale);
// Support UTF-8 commands:
// @see http://www.php.net/manual/en/function.shell-exec.php#85095
shell_exec("LANG=en_US.utf-8");
$output = shell_exec($cmd);
if (is_null($output)) {
throw new \Exception('Tika Exctractor is not available.');
}
return $output;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['java_path'] = [
'#type' => 'textfield',
'#title' => $this
->t('Path to java executable'),
'#description' => $this
->t('Enter the path to java executable. Example: "java".'),
'#default_value' => $this->configuration['java_path'],
'#required' => TRUE,
];
$form['tika_path'] = [
'#type' => 'textfield',
'#title' => $this
->t('Path to Tika .jar file'),
'#description' => $this
->t('Enter the full path to tika executable jar file. Example: "/var/apache-tika/tika-app-1.8.jar".'),
'#default_value' => $this->configuration['tika_path'],
'#required' => TRUE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValue([
'text_extractor_config',
]);
$java_path = $values['java_path'];
$tika_path = $values['tika_path'];
// Check java path.
exec($java_path, $output, $return_code);
// $return_code = 127 if it fails. 1 instead.
if ($return_code != 1) {
$form_state
->setError($form['text_extractor_config']['java_path'], $this
->t('Invalid path or filename %path for java executable.', [
'%path' => $java_path,
]));
return;
}
// Check tika path.
if (!file_exists($tika_path)) {
$form_state
->setError($form['text_extractor_config']['tika_path'], $this
->t('Invalid path or filename %path for tika application jar.', [
'%path' => $tika_path,
]));
}
else {
$cmd = $java_path . ' -jar ' . escapeshellarg($tika_path) . ' -V';
exec($cmd, $output, $return_code);
// $return_code = 1 if it fails. 0 instead.
if ($return_code) {
$form_state
->setError($form['text_extractor_config']['tika_path'], $this
->t('Tika could not be reached and executed.'));
}
else {
$this
->getMessenger()
->addStatus(t('Tika can be reached and be executed'));
}
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['java_path'] = $form_state
->getValue([
'text_extractor_config',
'java_path',
]);
$this->configuration['tika_path'] = $form_state
->getValue([
'text_extractor_config',
'tika_path',
]);
parent::submitConfigurationForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TextExtractorPluginBase:: |
protected | property | Config factory service. | |
TextExtractorPluginBase:: |
protected | property |
The messenger. Overrides MessengerTrait:: |
|
TextExtractorPluginBase:: |
protected | property | Mime type guesser service. | |
TextExtractorPluginBase:: |
protected | property | Stream wrapper manager service. | |
TextExtractorPluginBase:: |
public | function | ||
TextExtractorPluginBase:: |
constant | Name of the config being edited. | ||
TextExtractorPluginBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
2 |
TextExtractorPluginBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
|
TextExtractorPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
TextExtractorPluginBase:: |
public | function | ||
TextExtractorPluginBase:: |
public | function | Helper method to get the PDF MIME types. | |
TextExtractorPluginBase:: |
public | function | Helper method to get the real path from an uri. | |
TextExtractorPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
TextExtractorPluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
2 |
TikaExtractor:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
TikaExtractor:: |
public | function |
Extract file with Tika library. Overrides TextExtractorPluginBase:: |
|
TikaExtractor:: |
public | function |
Form submission handler. Overrides TextExtractorPluginBase:: |
|
TikaExtractor:: |
public | function |
Form validation handler. Overrides TextExtractorPluginBase:: |