Created
May 19, 2011 21:17
-
-
Save ngmaloney/981766 to your computer and use it in GitHub Desktop.
my.drush.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 node-delete blog' => | |
'Deletes all nodes of type blog.', | |
), | |
'arguments' => array( | |
'type' => 'Type of node to delete. Using all will delete all nodes.', | |
), | |
); | |
return $items; | |
} | |
function my_drush_node_delete($node_type = '') { | |
if(empty($node_type)) { | |
return drush_set_error('Node type argument required.'); | |
} | |
$query = new EntityFieldQuery; | |
if($node_type == 'all') { | |
$result = $query | |
->entityCondition('entity_type', 'node') | |
->execute(); | |
} | |
else { | |
$result = $query | |
->entityCondition('entity_type', 'node') | |
->propertyCondition('type', $node_type) | |
->execute(); | |
} | |
if(!isset($result['node']) || empty($result['node'])) { | |
$output = 'There were no nodes of type ' . $node_type . ' found.'; | |
return drush_set_error($output); | |
} | |
$proceed = drush_confirm('You are about to delete ' . count($result['node']) . ' nodes. Proceed?'); | |
if($proceed) { | |
foreach($result['node'] as $node) { | |
node_delete($node->nid); | |
} | |
$output = 'Successfully deleted ' . count($result['node']) . ' 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