tripal_core.drush.inc 11 KB

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