Calling all Drupal developers!
Help us get this on the first page of Digg. DIGG NOW!
Help us get this on the first page of Digg. DIGG NOW!
<?php
// $Id: drush_tools.module,v 1.8 2008/03/29 21:05:19 weitzman Exp $
/**
* @file drush_trools.sql
*
* A soup of site utilities.
*
*/
/**
* Implementation of hook_help().
*/
function drush_tools_help($section) {
switch ($section) {
case 'drush:watchdog show':
return t("Usage: drush [options] watchdog show <n> <type>\n\nShow the <n> (default 10) most recent watchdog messages. Optionally show only messages of type <type>.");
case 'drush:watchdog delete':
return t("Usage: drush [options] watchdog delete <type>\n\nDelete all messages of type <type>. Use 'all' to delete all messages, no matter which type they are of.");
case 'drush:sync':
return t("Usage: drush [options] sync <source> <destination>\n\nRsync the entire drupal directory or a subdirectory to a <destination> using ssh. Excludes .svn directories. Useful for pushing copies of your tree to a staging server, or retrieving a files directory from a remote site. Local paths should be specified relative to Drupal root.");
}
}
/**
* Implementation of hook_drush_command().
*/
function drush_tools_drush_command() {
$items['cache clear'] = array(
'callback' => 'drush_tools_cache_clear',
'description' => 'Clear all caches'
);
$items['cron'] = array(
'callback' => 'drush_tools_cron_run',
'description' => 'Run cron'
);
$items['watchdog show'] = array(
'callback' => 'drush_tools_watchdog_show',
'description' => 'Show the most recent watchdog log messages'
);
$items['watchdog delete'] = array(
'callback' => 'drush_tools_watchdog_delete',
'description' => 'Delete all messages of a certain type'
);
$items['sync'] = array(
'callback' => 'drush_tools_sync',
'description' => 'Rsync the Drupal tree to/from another server using ssh'
);
return $items;
}
function drush_tools_cron_run() {
drupal_cron_run();
drush_print(t('Cron run successfully.'));
}
function drush_tools_cache_clear() {
// clear preprocessor cache
drupal_clear_css_cache();
// clear core tables
$core = array('cache', 'cache_filter', 'cache_menu', 'cache_page');
$alltables = array_merge($core, module_invoke_all('devel_caches'));
foreach ($alltables as $table) {
cache_clear_all('*', $table, TRUE);
}
drush_print(t('Cache cleared.'));
}
/**
* Push files from or to the local Drupal install using SSH and RSync
*
* @return void
**/
function drush_tools_sync($source, $destination) {
// Local paths are relative to Drupal root
if (!strstr($source, ':')) {
$source = DRUSH_DRUPAL_ROOT. ":";
}
if (!strstr($destination, ':')) {
$destination = DRUSH_DRUPAL_ROOT. "/$destination";
}
// Prompt for confirmation. This is destructive.
if (!DRUSH_SIMULATE) {
drush_print(t("You will destroy data from !target and replace with data from !source", array('!source' => $source, '!target' => $destination)));
if (!drush_confirm(t('Do you really want to continue?'))) {
drush_die('Aborting.');
}
}
$options = '-az';
$exec = "rsync -e ssh $options --exclude \"*.svn*\" $source $destination";
if (DRUSH_VERBOSE) {
// the drush_op() will be verbose about the command that gets executed.
$options .= 'v';
}
return drush_op('system', $exec) !== FALSE;
}
/**
* Displays the most recent watchdog log messages (default: 10 messages).
*/
function drush_tools_watchdog_show($limit = 10, $type = NULL) {
$severities = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
$sql = 'SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid';
$sort = ' ORDER BY w.wid DESC';
$result = (!empty($type) ?
pager_query($sql . " WHERE w.type = '%s'" . $sort, (int)$limit, 0, NULL, $type) :
pager_query($sql . $sort, (int)$limit));
$rows = array();
while ($watchdog = db_fetch_object($result)) {
$rows[] = array(
format_date($watchdog->timestamp, 'small'),
str_pad($severities[$watchdog->severity], strlen(t('Severity'))),
t($watchdog->type),
_drush_tools_watchdog_format_message($watchdog->message),
theme('username', $watchdog),
);
}
if (count($rows) == 0) {
drush_die(t('No log messages available.'));
}
drush_verbose(t('Last !count watchdog log messages:', array('!count' => $limit)));
$rows[] = array(t('Date'), t('Severity'), t('Type'), t('Message'), t('User'));
drush_print_table(array_reverse($rows), 2, true);
}
function _drush_tools_watchdog_format_message($message, $length = 56) {
return strip_tags(
preg_replace('|<em>([^<]+)</em>|', '`$1\'',
truncate_utf8($message, $length, TRUE, TRUE)));
}
/**
* Deletes all log messages of a certain type from the watchdog log
* (default: all).
*/
function drush_tools_watchdog_delete($type = NULL) {
if ($type == "all") {
drush_op('db_query', 'DELETE FROM {watchdog}'); // indiscriminately delete all
drush_print(t('Deleted !n rows.', array('!n' => db_affected_rows())));
}
elseif (!empty($type)) {
drush_op('db_query', 'DELETE FROM {watchdog} WHERE type = \'%s\'', $type);
drush_print(t('Deleted !n rows.', array('!n' => db_affected_rows())));
}
else {
drush_print(t('Please specify a message type, or "all" to delete all messages.'));
}
}