views.drush.inc 15 KB

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