tripal_core.drush.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. /**
  3. * @file
  4. * Contains function relating to drush-integration of this module.
  5. */
  6. /**
  7. * @defgroup tripal_drush Tripal Drush Integration
  8. * @{
  9. * Contains function relating to drush-integration of various tripal modules.
  10. * @}
  11. */
  12. /**
  13. * Describes each drush command implemented by the module
  14. *
  15. * @return
  16. * The first line of description when executing the help for a given command
  17. *
  18. * @ingroup tripal_drush
  19. */
  20. function tripal_core_drush_help($command) {
  21. switch ($command) {
  22. // Deprecated functions
  23. case 'drush:tripal-launch-jobs':
  24. return dt('DEPRECATED. Please use: trp-run-jobs.');
  25. break;
  26. case 'drush:tripal-current-job':
  27. return dt('DEPRECATED. Please use: trp-get-currjob.');
  28. break;
  29. case 'drush:tripal-rerun-job':
  30. return dt('DEPRECATED. Please use: trp-rerun-job.');
  31. break;
  32. // Tripal Materialized Views
  33. case 'drush:tripal-update-mview':
  34. return dt('Updates the specified materialized view.');
  35. break;
  36. // Chado Specific
  37. case 'drush:tripal-chado-version':
  38. return dt('Returns the current version of chado associated with this drupal site.');
  39. break;
  40. case 'drush:tripal-chadotable-desc':
  41. return dt('Returns the table description as specified in the Tripal Schema API for the supplied table.');
  42. break;
  43. }
  44. }
  45. /**
  46. * Registers a drush command and constructs the full help for that command.
  47. *
  48. * @return
  49. * And array of command descriptions
  50. *
  51. * @ingroup tripal_drush
  52. */
  53. function tripal_core_drush_command() {
  54. $items = array();
  55. $items['trp-refresh-mview'] = array(
  56. 'description' => dt('Refreshes the contents of the specified materialized view.'),
  57. 'arguments' => array(),
  58. 'examples' => array(
  59. 'By Materialized View ID' => 'drush trp-refresh-mview --mview=5',
  60. 'By Table Name' => 'drush trp-refresh-mview --table=organism_feature_count'
  61. ),
  62. 'options' => array(
  63. 'mview' => dt('The ID of the materialized view to update'),
  64. 'table' => dt('The name of the materialized view table to update.'),
  65. ),
  66. );
  67. $items['trp-get-cversion'] = array(
  68. 'description' => dt('Returns the current installed version of Chado.'),
  69. 'arguments' => array(),
  70. 'examples' => array(
  71. 'Standard Example' => 'drush trp-get-cversion',
  72. ),
  73. );
  74. $items['trp-get-table'] = array(
  75. 'description' => dt('Returns a table description in Drupal Schema API format.'),
  76. 'arguments' => array(),
  77. 'examples' => array(
  78. 'By Table Name' => 'drush trp-get-table --table=feature',
  79. 'By Section' => 'drush trp-get-table --table=feature --section=fields'
  80. ),
  81. 'options' => array(
  82. 'table' => array(
  83. 'description' => dt('The name of the table. The table can be a true Chado table or a custom Chado table.'),
  84. 'required' => TRUE,
  85. ),
  86. 'section' => dt('Only return the specified section of the schema array. Possible sections include: description, fields, primary key, unique keys, foreign keys, indexes, referring_tables.'),
  87. ),
  88. );
  89. $items['trp-clean-nodes'] = array(
  90. 'description' => dt('Removes orphaned Drupal nodes.'),
  91. 'arguments' => array(),
  92. 'examples' => array(
  93. 'Standard Example' => 'drush trp-clean-nodes table=feature'
  94. ),
  95. 'options' => array(
  96. 'table' => array(
  97. 'description' => dt('The name of the table that corresonds to the node type to ' .
  98. 'clean up. (e.g organism, feature, stock, library, analysis, pub, etc.)'),
  99. 'required' => TRUE,
  100. ),
  101. ),
  102. );
  103. // DEPRECATED COMMANDS. Deprecated as of Tripal v2.0-rc
  104. $items['tripal-mview-update'] = array(
  105. 'description' => dt('DEPRECATED. Please see: trp-refresh-mview.'),
  106. 'arguments' => array(),
  107. 'examples' => array(
  108. 'By Materialized View ID' => 'drush tripal-update-mview --mview_id=5',
  109. 'By Table Name' => 'drush tripal-update-mview --table_name=organism_feature_count'
  110. ),
  111. 'options' => array(
  112. 'mview_id' => dt('The ID of the materialized view to update'),
  113. 'table_name' => dt('The name of the materialized view table to update.'),
  114. ),
  115. 'aliases' => array('trpmv-up')
  116. );
  117. $items['tripal-jobs-current'] = array(
  118. 'description' => dt('DEPRECATED. Please see: trp-get-currjob.'),
  119. 'arguments' => array(),
  120. 'examples' => array(
  121. 'Standard example' => 'drush tripal-jobs-current',
  122. ),
  123. 'aliases' => array('trpjob-cur'),
  124. );
  125. $items['tripal-jobs-launch'] = array(
  126. 'description' => dt('DEPRECATED. Please see: trp-run-jobs. '),
  127. 'examples' => array(
  128. 'Normal Job' => 'drush tripal-jobs-launch admin',
  129. 'Parallel Job' => 'drush tripal-jobs-launch admin --parallel=1'
  130. ),
  131. 'arguments' => array(
  132. 'username' => dt('The Drupal username under which the job should be run. The permissions for this user will be used.'),
  133. ),
  134. 'options' => array(
  135. 'parallel' => dt('Normally jobs are executed one at a time. But if you are certain no conflicts will occur with other currently running jobs you may set this argument to a value of 1 to make the job run in parallel with other running jobs.'),
  136. 'job_id' => dt('Provide a job_id to run a specific job. Only jobs that have not been run already can be used'),
  137. ),
  138. 'aliases' => array('trpjob-run')
  139. );
  140. $items['tripal-jobs-rerun'] = array(
  141. 'description' => dt('DEPRECATED. Please see: trp-rerun-job. '),
  142. 'examples' => array(
  143. 'Normal Job' => 'drush tripal-jobs-rerun admin 2',
  144. 'Parallel Job' => 'drush tripal-jobs-rerun admin 2 --parallel=1'
  145. ),
  146. 'arguments' => array(
  147. 'username' => dt('The Drupal username under which the job should be run. The permissions for this user will be used.'),
  148. 'job_id' => dt('The job ID to run.'),
  149. ),
  150. 'options' => array(
  151. 'parallel' => dt('Normally jobs are executed one at a time. But if you are certain no conflicts will occur with other currently running jobs you may set this argument to a value of 1 to make the job run in parallel with other running jobs.'),
  152. ),
  153. 'aliases' => array('trpjob-rerun')
  154. );
  155. $items['tripal-chado-version'] = array(
  156. 'description' => dt('DEPRECATED. Please see: trp-get-cversion. '),
  157. 'arguments' => array(),
  158. 'examples' => array(
  159. 'Standard Example' => 'drush tripal-chado-version',
  160. ),
  161. 'aliases' => array('trpchado-ver')
  162. );
  163. $items['tripal-chadotable-desc'] = array(
  164. 'description' => dt('DEPRECATED. Please see: trp-get-table. '),
  165. 'arguments' => array(
  166. 'table_name' => dt('The name of the chado table.'),
  167. ),
  168. 'examples' => array(
  169. 'By Table Name' => 'drush tripal-chadotable-desc --table_name=feature'
  170. ),
  171. 'options' => array(
  172. 'section' => dt('Only return the specified section of the schema table description. Possible sections include: description, fields, primary key, unique keys, foreign keys, indexes, referring_tables.'),
  173. ),
  174. 'aliases' => array('trpschema-tbl')
  175. );
  176. $items['tripal-node-clean'] = array(
  177. 'description' => dt('DEPRECATED. Please see: trp-clean-nodes.'),
  178. 'arguments' => array(
  179. 'module' => dt('The name of the chado-centric module to clean-up.'),
  180. ),
  181. 'examples' => array(
  182. 'By Table Name' => 'drush tripal-node-clean feature'
  183. ),
  184. 'options' => array(),
  185. 'aliases' => array('trpnode-cln')
  186. );
  187. return $items;
  188. }
  189. /**
  190. * Set the user to run a drush job.
  191. *
  192. * @ingroup tripal_drush
  193. */
  194. function drush_tripal_core_set_user($username) {
  195. drush_tripal_set_user($username);
  196. }
  197. /**
  198. * Executes jobs in the Tripal Jobs Queue.
  199. *
  200. * Executed when 'drush trp-run-job' is called.
  201. *
  202. * @ingroup tripal_drush
  203. */
  204. function drush_tripal_core_trp_run_jobs() {
  205. drush_tripal_trp_run_jobs();
  206. }
  207. /**
  208. * DEPRECATED. Executes jobs in the Tripal Jobs Queue.
  209. *
  210. * Executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called.
  211. *
  212. * @ingroup tripal_drush
  213. */
  214. function drush_tripal_core_tripal_jobs_launch($username) {
  215. $parallel = drush_get_option('parallel');
  216. $job_id = drush_get_option('job_id');
  217. drush_tripal_core_set_user($username);
  218. drush_print("\n\nDEPRECATED: This drush command is outdated.\nIt will ".
  219. "continue to work but please consider using the 'trp-run-jobs' command.\n\n");
  220. if ($parallel) {
  221. drush_print("Tripal Job Launcher (in parallel)");
  222. drush_print("Running as user '$username'");
  223. drush_print("-------------------");
  224. tripal_launch_job($parallel, $job_id);
  225. }
  226. else {
  227. drush_print("Tripal Job Launcher");
  228. drush_print("Running as user '$username'");
  229. drush_print("-------------------");
  230. tripal_launch_job(0, $job_id);
  231. }
  232. }
  233. /**
  234. * Executes jobs in the Tripal Jobs Queue.
  235. *
  236. * Executed when 'drush trp-rerun-job' is called.
  237. *
  238. * @ingroup tripal_drush
  239. */
  240. function drush_tripal_core_trp_rerun_job() {
  241. drush_tripal_trp_rerun_job();
  242. }
  243. /**
  244. * DEPRECATED. Executes jobs in the Tripal Jobs Queue.
  245. *
  246. * Executed when 'drush tripal-jobs-rerun' is called.
  247. *
  248. * @ingroup tripal_drush
  249. */
  250. function drush_tripal_core_tripal_jobs_rerun($username, $job_id) {
  251. drush_print("\n\nDEPRECATED: This drush command is outdated.\nIt will ".
  252. "continue to work but please consider using the 'trp-rerun-job' command.\n\n");
  253. drush_tripal_core_set_user($username);
  254. $new_job_id = tripal_rerun_job($job_id, FALSE);
  255. drush_tripal_core_tripal_jobs_launch($username, $new_job_id);
  256. }
  257. /**
  258. * Prints details about the current running job.
  259. *
  260. * Executed when 'drush trp-get-currjob' is called.
  261. *
  262. * @ingroup tripal_drush
  263. */
  264. function drush_tripal_core_trp_get_currjob() {
  265. drush_tripal_trp_get_currjob();
  266. }
  267. /**
  268. * DEPRECATED. Prints details about the current running job.
  269. *
  270. * Executed when 'drush trpjob-curr' or 'drush tripal-current-job' is called.
  271. *
  272. * @ingroup tripal_drush
  273. */
  274. function drush_tripal_core_tripal_jobs_current() {
  275. drush_print("\n\nDEPRECATED: This drush command is outdated.\nIt will ".
  276. "continue to work but please consider using the 'trp-get-currjob' command.\n\n");
  277. drush_tripal_core_trp_get_currjob();
  278. }
  279. /**
  280. * Updates the specified materialized view
  281. *
  282. * @ingroup tripal_drush
  283. */
  284. function drush_tripal_core_trp_refresh_mview() {
  285. $mview_id = drush_get_option('mview');
  286. $table_name = drush_get_option('table');
  287. // Either table_name or mview is required
  288. if (!$mview_id) {
  289. if ($table_name) {
  290. // if table_name supplied use that to get mview_id
  291. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table = :mv_table";
  292. $results = db_query($sql, array(':mv_table' => $table_name));
  293. $r = $resuls->fetchObject();
  294. if (!$r->mview_id) {
  295. drush_set_error('No Materialized View associated with that table_name.');
  296. }
  297. $mview_id=$r->mview_id;
  298. }
  299. else {
  300. drush_set_error('Plese provide one option of --mview or --table.');
  301. }
  302. }
  303. drush_print('Updating the Materialized View with ID=' . $mview_id);
  304. $status = tripal_populate_mview($mview_id);
  305. if ($status) {
  306. drush_log('Materialized View Updated', 'ok');
  307. }
  308. else {
  309. drush_set_error('Update failed.');
  310. }
  311. }
  312. /**
  313. * DEPRECATED. Updates the specified materialized view.
  314. *
  315. * @ingroup tripal_drush
  316. */
  317. function drush_tripal_core_tripal_update_mview() {
  318. $mview_id = drush_get_option('mview_id');
  319. $table_name = drush_get_option('table_name');
  320. drush_print("\n\nDEPRECATED: This drush command is outdated.\nIt will ".
  321. "continue to work but please consider using the 'trp-refresh-mview' command.\n\n");
  322. // Either table_name or mview is required
  323. if (!$mview_id) {
  324. if ($table_name) {
  325. // if table_name supplied use that to get mview_id
  326. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table = :mv_table";
  327. $results = db_query($sql, array(':mv_table' => $table_name));
  328. $r = $resuls->fetchObject();
  329. if (!$r->mview_id) {
  330. drush_set_error('No Materialized View associated with that table_name.');
  331. }
  332. $mview_id=$r->mview_id;
  333. }
  334. else {
  335. drush_set_error('Either mview_id OR table_name are required.');
  336. }
  337. }
  338. drush_print('Updating the Materialized View with ID=' . $mview_id);
  339. $status = tripal_populate_mview($mview_id);
  340. if ($status) {
  341. drush_log('Materialized View Updated', 'ok');
  342. }
  343. else {
  344. drush_set_error('Update failed.');
  345. }
  346. }
  347. /**
  348. * DEPRECATED. Returns the current version of chado.
  349. *
  350. * @ingroup tripal_drush
  351. */
  352. function drush_tripal_core_tripal_chado_version() {
  353. drush_print("\n\nDEPRECATED: This drush command is outdated.\nIt will ".
  354. "continue to work but please consider using the 'trp-get-cversion' command.\n\n");
  355. drush_tripal_core_trp_get_cversion();
  356. }
  357. /**
  358. * Returns the current version of chado.
  359. *
  360. * @ingroup tripal_drush
  361. */
  362. function drush_tripal_core_trp_get_cversion() {
  363. $version = $GLOBALS["exact_chado_version"];
  364. drush_print('Current Chado Version: ' . $version);
  365. }
  366. /**
  367. * Returns the Tripal Schema API Description of the given table
  368. *
  369. * @ingroup tripal_drush
  370. */
  371. function drush_tripal_core_trp_get_table() {
  372. $section = drush_get_option('section');
  373. $table_name = drush_get_option('table');
  374. drush_print("Schema API Description for $table_name:");
  375. $desc = chado_get_schema($table_name);
  376. if (!empty($section)) {
  377. drush_print("$section = " . print_r($desc[$section], TRUE));
  378. }
  379. else {
  380. drush_print(print_r($desc, TRUE));
  381. }
  382. }
  383. /**
  384. * DEPRECATED. Returns the Tripal Schema API Description of the given table.
  385. *
  386. * @param $table_name
  387. * The name of the table to return the description of
  388. *
  389. * @ingroup tripal_drush
  390. */
  391. function drush_tripal_core_tripal_chadotable_desc($table_name) {
  392. $section = drush_get_option('section');
  393. drush_print("\n\nDEPRECATED: This drush command is outdated.\nIt will ".
  394. "continue to work but please consider using the 'trp-get-table' command.\n\n");
  395. drush_print("Schema API Description for $table_name:");
  396. $desc = chado_get_schema($table_name);
  397. if (!empty($section)) {
  398. drush_print("$section = " . print_r($desc[$section], TRUE));
  399. }
  400. else {
  401. drush_print(print_r($desc, TRUE));
  402. }
  403. }
  404. /**
  405. * Clean-up orphaned Drupal nodes and chado records.
  406. *
  407. * @ingroup tripal_drush
  408. */
  409. function drush_tripal_core_trp_clean_nodes() {
  410. $table = drush_get_option('table');
  411. chado_cleanup_orphaned_nodes($table, 0);
  412. }
  413. /**
  414. * DEPRECATED. Clean-up orphaned Drupal nodes and chado records.
  415. *
  416. * @param $module
  417. * The name of a module with nodes associated with it. For example, feature
  418. *
  419. * @ingroup tripal_drush
  420. */
  421. function drush_tripal_core_tripal_node_clean($module) {
  422. chado_cleanup_orphaned_nodes($module, 0);
  423. }
  424. /**
  425. * Clean-up orphaned Drupal nodes and chado records.
  426. *
  427. * @ingroup tripal_drush
  428. */
  429. function drush_tripal_chado_trp_clean_nodes() {
  430. $table = drush_get_option('table');
  431. chado_cleanup_orphaned_nodes($table, 0);
  432. }