drush_tools.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/drush/drush_tools/drush_tools.module

drush_trools.sql

A soup of site utilities.

Functions & methods

NameDescription
drush_tools_cache_clear
drush_tools_cron_run
drush_tools_drush_commandImplementation of hook_drush_command().
drush_tools_eval
drush_tools_helpImplementation of hook_help().
drush_tools_syncPush files from or to the local Drupal install using SSH and RSync
drush_tools_watchdog_deleteDeletes all log messages of a certain type from the watchdog log (default: all).
drush_tools_watchdog_showDisplays the most recent watchdog log messages (default: 10 messages).
_drush_tools_watchdog_format_message

File

View source
  1. <?php
  2. /**
  3. * @file drush_trools.sql
  4. *
  5. * A soup of site utilities.
  6. *
  7. */
  8. /**
  9. * Implementation of hook_help().
  10. */
  11. function drush_tools_help($section) {
  12. switch ($section) {
  13. case 'drush:watchdog show':
  14. 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>.");
  15. case 'drush:watchdog delete':
  16. 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.");
  17. case 'drush:sync':
  18. 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.");
  19. case 'drush:eval':
  20. return t("Usage: drush eval <code>\n\nRun arbitrary PHP code in the context of Drupal. For example: drush eval \"variable_set('hello', 'world');\".");
  21. }
  22. }
  23. /**
  24. * Implementation of hook_drush_command().
  25. */
  26. function drush_tools_drush_command() {
  27. $items['cache clear'] = array(
  28. 'callback' => 'drush_tools_cache_clear',
  29. 'description' => 'Clear all caches'
  30. );
  31. $items['cron'] = array(
  32. 'callback' => 'drush_tools_cron_run',
  33. 'description' => 'Run cron'
  34. );
  35. $items['watchdog show'] = array(
  36. 'callback' => 'drush_tools_watchdog_show',
  37. 'description' => 'Show the most recent watchdog log messages'
  38. );
  39. $items['watchdog delete'] = array(
  40. 'callback' => 'drush_tools_watchdog_delete',
  41. 'description' => 'Delete all messages of a certain type'
  42. );
  43. $items['sync'] = array(
  44. 'callback' => 'drush_tools_sync',
  45. 'description' => 'Rsync the Drupal tree to/from another server using ssh'
  46. );
  47. $items['eval'] = array(
  48. 'callback' => 'drush_tools_eval',
  49. 'description' => 'Evaluate a command',
  50. );
  51. return $items;
  52. }
  53. function drush_tools_cron_run() {
  54. drupal_cron_run();
  55. drush_print(t('Cron run successfully.'));
  56. }
  57. function drush_tools_cache_clear() {
  58. // clear preprocessor cache
  59. drupal_clear_css_cache();
  60. // clear core tables
  61. $core = array('cache', 'cache_filter', 'cache_menu', 'cache_page');
  62. $alltables = array_merge($core, module_invoke_all('devel_caches'));
  63. foreach ($alltables as $table) {
  64. cache_clear_all('*', $table, TRUE);
  65. }
  66. drush_print(t('Cache cleared.'));
  67. }
  68. /**
  69. * Push files from or to the local Drupal install using SSH and RSync
  70. *
  71. * @return void
  72. **/
  73. function drush_tools_sync($source, $destination) {
  74. // Local paths are relative to Drupal root
  75. if (!strstr($source, ':')) {
  76. $source = DRUSH_DRUPAL_ROOT. "/$source";
  77. }
  78. if (!strstr($destination, ':')) {
  79. $destination = DRUSH_DRUPAL_ROOT. "/$destination";
  80. }
  81. // Prompt for confirmation. This is destructive.
  82. if (!DRUSH_SIMULATE) {
  83. drush_print(t("You will destroy data from !target and replace with data from !source", array('!source' => $source, '!target' => $destination)));
  84. if (!drush_confirm(t('Do you really want to continue?'))) {
  85. drush_die('Aborting.');
  86. }
  87. }
  88. $options = '-az';
  89. $exec = "rsync -e ssh $options --exclude \"*.svn*\" $source $destination";
  90. if (DRUSH_VERBOSE) {
  91. // the drush_op() will be verbose about the command that gets executed.
  92. $options .= 'v';
  93. }
  94. return drush_op('system', $exec) !== FALSE;
  95. }
  96. /**
  97. * Displays the most recent watchdog log messages (default: 10 messages).
  98. */
  99. function drush_tools_watchdog_show($limit = 10, $type = NULL) {
  100. $severities = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
  101. $sql = 'SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid';
  102. $sort = ' ORDER BY w.wid DESC';
  103. $result = (!empty($type) ?
  104. pager_query($sql . " WHERE w.type = '%s'" . $sort, (int)$limit, 0, NULL, $type) :
  105. pager_query($sql . $sort, (int)$limit));
  106. $rows = array();
  107. while ($watchdog = db_fetch_object($result)) {
  108. $rows[] = array(
  109. format_date($watchdog->timestamp, 'small'),
  110. str_pad($severities[$watchdog->severity], strlen(t('Severity'))),
  111. t($watchdog->type),
  112. _drush_tools_watchdog_format_message($watchdog->message),
  113. theme('username', $watchdog),
  114. );
  115. }
  116. if (count($rows) == 0) {
  117. drush_die(t('No log messages available.'));
  118. }
  119. drush_verbose(t('Last !count watchdog log messages:', array('!count' => $limit)));
  120. $rows[] = array(t('Date'), t('Severity'), t('Type'), t('Message'), t('User'));
  121. drush_print_table(array_reverse($rows), 2, true);
  122. }
  123. function _drush_tools_watchdog_format_message($message, $length = 56) {
  124. return strip_tags(
  125. preg_replace('|<em>([^<]+)</em>|', '`$1\'',
  126. truncate_utf8($message, $length, TRUE, TRUE)));
  127. }
  128. /**
  129. * Deletes all log messages of a certain type from the watchdog log
  130. * (default: all).
  131. */
  132. function drush_tools_watchdog_delete($type = NULL) {
  133. if ($type == "all") {
  134. drush_op('db_query', 'DELETE FROM {watchdog}'); // indiscriminately delete all
  135. drush_print(t('Deleted !n rows.', array('!n' => db_affected_rows())));
  136. }
  137. elseif (!empty($type)) {
  138. drush_op('db_query', 'DELETE FROM {watchdog} WHERE type = \'%s\'', $type);
  139. drush_print(t('Deleted !n rows.', array('!n' => db_affected_rows())));
  140. }
  141. else {
  142. drush_print(t('Please specify a message type, or "all" to delete all messages.'));
  143. }
  144. }
  145. function drush_tools_eval($command) {
  146. eval($command);
  147. }