1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Drush integration of views. |
---|
6 | * |
---|
7 | * * drush cache-clear views - Clears the views specific caches. |
---|
8 | * * views-revert - Drush command to revert views overridden in the system. |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Implement hook_drush_help(). |
---|
13 | */ |
---|
14 | function views_drush_help($section) { |
---|
15 | switch ($section) { |
---|
16 | case 'drush:views-revert': |
---|
17 | $help = dt('Reverts views in the drupal installation that have been overriden. '); |
---|
18 | $help .= dt('If no view names are specified, you will be presented with a list of overridden views to choose from. '); |
---|
19 | $help .= dt('To revert all views, do not specify any view names, and choose the option "All" from the options presented.'); |
---|
20 | return $help; |
---|
21 | case 'drush:views-list': |
---|
22 | return dt('Show a list of available views with information about them.'); |
---|
23 | case 'drush:views-enable': |
---|
24 | return dt('Enable the specified views. Follow the command with a space delimited list of view names'); |
---|
25 | case 'drush:views-disable': |
---|
26 | return dt('Disable the specified views. Follow the command with a space delimited list of view names'); |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | /** |
---|
31 | * Implement hook_drush_command(). |
---|
32 | */ |
---|
33 | function views_drush_command() { |
---|
34 | $items = array(); |
---|
35 | |
---|
36 | $items['views-revert'] = array( |
---|
37 | 'callback' => 'views_drush_revert_views', |
---|
38 | 'drupal dependencies' => array('views'), |
---|
39 | 'description' => 'Revert overridden views to their default state. Make sure to backup first.', |
---|
40 | 'arguments' => array( |
---|
41 | 'views' => 'A space delimited list of view names.', |
---|
42 | ), |
---|
43 | 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, |
---|
44 | 'aliases' => array('vr'), |
---|
45 | 'examples' => array( |
---|
46 | 'drush vr archive' => 'Reverts the "archive" view.', |
---|
47 | 'drush rln archive frontpage' => 'Reverts the "archive" and "frontpage" view.', |
---|
48 | 'drush vr' => 'Will present you with a list of overridden views to choose from, and an option to revert all overridden views.', |
---|
49 | ), |
---|
50 | ); |
---|
51 | |
---|
52 | $items['views-list'] = array( |
---|
53 | 'drupal dependencies' => array('views'), |
---|
54 | 'description' => 'Get a list of all views in the system.', |
---|
55 | 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, |
---|
56 | 'aliases' => array('vl'), |
---|
57 | 'options' => array( |
---|
58 | 'name' => 'String contained in view\'s name by which filter the results.', |
---|
59 | 'tags' => 'A comma-separated list of views tags by which to filter the results.', |
---|
60 | 'status' => 'Status of the views by which to filter the results. Choices: enabled, disabled.', |
---|
61 | 'type' => 'Type of the views by which to filter the results. Choices: normal, default or overridden.', |
---|
62 | ), |
---|
63 | 'examples' => array( |
---|
64 | 'drush vl' => 'Show a list of all available views.', |
---|
65 | 'drush vl --name=blog' => 'Show a list of views which names contain "blog".', |
---|
66 | 'drush vl --tags=tag1,tag2' => 'Show a list of views tagged with "tag1" or "tag2".', |
---|
67 | 'drush vl --status=enabled' => 'Show a list of enabled views.', |
---|
68 | 'drush vl --type=overridden' => 'Show a list of overridden views.', |
---|
69 | ), |
---|
70 | ); |
---|
71 | $items['views-analyze'] = array( |
---|
72 | 'drupal dependencies' => array('views', 'views_ui'), |
---|
73 | 'description' => 'Get a list of all Views analyze warnings', |
---|
74 | 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, |
---|
75 | 'aliases' => array('va'), |
---|
76 | ); |
---|
77 | $items['views-enable'] = array( |
---|
78 | 'drupal dependencies' => array('views'), |
---|
79 | 'description' => 'Enable the specified views.', |
---|
80 | 'arguments' => array( |
---|
81 | 'views' => 'A space delimited list of view names.', |
---|
82 | ), |
---|
83 | 'aliases' => array('ven'), |
---|
84 | 'examples' => array( |
---|
85 | 'drush ven frontpage taxonomy_term' => 'Enable the frontpage and taxonomy_term views.', |
---|
86 | ), |
---|
87 | ); |
---|
88 | $items['views-disable'] = array( |
---|
89 | 'drupal dependencies' => array('views'), |
---|
90 | 'description' => 'Disable the specified views.', |
---|
91 | 'arguments' => array( |
---|
92 | 'views' => 'A space delimited list of view names.', |
---|
93 | ), |
---|
94 | 'aliases' => array('vdis'), |
---|
95 | 'examples' => array( |
---|
96 | 'drush vdis frontpage taxonomy_term' => 'Disable the frontpage and taxonomy_term views.', |
---|
97 | ), |
---|
98 | ); |
---|
99 | |
---|
100 | return $items; |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Callback function for views-revert command. |
---|
105 | */ |
---|
106 | function views_drush_revert_views() { |
---|
107 | $views = views_get_all_views(); |
---|
108 | $i = 0; |
---|
109 | // The provided views names specified in the command. |
---|
110 | $viewnames = _convert_csv_to_array(func_get_args()); |
---|
111 | |
---|
112 | // Find all overridden views. |
---|
113 | foreach ($views as $view) { |
---|
114 | if ($view->disabled) { |
---|
115 | continue; |
---|
116 | } |
---|
117 | if ($view->type == dt('Overridden')) { |
---|
118 | $overridden[$view->name] = $view->name; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | // Return early if there are no overridden views in the system. |
---|
123 | if (empty($overridden)) { |
---|
124 | return drush_set_error(dt('There are no overridden views in the system.')); |
---|
125 | } |
---|
126 | |
---|
127 | // If the user specified in the command the views to be overridden. |
---|
128 | if (!empty($viewnames)) { |
---|
129 | foreach ($viewnames as $key => $viewname) { |
---|
130 | $is_overridden = key_exists($viewname, $overridden); |
---|
131 | // Check if the provided view name is in the system |
---|
132 | if ($viewname && !key_exists($viewname, $views)) { |
---|
133 | drush_set_error(dt("'@viewname' view is not present in the system.", array('@viewname' => $viewname))); |
---|
134 | } |
---|
135 | // Check if the provided view is overridden. |
---|
136 | elseif (!$is_overridden) { |
---|
137 | drush_set_error(dt("The view specified '@viewname' is not overridden.", array('@viewname' => $viewname))); |
---|
138 | } |
---|
139 | // If the view is overriden, revert it. |
---|
140 | elseif ($is_overridden){ |
---|
141 | views_drush_revert_view($views[$viewname]); |
---|
142 | $i++; |
---|
143 | } |
---|
144 | // We should never get here but well... |
---|
145 | else { |
---|
146 | drush_set_error(dt("The view specified '@viewname' is not provided in code, and thus cannot be reverted.", array('@viewname' => $viewname))); |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | // The user did not specify any views in the command, prompt the user |
---|
152 | else { |
---|
153 | // list of choices for the user |
---|
154 | $overridden['all'] = dt('Revert all overridden views'); // add a choice at the end |
---|
155 | $choice = drush_choice($overridden, 'Enter a number to choose which view to revert.', '!key'); // prompt the user |
---|
156 | |
---|
157 | if ($choice !== FALSE) { |
---|
158 | // revert all views option |
---|
159 | if ($choice == 'all') { |
---|
160 | $i = views_drush_revert_allviews($views); |
---|
161 | } |
---|
162 | // else the user specified a single view |
---|
163 | else { |
---|
164 | views_drush_revert_view($views[$choice]); |
---|
165 | $i++; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | } |
---|
170 | |
---|
171 | // final results output |
---|
172 | if ($i == 0) { |
---|
173 | drush_log(dt('No views were reverted.'), 'ok'); |
---|
174 | } |
---|
175 | else { |
---|
176 | drush_log(dt('Reverted a total of @count views.', array('@count' => $i)), 'ok'); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * Reverts all views |
---|
182 | * @param $views |
---|
183 | * All views in the system as provided by views_get_all_views(). |
---|
184 | */ |
---|
185 | function views_drush_revert_allviews($views) { |
---|
186 | $i = 0; |
---|
187 | foreach ($views as $view) { |
---|
188 | if ($view->disabled) { |
---|
189 | continue; |
---|
190 | } |
---|
191 | |
---|
192 | if ($view->type == t('Overridden')) { |
---|
193 | views_drush_revert_view($view); |
---|
194 | $i++; |
---|
195 | } |
---|
196 | } |
---|
197 | return $i; |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Reverts a specified view |
---|
202 | * @param $view |
---|
203 | * The view object to be reverted |
---|
204 | * |
---|
205 | * Checks on wether or not the view is overridden is handled in views_revert_views_revert() |
---|
206 | * We perform a check here anyway in case someone somehow calls this function on their own... |
---|
207 | */ |
---|
208 | function views_drush_revert_view($view) { |
---|
209 | // check anyway just in case |
---|
210 | if ($view->type == t('Overridden')) { |
---|
211 | // Revert the view. |
---|
212 | $view->delete(); |
---|
213 | // Clear its cache. |
---|
214 | views_object_cache_clear('view', $view->name); |
---|
215 | // Give feedback. |
---|
216 | $message = dt("Reverted the view '@viewname'", array('@viewname' => $view->name)); |
---|
217 | drush_log($message, 'success'); |
---|
218 | // Reverted one more view. |
---|
219 | } |
---|
220 | else { |
---|
221 | drush_set_error(dt("The view '@viewname' is not overridden.", array('@viewname' => $view->name))); |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * Callback function for views-list command. |
---|
227 | */ |
---|
228 | function drush_views_list() { |
---|
229 | // Initialize stuf |
---|
230 | $rows = array(); |
---|
231 | $disabled_views = array(); |
---|
232 | $enabled_views = array(); |
---|
233 | $overridden = 0; |
---|
234 | $indb = 0; |
---|
235 | $incode = 0; |
---|
236 | $disabled = 0; |
---|
237 | $total = 0; |
---|
238 | |
---|
239 | $views = views_get_all_views(); |
---|
240 | |
---|
241 | // get the --name option |
---|
242 | // TODO : take into account the case off a comma-separated list of names |
---|
243 | $name = drush_get_option_list('name'); |
---|
244 | $with_name = !empty($name) ? TRUE : FALSE; |
---|
245 | |
---|
246 | // get the --tags option |
---|
247 | $tags = drush_get_option_list('tags'); |
---|
248 | $with_tags = !empty($tags) ? TRUE : FALSE; |
---|
249 | |
---|
250 | // get the --status option |
---|
251 | // store user input appart to reuse it after |
---|
252 | $status_opt = drush_get_option_list('status'); |
---|
253 | // use the same logic than $view->disabled |
---|
254 | if (in_array('disabled', $status_opt)) { |
---|
255 | $status = TRUE; |
---|
256 | $with_status = TRUE; |
---|
257 | } |
---|
258 | elseif (in_array('enabled', $status_opt)) { |
---|
259 | $status = FALSE; |
---|
260 | $with_status = TRUE; |
---|
261 | } |
---|
262 | else { |
---|
263 | $status = NULL; |
---|
264 | // wrong or empty --status option |
---|
265 | $with_status = FALSE; |
---|
266 | } |
---|
267 | |
---|
268 | // get the --type option |
---|
269 | $type = drush_get_option_list('type'); |
---|
270 | // use the same logic than $view->type |
---|
271 | $with_type = FALSE; |
---|
272 | if (in_array('normal', $type) || in_array('default', $type)|| in_array('overridden', $type)) { |
---|
273 | $with_type = TRUE; |
---|
274 | } |
---|
275 | |
---|
276 | // set the table headers |
---|
277 | $header = array( |
---|
278 | dt('Machine name'), |
---|
279 | dt('Description'), |
---|
280 | dt('Type'), |
---|
281 | dt('Status'), |
---|
282 | dt('Tag'), |
---|
283 | ); |
---|
284 | |
---|
285 | // setup a row for each view |
---|
286 | foreach($views as $id => $view){ |
---|
287 | // if options were specified, check that first |
---|
288 | // mismatch push the loop to the next view |
---|
289 | if ($with_tags && !in_array($view->tag, $tags)) { |
---|
290 | continue; |
---|
291 | } |
---|
292 | if ($with_status && !$view->disabled == $status) { |
---|
293 | continue; |
---|
294 | } |
---|
295 | if ($with_type && strtolower($view->type) !== $type[0]) { |
---|
296 | continue; |
---|
297 | } |
---|
298 | if ($with_name && !stristr($view->name, $name[0])) { |
---|
299 | continue; |
---|
300 | } |
---|
301 | |
---|
302 | $row = array(); |
---|
303 | // each row entry should be in the same order as the header |
---|
304 | $row[] = $view->name; |
---|
305 | $row[] = $view->description; |
---|
306 | $row[] = $view->type; |
---|
307 | $row[] = $view->disabled ? dt('Disabled') : dt('Enabled'); |
---|
308 | $row[] = $view->tag; |
---|
309 | |
---|
310 | // place the row in the appropiate array, |
---|
311 | // so we can have disabled views at the bottom |
---|
312 | if($view->disabled) { |
---|
313 | $disabled_views[] = $row; |
---|
314 | } |
---|
315 | else{ |
---|
316 | $enabled_views[] = $row; |
---|
317 | } |
---|
318 | unset($row); |
---|
319 | |
---|
320 | // gather some statistics |
---|
321 | switch($view->type) { |
---|
322 | case dt('Normal'): |
---|
323 | $indb++; |
---|
324 | break; |
---|
325 | |
---|
326 | case dt('Overridden'): |
---|
327 | $overridden++; |
---|
328 | break; |
---|
329 | |
---|
330 | case dt('Default'): |
---|
331 | $incode++; |
---|
332 | break; |
---|
333 | } |
---|
334 | $total++; |
---|
335 | } |
---|
336 | |
---|
337 | $disabled = count($disabled_views); |
---|
338 | |
---|
339 | // sort alphabeticaly |
---|
340 | asort($disabled_views); |
---|
341 | asort($enabled_views); |
---|
342 | |
---|
343 | // if options were used |
---|
344 | $summary = ""; |
---|
345 | if ($with_name || $with_tags || $with_status || $with_type) { |
---|
346 | $summary = "Views"; |
---|
347 | |
---|
348 | if ($with_name) { |
---|
349 | $summary .= " named $name[0]"; |
---|
350 | } |
---|
351 | |
---|
352 | if ($with_tags) { |
---|
353 | $tags = implode(" or ", $tags); |
---|
354 | $summary .= " tagged $tags"; |
---|
355 | } |
---|
356 | |
---|
357 | if ($with_status) { |
---|
358 | $status_opt = implode("", $status_opt); |
---|
359 | $summary .= " which status is '$status_opt'"; |
---|
360 | } |
---|
361 | |
---|
362 | if ($with_type) { |
---|
363 | $type = ucfirst($type[0]); |
---|
364 | $summary .= " of type '$type'"; |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | if (!empty($summary)) { |
---|
369 | drush_print($summary . "\n"); |
---|
370 | } |
---|
371 | |
---|
372 | // print all rows as a table |
---|
373 | if ($total > 0) { |
---|
374 | $rows = array_merge($enabled_views, $disabled_views); |
---|
375 | // put the headers as first row |
---|
376 | array_unshift($rows, $header); |
---|
377 | |
---|
378 | drush_print_table($rows, TRUE); |
---|
379 | } |
---|
380 | |
---|
381 | // print the statistics messages |
---|
382 | drush_print(dt("A total of @total views were found in this Drupal installation:", array('@total' => $total))); |
---|
383 | drush_print(dt(" @indb views reside only in the database", array('@indb' => $indb ))); |
---|
384 | drush_print(dt(" @over views are overridden", array('@over' => $overridden))); |
---|
385 | drush_print(dt(" @incode views are in their default state", array('@incode' => $incode))); |
---|
386 | drush_print(dt(" @dis views are disabled\n", array('@dis' => $disabled))); |
---|
387 | } |
---|
388 | |
---|
389 | function drush_views_analyze() { |
---|
390 | views_include('analyze'); |
---|
391 | $messages_count = 0; |
---|
392 | $total = 0; |
---|
393 | |
---|
394 | foreach (views_get_all_views() as $view_name => $view) { |
---|
395 | $total++; |
---|
396 | if ($messages = views_analyze_view($view)) { |
---|
397 | drush_print($view_name); |
---|
398 | foreach ($messages as $message) { |
---|
399 | $messages_count++; |
---|
400 | drush_print($message['type'] .': '. $message['message'], 2); |
---|
401 | } |
---|
402 | } |
---|
403 | } |
---|
404 | drush_log(dt('A total of @total views were analyzed and @messages problems were found.', array('@total' => $total, '@messages' => $messages_count)), 'ok'); |
---|
405 | } |
---|
406 | |
---|
407 | /** |
---|
408 | * Enables views |
---|
409 | */ |
---|
410 | function drush_views_enable() { |
---|
411 | $viewnames = _convert_csv_to_array(func_get_args()); |
---|
412 | // Return early if no view names were specified. |
---|
413 | if (empty($viewnames)) { |
---|
414 | return drush_set_error(dt('Please specify a space delimited list of view names to enable')); |
---|
415 | } |
---|
416 | _views_drush_changestatus($viewnames, FALSE); |
---|
417 | } |
---|
418 | |
---|
419 | /** |
---|
420 | * Disables views |
---|
421 | */ |
---|
422 | function drush_views_disable() { |
---|
423 | $viewnames = _convert_csv_to_array(func_get_args()); |
---|
424 | // Return early if no view names were specified. |
---|
425 | if (empty($viewnames)) { |
---|
426 | return drush_set_error(dt('Please specify a space delimited list of view names to disable')); |
---|
427 | } |
---|
428 | _views_drush_changestatus($viewnames, TRUE); |
---|
429 | } |
---|
430 | |
---|
431 | /* |
---|
432 | * Helper function to enable / disable views |
---|
433 | * @param $viewnames: array of viewnames to process |
---|
434 | * @param $status: TRUE to disable or FALSE to enable the view |
---|
435 | */ |
---|
436 | function _views_drush_changestatus($viewnames = array(), $status = NULL) { |
---|
437 | if ($status !== NULL && !empty($viewnames)) { |
---|
438 | $changed = FALSE; |
---|
439 | $processed = $status ? dt('disabled') : dt('enabled'); |
---|
440 | $views_status = variable_get('views_defaults', array()); |
---|
441 | |
---|
442 | foreach ($viewnames as $key => $viewname) { |
---|
443 | if ($views_status[$viewname] !== $status) { |
---|
444 | $views_status[$viewname] = $status; |
---|
445 | $changed = TRUE; |
---|
446 | drush_log(dt("The view '!name' has been !processed", array('!name' => $viewname, '!processed' => $processed)), 'success'); |
---|
447 | } |
---|
448 | else { |
---|
449 | drush_set_error(dt("The view '!name' is already !processed", array('!name' => $viewname, '!processed' => $processed))); |
---|
450 | } |
---|
451 | } |
---|
452 | // If we made changes to views status, save them and clear caches |
---|
453 | if ($changed) { |
---|
454 | variable_set('views_defaults', $views_status); |
---|
455 | views_invalidate_cache(); |
---|
456 | drush_log(dt("Views cache was cleared"), 'ok'); |
---|
457 | menu_rebuild(); |
---|
458 | drush_log(dt("Menu cache was cleared"), 'ok'); |
---|
459 | } |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | /** |
---|
464 | * Adds a cache clear option for views. |
---|
465 | */ |
---|
466 | function views_drush_cache_clear(&$types) { |
---|
467 | $types['views'] = 'views_invalidate_cache'; |
---|
468 | } |
---|