Projet

Général

Profil

Paste
Télécharger (15,1 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / views / drush / views.drush.inc @ a5572547

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
    '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
  $items['views-dev'] = array(
52
    'callback' => 'views_development_settings',
53
    'drupal dependencies' => array('views'),
54
    'description' => 'Set the Views settings to more developer-oriented values.',
55
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
56
    'aliases' => array('vd'),
57
  );
58

    
59
  $items['views-list'] = array(
60
    'drupal dependencies' => array('views'),
61
    'description' => 'Get a list of all views in the system.',
62
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
63
    'aliases' => array('vl'),
64
    'options' => array(
65
      'name' => 'String contained in view\'s name by which filter the results.',
66
      'tags' => 'A comma-separated list of views tags by which to filter the results.',
67
      'status' => 'Status of the views by which to filter the results. Choices: enabled, disabled.',
68
      'type' => 'Type of the views by which to filter the results. Choices: normal, default or overridden.',
69
      ),
70
    'examples' => array(
71
      'drush vl' => 'Show a list of all available views.',
72
      'drush vl --name=blog' => 'Show a list of views which names contain "blog".',
73
      'drush vl --tags=tag1,tag2' => 'Show a list of views tagged with "tag1" or "tag2".',
74
      'drush vl --status=enabled' => 'Show a list of enabled views.',
75
      'drush vl --type=overridden' => 'Show a list of overridden views.',
76
    ),
77
  );
78
  $items['views-analyze'] = array(
79
    'drupal dependencies' => array('views', 'views_ui'),
80
    'description' => 'Get a list of all Views analyze warnings',
81
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
82
    'aliases' => array('va'),
83
  );
84
  $items['views-enable'] = array(
85
    'drupal dependencies' => array('views'),
86
    'description' => 'Enable the specified views.',
87
    'arguments' => array(
88
      'views' => 'A space delimited list of view names.',
89
    ),
90
    'aliases' => array('ven'),
91
    'examples' => array(
92
      'drush ven frontpage taxonomy_term' => 'Enable the frontpage and taxonomy_term views.',
93
    ),
94
  );
95
  $items['views-disable'] = array(
96
    'drupal dependencies' => array('views'),
97
    'description' => 'Disable the specified views.',
98
    'arguments' => array(
99
      'views' => 'A space delimited list of view names.',
100
    ),
101
    'aliases' => array('vdis'),
102
    'examples' => array(
103
      'drush vdis frontpage taxonomy_term' => 'Disable the frontpage and taxonomy_term views.',
104
    ),
105
  );
106

    
107
  return $items;
108
}
109

    
110
/**
111
 * Callback function for views-revert command.
112
 */
