From 9156ad6a045f2d8857cb3c5a2ea342becbf5ce51 Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Fri, 15 Oct 2010 14:31:46 +0200 Subject: [PATCH 1/1] Initial commit --- TODO | 3 ++ dgrep.drush.inc | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 TODO create mode 100644 dgrep.drush.inc diff --git a/TODO b/TODO new file mode 100644 index 0000000..817377f --- /dev/null +++ b/TODO @@ -0,0 +1,3 @@ +Add more commands following php preg_* functions +Implement grepping on blog +Factor out common parts to share with a drupal "frontend" module diff --git a/dgrep.drush.inc b/dgrep.drush.inc new file mode 100644 index 0000000..b5c603f --- /dev/null +++ b/dgrep.drush.inc @@ -0,0 +1,126 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * + * You can copy this file to any of the following + * 1. A .drush folder in your HOME folder. + * 2. Anywhere in a folder tree below an active module on your site. + * 3. /usr/share/drush/commands (configurable) + * 4. In an arbitrary folder specified with the --include option. + */ + +/** + * Implementation of hook_drush_command(). + */ +function dgrep_drush_command() { + $items = array(); + + $items['grep'] = array( + 'description' => "grep through content using PCREs.", + 'arguments' => array( + 'pattern' => 'The PCRE-style regular expression to match.', + ), + 'options' => array( + 'content-types' => 'Comma delimited list of content types to search into (node,comment,block).', + ), + 'examples' => array( + 'drush grep \'#https://[^"]*#\' --content-types=node', + ), + 'aliases' => array('dgrep'), + 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, // XXX: maybe something less. + ); + + return $items; +} + +/** + * Implementation of hook_drush_help(). + */ +function dgrep_drush_help($section) { + switch ($section) { + case 'drush:grep': + return dt("This command will allow you to grep through content using PCREs."); + } +} + +/* + * Implementation of drush_hook_COMMAND_validate(). + */ +function drush_dgrep_grep_validate($pattern) { + $ret = @preg_match($pattern, ''); + if ($ret === FALSE) { + return drush_set_error(dt('Error: pattern should be a valid PCRE.')); + } +} + +/** + * This is where the action takes place. + */ +function drush_dgrep_grep($pattern) { + $content_types = drush_get_option('content-types'); + if ($content_types) { + $list = explode(',', $content_types); + } + else { + $list = array('node', 'comment', 'block'); + } + + if (in_array('node', $list)) { + $sql = 'SELECT nid FROM {node}'; + $nids = db_query($sql); + + while ($nid = db_result($nids)) { + $node = node_load($nid); + + $ret = preg_match($pattern, $node->body, $matches); + if ($ret) { + $nid_str = sprintf("%-4d", $nid); + drush_print('Node: '. $nid_str . "\tTitle: " . $node->title); + drush_print("\t\tURL: " . drupal_get_path_alias('node/'.$nid)); + drush_print("\t\tMatch: " . $matches[0]); + drush_print(); + } + } + } + + if (in_array('comment', $list)) { + $sql = 'SELECT cid FROM {comments}'; + $cids = db_query($sql); + + while ($cid = db_result($cids)) { + $comment = _comment_load($cid); + + $ret = preg_match($pattern, $comment->comment, $matches); + if ($ret) { + $cid_str = sprintf("%-4d", $cid); + drush_print('Comment: '. $cid_str . "\tTitle: " . $comment->title); + drush_print("\t\tURL: " . drupal_get_path_alias('node/'.$comment->nid) . '#comment-' . $cid ); + drush_print("\t\tMatch: " . $matches[0]); + drush_print(); + } + } + } + + if (in_array('block', $list)) { + drush_print('TODO: grepping blocks is not supported yet'); + } + +} -- 2.1.4