From 60ed4656f50d52367a6da2bef1b9b8fa95dff6a3 Mon Sep 17 00:00:00 2001 From: Colan Schwartz Date: Tue, 14 Feb 2012 18:13:57 -0500 Subject: [PATCH] Issue #867578 by delta, colan: New Drush commands to create & delete aliases. --- pathauto.drush.inc | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++ pathauto.info | 1 + 2 files changed, 148 insertions(+), 0 deletions(-) create mode 100644 pathauto.drush.inc diff --git a/pathauto.drush.inc b/pathauto.drush.inc new file mode 100644 index 0000000..380e639 --- /dev/null +++ b/pathauto.drush.inc @@ -0,0 +1,147 @@ + 'pathauto_drush_bulk_update', + // A short description of your command. + 'description' => dt('Creates URL aliases for unaliased entity instances using the Batch API.'), + 'arguments' => array( + 'update_types' => dt('Comma-delimited list of alias types to create (e.g. node, taxonomy_term, user). If no types are specified, all three types will be created.'), + ), + ); + $items['pathauto-aliases-delete'] = array( + // The name of the function implementing your command. + 'callback' => 'pathauto_drush_delete_node_alias', + // A short description of your command + 'description' => dt('Delete all URL aliases'), + 'arguments' => array( + 'update_types' => dt('Comma-delimited list of alias types to delete (e.g. node, taxonomy_term, user). If no types are specified, all three types will be deleted.'), + ), + ); + return $items; +} + +/** + * Implementation of hook_drush_help(). + */ +function pathauto_drush_help($section) { + switch ($section) { + case 'drush:pathauto-aliases-create': + return dt("Creates URL aliases for unaliased entity instances using the Batch API. You can specify the type (e.g. node, taxonomy_term, user). If no types are specified, all three types will be run."); + break; + case 'drush:pathauto-aliases-delete': + return dt("Delete all URL aliases. You can specify the type (node, taxonomy_term, user). If no types are specified, aliases for all types will be deleted."); + break; + } +} + +/** + * Command callback. Delete URL alias. + * + * @param $types + * List of pathauto alias types delimited by comma: + * node/,taxonomy/term/,user/ + * Shortcut : node, taxonomy_term, user + * ...or custom Pathauto types like "another/module/" + */ +function pathauto_drush_delete_node_alias($types = 'all') { + // Set $list to contain all three types if no arguments passed. + if ($types == 'all') { + db_delete('url_alias') + ->execute(); + return dt("URL aliases deleted for all types."); + } + // Otherwise create $list from passed arguments. + else { + $list = explode(',', $types); + } + + foreach ($list as $key) { + $key = trim($key); + if ($key) { + // Replace types if it match. + if ($key == "node") { + $needle = "node/"; + } elseif ($key == "taxonomy_term") { + $needle = "taxonomy/term/"; + } elseif ($key == "user") { + $needle = "user/"; + } else { + // Let the user pass what (s)he wants, like "another_module/". + $needle = $key; + } + $objects = module_invoke_all('path_alias_types'); + if (array_key_exists($needle, $objects)) { + db_delete('url_alias') + ->condition('source', db_like($needle) . '%', 'LIKE') + ->execute(); + $msg .= dt("All of your %type path aliases have been deleted.", array('%type' => $key)) . "\n"; + } + } + } + return $msg . dt("Operation finished."); +} + +/** + * Command callback. Runs URL alias bulk creation using drush batch API. + * + * @param $types + * List of pathauto settings types delimited by comma node,taxonomy_term,user + */ +function pathauto_drush_bulk_update($types = 'all') { + $batch = array( + 'title' => t('Bulk updating URL aliases'), + 'operations' => array( + array('pathauto_bulk_update_batch_start', array()), + ), + 'finished' => 'pathauto_bulk_update_batch_finished', + 'file' => drupal_get_path('module', 'pathauto') . '/pathauto.admin.inc', + ); + + // Get pathauto settings for each callback type. + $pathauto_settings = module_invoke_all('pathauto', 'settings'); + foreach ($pathauto_settings as $settings) { + $callbacks[$settings->module] = $settings; + } + + // Set $list to contain all three types if no arguments passed. + if ($types == 'all') { + $list = array('node', 'taxonomy_term', 'user'); + } + // Otherwise create $list from passed arguments. + else { + $list = explode(',', $types); + } + + foreach ($list as $type) { + $settings = $callbacks[trim($type)]; + if (!empty($settings->batch_file)) { + $batch['operations'][] = array('pathauto_bulk_update_batch_process', array($settings->batch_update_callback, $settings)); + } + else { + $batch['operations'][] = array($settings->batch_update_callback, array()); + } + } + + batch_set($batch); + $batch =& batch_get(); + $batch['progressive'] = FALSE; + + // Process the batch. + drush_backend_batch_process(); + + $msg = dt("URL aliases generated for !types types.", array("!types" => $types)); + drush_print("\n" . $msg . "\n"); +} + diff --git a/pathauto.info b/pathauto.info index fbfc18a..9946d39 100644 --- a/pathauto.info +++ b/pathauto.info @@ -4,5 +4,6 @@ dependencies[] = path dependencies[] = token core = 7.x files[] = pathauto.test +files[] = pathauto.drush.inc configure = admin/config/search/path/patterns recommends[] = redirect -- 1.7.0.4