113
function views_revert_views() {
114
  $views = views_get_all_views();
115
  $i = 0;
116
  // The provided views names specified in the command.
117
  $viewnames = _convert_csv_to_array(func_get_args());
118

    
119
  // Find all overridden views.
120
  foreach ($views as $view) {
121
    if ($view->disabled) {
122
      continue;
123
    }
124
    if ($view->type == dt('Overridden')) {
125
      $overridden[$view->name] = $view->name;
126
    }
127
  }
128

    
129
  // Return early if there are no overridden views in the system.
130
  if (empty($overridden)) {
131
    return drush_set_error(dt('There are no overridden views in the system.'));
132
  }
133

    
134
  // If the user specified in the command the views to be overridden.
135
  if (!empty($viewnames)) {
136
    foreach ($viewnames as $key => $viewname) {
137
      $is_overridden = key_exists($viewname, $overridden);
138
      // Check if the provided view name is in the system
139
      if ($viewname && !key_exists($viewname, $views)) {
140
        drush_set_error(dt("'@viewname' view is not present in the system.", array('@viewname' => $viewname)));
141
      }
142
      // Check if the provided view is overridden.
143
      elseif (!$is_overridden) {
144
        drush_set_error(dt("The view specified '@viewname' is not overridden.", array('@viewname' => $viewname)));
145
      }
146
      // If the view is overriden, revert it.
147
      elseif ($is_overridden){
148
        views_revert_view($views[$viewname]);
149
        $i++;
150
      }
151
      // We should never get here but well...
152
      else {
153
        drush_set_error(dt("The view specified '@viewname' is not provided in code, and thus cannot be reverted.", array('@viewname' => $viewname)));
154
      }
155
    }
156
  }
157

    
158
  // The user did not specify any views in the command, prompt the user
159
  else {
160
    // list of choices for the user
161
    $overridden['all'] = dt('Revert all overridden views'); // add a choice at the end
162
    $choice = drush_choice($overridden, 'Enter a number to choose which view to revert.', '!key'); // prompt the user
163

    
164
    if ($choice !== FALSE) {
165
      // revert all views option
166
      if ($choice == 'all') {
167
        $i = views_revert_allviews($views);
168
      }
169
      // else the user specified a single view
170
      else {
171
        views_revert_view($views[$choice]);
172
        $i++;
173
      }
174
    }
175

    
176
  }
177

    
178
  // final results output
179
  if ($i == 0) {
180
    drush_log(dt('No views were reverted.'), 'ok');
181
  }
182
  else {
183
    drush_log(dt('Reverted a total of @count views.', array('@count' => $i)), 'ok');
184
  }
185
}
186

    
187
/**
188
 * Reverts all views
189
 * @param $views
190
 * All views in the system as provided by views_get_all_views().
191
 */
192
function views_revert_allviews($views) {
193
  $i = 0;
194
  foreach ($views as $view) {
195
    if ($view->disabled) {
196
      continue;
197
    }
198

    
199
    if ($view->type == t('Overridden')) {
200
      views_revert_view($view);
201
      $i++;
202
    }
203
  }
204
  return $i;
205
}
206

    
207
/**
208
 * Revert a specified view
209
 * @param $view
210
 * The view object to be reverted
211
 *
212
 * Checks on wether or not the view is overridden is handled in views_revert_views_revert()
213
 * We perform a check here anyway in case someone somehow calls this function on their own...
214
 */
215
function views_revert_view($view) {
216
  // check anyway just in case
217
  if ($view->type == t('Overridden')) {
218
    // Revert the view.
219
    $view->delete();
220
    // Clear its cache.
221
    ctools_include('object-cache');
222
    ctools_object_cache_clear('view', $view->name);
223
    // Give feedback.
224
    $message = dt("Reverted the view '@viewname'", array('@viewname' => $view->name));
225
    drush_log($message, 'success');
226
    // Reverted one more view.
227
  }
228
  else {
229
    drush_set_error(dt("The view '@viewname' is not overridden.", array('@viewname' => $view->name)));
230
  }
231
}
232

    
233
/**
234
 * Change the settings to a more developer oriented value.
235
 */
236
function views_development_settings() {
237
  variable_set('views_ui_show_listing_filters', TRUE);
238
  variable_set('views_ui_show_master_display', TRUE);
239
  variable_set('views_ui_show_advanced_column', TRUE);
240
  variable_set('views_ui_always_live_preview', FALSE);
241
  variable_set('views_ui_always_live_preview_button', TRUE);
242
  variable_set('views_ui_show_preview_information', TRUE);
243
  variable_set('views_ui_show_sql_query', TRUE);
244
  variable_set('views_ui_show_performance_statistics', TRUE);
245
  variable_set('views_show_additional_queries', TRUE);
246
  variable_set('views_devel_output', TRUE);
247
  variable_set('views_devel_region', 'message');
248
  variable_set('views_ui_display_embed', TRUE);
249
  $message = dt("Setup the new views settings.");
250
  drush_log($message, 'success');
251
}
252

    
253

    
254
/**
255
 * Callback function for views-list command.
256
 */
