Révision 5d12d676
Ajouté par Assos Assos il y a environ 6 ans
drupal7/sites/all/modules/views/drush/views.drush.inc | ||
---|---|---|
2 | 2 |
|
3 | 3 |
/** |
4 | 4 |
* @file |
5 |
* Drush integration of views.
|
|
5 |
* Drush integration for Views.
|
|
6 | 6 |
* |
7 |
* drush cache-clear views - Clears the views specific caches. |
|
8 |
* views-revert - Drush command to revert views overridden in the system. |
|
7 |
* Useful commands: |
|
8 |
* - drush cache-clear views - Clears the views specific caches. |
|
9 |
* - views-revert - Drush command to revert views overridden in the system. |
|
9 | 10 |
*/ |
10 | 11 |
|
11 | 12 |
/** |
12 |
* Implement hook_drush_help(). |
|
13 |
* Implements hook_drush_help().
|
|
13 | 14 |
*/ |
14 | 15 |
function views_drush_help($section) { |
15 | 16 |
switch ($section) { |
... | ... | |
18 | 19 |
$help .= dt('If no view names are specified, you will be presented with a list of overridden views to choose from. '); |
19 | 20 |
$help .= dt('To revert all views, do not specify any view names, and choose the option "All" from the options presented.'); |
20 | 21 |
return $help; |
22 |
|
|
21 | 23 |
case 'drush:views-list': |
22 | 24 |
return dt('Show a list of available views with information about them.'); |
25 |
|
|
23 | 26 |
case 'drush:views-enable': |
24 | 27 |
return dt('Enable the specified views. Follow the command with a space delimited list of view names'); |
28 |
|
|
25 | 29 |
case 'drush:views-disable': |
26 | 30 |
return dt('Disable the specified views. Follow the command with a space delimited list of view names'); |
27 | 31 |
} |
28 | 32 |
} |
29 | 33 |
|
30 | 34 |
/** |
31 |
* Implement hook_drush_command(). |
|
35 |
* Implements hook_drush_command().
|
|
32 | 36 |
*/ |
33 | 37 |
function views_drush_command() { |
34 | 38 |
$items = array(); |
... | ... | |
70 | 74 |
'tags' => 'A comma-separated list of views tags by which to filter the results.', |
71 | 75 |
'status' => 'Status of the views by which to filter the results. Choices: enabled, disabled.', |
72 | 76 |
'type' => 'Type of the views by which to filter the results. Choices: normal, default or overridden.', |
73 |
),
|
|
77 |
), |
|
74 | 78 |
'examples' => array( |
75 | 79 |
'drush vl' => 'Show a list of all available views.', |
76 | 80 |
'drush vl --name=blog' => 'Show a list of views which names contain "blog".', |
... | ... | |
143 | 147 |
// If the user specified a list of views on the CLI, revert those. |
144 | 148 |
elseif (!empty($viewnames)) { |
145 | 149 |
foreach ($viewnames as $key => $viewname) { |
146 |
$is_overridden = key_exists($viewname, $overridden); |
|
150 |
$is_overridden = array_key_exists($viewname, $overridden);
|
|
147 | 151 |
|
148 |
// Check if the provided view name is in the system |
|
149 |
if ($viewname && !key_exists($viewname, $views)) { |
|
152 |
// Check if the provided view name is in the system.
|
|
153 |
if ($viewname && !array_key_exists($viewname, $views)) {
|
|
150 | 154 |
drush_set_error(dt("'@viewname' view is not present in the system.", array('@viewname' => $viewname))); |
151 | 155 |
} |
152 | 156 |
// Check if the provided view is overridden. |
... | ... | |
158 | 162 |
views_revert_view($views[$viewname]); |
159 | 163 |
$i++; |
160 | 164 |
} |
161 |
// We should never get here but well...
|
|
165 |
// We should never get here but well. |
|
162 | 166 |
else { |
163 | 167 |
drush_set_error(dt( |
164 | 168 |
"The view specified '@viewname' is not provided in code, and thus cannot be reverted.", |
... | ... | |
168 | 172 |
} |
169 | 173 |
} |
170 | 174 |
|
171 |
// The user neither selected the "--all" option, nor provided a list of views to revert.
|
|
172 |
// Prompt the user. |
|
175 |
// The user neither selected the "--all" option, nor provided a list of views |
|
176 |
// to revert. Prompt the user.
|
|
173 | 177 |
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 |
// List of choices for the user. |
|
179 |
$overridden['all'] = dt('Revert all overridden views'); |
|
180 |
// Add a choice at the end. |
|
181 |
$choice = drush_choice($overridden, 'Enter a number to choose which view to revert.', '!key'); |
|
182 |
// Prompt the user. |
|
178 | 183 |
if ($choice !== FALSE) { |
179 |
// revert all views option
|
|
184 |
// Revert all views option.
|
|
180 | 185 |
if ($choice == 'all') { |
181 | 186 |
$i = views_revert_allviews($views); |
182 | 187 |
} |
183 |
// else the user specified a single view
|
|
188 |
// Else the user specified a single view.
|
|
184 | 189 |
else { |
185 | 190 |
views_revert_view($views[$choice]); |
186 | 191 |
$i++; |
... | ... | |
189 | 194 |
|
190 | 195 |
} |
191 | 196 |
|
192 |
// final results output
|
|
197 |
// Final results output.
|
|
193 | 198 |
if ($i == 0) { |
194 | 199 |
drush_log(dt('No views were reverted.'), 'ok'); |
195 | 200 |
} |
... | ... | |
199 | 204 |
} |
200 | 205 |
|
201 | 206 |
/** |
202 |
* Reverts all views |
|
203 |
* @param $views |
|
204 |
* All views in the system as provided by views_get_all_views(). |
|
207 |
* Reverts all views. |
|
208 |
* |
|
209 |
* @param array $views |
|
210 |
* All views in the system as provided by views_get_all_views(). |
|
205 | 211 |
*/ |
206 | 212 |
function views_revert_allviews($views) { |
207 | 213 |
$i = 0; |
... | ... | |
219 | 225 |
} |
220 | 226 |
|
221 | 227 |
/** |
222 |
* Revert a specified view |
|
223 |
* @param $view |
|
224 |
* The view object to be reverted |
|
228 |
* Revert a specified view. |
|
229 |
* |
|
230 |
* Checks on wether or not the view is overridden is handled in |
|
231 |
* views_revert_views_revert(). We perform a check here anyway in case someone |
|
232 |
* somehow calls this function on their own... |
|
225 | 233 |
* |
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...
|
|
234 |
* @param object $view
|
|
235 |
* The view object to be reverted.
|
|
228 | 236 |
*/ |
229 | 237 |
function views_revert_view($view) { |
230 |
// check anyway just in case
|
|
238 |
// Check anyway just in case.
|
|
231 | 239 |
if ($view->type == t('Overridden')) { |
232 | 240 |
// Revert the view. |
233 | 241 |
$view->delete(); |
... | ... | |
269 | 277 |
* Callback function for views-list command. |
270 | 278 |
*/ |
271 | 279 |
function drush_views_list() { |
272 |
// Initialize stuf |
|
280 |
// Initialize stuff.
|
|
273 | 281 |
$rows = array(); |
274 | 282 |
$disabled_views = array(); |
275 | 283 |
$enabled_views = array(); |
... | ... | |
281 | 289 |
|
282 | 290 |
$views = views_get_all_views(); |
283 | 291 |
|
284 |
// get the --name option
|
|
285 |
// TODO : take into account the case off a comma-separated list of names
|
|
292 |
// Get the --name option.
|
|
293 |
// @todo Take into account the case off a comma-separated list of names.
|
|
286 | 294 |
$name = drush_get_option_list('name'); |
287 | 295 |
$with_name = !empty($name) ? TRUE : FALSE; |
288 | 296 |
|
289 |
// get the --tags option
|
|
297 |
// Get the --tags option.
|
|
290 | 298 |
$tags = drush_get_option_list('tags'); |
291 | 299 |
$with_tags = !empty($tags) ? TRUE : FALSE; |
292 | 300 |
|
293 |
// get the --status option |
|
294 |
// store user input appart to reuse it after |
|
301 |
// Get the --status option. Store user input appart to reuse it after. |
|
295 | 302 |
$status_opt = drush_get_option_list('status'); |
296 |
// use the same logic than $view->disabled
|
|
303 |
// Use the same logic than $view->disabled.
|
|
297 | 304 |
if (in_array('disabled', $status_opt)) { |
298 | 305 |
$status = TRUE; |
299 | 306 |
$with_status = TRUE; |
... | ... | |
304 | 311 |
} |
305 | 312 |
else { |
306 | 313 |
$status = NULL; |
307 |
// wrong or empty --status option
|
|
314 |
// Wrong or empty --status option.
|
|
308 | 315 |
$with_status = FALSE; |
309 | 316 |
} |
310 | 317 |
|
311 |
// get the --type option
|
|
318 |
// Get the --type option.
|
|
312 | 319 |
$type = drush_get_option_list('type'); |
313 |
// use the same logic than $view->type
|
|
320 |
// Use the same logic than $view->type.
|
|
314 | 321 |
$with_type = FALSE; |
315 | 322 |
if (in_array('normal', $type) || in_array('default', $type)|| in_array('overridden', $type)) { |
316 | 323 |
$with_type = TRUE; |
317 | 324 |
} |
318 | 325 |
|
319 |
// set the table headers
|
|
326 |
// Set the table headers.
|
|
320 | 327 |
$header = array( |
321 | 328 |
dt('Machine name'), |
322 | 329 |
dt('Description'), |
... | ... | |
325 | 332 |
dt('Tag'), |
326 | 333 |
); |
327 | 334 |
|
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
|
|
335 |
// Setup a row for each view.
|
|
336 |
foreach ($views as $id => $view) {
|
|
337 |
// If options were specified, check that first mismatch push the loop to
|
|
338 |
// the next view.
|
|
332 | 339 |
if ($with_tags && !in_array($view->tag, $tags)) { |
333 | 340 |
continue; |
334 | 341 |
} |
... | ... | |
343 | 350 |
} |
344 | 351 |
|
345 | 352 |
$row = array(); |
346 |
// each row entry should be in the same order as the header
|
|
353 |
// Each row entry should be in the same order as the header.
|
|
347 | 354 |
$row[] = $view->name; |
348 | 355 |
$row[] = $view->description; |
349 | 356 |
$row[] = $view->type; |
350 | 357 |
$row[] = $view->disabled ? dt('Disabled') : dt('Enabled'); |
351 | 358 |
$row[] = $view->tag; |
352 | 359 |
|
353 |
// place the row in the appropiate array,
|
|
354 |
// so we can have disabled views at the bottom
|
|
355 |
if($view->disabled) { |
|
360 |
// Place the row in the appropiate array so we can have disabled views at
|
|
361 |
// the bottom.
|
|
362 |
if ($view->disabled) {
|
|
356 | 363 |
$disabled_views[] = $row; |
357 |
}
|
|
358 |
else{ |
|
364 |
} |
|
365 |
else {
|
|
359 | 366 |
$enabled_views[] = $row; |
360 | 367 |
} |
361 | 368 |
unset($row); |
362 | 369 |
|
363 |
// gather some statistics
|
|
364 |
switch($view->type) { |
|
370 |
// Gather some statistics.
|
|
371 |
switch ($view->type) {
|
|
365 | 372 |
case dt('Normal'): |
366 | 373 |
$indb++; |
367 | 374 |
break; |
... | ... | |
379 | 386 |
|
380 | 387 |
$disabled = count($disabled_views); |
381 | 388 |
|
382 |
// sort alphabeticaly
|
|
389 |
// Sort alphabeticaly.
|
|
383 | 390 |
asort($disabled_views); |
384 | 391 |
asort($enabled_views); |
385 | 392 |
|
386 |
// if options were used
|
|
393 |
// If options were used.
|
|
387 | 394 |
$summary = ""; |
388 | 395 |
if ($with_name || $with_tags || $with_status || $with_type) { |
389 | 396 |
$summary = "Views"; |
... | ... | |
412 | 419 |
drush_print($summary . "\n"); |
413 | 420 |
} |
414 | 421 |
|
415 |
// print all rows as a table
|
|
422 |
// Print all rows as a table.
|
|
416 | 423 |
if ($total > 0) { |
417 | 424 |
$rows = array_merge($enabled_views, $disabled_views); |
418 |
// put the headers as first row
|
|
425 |
// Put the headers as first row.
|
|
419 | 426 |
array_unshift($rows, $header); |
420 | 427 |
|
421 | 428 |
drush_print_table($rows, TRUE); |
422 | 429 |
} |
423 | 430 |
|
424 |
// print the statistics messages
|
|
431 |
// Print the statistics messages.
|
|
425 | 432 |
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 )));
|
|
433 |
drush_print(dt(" @indb views reside only in the database", array('@indb' => $indb))); |
|
427 | 434 |
drush_print(dt(" @over views are overridden", array('@over' => $overridden))); |
428 | 435 |
drush_print(dt(" @incode views are in their default state", array('@incode' => $incode))); |
429 | 436 |
drush_print(dt(" @dis views are disabled\n", array('@dis' => $disabled))); |
430 | 437 |
} |
431 | 438 |
|
439 |
/** |
|
440 |
* Analyze all installed views. |
|
441 |
*/ |
|
432 | 442 |
function drush_views_analyze() { |
433 | 443 |
views_include('analyze'); |
434 | 444 |
$messages_count = 0; |
... | ... | |
440 | 450 |
drush_print($view_name); |
441 | 451 |
foreach ($messages as $message) { |
442 | 452 |
$messages_count++; |
443 |
drush_print($message['type'] .': '. $message['message'], 2);
|
|
453 |
drush_print($message['type'] . ': ' . $message['message'], 2);
|
|
444 | 454 |
} |
445 | 455 |
} |
446 | 456 |
} |
... | ... | |
448 | 458 |
} |
449 | 459 |
|
450 | 460 |
/** |
451 |
* Enables views |
|
461 |
* Enables views.
|
|
452 | 462 |
*/ |
453 | 463 |
function drush_views_enable() { |
454 | 464 |
$viewnames = _convert_csv_to_array(func_get_args()); |
... | ... | |
460 | 470 |
} |
461 | 471 |
|
462 | 472 |
/** |
463 |
* Disables views |
|
473 |
* Disables views.
|
|
464 | 474 |
*/ |
465 | 475 |
function drush_views_disable() { |
466 | 476 |
$viewnames = _convert_csv_to_array(func_get_args()); |
... | ... | |
472 | 482 |
} |
473 | 483 |
|
474 | 484 |
/** |
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 |
|
485 |
* Helper function to enable / disable views. |
|
486 |
* |
|
487 |
* @param array $viewnames |
|
488 |
* Names of the views to process. |
|
489 |
* @param bool $status |
|
490 |
* TRUE to disable or FALSE to enable the view. |
|
478 | 491 |
*/ |
479 | 492 |
function _views_drush_changestatus($viewnames = array(), $status = NULL) { |
480 | 493 |
if ($status !== NULL && !empty($viewnames)) { |
... | ... | |
492 | 505 |
drush_set_error(dt("The view '!name' is already !processed", array('!name' => $viewname, '!processed' => $processed))); |
493 | 506 |
} |
494 | 507 |
} |
495 |
// If we made changes to views status, save them and clear caches |
|
508 |
// If we made changes to views status, save them and clear caches.
|
|
496 | 509 |
if ($changed) { |
497 | 510 |
variable_set('views_defaults', $views_status); |
498 | 511 |
views_invalidate_cache(); |
... | ... | |
504 | 517 |
|
505 | 518 |
/** |
506 | 519 |
* Adds a cache clear option for views. |
520 |
* |
|
521 |
* @param array $types |
|
522 |
* The list of cache types that are available. |
|
507 | 523 |
*/ |
508 | 524 |
function views_drush_cache_clear(&$types) { |
509 | 525 |
$types['views'] = 'views_invalidate_cache'; |
Formats disponibles : Unified diff
Weekly update of contrib modules