tripal_core.drush.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. // TRIPAL JOBS
  23. case 'trp-run-jobs':
  24. return dt('Launches pending jobs waiting in the queue.');
  25. break;
  26. case 'trp-rerun-job':
  27. return dt('Rerun a job in the queue.');
  28. break;
  29. case 'trp-get-currjob':
  30. return dt('Returns details about the currently running tripal job including percent complete.');
  31. break;
  32. // Placeholders for unimplmeneted jobs
  33. case 'trp-show-job':
  34. break;
  35. case 'trp-revert-jobs':
  36. break;
  37. case 'trp-cancel-job':
  38. break;
  39. case 'trp-list-jobs':
  40. break;
  41. // Deprecated functions
  42. case 'drush:tripal-launch-jobs':
  43. return dt('DEPRECATED. Please use: trp-run-jobs.');
  44. break;
  45. case 'drush:tripal-current-job':
  46. return dt('DEPRECATED. Please use: trp-get-currjob.');
  47. break;
  48. case 'drush:tripal-rerun-job':
  49. return dt('DEPRECATED. Please use: trp-rerun-job.');
  50. break;
  51. // Tripal Materialized Views
  52. case 'drush:tripal-update-mview':
  53. return dt('Updates the specified materialized view.');
  54. break;
  55. // Chado Specific
  56. case 'drush:tripal-chado-version':
  57. return dt('Returns the current version of chado associated with this drupal site.');
  58. break;
  59. case 'drush:tripal-chadotable-desc':
  60. return dt('Returns the table description as specified in the Tripal Schema API for the supplied table.');
  61. break;
  62. }
  63. }
  64. /**
  65. * Registers a drush command and constructs the full help for that command.
  66. *
  67. * @return
  68. * And array of command descriptions
  69. *
  70. * @ingroup tripal_drush
  71. */
  72. function tripal_core_drush_command() {
  73. $items = array();
  74. $items['trp-refresh-mview'] = array(
  75. 'description' => dt('Refreshes the contents of the specified materialized view.'),
  76. 'arguments' => array(),
  77. 'examples' => array(
  78. 'By Materialized View ID' => 'drush trp-refresh-mview --mview=5',
  79. 'By Table Name' => 'drush trp-refresh-mview --table=organism_feature_count'
  80. ),
  81. 'options' => array(
  82. 'mview' => dt('The ID of the materialized view to update'),
  83. 'table' => dt('The name of the materialized view table to update.'),
  84. ),
  85. );
  86. $items['trp-get-currjob'] = array(
  87. 'description' => dt('Returns details about the currently running job including percent complete.'),
  88. 'arguments' => array(),
  89. 'examples' => array(
  90. 'Standard example' => 'drush trp-get-currjob',
  91. ),
  92. );
  93. $items['trp-run-jobs'] = array(
  94. 'description' => dt('Launches jobs waiting in the queue. Only one job can execute at a time unless the --parllel=1 option is provided.'),
  95. 'examples' => array(
  96. 'Single Job' => 'drush trp-run-jobs --user=administrator',
  97. 'Parallel Job' => 'drush trp-run-jobs --user=administrator --parallel=1'
  98. ),
  99. 'arguments' => array(),
  100. 'options' => array(
  101. 'user' => array(
  102. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  103. 'required' => TRUE,
  104. ),
  105. '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.'),
  106. 'job_id' => dt('Provide a job_id to run a specific job. Only jobs that have not been run already can be used'),
  107. ),
  108. );
  109. $items['trp-rerun-job'] = array(
  110. 'description' => dt('Re-run a specific job from the queue.'),
  111. 'examples' => array(
  112. 'Single Job' => 'drush trp-rerun-job --user=administrator --job_id=2',
  113. 'Parallel Job' => 'drush trp-rerun-job --user=administrator --job_id=2 --parallel=1'
  114. ),
  115. 'arguments' => array(),
  116. 'options' => array(
  117. 'user' => array(
  118. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  119. 'required' => TRUE,
  120. ),
  121. 'job_id' => array(
  122. 'description' => dt('The job ID to run.'),
  123. 'required' => TRUE,
  124. ),
  125. '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.'),
  126. ),
  127. );
  128. $items['trp-get-cversion'] = array(
  129. 'description' => dt('Returns the current installed version of Chado.'),
  130. 'arguments' => array(),
  131. 'examples' => array(
  132. 'Standard Example' => 'drush trp-get-cversion',
  133. ),
  134. );
  135. $items['trp-get-table'] = array(
  136. 'description' => dt('Returns a table description in Drupal Schema API format.'),
  137. 'arguments' => array(),
  138. 'examples' => array(
  139. 'By Table Name' => 'drush trp-get-table --table=feature',
  140. 'By Section' => 'drush trp-get-table --table=feature --section=fields'
  141. ),
  142. 'options' => array(
  143. 'table' => array(
  144. 'description' => dt('The name of the table. The table can be a true Chado table or a custom Chado table.'),
  145. 'required' => TRUE,
  146. ),
  147. '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.'),
  148. ),
  149. );
  150. $items['trp-clean-nodes'] = array(
  151. 'description' => dt('Removes orphaned Drupal nodes.'),
  152. 'arguments' => array(),
  153. 'examples' => array(
  154. 'Standard Example' => 'drush trp-clean-nodes table=feature'
  155. ),
  156. 'options' => array(
  157. 'table' => array(
  158. 'description' => dt('The name of the table that corresonds to the node type to ' .
  159. 'clean up. (e.g organism, feature, stock, library, analysis, pub, etc.)'),
  160. 'required' => TRUE,
  161. ),
  162. ),
  163. );
  164. // DEPRECATED COMMANDS. Deprecated as of Tripal v2.0-rc
  165. $items['tripal-mview-update'] = array(
  166. 'description' => dt('DEPRECATED. Please see: trp-refresh-mview.'),
  167. 'arguments' => array(),
  168. 'examples' => array(
  169. 'By Materialized View ID' => 'drush tripal-update-mview --mview_id=5',
  170. 'By Table Name' => 'drush tripal-update-mview --table_name=organism_feature_count'
  171. ),
  172. 'options' => array(
  173. 'mview_id' => dt('The ID of the materialized view to update'),
  174. 'table_name' => dt('The name of the materialized view table to update.'),
  175. ),
  176. 'aliases' => array('trpmv-up')
  177. );
  178. $items['tripal-jobs-current'] = array(
  179. 'description' => dt('DEPRECATED. Please see: trp-get-currjob.'),
  180. 'arguments' => array(),
  181. 'examples' => array(
  182. 'Standard example' => 'drush tripal-jobs-current',
  183. ),
  184. 'aliases' => array('trpjob-cur'),
  185. );
  186. $items['tripal-jobs-launch'] = array(
  187. 'description' => dt('DEPRECATED. Please see: trp-run-jobs. '),
  188. 'examples' => array(
  189. 'Normal Job' => 'drush tripal-jobs-launch admin',
  190. 'Parallel Job' => 'drush tripal-jobs-launch admin --parallel=1'
  191. ),
  192. 'arguments' => array(
  193. 'user' => dt('The Drupal username under which the job should be run. The permissions for this user will be used.'),
  194. ),
  195. 'options' => array(
  196. '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.'),
  197. 'job_id' => dt('Provide a job_id to run a specific job. Only jobs that have not been run already can be used'),
  198. ),
  199. 'aliases' => array('trpjob-run')
  200. );
  201. $items['tripal-jobs-rerun'] = array(
  202. 'description' => dt('DEPRECATED. Please see: trp-rerun-job. '),
  203. 'examples' => array(
  204. 'Normal Job' => 'drush tripal-jobs-rerun admin 2',
  205. 'Parallel Job' => 'drush tripal-jobs-rerun admin 2 --parallel=1'
  206. ),
  207. 'arguments' => array(
  208. 'user' => dt('The Drupal username under which the job should be run. The permissions for this user will be used.'),
  209. 'job_id' => dt('The job ID to run.'),
  210. ),
  211. 'options' => array(
  212. '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.'),
  213. ),
  214. 'aliases' => array('trpjob-rerun')
  215. );
  216. $items['tripal-chado-version'] = array(
  217. 'description' => dt('DEPRECATED. Please see: trp-get-cversion. '),
  218. 'arguments' => array(),
  219. 'examples' => array(
  220. 'Standard Example' => 'drush tripal-chado-version',
  221. ),
  222. 'aliases' => array('trpchado-ver')
  223. );
  224. $items['tripal-chadotable-desc'] = array(
  225. 'description' => dt('DEPRECATED. Please see: trp-get-table. '),
  226. 'arguments' => array(
  227. 'table_name' => dt('The name of the chado table.'),
  228. ),
  229. 'examples' => array(
  230. 'By Table Name' => 'drush tripal-chadotable-desc --table_name=feature'
  231. ),
  232. 'options' => array(
  233. '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.'),
  234. ),
  235. 'aliases' => array('trpschema-tbl')
  236. );
  237. $items['tripal-node-clean'] = array(
  238. 'description' => dt('DEPRECATED. Please see: trp-clean-nodes.'),
  239. 'arguments' => array(
  240. 'module' => dt('The name of the chado-centric module to clean-up.'),
  241. ),
  242. 'examples' => array(
  243. 'By Table Name' => 'drush tripal-node-clean feature'
  244. ),
  245. 'options' => array(),
  246. 'aliases' => array('trpnode-cln')
  247. );
  248. return $items;
  249. }
  250. /**
  251. * Set the user to run a drush job.
  252. *
  253. * @ingroup tripal_drush
  254. */
  255. function drush_tripal_core_set_user($username) {
  256. if ($username) {
  257. $sql = "SELECT uid FROM {users} WHERE name = :name";
  258. $results = db_query($sql, array(':name' => $username));
  259. $u = $results->fetchObject();
  260. if (!$u) {
  261. drush_print('ERROR: Please provide a valid username for running this job.');
  262. exit;
  263. }
  264. global $user;
  265. $user = user_load($u->uid);
  266. return $u->uid;
  267. }
  268. else {
  269. drush_print('ERROR: Please provide a username for running this job.');
  270. exit;
  271. }
  272. }
  273. /**
  274. * Executes jobs in the Tripal Jobs Queue.
  275. *
  276. * Executed when 'drush trp-run-job' is called.
  277. *
  278. * @ingroup tripal_drush
  279. */
  280. function drush_tripal_core_trp_run_jobs() {
  281. $username = drush_get_option('user');
  282. $parallel = drush_get_option('parallel');
  283. $job_id = drush_get_option('job_id');
  284. drush_tripal_core_set_user($username);
  285. if ($parallel) {
  286. drush_print("Tripal Job Launcher (in parallel)");
  287. drush_print("Running as user '$username'");
  288. drush_print("-------------------");
  289. tripal_launch_job($parallel, $job_id);
  290. }
  291. else {
  292. drush_print("Tripal Job Launcher");
  293. drush_print("Running as user '$username'");
  294. drush_print("-------------------");
  295. tripal_launch_job(0, $job_id);
  296. }
  297. }
  298. /**
  299. * DEPRECATED. Executes jobs in the Tripal Jobs Queue.
  300. *
  301. * Executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called.
  302. *
  303. * @ingroup tripal_drush
  304. */
  305. function drush_tripal_core_tripal_jobs_launch($username) {
  306. $parallel = drush_get_option('parallel');
  307. $job_id = drush_get_option('job_id');
  308. drush_tripal_core_set_user($username);
  309. drush_print("\n\nDEPRECATED: This drush command is outdated.\nIt will ".
  310. "continue to work but please consider using the 'trp-run-jobs' command.\n\n");
  311. if ($parallel) {
  312. drush_print("Tripal Job Launcher (in parallel)");
  313. drush_print("Running as user '$username'");
  314. drush_print("-------------------");
  315. tripal_launch_job($parallel, $job_id);
  316. }
  317. else {
  318. drush_print("Tripal Job Launcher");
  319. drush_print("Running as user '$username'");
  320. drush_print("-------------------");
  321. tripal_launch_job(0, $job_id);
  322. }
  323. }
  324. /**
  325. * Executes jobs in the Tripal Jobs Queue.
  326. *
  327. * Executed when 'drush trp-rerun-job' is called.
  328. *
  329. * @ingroup tripal_drush
  330. */
  331. function drush_tripal_core_trp_rerun_job() {
  332. $username = drush_get_option('user');
  333. $parallel = drush_get_option('parallel');
  334. $job_id = drush_get_option('job_id');
  335. drush_tripal_core_set_user($username);
  336. $new_job_id = tripal_rerun_job($job_id, FALSE);
  337. if ($parallel) {
  338. drush_print("Tripal Job Launcher (in parallel)");
  339. drush_print("Running as user '$username'");
  340. drush_print("-------------------");
  341. tripal_launch_job($parallel, $new_job_id);
  342. }
  343. else {
  344. drush_print("Tripal Job Launcher");
  345. drush_print("Running as user '$username'");
  346. drush_print("-------------------");
  347. tripal_launch_job(0, $new_job_id);
  348. }
  349. }
  350. /**
  351. * DEPRECATED. Executes jobs in the Tripal Jobs Queue.
  352. *
  353. * Executed when 'drush tripal-jobs-rerun' is called.
  354. *
  355. * @ingroup tripal_drush
  356. */
  357. function drush_tripal_core_tripal_jobs_rerun($username, $job_id) {
  358. drush_print("\n\nDEPRECATED: This drush command is outdated.\nIt will ".
  359. "continue to work but please consider using the 'trp-rerun-job' command.\n\n");
  360. drush_tripal_core_set_user($username);
  361. $new_job_id = tripal_rerun_job($job_id, FALSE);
  362. drush_tripal_core_tripal_jobs_launch($username, $new_job_id);
  363. }
  364. /**
  365. * Prints details about the current running job.
  366. *
  367. * Executed when 'drush trp-get-currjob' is called.
  368. *
  369. * @ingroup tripal_drush
  370. */
  371. function drush_tripal_core_trp_get_currjob() {
  372. $sql = "
  373. SELECT *
  374. FROM {tripal_jobs} TJ
  375. WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL
  376. ";
  377. $jobs = db_query($sql);
  378. foreach ($jobs as $job) {
  379. $job_pid = $job->pid;
  380. $output = "Name: " . $job->job_name . "\n" .
  381. "Submitted: " . date(DATE_RFC822, $job->submit_date) . "\n" .
  382. "Started: " . date(DATE_RFC822, $job->start_time) . "\n" .
  383. "Module: " . $job->modulename . "\n" .
  384. "Callback: " . $job->callback . "\n" .
  385. "Process ID: " . $job->pid . "\n" .
  386. "Progress: " . $job->progress . "%\n";
  387. drush_print($output);
  388. }
  389. if (!$job_pid) {
  390. drush_print('There are currently no running jobs.');
  391. }
  392. //log to the command line with an OK status
  393. drush_log('Running tripal-current-job', 'ok');
  394. }
  395. /**
  396. * DEPRECATED. Prints details about the current running job.
  397. *
  398. * Executed when 'drush trpjob-curr' or 'drush tripal-current-job' is called.
  399. *
  400. * @ingroup tripal_drush
  401. */
  402. function drush_tripal_core_tripal_jobs_current() {
  403. drush_print("\n\nDEPRECATED: This drush command is outdated.\nIt will ".
  404. "continue to work but please consider using the 'trp-get-currjob' command.\n\n");
  405. drush_tripal_core_trp_get_currjob();
  406. }
  407. /**
  408. * Updates the specified materialized view
  409. *
  410. * @ingroup tripal_drush
  411. */
  412. function drush_tripal_core_trp_refresh_mview() {
  413. $mview_id = drush_get_option('mview');
  414. $table_name = drush_get_option('table');
  415. // Either table_name or mview is required
  416. if (!$mview_id) {
  417. if ($table_name) {
  418. // if table_name supplied use that to get mview_id
  419. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table = :mv_table";
  420. $results = db_query($sql, array(':mv_table' => $table_name));
  421. $r = $resuls->fetchObject();
  422. if (!$r->mview_id) {
  423. drush_set_error('No Materialized View associated with that table_name.');
  424. }
  425. $mview_id=$r->mview_id;
  426. }
  427. else {
  428. drush_set_error('Plese provide one option of --mview or --table.');
  429. }
  430. }
  431. drush_print('Updating the Materialized View with ID=' . $mview_id);
  432. $status = tripal_populate_mview($mview_id);
  433. if ($status) {
  434. drush_log('Materialized View Updated', 'ok');
  435. }
  436. else {
  437. drush_set_error('Update failed.');
  438. }
  439. }
  440. /**
  441. * DEPRECATED. Updates the specified materialized view.
  442. *
  443. * @ingroup tripal_drush
  444. */
  445. function drush_tripal_core_tripal_update_mview() {
  446. $mview_id = drush_get_option('mview_id');
  447. $table_name = drush_get_option('table_name');
  448. drush_print("\n\nDEPRECATED: This drush command is outdated.\nIt will ".
  449. "continue to work but please consider using the 'trp-refresh-mview' command.\n\n");
  450. // Either table_name or mview is required
  451. if (!$mview_id) {
  452. if ($table_name) {
  453. // if table_name supplied use that to get mview_id
  454. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table = :mv_table";
  455. $results = db_query($sql, array(':mv_table' => $table_name));
  456. $r = $resuls->fetchObject();
  457. if (!$r->mview_id) {
  458. drush_set_error('No Materialized View associated with that table_name.');
  459. }
  460. $mview_id=$r->mview_id;
  461. }
  462. else {
  463. drush_set_error('Either mview_id OR table_name are required.');
  464. }
  465. }
  466. drush_print('Updating the Materialized View with ID=' . $mview_id);
  467. $status = tripal_populate_mview($mview_id);
  468. if ($status) {
  469. drush_log('Materialized View Updated', 'ok');
  470. }
  471. else {
  472. drush_set_error('Update failed.');
  473. }
  474. }
  475. /**
  476. * DEPRECATED. Returns the current version of chado.
  477. *
  478. * @ingroup tripal_drush
  479. */
  480. function drush_tripal_core_tripal_chado_version() {
  481. drush_print("\n\nDEPRECATED: This drush command is outdated.\nIt will ".
  482. "continue to work but please consider using the 'trp-get-cversion' command.\n\n");
  483. drush_tripal_core_trp_get_cversion();
  484. }
  485. /**
  486. * Returns the current version of chado.
  487. *
  488. * @ingroup tripal_drush
  489. */
  490. function drush_tripal_core_trp_get_cversion() {
  491. $version = $GLOBALS["exact_chado_version"];
  492. drush_print('Current Chado Version: ' . $version);
  493. }
  494. /**
  495. * Returns the Tripal Schema API Description of the given table
  496. *
  497. * @ingroup tripal_drush
  498. */
  499. function drush_tripal_core_trp_get_table() {
  500. $section = drush_get_option('section');
  501. $table_name = drush_get_option('table');
  502. drush_print("Schema API Description for $table_name:");
  503. $desc = chado_get_schema($table_name);
  504. if (!empty($section)) {
  505. drush_print("$section = " . print_r($desc[$section], TRUE));
  506. }
  507. else {
  508. drush_print(print_r($desc, TRUE));
  509. }
  510. }
  511. /**
  512. * DEPRECATED. Returns the Tripal Schema API Description of the given table.
  513. *
  514. * @param $table_name
  515. * The name of the table to return the description of
  516. *
  517. * @ingroup tripal_drush
  518. */
  519. function drush_tripal_core_tripal_chadotable_desc($table_name) {
  520. $section = drush_get_option('section');
  521. drush_print("\n\nDEPRECATED: This drush command is outdated.\nIt will ".
  522. "continue to work but please consider using the 'trp-get-table' command.\n\n");
  523. drush_print("Schema API Description for $table_name:");
  524. $desc = chado_get_schema($table_name);
  525. if (!empty($section)) {
  526. drush_print("$section = " . print_r($desc[$section], TRUE));
  527. }
  528. else {
  529. drush_print(print_r($desc, TRUE));
  530. }
  531. }
  532. /**
  533. * Clean-up orphaned Drupal nodes and chado records.
  534. *
  535. * @ingroup tripal_drush
  536. */
  537. function drush_tripal_core_trp_clean_nodes() {
  538. $table = drush_get_option('table');
  539. chado_cleanup_orphaned_nodes($table, 0);
  540. }
  541. /**
  542. * DEPRECATED. Clean-up orphaned Drupal nodes and chado records.
  543. *
  544. * @param $module
  545. * The name of a module with nodes associated with it. For example, feature
  546. *
  547. * @ingroup tripal_drush
  548. */
  549. function drush_tripal_core_tripal_node_clean($module) {
  550. chado_cleanup_orphaned_nodes($module, 0);
  551. }