00001 <?php
00013 function views_drush_help($section) {
00014 switch ($section) {
00015 case 'drush:views-revert':
00016 $help = dt('Reverts views in the drupal installation that have been overriden. ');
00017 $help .= dt('If no view names are specified, you will be presented with a list of overridden views to choose from. ');
00018 $help .= dt('To revert all views, do not specify any view names, and choose the option "All" from the options presented.');
00019 return $help;
00020 case 'drush:views-list':
00021 return dt('Show a list of available views with information about them.');
00022 case 'drush:views-enable':
00023 return dt('Enable the specified views. Follow the command with a space delimited list of view names');
00024 case 'drush:views-disable':
00025 return dt('Disable the specified views. Follow the command with a space delimited list of view names');
00026 }
00027 }
00028
00032 function views_drush_command() {
00033 $items = array();
00034
00035 $items['views-revert'] = array(
00036 'callback' => 'views_revert_views',
00037 'drupal dependencies' => array('views'),
00038 'description' => 'Revert overridden views to their default state. Make sure to backup first.',
00039 'arguments' => array(
00040 'views' => 'A space delimited list of view names.',
00041 ),
00042 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
00043 'aliases' => array('vr'),
00044 'examples' => array(
00045 'drush vr archive' => 'Reverts the "archive" view.',
00046 'drush rln archive frontpage' => 'Reverts the "archive" and "frontpage" view.',
00047 'drush vr' => 'Will present you with a list of overridden views to choose from, and an option to revert all overridden views.',
00048 ),
00049 );
00050 $items['views-dev'] = array(
00051 'callback' => 'views_development_settings',
00052 'drupal dependencies' => array('views'),
00053 'description' => 'Setup the views settings to a more developer oriented value..',
00054 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
00055 'aliases' => array('vd'),
00056 );
00057
00058 $items['views-list'] = array(
00059 'drupal dependencies' => array('views'),
00060 'description' => 'Get a list of all views in the system.',
00061 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
00062 'aliases' => array('vl'),
00063 'options' => array(
00064 'name' => 'String contained in view\'s name by which filter the results.',
00065 'tags' => 'A comma-separated list of views tags by which to filter the results.',
00066 'status' => 'Status of the views by which to filter the results. Choices: enabled, disabled.',
00067 'type' => 'Type of the views by which to filter the results. Choices: normal, default or overridden.',
00068 ),
00069 'examples' => array(
00070 'drush vl' => 'Show a list of all available views.',
00071 'drush vl --name=blog' => 'Show a list of views which names contain "blog".',
00072 'drush vl --tags=tag1,tag2' => 'Show a list of views tagged with "tag1" or "tag2".',
00073 'drush vl --status=enabled' => 'Show a list of enabled views.',
00074 'drush vl --type=overridden' => 'Show a list of overridden views.',
00075 ),
00076 );
00077 $items['views-analyze'] = array(
00078 'drupal dependencies' => array('views', 'views_ui'),
00079 'description' => 'Get a list of all Views analyze warnings',
00080 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
00081 'aliases' => array('va'),
00082 );
00083 $items['views-enable'] = array(
00084 'drupal dependencies' => array('views'),
00085 'description' => 'Enable the specified views.',
00086 'arguments' => array(
00087 'views' => 'A space delimited list of view names.',
00088 ),
00089 'aliases' => array('ven'),
00090 'examples' => array(
00091 'drush ven frontpage taxonomy_term' => 'Enable the frontpage and taxonomy_term views.',
00092 ),
00093 );
00094 $items['views-disable'] = array(
00095 'drupal dependencies' => array('views'),
00096 'description' => 'Disable the specified views.',
00097 'arguments' => array(
00098 'views' => 'A space delimited list of view names.',
00099 ),
00100 'aliases' => array('vdis'),
00101 'examples' => array(
00102 'drush vdis frontpage taxonomy_term' => 'Disable the frontpage and taxonomy_term views.',
00103 ),
00104 );
00105
00106 return $items;
00107 }
00108
00112 function views_revert_views() {
00113 $views = views_get_all_views();
00114 $i = 0;
00115
00116 $viewnames = _convert_csv_to_array(func_get_args());
00117
00118
00119 foreach ($views as $view) {
00120 if ($view->disabled) {
00121 continue;
00122 }
00123 if ($view->type == dt('Overridden')) {
00124 $overridden[$view->name] = $view->name;
00125 }
00126 }
00127
00128
00129 if (empty($overridden)) {
00130 return drush_set_error(dt('There are no overridden views in the system.'));
00131 }
00132
00133
00134 if (!empty($viewnames)) {
00135 foreach ($viewnames as $key => $viewname) {
00136 $is_overridden = key_exists($viewname, $overridden);
00137
00138 if ($viewname && !key_exists($viewname, $views)) {
00139 drush_set_error(dt("'@viewname' view is not present in the system.", array('@viewname' => $viewname)));
00140 }
00141
00142 elseif (!$is_overridden) {
00143 drush_set_error(dt("The view specified '@viewname' is not overridden.", array('@viewname' => $viewname)));
00144 }
00145
00146 elseif ($is_overridden){
00147 views_revert_view($views[$viewname]);
00148 $i++;
00149 }
00150
00151 else {
00152 drush_set_error(dt("The view specified '@viewname' is not provided in code, and thus cannot be reverted.", array('@viewname' => $viewname)));
00153 }
00154 }
00155 }
00156
00157
00158 else {
00159
00160 $overridden['all'] = dt('Revert all overridden views');
00161 $choice = drush_choice($overridden, 'Enter a number to choose which view to revert.', '!key');
00162
00163 if ($choice !== FALSE) {
00164
00165 if ($choice == 'all') {
00166 $i = views_revert_allviews($views);
00167 }
00168
00169 else {
00170 views_revert_view($views[$choice]);
00171 $i++;
00172 }
00173 }
00174
00175 }
00176
00177
00178 if ($i == 0) {
00179 drush_log(dt('No views were reverted.'), 'ok');
00180 }
00181 else {
00182 drush_log(dt('Reverted a total of @count views.', array('@count' => $i)), 'ok');
00183 }
00184 }
00185
00191 function views_revert_allviews($views) {
00192 $i = 0;
00193 foreach ($views as $view) {
00194 if ($view->disabled) {
00195 continue;
00196 }
00197
00198 if ($view->type == t('Overridden')) {
00199 views_revert_view($view);
00200 $i++;
00201 }
00202 }
00203 return $i;
00204 }
00205
00214 function views_revert_view($view) {
00215
00216 if ($view->type == t('Overridden')) {
00217
00218 $view->delete();
00219
00220 ctools_include('object-cache');
00221 ctools_object_cache_clear('view', $view->name);
00222
00223 $message = dt("Reverted the view '@viewname'", array('@viewname' => $view->name));
00224 drush_log($message, 'success');
00225
00226 }
00227 else {
00228 drush_set_error(dt("The view '@viewname' is not overridden.", array('@viewname' => $view->name)));
00229 }
00230 }
00231
00235 function views_development_settings() {
00236 variable_set('views_ui_show_listing_filters', TRUE);
00237 variable_set('views_ui_show_master_display', TRUE);
00238 variable_set('views_ui_show_advanced_column', TRUE);
00239 variable_set('views_ui_always_live_preview_button', TRUE);
00240 variable_set('views_ui_show_preview_information', TRUE);
00241 variable_set('views_ui_show_sql_query', TRUE);
00242 variable_set('views_ui_show_performance_statistics', TRUE);
00243 variable_set('views_show_additional_queries', TRUE);
00244 variable_set('views_devel_output', TRUE);
00245 variable_set('views_devel_region', 'message');
00246 variable_set('views_ui_display_embed', TRUE);
00247 $message = dt("Setup the new views settings.");
00248 drush_log($message, 'success');
00249 }
00250
00251
00255 function drush_views_list() {
00256
00257 $rows = array();
00258 $disabled_views = array();
00259 $enabled_views = array();
00260 $overridden = 0;
00261 $indb = 0;
00262 $incode = 0;
00263 $disabled = 0;
00264 $total = 0;
00265
00266 $views = views_get_all_views();
00267
00268
00269
00270 $name = drush_get_option_list('name');
00271 $with_name = !empty($name) ? TRUE : FALSE;
00272
00273
00274 $tags = drush_get_option_list('tags');
00275 $with_tags = !empty($tags) ? TRUE : FALSE;
00276
00277
00278
00279 $status_opt = drush_get_option_list('status');
00280
00281 if (in_array('disabled', $status_opt)) {
00282 $status = TRUE;
00283 $with_status = TRUE;
00284 }
00285 elseif (in_array('enabled', $status_opt)) {
00286 $status = FALSE;
00287 $with_status = TRUE;
00288 }
00289 else {
00290 $status = NULL;
00291
00292 $with_status = FALSE;
00293 }
00294
00295
00296 $type = drush_get_option_list('type');
00297
00298 $with_type = FALSE;
00299 if (in_array('normal', $type) || in_array('default', $type)|| in_array('overridden', $type)) {
00300 $with_type = TRUE;
00301 }
00302
00303
00304 $header = array(
00305 dt('Machine name'),
00306 dt('Description'),
00307 dt('Type'),
00308 dt('Status'),
00309 dt('Tag'),
00310 );
00311
00312
00313 foreach($views as $id => $view){
00314
00315
00316 if ($with_tags && !in_array($view->tag, $tags)) {
00317 continue;
00318 }
00319 if ($with_status && !$view->disabled == $status) {
00320 continue;
00321 }
00322 if ($with_type && strtolower($view->type) !== $type[0]) {
00323 continue;
00324 }
00325 if ($with_name && !stristr($view->name, $name[0])) {
00326 continue;
00327 }
00328
00329 $row = array();
00330
00331 $row[] = $view->name;
00332 $row[] = $view->description;
00333 $row[] = $view->type;
00334 $row[] = $view->disabled ? dt('Disabled') : dt('Enabled');
00335 $row[] = $view->tag;
00336
00337
00338
00339 if($view->disabled) {
00340 $disabled_views[] = $row;
00341 }
00342 else{
00343 $enabled_views[] = $row;
00344 }
00345 unset($row);
00346
00347
00348 switch($view->type) {
00349 case dt('Normal'):
00350 $indb++;
00351 break;
00352
00353 case dt('Overridden'):
00354 $overridden++;
00355 break;
00356
00357 case dt('Default'):
00358 $incode++;
00359 break;
00360 }
00361 $total++;
00362 }
00363
00364 $disabled = count($disabled_views);
00365
00366
00367 asort($disabled_views);
00368 asort($enabled_views);
00369
00370
00371 $summary = "";
00372 if ($with_name || $with_tags || $with_status || $with_type) {
00373 $summary = "Views";
00374
00375 if ($with_name) {
00376 $summary .= " named $name[0]";
00377 }
00378
00379 if ($with_tags) {
00380 $tags = implode(" or ", $tags);
00381 $summary .= " tagged $tags";
00382 }
00383
00384 if ($with_status) {
00385 $status_opt = implode("", $status_opt);
00386 $summary .= " which status is '$status_opt'";
00387 }
00388
00389 if ($with_type) {
00390 $type = ucfirst($type[0]);
00391 $summary .= " of type '$type'";
00392 }
00393 }
00394
00395 if (!empty($summary)) {
00396 drush_print($summary . "\n");
00397 }
00398
00399
00400 if ($total > 0) {
00401 $rows = array_merge($enabled_views, $disabled_views);
00402
00403 array_unshift($rows, $header);
00404
00405 drush_print_table($rows, TRUE);
00406 }
00407
00408
00409 drush_print(dt("A total of @total views were found in this Drupal installation:", array('@total' => $total)));
00410 drush_print(dt(" @indb views reside only in the database", array('@indb' => $indb )));
00411 drush_print(dt(" @over views are overridden", array('@over' => $overridden)));
00412 drush_print(dt(" @incode views are in their default state", array('@incode' => $incode)));
00413 drush_print(dt(" @dis views are disabled\n", array('@dis' => $disabled)));
00414 }
00415
00416 function drush_views_analyze() {
00417 views_include('analyze');
00418 $messages_count = 0;
00419 $total = 0;
00420
00421 foreach (views_get_all_views() as $view_name => $view) {
00422 $total++;
00423 if ($messages = views_analyze_view($view)) {
00424 drush_print($view_name);
00425 foreach ($messages as $message) {
00426 $messages_count++;
00427 drush_print($message['type'] .': '. $message['message'], 2);
00428 }
00429 }
00430 }
00431 drush_log(dt('A total of @total views were analyzed and @messages problems were found.', array('@total' => $total, '@messages' => $messages_count)), 'ok');
00432 }
00433
00437 function drush_views_enable() {
00438 $viewnames = _convert_csv_to_array(func_get_args());
00439
00440 if (empty($viewnames)) {
00441 return drush_set_error(dt('Please specify a space delimited list of view names to enable'));
00442 }
00443 _views_drush_changestatus($viewnames, FALSE);
00444 }
00445
00449 function drush_views_disable() {
00450 $viewnames = _convert_csv_to_array(func_get_args());
00451
00452 if (empty($viewnames)) {
00453 return drush_set_error(dt('Please specify a space delimited list of view names to disable'));
00454 }
00455 _views_drush_changestatus($viewnames, TRUE);
00456 }
00457
00458
00459
00460
00461
00462
00463 function _views_drush_changestatus($viewnames = array(), $status = NULL) {
00464 if ($status !== NULL && !empty($viewnames)) {
00465 $changed = FALSE;
00466 $processed = $status ? dt('disabled') : dt('enabled');
00467 $views_status = variable_get('views_defaults', array());
00468
00469 foreach ($viewnames as $key => $viewname) {
00470 if ($views_status[$viewname] !== $status) {
00471 $views_status[$viewname] = $status;
00472 $changed = TRUE;
00473 drush_log(dt("The view '!name' has been !processed", array('!name' => $viewname, '!processed' => $processed)), 'success');
00474 }
00475 else {
00476 drush_set_error(dt("The view '!name' is already !processed", array('!name' => $viewname, '!processed' => $processed)));
00477 }
00478 }
00479
00480 if ($changed) {
00481 variable_set('views_defaults', $views_status);
00482 views_invalidate_cache();
00483 drush_log(dt("Views cache was cleared"), 'ok');
00484 menu_rebuild();
00485 drush_log(dt("Menu cache was cleared"), 'ok');
00486 }
00487 }
00488 }
00489
00493 function views_drush_cache_clear(&$types) {
00494 $types['views'] = 'views_invalidate_cache';
00495 }