You are here

public function SynonymDrushCommands::export in Search API Synonym 8

Export search synonyms to a specific format.

@command search-api-synonym:export @option plugin Machine name of the export plugin. E.g. solr. @option langcode Language being exported. Use the language code. E.g. en or da. @option type Synonym type. Allowed values: synonym = Synomyms, spelling_error = Spelling errors, all = All types (synonyms and spelling errors). Defaults to "alL". @option filter Export filter. Allowed values: nospace = Skip all words containing a space, onlyspace = Skip all words without a space. Defaults to "all". @option incremental Incremental export - use Unix timestamp. Only export synonyms changed after the provided timestamp. @option file File name used when saving the exported file. Include extension but not folder name!. @aliases sapi-syn:export, sapi-syn-ex @validate-module-enabled search_api_synonym @usage drush search-api-synonym:export --plugin=solr langcode=da type=spelling_error filter=all Export all Danish spelling errors in the Solr format. @usage sapi-syn:export --plugin=solr langcode=da type=spelling_error filter=all Export all Danish spelling errors in the Solr format.

File

src/Command/SynonymDrushCommands.php, line 52
Drush commands for the Search API Synonym module.

Class

SynonymDrushCommands
Drush commands for the Search API Synonym module.

Namespace

Drupal\search_api_synonym\Command

Code

public function export($options = [
  'plugin' => '',
  'langcode' => '',
  'type' => 'all',
  'filter' => 'all',
  'incremental' => 0,
  'file' => '',
]) {
  $error = FALSE;

  // Validate option: plugin
  if (!$this->pluginManager
    ->validatePlugin($options['plugin'])) {
    $error = TRUE;
    throw new \Exception('--plugin is not valid. Please, use an existing plugin machine name.');
  }

  // Validate option: langcode
  if (empty($options['langcode'])) {
    $error = TRUE;
    throw new \Exception('--langcode is not valid. Please, use an existing language code.');
  }

  // Validate option: type
  if (!$this
    ->validateOptionType($options['type'])) {
    $error = TRUE;
    throw new \Exception('--type option is not valid. The only allowed values are "synonym", "spelling_error", "all"');
  }

  // Validate option: filter
  if (!$this
    ->validateOptionFilter($options['filter'])) {
    $error = TRUE;
    throw new \Exception('--filter option is not valid. The only allowed values are "nospace", "onlyspace", "all"');
  }

  // Prepare export
  if (!$error) {
    $this
      ->output()
      ->writeln(dt('Starting synonym export....'));
    $options['incremental'] = (int) $options['incremental'];
    $this->pluginManager
      ->setPluginId($options['plugin']);
    $this->pluginManager
      ->setExportOptions($options);

    // Execute export
    if ($result = $this->pluginManager
      ->executeExport()) {

      // Output result
      $this
        ->output()
        ->writeln(dt('Synonyms export and saved in the following file:'));
      $this
        ->output()
        ->writeln($result);
    }
  }
}