tripal_core.drush.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 Materialized Views
  23. case 'drush:tripal-update-mview':
  24. return dt('Updates the specified materialized view.');
  25. // Tripal Jobs
  26. case 'drush:tripal-launch-jobs':
  27. return dt('Launches any Tripal jobs waiting in the queue.');
  28. case 'drush:tripal-current-job':
  29. return dt('Returns details about the currently running tripal job including percent complete.');
  30. case 'drush:tripal-rerun-job':
  31. return dt('Rerun any tripal job in the queue.');
  32. // Chado-specific
  33. case 'drush:tripal-chado-version':
  34. return dt('Returns the current version of chado associated with this drupal site.');
  35. case 'drush:tripal-chadotable-desc':
  36. return dt('Returns the table description as specified in the Tripal Schema API for the supplied table.');
  37. }
  38. }
  39. /**
  40. * Registers a drush command and constructs the full help for that command
  41. *
  42. * @return
  43. * And array of command descriptions
  44. *
  45. * @ingroup tripal_drush
  46. */
  47. function tripal_core_drush_command() {
  48. $items = array();
  49. // Materialized Views
  50. $items['tripal-mview-update'] = array(
  51. // used by drush help
  52. 'description' => dt('Updates the specified materialized view.'),
  53. 'arguments' => array(
  54. 'mview_id' => dt('The ID of the materialized view to update'),
  55. 'table_name' => dt('The name of the materialized view table to update.'),
  56. ),
  57. 'examples' => array(
  58. 'By Materialized View ID' => 'drush tripal-update-mview --mview_id=5',
  59. 'By Table Name' => 'drush tripal-update-mview --table_name=organism_feature_count'
  60. ),
  61. // supply options
  62. 'options' => array(
  63. 'mview_id',
  64. 'table_name'
  65. ),
  66. 'aliases' => array('trpmv-up', 'tripal-update-mview')
  67. );
  68. // Jobs
  69. $items['tripal-jobs-current'] = array(
  70. 'description' => dt('Returns details about the currently running tripal job including percent complete.'),
  71. 'arguments' => array(
  72. ),
  73. 'examples' => array(
  74. 'Standard example' => 'drush tripal-jobs-current',
  75. ),
  76. 'aliases' => array('trpjob-cur', 'tripal-current-job'),
  77. );
  78. $items['tripal-jobs-launch'] = array(
  79. // used by drush help
  80. 'description' => dt('Lauches any jobs waiting in the queue.'),
  81. 'examples' => array(
  82. 'Normal Job' => 'drush tripal-jobs-launch admin',
  83. 'Parallel Job' => 'drush tripal-jobs-launch admin --parallel=1'
  84. ),
  85. 'arguments' => array(
  86. 'user' => dt('The Drupal username under which the job should be run. The permissions for this user will be used.'),
  87. ),
  88. // supply options
  89. 'options' => array(
  90. '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.'),
  91. 'job_id' => dt('Provide a job_id to run a specific job. Only jobs that have not been run already can be used'),
  92. ),
  93. 'aliases' => array('trpjob-run', 'tripal-launch-jobs')
  94. );
  95. $items['tripal-jobs-rerun'] = array(
  96. // used by drush help
  97. 'description' => dt('Rerun any job in the queue.'),
  98. 'examples' => array(
  99. 'Normal Job' => 'drush tripal-jobs-rerun admin 2',
  100. 'Parallel Job' => 'drush tripal-jobs-rerun admin 2 --parallel=1'
  101. ),
  102. 'arguments' => array(
  103. 'user' => dt('The Drupal username under which the job should be run. The permissions for this user will be used.'),
  104. 'job_id' => dt('The job ID to run.'),
  105. ),
  106. // supply options
  107. 'options' => array(
  108. '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.'),
  109. ),
  110. 'aliases' => array('trpjob-rerun', 'tripal-rerun-job')
  111. );
  112. // Chado-specific
  113. $items['tripal-chado-version'] = array(
  114. // used by drush help
  115. 'description' => dt('Returns the current version of chado associated with this drupal site.'),
  116. 'arguments' => array(
  117. ),
  118. 'examples' => array(
  119. 'Standard Example' => 'drush tripal-chado-version',
  120. ),
  121. 'aliases' => array('trpchado-ver')
  122. );
  123. $items['tripal-chadotable-desc'] = array(
  124. // used by drush help
  125. 'description' => dt('Returns the table description as specified in the Tripal Schema API for the supplied table.'),
  126. 'arguments' => array(
  127. 'table_name' => dt('The name of the chado table.'),
  128. ),
  129. 'examples' => array(
  130. 'By Table Name' => 'drush tripal-chadotable-desc --table_name=feature'
  131. ),
  132. // supply options
  133. 'options' => array(
  134. '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.'),
  135. ),
  136. 'aliases' => array('trpschema-tbl')
  137. );
  138. // Tripal Node-Module Specific
  139. $items['tripal-node-sync'] = array(
  140. // used by drush help
  141. 'description' => dt('Sync\'s chado records with drupal creating nodes for the given chado-centric module.'),
  142. 'arguments' => array(
  143. 'module' => dt('The name of the chado-centric module to sync.'),
  144. ),
  145. 'examples' => array(
  146. 'By Module' => 'drush tripal-node-sync feature'
  147. ),
  148. // supply options
  149. 'options' => array(
  150. ),
  151. 'aliases' => array('trpnode-sync')
  152. );
  153. $items['tripal-node-clean'] = array(
  154. // used by drush help
  155. 'description' => dt('Clean-up orphaned Drupal nodes and chado records.'),
  156. 'arguments' => array(
  157. 'module' => dt('The name of the chado-centric module to clean-up.'),
  158. ),
  159. 'examples' => array(
  160. 'By Table Name' => 'drush tripal-node-clean feature'
  161. ),
  162. // supply options
  163. 'options' => array(
  164. ),
  165. 'aliases' => array('trpnode-cln')
  166. );
  167. return $items;
  168. }
  169. /**
  170. * Set the user to run a drush job.
  171. *
  172. * @ingroup tripal_drush
  173. */
  174. function drush_tripal_core_set_user($username) {
  175. if ($username) {
  176. $sql = "SELECT uid FROM {users} WHERE name = :name";
  177. $results = db_query($sql, array(':name' => $username));
  178. $u = $results->fetchObject();
  179. if (!$u) {
  180. drush_print('ERROR: Please provide a valid username for running this job.');
  181. exit;
  182. }
  183. global $user;
  184. $user = user_load($u->uid);
  185. return $u->uid;
  186. }
  187. else {
  188. drush_print('ERROR: Please provide a username for running this job.');
  189. exit;
  190. }
  191. }
  192. /**
  193. * Executes jobs in the Tripal Jobs Queue
  194. *
  195. * NOTE: The following code is executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called
  196. *
  197. * @ingroup tripal_drush
  198. */
  199. function drush_tripal_core_tripal_jobs_launch($username) {
  200. $parallel = drush_get_option('parallel');
  201. $job_id = drush_get_option('job_id');
  202. drush_tripal_core_set_user($username);
  203. if ($parallel) {
  204. drush_print("Tripal Job Launcher (in parallel)");
  205. drush_print("Running as user '$username'");
  206. drush_print("-------------------");
  207. tripal_launch_job($parallel, $job_id);
  208. }
  209. else {
  210. drush_print("Tripal Job Launcher");
  211. drush_print("Running as user '$username'");
  212. drush_print("-------------------");
  213. tripal_launch_job(0, $job_id);
  214. }
  215. }
  216. /**
  217. * Executes jobs in the Tripal Jobs Queue
  218. *
  219. * NOTE: The following code is executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called
  220. *
  221. * @ingroup tripal_drush
  222. */
  223. function drush_tripal_core_tripal_jobs_rerun($username, $job_id) {
  224. drush_tripal_core_set_user($username);
  225. $new_job_id = tripal_rerun_job($job_id, FALSE);
  226. drush_tripal_core_tripal_jobs_launch($username, $new_job_id);
  227. }
  228. /**
  229. * Prints details about the current running job
  230. *
  231. * NOTE: The following code is executed when 'drush trpjob-curr' or 'drush tripal-current-job' is called
  232. *
  233. * @ingroup tripal_drush
  234. */
  235. function drush_tripal_core_tripal_jobs_current() {
  236. $sql = "
  237. SELECT *
  238. FROM {tripal_jobs} TJ
  239. WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL
  240. ";
  241. $jobs = db_query($sql);
  242. foreach ($jobs as $job) {
  243. $job_pid = $job->pid;
  244. $output = "Name: " . $job->job_name . "\n" .
  245. "Submitted: " . date(DATE_RFC822, $job->submit_date) . "\n" .
  246. "Started: " . date(DATE_RFC822, $job->start_time) . "\n" .
  247. "Module: " . $job->modulename . "\n" .
  248. "Callback: " . $job->callback . "\n" .
  249. "Process ID: " . $job->pid . "\n" .
  250. "Progress: " . $job->progress . "%\n";
  251. drush_print($output);
  252. }
  253. if (!$job_pid) {
  254. drush_print('There are currently no running jobs.');
  255. }
  256. //log to the command line with an OK status
  257. drush_log('Running tripal-current-job', 'ok');
  258. }
  259. /**
  260. * Updates the specified materialized view
  261. *
  262. * @param $mview_id
  263. * The ID of the materialized view (tripal_mview.mview_id)
  264. * @param $table_name
  265. * The name of the table storing the materialized view (tripal_mview.mv_table)
  266. *
  267. * Note: Either $mview_id OR $table_name is required
  268. *
  269. * @ingroup tripal_drush
  270. */
  271. function drush_tripal_core_tripal_update_mview() {
  272. $mview_id = drush_get_option('mview_id');
  273. $table_name = drush_get_option('table_name');
  274. // Either table_name or mview is required
  275. if (!$mview_id) {
  276. if ($table_name) {
  277. // if table_name supplied use that to get mview_id
  278. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table = :mv_table";
  279. $results = db_query($sql, array(':mv_table' => $table_name));
  280. $r = $resuls->fetchObject();
  281. if (!$r->mview_id) {
  282. drush_set_error('No Materialized View associated with that table_name.');
  283. }
  284. $mview_id=$r->mview_id;
  285. }
  286. else {
  287. drush_set_error('Either mview_id OR table_name are required.');
  288. }
  289. }
  290. drush_print('Updating the Materialized View with ID=' . $mview_id);
  291. $status = tripal_populate_mview($mview_id);
  292. if ($status) {
  293. drush_log('Materialized View Updated', 'ok');
  294. }
  295. else {
  296. drush_set_error('Update failed.');
  297. }
  298. }
  299. /**
  300. * Returns the current version of chado
  301. *
  302. * @ingroup tripal_drush
  303. */
  304. function drush_tripal_core_tripal_chado_version() {
  305. $version = $GLOBALS["exact_chado_version"];
  306. drush_print('Current Chado Version: ' . $version);
  307. }
  308. /**
  309. * Returns the Tripal Schema API Description of the given table
  310. *
  311. * @param $table_name
  312. * The name of the table to return the description of
  313. *
  314. * @ingroup tripal_drush
  315. */
  316. function drush_tripal_core_tripal_chadotable_desc($table_name) {
  317. $section = drush_get_option('section');
  318. drush_print("Schema API Description for $table_name:");
  319. $desc = chado_get_schema($table_name);
  320. if (!empty($section)) {
  321. drush_print("$section = " . print_r($desc[$section], TRUE));
  322. }
  323. else {
  324. drush_print(print_r($desc, TRUE));
  325. }
  326. }
  327. /**
  328. * Sync's chado records with drupal creating nodes for the given chado-centric module.
  329. *
  330. * @param $module
  331. * The name of a module with nodes associated with it. For example, feature
  332. *
  333. * @ingroup tripal_drush
  334. */
  335. function drush_tripal_core_tripal_node_sync($module) {
  336. switch ($module) {
  337. case 'analysis':
  338. call_user_func('tripal_analysis_sync_analyses');
  339. break;
  340. case 'feature':
  341. call_user_func('tripal_feature_sync_features');
  342. break;
  343. case 'featuremap':
  344. call_user_func('tripal_featuremap_sync_featuremaps');
  345. break;
  346. case 'library':
  347. call_user_func('tripal_library_sync_libraries');
  348. break;
  349. case 'organism':
  350. call_user_func('tripal_organism_sync_organisms');
  351. break;
  352. case 'project':
  353. call_user_func('tripal_project_sync_all_projects');
  354. break;
  355. case 'stock':
  356. call_user_func('tripal_stock_sync_stocks');
  357. break;
  358. }
  359. }
  360. /**
  361. * Clean-up orphaned Drupal nodes and chado records.
  362. *
  363. * @param $module
  364. * The name of a module with nodes associated with it. For example, feature
  365. *
  366. * @ingroup tripal_drush
  367. */
  368. function drush_tripal_core_tripal_node_clean($module) {
  369. chado_cleanup_orphaned_nodes($module, 0);
  370. }