Skip to content

Instantly share code, notes, and snippets.

@kotnik
Forked from ngmaloney/gist:981766
Created August 31, 2011 14:18
Show Gist options
  • Save kotnik/1183650 to your computer and use it in GitHub Desktop.
Save kotnik/1183650 to your computer and use it in GitHub Desktop.
my.drush.inc
<?php
/**
* @file My Drush Commands
*/
/**
* Implementation of hook_drush_help()
*/
function my_drush_help($section) {
switch ($section) {
case 'drush:my':
return dt("My custom drush commands");
case 'meta:my:title':
return dt('My Custom Drush Commands');
case 'meta:my:summary':
return dt('My Custom Drush Commands');
}
}
/**
* Implementation of hook_drush_command().
*/
function my_drush_command() {
$items = array();
$items['node-delete'] = array(
'callback' => 'my_drush_node_delete',
'description' => 'Performs a bulk delete operation on nodes.',
'aliases' => array('ndel'),
'examples' => array(
'drush --user=1 node-delete blog' => 'Deletes all nodes of type blog.',
),
'arguments' => array(
'type' => 'Type of node to delete. Using all will delete all nodes.',
),
'bootstrap' => DRUSH_BOOTSTRAP_MAX,
);
return $items;
}
function my_drush_node_delete($node_type = '') {
if(empty($node_type)) {
return drush_set_error('Node type argument required.');
}
if($node_type == 'all') {
$rsc = drush_db_select('node', 'nid');
}
else {
$rsc = drush_db_select('node', 'nid', 'type = :type', array(':type' => $node_type));
}
$nodes = array();
while ($nid = drush_db_fetch_object($rsc)) {
$nodes[] = $nid;
}
if (empty($nodes)) {
return drush_set_error(dt('No nodes found.'));
}
$proceed = drush_confirm('You are about to delete ' . count($nodes) . ' nodes. Proceed?');
if($proceed) {
foreach($nodes as $node) {
node_delete($node->nid);
}
$output = 'Successfully deleted ' . count($nodes) . ' nodes of type ' . $node_type;
drush_log($output, 'success');
}
else {
drush_print("Node deletion cancelled.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment