2ec28899708e252b323f2fe900beb92df0e7e00c
[project/coder.git] / coder_review / coder_review.drush.inc
1 <?php
2
3 /**
4 * @file
5 * Command line utility for coder_review.
6 */
7
8 /**
9 * Implements hook_drush_help().
10 */
11 function coder_review_drush_help($section) {
12 switch ($section) {
13 case 'drush:coder-review':
14 return dt("usage: drush coder-review [options] [severity] [review] [what]"
15 . "\n options:"
16 . "\n summary - display summary information only, no warning output"
17 . "\n no-empty - hide results that are empty"
18 . "\n severity: show warnings at or above severity level"
19 . "\n major|minor|critical"
20 . "\n review: one or more reviews"
21 . "\n upgrade7x|comment|i18n|security|sql|style"
22 . "\n * style is the default"
23 . "\n what:"
24 . "\n all - review all modules and themes"
25 . "\n * this is the default if nothing is explicitly specified"
26 . "\n active - review all active modules and themes"
27 . "\n core - review all of core, modules, themes, and includes"
28 . "\n contrib - review all non-core modules"
29 . "\n default - review 'default' modules and themes, as defined by settings"
30 . "\n <name> - review specified module or theme name, i.e. taxonomy"
31 . "\n no-<name> - exclude named modules and themes, i.e. no-taxonomy"
32 . "\n combine with active|core|contrib|all|default"
33 . "\n <patch-url> - URL to patch file"
34 . "\n <file-path> - path to file or multiple files, relative to Drupal installation"
35 );
36 }
37 }
38
39 /**
40 * Implements hook_drush_command().
41 */
42 function coder_review_drush_command() {
43 $items['coder-review'] = array(
44 'callback' => 'coder_review_drush_review',
45 'description' => dt('Run code reviews'),
46 'drupal dependencies' => array('coder_review'),
47 );
48 return $items;
49 }
50
51 function _coder_review_set_form_modules(&$settings) {
52 if (isset($settings['coder_modules'])) {
53 foreach ($settings['coder_modules'] as $key => $value) {
54 if ($value == 1) {
55 $settings['coder_modules-' . $key] = 1;
56 }
57 }
58 unset($settings['coder_modules']);
59 }
60 }
61
62 function coder_review_drush_review() {
63 _coder_review_drush_set_option('drush');
64
65 // process command line arguments
66 $args = func_get_args();
67 $reviews = array();
68 $modules = array();
69 $settings = _coder_review_get_default_settings();
70 if (count($args)) {
71 $avail_reviews = _coder_review_reviews();
72 foreach ($args as $arg) {
73 switch ($arg) {
74 case 'summary':
75 case 'no-empty':
76 _coder_review_drush_set_option($arg);
77 break;
78 case 'active':
79 case 'core':
80 case 'contrib':
81 case 'all':
82 case 'default':
83 $settings = _coder_review_get_default_settings($arg);
84 $settings['coder_includes'] = TRUE;
85 break;
86 case 'major':
87 case 'minor':
88 case 'critical':
89 $severity_name = $arg;
90 break;
91 default:
92 if (isset($avail_reviews[$arg])) {
93 $reviews[$arg] = $arg;
94 }
95 elseif (drupal_substr($arg, 0, 3) == 'no-') {
96 _coder_review_set_form_modules($settings);
97 unset($settings['coder_modules-' . drupal_substr($arg, 3)]);
98 }
99 elseif (strpos($arg, '*') !== FALSE || strpos($arg, '%') !== FALSE) {
100 $result = db_query('SELECT name FROM {system} WHERE name LIKE :name', array(':name' => str_replace('*', '%', $arg)));
101 foreach ($result as $system) {
102 $settings['coder_modules-' . $system->name] = 1;
103 $match = TRUE;
104 }
105 if (!isset($match)) {
106 drush_print(dt('no matches found for @name', array('@name' => $arg)));
107 return;
108 }
109 unset($settings['coder_active_modules']);
110 unset($settings['coder_core']);
111 unset($settings['coder_all']);
112 unset($settings['coder_modules']);
113 $settings['coder_includes'] = TRUE;
114 }
115 else {
116 if (file_exists($arg)) {
117 $settings['coder_files'] = 1;
118 if (empty($settings['coder_file_list'])) {
119 $settings['coder_file_list'] = '';
120 }
121 $settings['coder_file_list'] .= $arg . "\n";
122 }
123 elseif (_coder_review_drush_is_patch_arg($arg)) {
124 $settings['coder_patches'] = 1;
125 $settings['coder_patch_link'] = $arg;
126 }
127 else {
128 $settings['coder_modules-' . $arg] = 1;
129 $settings['coder_includes'] = TRUE;
130 }
131 unset($settings['coder_active_modules']);
132 unset($settings['coder_core']);
133 unset($settings['coder_all']);
134 unset($settings['coder_modules']);
135 }
136 break;
137 }
138 }
139 $output = array();
140 if (isset($severity_name)) {
141 $output[] = dt('Severity @severity_name', array('@severity_name' => $severity_name));
142 $settings['coder_severity'] = _coder_review_severity($severity_name);
143 }
144 if (count($reviews)) {
145 foreach ($reviews as $review) {
146 $output[] = $avail_reviews[$review]['#title'];
147 }
148 $settings['coder_reviews'] = $reviews;
149 }
150 if (count($output) && !_coder_review_drush_is_option('summary')) {
151 drush_print(implode(', ', $output) . "\n");
152 }
153 }
154 _coder_review_set_form_modules($settings);
155 $settings['op'] = 'drush';
156
157 $form_state['storage'] = $settings;
158 coder_review_page_form(array(), $form_state);
159 }
160
161 function _coder_review_drush_is_patch_arg(&$arg) {
162 if (drupal_substr($arg, 0, 7) == 'http://' || is_file(realpath($arg))) {
163 return TRUE;
164 }
165 // @NOTE: relies on http://drupal.org/node/297611
166 if (isset($_SERVER['OLDPWD'])) {
167 $path = $_SERVER['OLDPWD'] . '/' . $arg;
168 if (is_file($path)) {
169 $arg = $path;
170 return TRUE;
171 }
172 }
173 }
174
175 function theme_drush_coder_review($variables) {
176 $name = $variables['name'];
177 $filename = $variables['filename'];
178 $results = $variables['results'];
179 if (!_coder_review_drush_is_option('summary') && !empty($results) && ((count($results) && !isset($results[0])) || !_coder_review_drush_is_option('no-empty'))) {
180 drush_print($filename . ":\n " . implode("\n ", $results) . "\n");
181 }
182 }
183
184 function theme_drush_coder_review_warning($variables) {
185 $warning = $variables['warning'];
186 $severity_name = $variables['severity_name'];
187 $lineno = isset($variables['lineno']) ? $variables['lineno'] : 0;
188 $line = isset($variables['line']) ? $variables['line'] : '';
189 $output = $lineno ? '+' . $lineno . ': ' : '';
190 $output .= '[' . $severity_name . '] ';
191 $output .= is_array($warning) ? $warning['#warning'] : $warning;
192 return _coder_review_drush_output($output);
193 }
194
195 function coder_review_print_drush_messages() {
196 foreach (drupal_get_messages() as $type => $messages) {
197 drush_print(dt(drupal_ucfirst($type) . ' Messages') . ":\n " . _coder_review_drush_output(implode("\n ", $messages)) . "\n");
198 }
199 }
200
201 function _coder_review_drush_output($output) {
202 return html_entity_decode(strip_tags($output));
203 }
204
205 function _coder_review_drush_set_option($option) {
206 global $_coder_drush_options;
207 if (!isset($_coder_drush_options)) {
208 $_coder_drush_options = array();
209 }
210 $_coder_drush_options[$option] = TRUE;
211 }
212
213 function _coder_review_drush_is_option($option) {
214 global $_coder_drush_options;
215 return isset($_coder_drush_options[$option]);
216 }