257
function drush_views_list() {
258
  // Initialize stuf
259
  $rows = array();
260
  $disabled_views = array();
261
  $enabled_views = array();
262
  $overridden = 0;
263
  $indb = 0;
264
  $incode = 0;
265
  $disabled = 0;
266
  $total = 0;
267

    
268
  $views = views_get_all_views();
269

    
270
  // get the --name option
271
  // TODO : take into account the case off a comma-separated list of names
272
  $name = drush_get_option_list('name');
273
  $with_name = !empty($name) ? TRUE : FALSE;
274

    
275
  // get the --tags option
276
  $tags = drush_get_option_list('tags');
277
  $with_tags = !empty($tags) ? TRUE : FALSE;
278

    
279
  // get the --status option
280
  // store user input appart to reuse it after
281
  $status_opt = drush_get_option_list('status');
282
  // use the same logic than $view->disabled
283
  if (in_array('disabled', $status_opt)) {
284
    $status = TRUE;
285
    $with_status = TRUE;
286
  }
287
  elseif (in_array('enabled', $status_opt)) {
288
    $status = FALSE;
289
    $with_status = TRUE;
290
  }
291
  else {
292
    $status = NULL;
293
    // wrong or empty --status option
294
    $with_status = FALSE;
295
  }
296

    
297
  // get the --type option
298
  $type = drush_get_option_list('type');
299
  // use the same logic than $view->type
300
  $with_type = FALSE;
301
  if (in_array('normal', $type) || in_array('default', $type)|| in_array('overridden', $type)) {
302
    $with_type = TRUE;
303
  }
304

    
305
  // set the table headers
306
  $header = array(
307
    dt('Machine name'),
308
    dt('Description'),
309
    dt('Type'),
310
    dt('Status'),
311
    dt('Tag'),
312
  );
313

    
314
  // setup a row for each view
315
  foreach($views as $id => $view){
316
    // if options were specified, check that first
317
    // mismatch push the loop to the next view
318
    if ($with_tags && !in_array($view->tag, $tags)) {
319
      continue;
320
    }
321
    if ($with_status && !$view->disabled == $status) {
322
      continue;
323
    }
324
    if ($with_type && strtolower($view->type) !== $type[0]) {
325
      continue;
326
    }
327
    if ($with_name && !stristr($view->name, $name[0])) {
328
      continue;
329
    }
330

    
331
    $row = array();
332
    // each row entry should be in the same order as the header
333
    $row[] = $view->name;
334
    $row[] = $view->description;
335
    $row[] = $view->type;
336
    $row[] = $view->disabled ? dt('Disabled') : dt('Enabled');
337
    $row[] = $view->tag;
338

    
339
    // place the row in the appropiate array,
340
    // so we can have disabled views at the bottom
341
    if($view->disabled) {
342
      $disabled_views[] = $row;
343
      }
344
    else{
345
      $enabled_views[] = $row;
346
    }
347
    unset($row);
348

    
349
    // gather some statistics
350
    switch($view->type) {
351
      case dt('Normal'):
352
        $indb++;
353
        break;
354

    
355
      case dt('Overridden'):
356
        $overridden++;
357
        break;
358

    
359
      case dt('Default'):
360
        $incode++;
361
        break;
362
    }
363
    $total++;
364
  }
365

    
366
  $disabled = count($disabled_views);
367

    
368
  // sort alphabeticaly
369
  asort($disabled_views);
370
  asort($enabled_views);
371

    
372
  // if options were used
373
  $summary = "";
374
  if ($with_name || $with_tags || $with_status || $with_type) {
375
    $summary = "Views";
376

    
377
    if ($with_name) {
378
      $summary .= " named $name[0]";
379
    }
380

    
381
    if ($with_tags) {
382
      $tags = implode(" or ", $tags);
383
      $summary .= " tagged $tags";
384
    }
385

    
386
    if ($with_status) {
387
      $status_opt = implode("", $status_opt);
388
      $summary .= " which status is '$status_opt'";
389
    }
390

    
391
    if ($with_type) {
392
      $type = ucfirst($type[0]);
393
      $summary .= " of type '$type'";
394
    }
395
  }
396

    
397
  if (!empty($summary)) {
398
    drush_print($summary . "\n");
399
  }
400

    
401
  // print all rows as a table
402
  if ($total > 0) {
403
    $rows = array_merge($enabled_views, $disabled_views);
404
    // put the headers as first row
405
    array_unshift($rows, $header);
406

    
407
    drush_print_table($rows, TRUE);
408
  }
409

    
410
  // print the statistics messages
411
  drush_print(dt("A total of @total views were found in this Drupal installation:", array('@total' => $total)));
412
  drush_print(dt("  @indb views reside only in the database", array('@indb' => $indb )));
413
  drush_print(dt("  @over views are overridden", array('@over' => $overridden)));
414
  drush_print(dt("  @incode views are in their default state", array('@incode' => $incode)));
415
  drush_print(dt("  @dis views are disabled\n", array('@dis' => $disabled)));
416
}
417

    
418
function drush_views_analyze() {
419
  views_include('analyze');
420
  $messages_count = 0;
421
  $total = 0;
422

    
423
  foreach (views_get_all_views() as $view_name => $view) {
424
    $total++;
425
    if ($messages = views_analyze_view($view)) {
426
      drush_print($view_name);
427
      foreach ($messages as $message) {
428
        $messages_count++;
429
        drush_print($message['type'] .': '. $message['message'], 2);
430
      }
431
    }
432
  }
433
  drush_log(dt('A total of @total views were analyzed and @messages problems were found.', array('@total' => $total, '@messages' => $messages_count)), 'ok');
434
}
435

    
436
/**
437
 * Enables views
438
 */
