views.drush.inc

  1. 3.x drush/views.drush.inc
  2. 2.x views.drush.inc

Drush integration of views.

drush cache-clear views - Clears the views specific caches. views-revert - Drush command to revert views overridden in the system.

File

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