views_revert.drush.inc

views-revert - Drush command to revert views overridden in the system.

File

views_revert.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * views-revert - Drush command to revert views overridden in the system.
  5. */
  6. /**
  7. * Implement hook_drush_help().
  8. */
  9. function views_revert_drush_help($section) {
  10. switch ($section) {
  11. case 'drush:revert-views':
  12. return dt('Reverts all views in the drupal installation that have been overriden. Careful, use with care.');
  13. }
  14. }
  15. /**
  16. * Implement hook_drush_command().
  17. */
  18. function views_revert_drush_command() {
  19. $items = array();
  20. $items['views-revert'] = array(
  21. 'callback' => 'views_revert_views',
  22. 'drupal dependencies' => array('views'),
  23. 'description' => dt('Revert overridden views to their default state. Make sure to backup first.'),
  24. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  25. 'aliases' => array('vr'),
  26. );
  27. return $items;
  28. }
  29. /**
  30. * Callback function for views-revert command.
  31. */
  32. function views_revert_views() {
  33. $views = views_get_all_views();
  34. $i = 0;
  35. // The provided view names specified in the command.
  36. $viewnames = _convert_csv_to_array(func_get_args());
  37. // Find all overridden views.
  38. foreach ($views as $view) {
  39. if ($view->disabled) {
  40. continue;
  41. }
  42. if ($view->type == dt('Overridden')) {
  43. $overridden[$view->name] = $view->name;
  44. }
  45. }
  46. // Return early if there are no views overridden in the system.
  47. if (empty($overridden)) {
  48. return drush_set_error(dt('There are no overridden views in the system.'));
  49. }
  50. // If the user specified in the command the views to be overridden.
  51. if (!empty($viewnames)) {
  52. foreach ($viewnames as $key => $viewname) {
  53. $is_overridden = key_exists($viewname, $overridden);
  54. // Check if the provided view name is in the system
  55. if ($viewname && !key_exists($viewname, $views)) {
  56. drush_set_error(dt("'@viewname' view is not present in the system.", array('@viewname' => $viewname)));
  57. }
  58. // Check if the provided view is overridden.
  59. elseif (!$is_overridden) {
  60. drush_set_error(dt("The view specified '@viewname' is not overridden.", array('@viewname' => $viewname)));
  61. }
  62. // If the view is overriden, revert it.
  63. elseif ($is_overridden){
  64. views_revert_view($views[$viewname]);
  65. $i++;
  66. }
  67. // We should never get here but well...
  68. else {
  69. drush_set_error(dt("The view specified '@viewname' is not provided in code, and thus cannot be reverted.", array('@viewname' => $viewname)));
  70. }
  71. }
  72. }
  73. // The user did not specify any views in the command, prompt the user
  74. else {
  75. // list of choices for the user
  76. $overridden['all'] = dt('Revert all overridden views'); // add a choice at the end
  77. $choice = drush_choice($overridden, 'Enter a number to choose which view to revert.', '!key'); // prompt the user
  78. if ($choice !== FALSE) {
  79. // revert all views option
  80. if ($choice == 'all') {
  81. $i = views_revert_allviews($views);
  82. }
  83. // else the user specified a single view
  84. else {
  85. views_revert_view($views[$choice]);
  86. $i++;
  87. }
  88. }
  89. }
  90. // final results output
  91. if ($i == 0) {
  92. drush_log(dt('No views were reverted.'), 'ok');
  93. }
  94. else {
  95. drush_log(dt('Reverted a total of @count views.', array('@count' => $i)), 'ok');
  96. }
  97. }
  98. /**
  99. * Reverts all views
  100. * @param $views
  101. * All views in the system as provided by views_get_all_views().
  102. */
  103. function views_revert_allviews($views) {
  104. $i = 0;
  105. foreach ($views as $view) {
  106. if ($view->disabled) {
  107. continue;
  108. }
  109. if ($view->type == t('Overridden')) {
  110. views_revert_view($view);
  111. $i++;
  112. }
  113. }
  114. return $i;
  115. }
  116. /**
  117. * Revert a specified view
  118. * @param $view
  119. * The view object to be reverted
  120. *
  121. * Checks on wether or not the view is overridden is handled in views_revert_views_revert()
  122. * We perform a check here anyway in case someone somehow calls this function on their own...
  123. */
  124. function views_revert_view($view) {
  125. // check anyway just in case
  126. if ($view->type == t('Overridden')) {
  127. // Revert the view.
  128. $view->delete();
  129. // Clear its cache.
  130. views_object_cache_clear('view', $view->name);
  131. // Give feedback.
  132. $message = dt("Reverted the view '@viewname'", array('@viewname' => $view->name));
  133. drush_log($message, 'success');
  134. // Reverted one more view.
  135. }
  136. else {
  137. drush_set_error(dt("The view '@viewname' is not overridden.", array('@viewname' => $view->name)));
  138. }
  139. }