439
function drush_views_enable() {
440
  $viewnames = _convert_csv_to_array(func_get_args());
441
  // Return early if no view names were specified.
442
  if (empty($viewnames)) {
443
    return drush_set_error(dt('Please specify a space delimited list of view names to enable'));
444
  }
445
  _views_drush_changestatus($viewnames, FALSE);
446
}
447

    
448
/**
449
 * Disables views
450
 */
451
function drush_views_disable() {
452
  $viewnames = _convert_csv_to_array(func_get_args());
453
  // Return early if no view names were specified.
454
  if (empty($viewnames)) {
455
    return drush_set_error(dt('Please specify a space delimited list of view names to disable'));
456
  }
457
  _views_drush_changestatus($viewnames, TRUE);
458
}
459

    
460
/**
461
 * Helper function to enable / disable views
462
 * @param $viewnames: array of viewnames to process
463
 * @param $status: TRUE to disable or FALSE to enable the view
464
 */
465
function _views_drush_changestatus($viewnames = array(), $status = NULL) {
466
  if ($status !== NULL && !empty($viewnames)) {
467
    $changed = FALSE;
468
    $processed = $status ? dt('disabled') : dt('enabled');
469
    $views_status = variable_get('views_defaults', array());
470

    
471
    foreach ($viewnames as $key => $viewname) {
472
      if ($views_status[$viewname] !== $status) {
473
        $views_status[$viewname] = $status;
474
        $changed = TRUE;
475
        drush_log(dt("The view '!name' has been !processed", array('!name' => $viewname, '!processed' => $processed)), 'success');
476
      }
477
      else {
478
        drush_set_error(dt("The view '!name' is already !processed", array('!name' => $viewname, '!processed' => $processed)));
479
      }
480
    }
481
    // If we made changes to views status, save them and clear caches
482
    if ($changed) {
483
      variable_set('views_defaults', $views_status);
484
      views_invalidate_cache();
485
      drush_log(dt("Views cache was cleared"), 'ok');
486
      drush_log(dt("Menu cache is set to be rebuilt on the next request."), 'ok');
487
    }
488
  }
489
}
490

    
491
/**
492
 * Adds a cache clear option for views.
493
 */
494
function views_drush_cache_clear(&$types) {
495
  $types['views'] = 'views_invalidate_cache';
496
}