Skip to content

Instantly share code, notes, and snippets.

@msonnabaum
Created April 25, 2011 15:56
Show Gist options
  • Save msonnabaum/940710 to your computer and use it in GitHub Desktop.
Save msonnabaum/940710 to your computer and use it in GitHub Desktop.
<?php
// $Id: $
/**
* @file
* pathauto.drush.inc
*/
/**
* Implementation of hook_drush_command().
*
* @see drush_parse_command()
* For a list of recognized keys.
*
* @return
* An associative array describing your command(s).
*/
function pathauto_drush_command() {
$items = array();
$items['pathauto-bulk-update'] = array(
'description' => "Pathauto Bulk Update",
'aliases' => array('pabu'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'options' => array(
'--max' => 'Maximum number of objects to alias in a bulk update',
),
);
return $items;
}
/**
* Implementation of hook_drush_help().
*
* @param
* A string with the help section (prepend with 'drush:')
*
* @return
* A string with the help text for your command.
*/
function pathauto_drush_help($section) {
switch ($section) {
case 'drush:pathauto-bulk-update':
return dt("Pathauto Bulk Update.");
break;
}
}
/**
* Command callback for drush pathauto-bulk-update.
*/
function drush_pathauto_bulk_update() {
if (!drush_get_context('DRUSH_BACKEND')) {
// Subsequent run in the parent process. Spawn subshells ad infinitum.
while (TRUE) {
$override['pathauto_max_bulk_update'] = drush_get_option('max', 100);
$return = drush_pathauto_backend_invoke();
$return = $return['object'];
drush_print('RETURN: ' . $return);
if ($return === FALSE) {
drush_log(dt('error?', $return,'warning'));
}
elseif (empty($return) || $return == $last_return) {
break;
}
$last_return = $return;
$i++;
}
}
else {
// I'm in a subshell. Import then set return value so parent process can respawn or move on.
_pathauto_include();
node_pathauto_bulkupdate();
$sql = "SELECT COUNT(n.nid) FROM {node} n LEFT JOIN {url_alias} ua ON CONCAT('node/', CAST(n.nid AS CHAR)) = ua.src WHERE ua.src IS NULL AND n.nid > %d ORDER BY n.nid";
// Get the total amount of items to process.
$remaining = db_result(db_query($sql, 0));
drush_backend_set_result($remaining);
}
if ($i > 1) {
drush_log(dt('Completed bulk update of !i aliases.', array('!i' => $i)), 'debug');
}
if ($stop) {
break;
}
}
/**
* Get the value of all pathauto related options. Used when spawning a subshell.
* Don't pass along stop and rollback options.
*
* @return
* An array of command specific options and their values.
*/
function drush_pathauto_get_options() {
$options = array();
$command = drush_parse_command();
foreach ($command['options'] as $key => $value) {
// Strip leading --
$key = ltrim($key, '-');
$value = drush_get_option($key);
if (isset($value)) {
$options[$key] = $value;
}
}
return $options;
}
/*
* Spawn a subshell which runs the same command we are currently running.
*/
function drush_pathauto_backend_invoke() {
$args = drush_get_arguments();
$options = drush_pathauto_get_options();
// @todo: use drush_backend_invoke_args() as per http://drupal.org/node/658420.
return drush_backend_invoke(implode(' ', $args), $options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment