ctools.drush.inc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. <?php
  2. /**
  3. * @file
  4. * CTools Drush commands.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function ctools_drush_command() {
  10. $items = array();
  11. $module_text = 'Filter the list of exportables by module. This will come from the \'export_module\' key on the exportable.';
  12. $all_text = 'Perform this operation all CTools exportables available on the system (all tables).';
  13. $items['ctools-export'] = array(
  14. 'aliases' => array('ctex'),
  15. 'callback' => 'ctools_drush_export',
  16. 'description' => 'Export multiple CTools exportable objects directly to code.',
  17. 'arguments' => array(
  18. 'module' => 'Name of your module.',
  19. ),
  20. 'options' => array(
  21. 'subdir' => 'The name of the sub directory to create the module in. Defaults to ctools_export which will be placed into sites/all/modules.',
  22. 'remove' => 'Remove existing files before writing, except the .module file.',
  23. 'filter' => 'Filter the list of exportables by status. Available options are enabled, disabled, overridden, database, code and all. Defaults to enabled.',
  24. 'tables' => 'Comma separated list of exportable table names to filter by.',
  25. ),
  26. 'examples' => array(
  27. 'drush ctex export_module' => 'Export CTools exportables to a module called "export_module".',
  28. 'drush ctex export_module --subdir=exports' => 'Same as above, but into the sites/all/modules/exports directory.',
  29. 'drush ctex export_module --subdir=exports --remove' => 'Same as above, but automatically removing all files, except for the .module file.',
  30. 'drush ctex --filter="views_view"' => 'Filter export selection to the views_view table only.',
  31. ),
  32. );
  33. $items['ctools-export-info'] = array(
  34. 'aliases' => array('ctei'),
  35. 'callback' => 'ctools_drush_export_info',
  36. 'description' => 'Show available CTools exportable objects.',
  37. 'arguments' => array(),
  38. 'options' => array(
  39. 'format' => 'Display exportables info in a different format such as print_r, json, export. The default is to show in a tabular format.',
  40. 'tables-only' => 'Only show list of exportable types/table names and not available objects.',
  41. 'filter' => 'Filter the list of exportables by status. Available options are enabled, disabled, overridden, database, and code.',
  42. 'module' => $module_text,
  43. ),
  44. 'examples' => array(
  45. 'drush ctools-export-info' => 'View export info on all exportables.',
  46. 'drush ctools-export-info views_view variable' => 'View export info for views_view and variable exportable types only.',
  47. 'drush ctei --filter=enabled' => 'Show all enabled exportables.',
  48. 'drush ctei views_view --filter=disabled' => 'Show all enabled exportables.',
  49. 'drush ctei views_view --module=node' => 'Show all exportables provided by/on behalf of the node module.',
  50. ),
  51. );
  52. $items['ctools-export-view'] = array(
  53. 'aliases' => array('ctev'),
  54. 'callback' => 'ctools_drush_export_op_command',
  55. 'description' => 'View CTools exportable object code output.',
  56. 'arguments' => array(
  57. 'table name' => 'Base table of the exportable you want to view.',
  58. 'machine names' => 'Space separated list of exportables you want to view.',
  59. ),
  60. 'options' => array(
  61. 'indent' => 'The string to use for indentation when displaying the exportable export code. Defaults to \'\'.',
  62. 'no-colour' => 'Remove any colour formatting from export string output. Ideal if you are sending the output of this command to a file.',
  63. 'module' => $module_text,
  64. 'all' => $all_text,
  65. ),
  66. 'examples' => array(
  67. 'drush ctools-export-view views_view' => 'View all views exportable objects.',
  68. 'drush ctools-export-view views_view archive' => 'View default views archive view.',
  69. ),
  70. );
  71. $items['ctools-export-revert'] = array(
  72. 'aliases' => array('cter'),
  73. 'callback' => 'ctools_drush_export_op_command',
  74. 'description' => 'Revert CTools exportables from changes overridden in the database.',
  75. 'arguments' => array(
  76. 'table name' => 'Base table of the exportable you want to revert.',
  77. 'machine names' => 'Space separated list of exportables you want to revert.',
  78. ),
  79. 'options' => array(
  80. 'module' => $module_text,
  81. 'all' => $all_text,
  82. ),
  83. 'examples' => array(
  84. 'drush ctools-export-revert views_view' => 'Revert all overridden views exportable objects.',
  85. 'drush ctools-export-revert views_view archive' => 'Revert overridden default views archive view.',
  86. 'drush ctools-export-revert --all' => 'Revert all exportables on the system.',
  87. ),
  88. );
  89. $items['ctools-export-enable'] = array(
  90. 'aliases' => array('ctee'),
  91. 'callback' => 'ctools_drush_export_op_command',
  92. 'description' => 'Enable CTools exportables.',
  93. 'arguments' => array(
  94. 'table name' => 'Base table of the exportable you want to enable.',
  95. 'machine names' => 'Space separated list of exportables you want to enable.',
  96. ),
  97. 'options' => array(
  98. 'module' => $module_text,
  99. 'all' => $all_text,
  100. ),
  101. 'examples' => array(
  102. 'drush ctools-export-enable views_view' => 'Enable all overridden views exportable objects.',
  103. 'drush ctools-export-enable views_view archive' => 'Enable overridden default views archive view.',
  104. ),
  105. );
  106. $items['ctools-export-disable'] = array(
  107. 'aliases' => array('cted'),
  108. 'callback' => 'ctools_drush_export_op_command',
  109. 'description' => 'Disable CTools exportables.',
  110. 'arguments' => array(
  111. 'table name' => 'Base table of the exportable you want to disable.',
  112. 'machine names' => 'Space separated list of exportables you want to disable.',
  113. ),
  114. 'options' => array(
  115. 'module' => $module_text,
  116. 'all' => $all_text,
  117. ),
  118. 'examples' => array(
  119. 'drush ctools-export-disable views_view' => 'Disable all overridden views exportable objects.',
  120. 'drush ctools-export-disable views_view archive' => 'Disable overridden default views archive view.',
  121. ),
  122. );
  123. return $items;
  124. }
  125. /**
  126. * Implementation of hook_drush_help().
  127. */
  128. function ctools_drush_help($section) {
  129. switch ($section) {
  130. case 'meta:ctools:title':
  131. return dt('CTools commands');
  132. case 'meta:entity:summary':
  133. return dt('CTools drush commands.');
  134. }
  135. }
  136. /**
  137. * Drush callback: export
  138. */
  139. function ctools_drush_export($module = 'foo') {
  140. $error = FALSE;
  141. if (preg_match('@[^a-z_]+@', $module)) {
  142. $error = dt('The name of the module must contain only lowercase letters and underscores') . '.';
  143. drush_log($error, 'error');
  144. return;
  145. }
  146. // Filter by tables.
  147. $tables = _ctools_drush_explode_options('tables');
  148. // Check status.
  149. $filter = drush_get_option('filter', FALSE);
  150. if (empty($filter)) {
  151. drush_set_option('filter', 'enabled');
  152. }
  153. // Selection.
  154. $options = array('all' => dt('Export everything'), 'select' => dt('Make selection'));
  155. $selection = drush_choice($options, dt('Select to proceed'));
  156. if (!$selection) {
  157. return;
  158. }
  159. // Present the selection screens.
  160. if ($selection == 'select') {
  161. $selections = _ctools_drush_selection_screen($tables);
  162. }
  163. else {
  164. $info = _ctools_drush_export_info($tables, TRUE);
  165. $selections = $info['exportables'];
  166. }
  167. // Subdirectory.
  168. $dest_exists = FALSE;
  169. $subdir = drush_get_option('subdir', 'ctools_export');
  170. $dest = 'sites/all/modules/' . $subdir . '/' . $module;
  171. // Overwriting files can be set with 'remove' argument.
  172. $remove = drush_get_option('remove', FALSE);
  173. // Check if folder exists.
  174. if (file_exists($dest)) {
  175. $dest_exists = TRUE;
  176. if ($remove) {
  177. if (drush_confirm(dt('All files except for the .info and .module files in !module will be removed. You can choose later if you want to overwrite these files as well. Are you sure you want to proceed ?', array('!module' => $module)))) {
  178. $remove = TRUE;
  179. drush_log(dt('Files will be removed'), 'success');
  180. }
  181. else {
  182. drush_log(dt('Export aborted.'), 'success');
  183. return;
  184. }
  185. }
  186. }
  187. // Remove files (except for the .module file) if the destination folder exists.
  188. if ($remove && $dest_exists) {
  189. _ctools_drush_file_delete($dest);
  190. }
  191. // Create new dir if needed.
  192. if (!$dest_exists) {
  193. if (!file_exists('sites/all/modules/' . $subdir)) {
  194. drush_mkdir('sites/all/modules/' . $subdir);
  195. }
  196. }
  197. // Create destination directory.
  198. drush_mkdir($dest);
  199. // Load bulk export module.
  200. module_load_include('module', 'bulk_export');
  201. // Create options and call Bulk export function.
  202. // We create an array, because maybe in the future we can pass in more
  203. // options to the export function (pre-selected modules and/or exportables).
  204. $options = array(
  205. 'name' => $module,
  206. 'selections' => $selections,
  207. );
  208. $files = bulk_export_export(TRUE, $options);
  209. $alter = array(
  210. 'module' => $module,
  211. 'files' => $files,
  212. );
  213. // Let other drush commands alter the files.
  214. drush_command_invoke_all_ref('drush_ctex_files_alter', $alter);
  215. $files = $alter['files'];
  216. // Start writing.
  217. if (is_array($files)) {
  218. foreach ($files as $base_file => $data) {
  219. $filename = $dest . '/' . $base_file;
  220. // Extra check for .module file.
  221. if ($base_file == ($module . '.module' || $module . '.info') && file_exists($filename)) {
  222. if (!drush_confirm(dt('Do you want to overwrite !module_file', array('!module_file' => $base_file)))) {
  223. drush_log(dt('Writing of !filename skipped. This is the code that was supposed to be written:', array('!filename' => $filename)), 'success');
  224. drush_print('---------');
  225. drush_print(shellColours::getColouredOutput("\n$data", 'light_green'));
  226. drush_print('---------');
  227. continue;
  228. }
  229. }
  230. if (file_put_contents($filename, $data)) {
  231. drush_log(dt('Succesfully written !filename', array('!filename' => $filename)), 'success');
  232. }
  233. else {
  234. drush_log(dt('Error writing !filename', array('!filename' => $filename)), 'error');
  235. }
  236. }
  237. }
  238. else {
  239. drush_log(dt('No files were found to be written.'), 'error');
  240. }
  241. }
  242. /**
  243. * Helper function to select the exportables. By default, all exportables
  244. * will be selected, so it will be easier to deselect them.
  245. *
  246. * @param $tables
  247. */
  248. function _ctools_drush_selection_screen(array $tables = array()) {
  249. $selections = $build = array();
  250. $files = system_rebuild_module_data();
  251. $selection_number = 0;
  252. $info = _ctools_drush_export_info($tables, TRUE);
  253. $exportables = $info['exportables'];
  254. $schemas = $info['schemas'];
  255. $export_tables = array();
  256. foreach (array_keys($exportables) as $table) {
  257. natcasesort($exportables[$table]);
  258. $export_tables[$table] = $files[$schemas[$table]['module']]->info['name'] . ' (' . $table . ')';
  259. }
  260. foreach ($export_tables as $table => $table_title) {
  261. if (!empty($exportables[$table])) {
  262. $table_count = count($exportables[$table]);
  263. $selection_number += $table_count;
  264. foreach ($exportables[$table] as $key => $title) {
  265. $build[$table]['title'] = $table_title;
  266. $build[$table]['items'][$key] = $title;
  267. $build[$table]['count'] = $table_count;
  268. $selections[$table][$key] = $key;
  269. }
  270. }
  271. }
  272. drush_print(dt('Number of exportables selected: !number', array('!number' => $selection_number)));
  273. drush_print(dt('By default all exportables are selected. Select a table to deselect exportables. Select "cancel" to start writing the files.'));
  274. // Let's go into a loop.
  275. $return = FALSE;
  276. while (!$return) {
  277. // Present the tables choice.
  278. $table_rows = array();
  279. foreach ($build as $table => $info) {
  280. $table_rows[$table] = $info['title'] . ' (' . $info['count'] . ')';
  281. }
  282. $table_choice = drush_choice($table_rows, dt('Select a table. Select cancel to start writing files.'));
  283. // Bail out.
  284. if (!$table_choice) {
  285. drush_log(dt('Selection mode done, starting to write the files.'), 'notice');
  286. $return = TRUE;
  287. return $selections;
  288. }
  289. // Present the exportables choice, using the drush_choice_multiple.
  290. $max = count($build[$table_choice]['items']);
  291. $exportable_rows = array();
  292. foreach ($build[$table_choice]['items'] as $key => $title) {
  293. $exportable_rows[$key] = $title;
  294. }
  295. drush_print(dt('Exportables from !table', array('!table' => $build[$table_choice]['title'])));
  296. $multi_select = drush_choice_multiple($exportable_rows, $selections[$table_choice], dt('Select exportables.'), '!value', '!value (selected)', 0, $max);
  297. // Update selections.
  298. if (is_array($multi_select)) {
  299. $build[$table_choice]['count'] = count($multi_select);
  300. $selections[$table_choice] = array();
  301. foreach ($multi_select as $key) {
  302. $selections[$table_choice][$key] = $key;
  303. }
  304. }
  305. }
  306. }
  307. /**
  308. * Delete files in a directory, keeping the .module and .info files.
  309. *
  310. * @param $path
  311. * Path to directory in which to remove files.
  312. */
  313. function _ctools_drush_file_delete($path) {
  314. if (is_dir($path)) {
  315. $files = new DirectoryIterator($path);
  316. foreach ($files as $fileInfo) {
  317. if (!$fileInfo->isDot() && !in_array($fileInfo->getExtension(), array('module', 'info'))) {
  318. unlink($fileInfo->getPathname());
  319. }
  320. }
  321. }
  322. }
  323. /**
  324. * Drush callback: Export info.
  325. *
  326. * @params $table_names
  327. * Each argument will be taken as a CTools exportable table name.
  328. */
  329. function ctools_drush_export_info() {
  330. // Collect array of table names from args.
  331. $table_names = func_get_args();
  332. // Get format option to allow for alternative output.
  333. $format = drush_get_option('format', FALSE);
  334. $tables_only = drush_get_option('tables-only', FALSE);
  335. $filter = drush_get_option('filter', FALSE);
  336. $export_module = drush_get_option('module', FALSE);
  337. $load = (bool) $filter || $export_module;
  338. // Get info on these tables, or all tables if none specified.
  339. $info = _ctools_drush_export_info($table_names, $load);
  340. $schemas = $info['schemas'];
  341. $exportables = $info['exportables'];
  342. if (empty($exportables)) {
  343. drush_log(dt('There are no exportables available.'), 'warning');
  344. return;
  345. }
  346. // Filter by export module.
  347. if (is_string($export_module)) {
  348. $exportables = _ctools_drush_export_module_filter($exportables, $export_module);
  349. }
  350. if (empty($exportables)) {
  351. drush_log(dt('There are no exportables matching this criteria.'), 'notice');
  352. return;
  353. }
  354. $exportable_counts = _ctools_drush_count_exportables($exportables);
  355. // Only use array keys if --tables-only option is set.
  356. if ($tables_only) {
  357. $exportables = array_keys($exportables);
  358. }
  359. // Use format from --format option if it's present, and send to drush_format.
  360. if ($format) {
  361. // Create array with all exportable info and counts in one.
  362. $output = array(
  363. 'exportables' => $exportables,
  364. 'count' => $exportable_counts,
  365. );
  366. drush_print(drush_format($output, NULL, $format));
  367. }
  368. // Build a tabular output as default.
  369. else {
  370. $header = $tables_only ? array() : array(dt('Module'), dt('Base table'), dt('Exportables'));
  371. $rows = array();
  372. foreach ($exportables as $table => $info) {
  373. if (is_array($info)) {
  374. $row = array(
  375. $schemas[$table]['module'],
  376. $table,
  377. // Machine name is better for this?
  378. shellColours::getColouredOutput(implode("\n", array_keys($info)), 'light_green') . "\n",
  379. );
  380. $rows[] = $row;
  381. }
  382. else {
  383. $rows[] = array($info);
  384. }
  385. }
  386. if (!empty($rows)) {
  387. drush_print("\n");
  388. array_unshift($rows, $header);
  389. drush_print_table($rows, TRUE, array(20, 20));
  390. drush_print(dt('Total exportables found: !total', array('!total' => $exportable_counts['total'])));
  391. foreach ($exportable_counts['exportables'] as $table_name => $count) {
  392. drush_print(dt('!table_name (!count)', array('!table_name' => $table_name, '!count' => $count)), 2);
  393. }
  394. drush_print("\n");
  395. }
  396. }
  397. }
  398. /**
  399. * Drush callback: Acts as the hub for all op commands to keep
  400. * all arg handling etc in one place.
  401. */
  402. function ctools_drush_export_op_command() {
  403. // Get all info for the current drush command.
  404. $command = drush_get_command();
  405. $op = '';
  406. switch ($command['command']) {
  407. case 'ctools-export-view':
  408. $op = 'view';
  409. break;
  410. case 'ctools-export-revert':
  411. // Revert is same as deleting. As any objects in the db are deleted.
  412. $op = 'delete';
  413. break;
  414. case 'ctools-export-enable':
  415. $op = 'enable';
  416. break;
  417. case 'ctools-export-disable':
  418. $op = 'disable';
  419. break;
  420. }
  421. if (!$op) {
  422. return;
  423. }
  424. if (drush_get_option('all', FALSE)) {
  425. $info = _ctools_drush_export_info('', TRUE);
  426. $exportable_info = $info['exportables'];
  427. $all = drush_confirm(dt('Are you sure you would like to !op all exportables on the system?',
  428. array('!op' => _ctools_drush_export_op_alias($op))));
  429. if ($all && $exportable_info) {
  430. foreach ($exportable_info as $table => $exportables) {
  431. if (!empty($exportables)) {
  432. ctools_drush_export_op($op, $table, $exportables);
  433. }
  434. }
  435. }
  436. }
  437. else {
  438. $args = func_get_args();
  439. // Table name should always be first arg...
  440. $table_name = array_shift($args);
  441. // Any additional args are assumed to be exportable names.
  442. $object_names = $args;
  443. // Return any exportables based on table name, object names, options.
  444. $exportables = _ctools_drush_export_op_command_logic($op, $table_name, $object_names);
  445. if ($exportables) {
  446. ctools_drush_export_op($op, $table_name, $exportables);
  447. }
  448. }
  449. }
  450. /**
  451. * Iterate through exportable object names, load them, and pass each
  452. * object to the correct op function.
  453. *
  454. * @param $op
  455. * @param $table_name
  456. * @param $exportables
  457. *
  458. */
  459. function ctools_drush_export_op($op = '', $table_name = '', $exportables = NULL) {
  460. $objects = ctools_export_crud_load_multiple($table_name, array_keys($exportables));
  461. $function = '_ctools_drush_export_' . $op;
  462. if (function_exists($function)) {
  463. foreach ($objects as $object) {
  464. $function($table_name, $object);
  465. }
  466. }
  467. else {
  468. drush_log(dt('CTools CRUD function !function doesn\'t exist.',
  469. array('!function' => $function)), 'error');
  470. }
  471. }
  472. /**
  473. * Helper function to abstract logic for selecting exportable types/objects
  474. * from individual commands as they will all share this same error
  475. * handling/arguments for returning list of exportables.
  476. *
  477. * @param $table_name
  478. * @param $object_names
  479. *
  480. * @return
  481. * Array of exportable objects (filtered if necessary, by name etc..) or FALSE if not.
  482. */
  483. function _ctools_drush_export_op_command_logic($op = '', $table_name = NULL, array $object_names = array()) {
  484. if (!$table_name) {
  485. drush_log(dt('Exportable table name empty. Use the --all command if you want to perform this operation on all tables.'), 'error');
  486. return FALSE;
  487. }
  488. // Get export info based on table name.
  489. $info = _ctools_drush_export_info(array($table_name), TRUE);
  490. if (!isset($info['exportables'][$table_name])) {
  491. drush_log(dt('Exportable table name not found.'), 'error');
  492. return FALSE;
  493. }
  494. $exportables = &$info['exportables'];
  495. if (empty($object_names)) {
  496. $all = drush_confirm(dt('No object names entered. Would you like to try and !op all exportables of type !type',
  497. array('!op' => _ctools_drush_export_op_alias($op), '!type' => $table_name)));
  498. if (!$all) {
  499. drush_log(dt('Command cancelled'), 'success');
  500. return FALSE;
  501. }
  502. }
  503. else {
  504. // Iterate through object names and check they exist in exportables array.
  505. // Log error and unset them if they don't.
  506. foreach ($object_names as $object_name) {
  507. if (!isset($exportables[$table_name][$object_name])) {
  508. drush_log(dt('Invalid exportable: !exportable', array('!exportable' => $object_name)), 'error');
  509. unset($object_names[$table_name][$object_name]);
  510. }
  511. }
  512. // Iterate through exportables to get just a list of selected ones.
  513. foreach (array_keys($exportables[$table_name]) as $exportable) {
  514. if (!in_array($exportable, $object_names)) {
  515. unset($exportables[$table_name][$exportable]);
  516. }
  517. }
  518. }
  519. $export_module = drush_get_option('module', FALSE);
  520. if (is_string($export_module)) {
  521. $exportables = _ctools_drush_export_module_filter($exportables, $export_module);
  522. }
  523. return $exportables[$table_name];
  524. }
  525. /**
  526. * Return array of CTools exportable info based on available tables returned from
  527. * ctools_export_get_schemas().
  528. *
  529. * @param $table_names
  530. * Array of table names to return.
  531. * @param $load
  532. * (bool) should ctools exportable objects be loaded for each type.
  533. * The default behaviour will load just a list of exportable names.
  534. *
  535. * @return
  536. * Nested arrays of available exportables, keyed by table name.
  537. */
  538. function _ctools_drush_export_info(array $table_names = array(), $load = FALSE) {
  539. ctools_include('export');
  540. // Get available schemas that declare exports.
  541. $schemas = ctools_export_get_schemas(TRUE);
  542. $exportables = array();
  543. if (!empty($schemas)) {
  544. // Remove types we don't want, if any.
  545. if (!empty($table_names)) {
  546. foreach (array_keys($schemas) as $table_name) {
  547. if (!in_array($table_name, $table_names)) {
  548. unset($schemas[$table_name]);
  549. }
  550. }
  551. }
  552. // Load array of available exportables for each schema.
  553. foreach ($schemas as $table_name => $schema) {
  554. // Load all objects.
  555. if ($load) {
  556. $exportables[$table_name] = ctools_export_crud_load_all($table_name);
  557. }
  558. // Get a list of exportable names.
  559. else {
  560. if (!empty($schema['export']['list callback']) && function_exists($schema['export']['list callback'])) {
  561. $exportables[$table_name] = $schema['export']['list callback']();
  562. }
  563. else {
  564. $exportables[$table_name] = ctools_export_default_list($table_name, $schema);
  565. }
  566. }
  567. }
  568. }
  569. if ($load) {
  570. $filter = drush_get_option('filter', FALSE);
  571. $exportables = _ctools_drush_filter_exportables($exportables, $filter);
  572. }
  573. return array('exportables' => $exportables, 'schemas' => $schemas);
  574. }
  575. /*
  576. * View a single object.
  577. *
  578. * @param $table_name
  579. * @param $object
  580. */
  581. function _ctools_drush_export_view($table_name, $object) {
  582. $indent = drush_get_option('indent', '');
  583. $no_colour = drush_get_option('no-colour', FALSE);
  584. $export = ctools_export_crud_export($table_name, $object, $indent);
  585. if ($no_colour) {
  586. drush_print("\n$export");
  587. }
  588. else {
  589. drush_print(shellColours::getColouredOutput("\n$export", 'light_green'));
  590. }
  591. }
  592. /*
  593. * Revert a single object.
  594. *
  595. * @param $table_name
  596. * @param $object
  597. */
  598. function _ctools_drush_export_delete($table_name, $object) {
  599. $name = _ctools_drush_get_export_name($table_name, $object);
  600. if (_ctools_drush_object_is_overridden($object)) {
  601. // Remove from db.
  602. ctools_export_crud_delete($table_name, $object);
  603. drush_log("Reverted object: $name", 'success');
  604. }
  605. else {
  606. drush_log("Nothing to revert for: $name", 'notice');
  607. }
  608. }
  609. /*
  610. * Enable a single object.
  611. *
  612. * @param $table_name
  613. * @param $object
  614. */
  615. function _ctools_drush_export_enable($table_name, $object) {
  616. $name = _ctools_drush_get_export_name($table_name, $object);
  617. if (_ctools_drush_object_is_disabled($object)) {
  618. // Enable object.
  619. ctools_export_crud_enable($table_name, $object);
  620. drush_log("Enabled object: $name", 'success');
  621. }
  622. else {
  623. drush_log("$name is already Enabled", 'notice');
  624. }
  625. }
  626. /*
  627. * Disable a single object.
  628. *
  629. * @param $table_name
  630. * @param $object
  631. */
  632. function _ctools_drush_export_disable($table_name, $object) {
  633. $name = _ctools_drush_get_export_name($table_name, $object);
  634. if (!_ctools_drush_object_is_disabled($object)) {
  635. // Disable object.
  636. ctools_export_crud_disable($table_name, $object);
  637. drush_log("Disabled object: $name", 'success');
  638. }
  639. else {
  640. drush_log("$name is already disabled", 'notice');
  641. }
  642. }
  643. /**
  644. * Filter a nested array of exportables by export module.
  645. *
  646. * @param $exportables array
  647. * Passed by reference. A nested array of exportables, keyed by table name.
  648. * @param $export_module string
  649. * The name of the export module providing the exportable.
  650. */
  651. function _ctools_drush_export_module_filter($exportables, $export_module) {
  652. $module_list = module_list();
  653. if (!isset($module_list[$export_module])) {
  654. drush_log(dt('Invalid export module: !export_module', array('!export_module' => $export_module)), 'error');
  655. }
  656. foreach ($exportables as $table => $objects) {
  657. foreach ($objects as $key => $object) {
  658. if (empty($object->export_module) || ($object->export_module !== $export_module)) {
  659. unset($exportables[$table][$key]);
  660. }
  661. }
  662. }
  663. return array_filter($exportables);
  664. }
  665. /**
  666. * Gets the key for an exportable type.
  667. *
  668. * @param $table_name
  669. * The exportable table name.
  670. * @param $object
  671. * The exportable object.
  672. *
  673. * @return string
  674. * The key defined in the export schema data.
  675. */
  676. function _ctools_drush_get_export_name($table_name, $object) {
  677. $info = _ctools_drush_export_info(array($table_name));
  678. $key = $info['schemas'][$table_name]['export']['key'];
  679. return $object->{$key};
  680. }
  681. /**
  682. * Determine if an object is disabled.
  683. *
  684. * @param $object
  685. * Loaded CTools exportable object.
  686. *
  687. * @return TRUE or FALSE
  688. */
  689. function _ctools_drush_object_is_disabled($object) {
  690. return (isset($object->disabled) && ($object->disabled == TRUE)) ? TRUE : FALSE;
  691. }
  692. /**
  693. * Determine if an object is enabled.
  694. *
  695. * @see _ctools_drush_object_is_disabled.
  696. */
  697. function _ctools_drush_object_is_enabled($object) {
  698. return (empty($object->disabled)) ? TRUE : FALSE;
  699. }
  700. /**
  701. * Determine if an object is overridden.
  702. */
  703. function _ctools_drush_object_is_overridden($object) {
  704. $status = EXPORT_IN_CODE + EXPORT_IN_DATABASE;
  705. return ($object->export_type == $status) ? TRUE : FALSE;
  706. }
  707. /**
  708. * Determine if an object is not overridden.
  709. */
  710. function _ctools_drush_object_is_not_overridden($object) {
  711. $status = EXPORT_IN_CODE + EXPORT_IN_DATABASE;
  712. return ($object->export_type == $status) ? FALSE : TRUE;
  713. }
  714. /**
  715. * Determine if an object is only in the db.
  716. */
  717. function _ctools_drush_object_is_db_only($object) {
  718. return ($object->export_type == EXPORT_IN_DATABASE) ? TRUE : FALSE;
  719. }
  720. /**
  721. * Determine if an object is not in the db.
  722. */
  723. function _ctools_drush_object_is_not_db_only($object) {
  724. return ($object->export_type == EXPORT_IN_DATABASE) ? FALSE : TRUE;
  725. }
  726. /**
  727. * Determine if an object is a code only default.
  728. */
  729. function _ctools_drush_object_is_code_only($object) {
  730. return ($object->export_type == EXPORT_IN_CODE) ? TRUE : FALSE;
  731. }
  732. /**
  733. * Determine if an object is not a code only default.
  734. */
  735. function _ctools_drush_object_is_not_code_only($object) {
  736. return ($object->export_type == EXPORT_IN_CODE) ? FALSE : TRUE;
  737. }
  738. /**
  739. * Return an array of count information based on exportables array.
  740. *
  741. * @param $exportables
  742. * Array of exportables to count.
  743. *
  744. * @return
  745. * Array of count data containing the following:
  746. * 'total' - A total count of all exportables.
  747. * 'exportables' - An array of exportable counts per table.
  748. */
  749. function _ctools_drush_count_exportables($exportables) {
  750. $count = array('exportables' => array());
  751. foreach ($exportables as $table => $objects) {
  752. // Add the object count for each table.
  753. $count['exportables'][$table] = count($objects);
  754. }
  755. // Once all tables have been counted, total these up.
  756. $count['total'] = array_sum($count['exportables']);
  757. return $count;
  758. }
  759. /**
  760. * Filters a collection of exportables based on filters.
  761. *
  762. * @param $exportables
  763. * @param $filter
  764. */
  765. function _ctools_drush_filter_exportables($exportables, $filter) {
  766. $eval = FALSE;
  767. if (is_string($filter)) {
  768. switch ($filter) {
  769. // Show enabled exportables only.
  770. case 'enabled':
  771. $eval = '_ctools_drush_object_is_disabled';
  772. break;
  773. // Show disabled exportables only.
  774. case 'disabled':
  775. $eval = '_ctools_drush_object_is_enabled';
  776. break;
  777. // Show overridden exportables only.
  778. case 'overridden':
  779. $eval = '_ctools_drush_object_is_not_overridden';
  780. break;
  781. // Show database only exportables.
  782. case 'database':
  783. $eval = '_ctools_drush_object_is_not_db_only';
  784. break;
  785. // Show code only exportables.
  786. case 'code':
  787. $eval = '_ctools_drush_object_is_not_code_only';
  788. break;
  789. // Do nothing.
  790. case 'all':
  791. break;
  792. default:
  793. drush_log(dt('Invalid filter option. Available options are: enabled, disabled, overridden, database, and code.'), 'error');
  794. return;
  795. }
  796. if ($eval) {
  797. foreach ($exportables as $table => $objects) {
  798. foreach ($objects as $key => $object) {
  799. if ($eval($object)) {
  800. unset($exportables[$table][$key]);
  801. }
  802. }
  803. }
  804. }
  805. }
  806. return array_filter($exportables);
  807. }
  808. /**
  809. * Return an alias for an op, that will be used to show as output.
  810. * For now, this is mainly necessary for delete => revert alias.
  811. *
  812. * @param $op
  813. * The op name. Such as 'enable', 'disable', or 'delete'.
  814. *
  815. * @return
  816. * The matched alias value or the original $op passed in if not found.
  817. */
  818. function _ctools_drush_export_op_alias($op) {
  819. $aliases = array(
  820. 'delete' => 'revert',
  821. );
  822. if (isset($aliases[$op])) {
  823. return $aliases[$op];
  824. }
  825. return $op;
  826. }
  827. /**
  828. * Convert the drush options from a csv list into an array.
  829. *
  830. * @param $drush_option
  831. * The drush option name to invoke.
  832. *
  833. * @return
  834. * Exploded array of options.
  835. */
  836. function _ctools_drush_explode_options($drush_option) {
  837. $options = drush_get_option($drush_option, array());
  838. if (!empty($options)) {
  839. $options = explode(',', $options);
  840. return array_map('trim', $options);
  841. }
  842. return $options;
  843. }
  844. /**
  845. * Class to deal with wrapping output strings with
  846. * colour formatting for the shell.
  847. */
  848. class shellColours {
  849. private static $foreground_colours = array(
  850. 'black' => '0;30',
  851. 'dark_gray' => '1;30',
  852. 'blue' => '0;34',
  853. 'light_blue' => '1;34',
  854. 'green' => '0;32',
  855. 'light_green' => '1;32',
  856. 'cyan' => '0;36',
  857. 'light_cyan' => '1;36',
  858. 'red' => '0;31',
  859. 'light_red' => '1;31',
  860. 'purple' => '0;35',
  861. 'light_purple' => '1;35',
  862. 'brown' => '0;33',
  863. 'yellow' => '1;33',
  864. 'light_gray' => '0;37',
  865. 'white' => '1;37',
  866. );
  867. private static $background_colours = array(
  868. 'black' => '40',
  869. 'red' => '41',
  870. 'green' => '42',
  871. 'yellow' => '43',
  872. 'blue' => '44',
  873. 'magenta' => '45',
  874. 'cyan' => '46',
  875. 'light_gray' => '47',
  876. );
  877. private function __construct() {}
  878. // Returns coloured string
  879. public static function getColouredOutput($string, $foreground_colour = NULL, $background_colour = NULL) {
  880. $coloured_string = "";
  881. // Check if given foreground colour found
  882. if ($foreground_colour) {
  883. $coloured_string .= "\033[" . self::$foreground_colours[$foreground_colour] . "m";
  884. }
  885. // Check if given background colour found
  886. if ($background_colour) {
  887. $coloured_string .= "\033[" . self::$background_colours[$background_colour] . "m";
  888. }
  889. // Add string and end colouring
  890. $coloured_string .= $string . "\033[0m";
  891. return $coloured_string;
  892. }
  893. // Returns all foreground colour names
  894. public static function getForegroundColours() {
  895. return array_keys(self::$foreground_colours);
  896. }
  897. // Returns all background colour names
  898. public static function getBackgroundColours() {
  899. return array_keys(self::$background_colours);
  900. }
  901. } // shellColours