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