4 * dgrep - Drush commands to grep through content using PCREs.
6 * Copyright (C) 2010 Antonio Ospite <ospite@studenti.unina.it>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * You can copy this file to any of the following
24 * 1. A .drush folder in your HOME folder.
25 * 2. Anywhere in a folder tree below an active module on your site.
26 * 3. /usr/share/drush/commands (configurable)
27 * 4. In an arbitrary folder specified with the --include option.
31 * Implementation of hook_drush_command().
33 function dgrep_drush_command() {
36 $items['grep'] = array(
37 'description' => "grep through content using PCREs.",
39 'pattern' => 'The PCRE-style regular expression to match.',
42 'content-types' => 'Comma delimited list of content types to search into (node,comment,block).',
45 'drush grep \'#https://[^"]*#\' --content-types=node',
47 'aliases' => array('dgrep'),
48 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, // XXX: maybe something less.
55 * Implementation of hook_drush_help().
57 function dgrep_drush_help($section) {
60 return dt("This command will allow you to grep through content using PCREs.");
65 * Implementation of drush_hook_COMMAND_validate().
67 function drush_dgrep_grep_validate($pattern) {
68 $ret = @preg_match($pattern, '');
70 return drush_set_error(dt('Error: pattern should be a valid PCRE.'));
75 * This is where the action takes place.
77 function drush_dgrep_grep($pattern) {
78 $content_types = drush_get_option('content-types');
80 $list = explode(',', $content_types);
83 $list = array('node', 'comment', 'block');
86 if (in_array('node', $list)) {
87 $sql = 'SELECT nid FROM {node}';
88 $nids = db_query($sql);
90 while ($nid = db_result($nids)) {
91 $node = node_load($nid);
93 $ret = preg_match($pattern, $node->body, $matches);
95 $nid_str = sprintf("%-4d", $nid);
96 drush_print('Node: '. $nid_str . "\tTitle: " . $node->title);
97 drush_print("\t\tURL: " . drupal_get_path_alias('node/'.$nid));
98 drush_print("\t\tMatch: " . $matches[0]);
104 if (in_array('comment', $list)) {
105 $sql = 'SELECT cid FROM {comments}';
106 $cids = db_query($sql);
108 while ($cid = db_result($cids)) {
109 $comment = _comment_load($cid);
111 $ret = preg_match($pattern, $comment->comment, $matches);
113 $cid_str = sprintf("%-4d", $cid);
114 drush_print('Comment: '. $cid_str . "\tTitle: " . $comment->title);
115 drush_print("\t\tURL: " . drupal_get_path_alias('node/'.$comment->nid) . '#comment-' . $cid );
116 drush_print("\t\tMatch: " . $matches[0]);
122 if (in_array('block', $list)) {
123 drush_print('TODO: grepping blocks is not supported yet');