You are here

function drush_patterns_import in Patterns 6.2

Imports the specified patterns file

Parameters

string path to the patterns file:

string optional machine readable name for the pattern you are importing:

Return value

mixed bool|string FALSE upon failure; the name of the imported pattern upon success

1 call to drush_patterns_import()
drush_patterns_run in ./patterns.drush.inc
Imports, Enables, and Runs the specified pattern file
1 string reference to 'drush_patterns_import'
patterns_drush_command in ./patterns.drush.inc
Implementation of hook_drush_command().

File

./patterns.drush.inc, line 177
Drush Patterns module commands

Code

function drush_patterns_import($pattern_file, $pattern_name = '') {
  static $patterns_dir = NULL;
  drush_print(dt("Importing new pattern file '{$pattern_file}'"));
  if (!file_exists($pattern_file)) {
    drush_set_error(dt("File '{$pattern_file}' does not exist."));
    return FALSE;
  }
  $form_state = array();
  $form_state['values'] = array();
  $pattern_path_info = pathinfo($pattern_file);
  $result = TRUE;
  $err_msg = '';
  if ($pattern_path_info['extension'] == 'yaml') {
    drush_set_error(dt('Import feature currently supports only XML file format.'), 'warning');
  }
  else {

    //assume xml
    $xml = file_get_contents($pattern_file);
    if (!$xml) {
      drush_set_error(dt("XML pattern file '{$pattern_file}' is unreadable."));
      $result = FALSE;
    }
  }
  if (!$result) {
    drush_set_error(dt("Failed to import '{$pattern_file}'."));
    return FALSE;
  }

  // no pretty support in simplexml_..., so using DOMDocument
  $xmldoc = new DOMDocument();
  $xmldoc->formatOutput = TRUE;
  $xmldoc
    ->loadXML($xml);
  $pattern_source = $xmldoc
    ->saveXML();
  if (empty($pattern_name)) {
    $xpath = new DOMXpath($xmldoc);
    $query = 'info/title';
    $entries = $xpath
      ->query($query);

    //returns DOMNodeList
    foreach ($entries as $entry) {
      $pattern_name = $entry->textContent;
      break;
    }
  }

  /**
   * Make sure pattern name is machine readable
   *
   * would rather do this all in one go, but I can't seem to make the order
   * of the replacements work right.
   */
  $pattern_name = preg_replace(array(
    '/^_/',
    '/[^a-zA-Z0-9\\s_]+/',
  ), '', $pattern_name);
  $pattern_name = preg_replace('/\\s+/', '_', $pattern_name);

  // Mimic the form in patterns_import_source.
  $form_state['post'] = TRUE;
  $form_state['values']['xmlsource'] = $pattern_source;
  $form_state['values']['xmlname'] = $pattern_name;
  $return = $pattern_name;

  //Ok. so let's use the patterns API for executing this ad hoc form.
  patterns_execute_action('patterns_import_source', $form_state, array());
  $errors = form_get_errors();
  if ($errors) {
    $output = '';
    while (list($field, $err) = each($errors)) {
      $output .= "[{$field}]:\t{$err}\n";
    }
    drush_set_error(dt($output));
    $return = FALSE;
  }
  $msgs = drupal_get_messages();
  if ($msgs) {
    $output = '';
    while (list($type, $ms) = each($msgs)) {
      $output .= "[{$type}]:\n\t" . implode("\n", $ms) . "\n\n";
    }
    drush_print(dt($output));
  }

  //give back the name of the pattern upon success, FALSE on errors
  return $return;
}