Initial commit
[dgrep.git] / dgrep.drush.inc
1 <?php
2 /**
3  * @file
4  * dgrep - Drush commands to grep through content using PCREs.
5  *
6  * Copyright (C) 2010  Antonio Ospite <ospite@studenti.unina.it>
7  *
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.
12  *
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.
17  *
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.
21  *
22  *
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.
28  */
29
30 /**
31  * Implementation of hook_drush_command().
32  */
33 function dgrep_drush_command() {
34   $items = array();
35
36   $items['grep'] = array(
37     'description' => "grep through content using PCREs.",
38     'arguments' => array(
39       'pattern' => 'The PCRE-style regular expression to match.',
40     ),
41     'options' => array(
42       'content-types' => 'Comma delimited list of content types to search into (node,comment,block).',
43     ),
44     'examples' => array(
45       'drush grep \'#https://[^"]*#\' --content-types=node',
46     ),
47     'aliases' => array('dgrep'),
48     'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, // XXX: maybe something less.
49   );
50
51   return $items;
52 }
53
54 /**
55  * Implementation of hook_drush_help().
56  */
57 function dgrep_drush_help($section) {
58   switch ($section) {
59     case 'drush:grep':
60       return dt("This command will allow you to grep through content using PCREs.");
61   }
62 }
63
64 /*
65  * Implementation of drush_hook_COMMAND_validate().
66  */
67 function drush_dgrep_grep_validate($pattern) {
68   $ret = @preg_match($pattern, '');
69   if ($ret === FALSE) {
70     return drush_set_error(dt('Error: pattern should be a valid PCRE.'));
71   }
72 }
73
74 /**
75  * This is where the action takes place.
76  */
77 function drush_dgrep_grep($pattern) {
78   $content_types = drush_get_option('content-types');
79   if ($content_types) {
80     $list = explode(',', $content_types);
81   }
82   else {
83     $list = array('node', 'comment', 'block');
84   }
85
86   if (in_array('node', $list)) {
87     $sql = 'SELECT nid FROM {node}';
88     $nids = db_query($sql);
89
90     while ($nid = db_result($nids)) {
91       $node = node_load($nid);
92
93       $ret = preg_match($pattern, $node->body, $matches);
94       if ($ret) {
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]);
99         drush_print();
100       }
101     }
102   }
103
104   if (in_array('comment', $list)) {
105     $sql = 'SELECT cid FROM {comments}';
106     $cids = db_query($sql);
107
108     while ($cid = db_result($cids)) {
109       $comment = _comment_load($cid);
110
111       $ret = preg_match($pattern, $comment->comment, $matches);
112       if ($ret) {
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]);
117         drush_print();
118       }
119     }
120   }
121
122   if (in_array('block', $list)) {
123         drush_print('TODO: grepping blocks is not supported yet');
124   }
125
126 }