You are here

function drush_patterns_run in Patterns 6.2

Same name and namespace in other branches
  1. 7.2 patterns.drush.inc \drush_patterns_run()
  2. 7 patterns.drush.inc \drush_patterns_run()

Imports, Enables, and Runs the specified pattern file

Parameters

string pattern file pathname:

string optional machine readable pattern name:

1 string reference to 'drush_patterns_run'
patterns_drush_command in ./patterns.drush.inc
Implementation of hook_drush_command().

File

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

Code

function drush_patterns_run($pattern_file, $pattern_name = '') {

  /** Since I want to be able to specify a file name in my interface, in
   * order to use the patterns_get_pattern() logic as is, I need to
   * standardize on pattern name, and fire off this "redundant" query to do
   * so. I'm trading off efficiency for ensuring future compatibility with
   * patterns module.*/
  $name = db_result(db_query("SELECT name FROM {patterns} WHERE `file` LIKE '%s'", "%{$pattern_file}"));
  $result = FALSE;
  $subpattern_mode = 'always';

  //is this right?
  if (!$name) {

    // we should also be allowed to specify an existing pattern by name
    $pattern = patterns_get_pattern($pattern_file);
    $pattern_file = $pattern->file;
    $name = db_result(db_query("SELECT name FROM {patterns} WHERE `file` LIKE '%s'", "%{$pattern_file}"));
    if (!$name) {
      if (drush_confirm(dt("Pattern '{$pattern_file}' has not been imported. Do you want to import it now? "))) {
        $name = drush_patterns_import($pattern_file, $pattern_name);
      }
      else {
        drush_print(dt('patterns-run has been cancelled.'));
        return FALSE;
      }
    }
  }

  // makes sure the most recent version is loaded. who wants an old pattern?
  $patterns = patterns_get_patterns();
  if ($name) {
    $result = drush_patterns_enable($name, $subpattern_mode);
  }
  if (!$result) {
    drush_set_error(dt("Failed to run pattern '{$name}' from pattern file '{$pattern_file}'"));
    return FALSE;
  }
  return TRUE;
}