tripal_core.drush.inc

  1. 2.x tripal_core/tripal_core.drush.inc
  2. 3.x legacy/tripal_core/tripal_core.drush.inc
  3. 1.x tripal_core/tripal_core.drush.inc

Contains function relating to drush-integration of this module.

File

tripal_core/tripal_core.drush.inc
View source
  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. global $user;
  169. $user = user_load(array('name' => $username));
  170. }
  171. else {
  172. drush_print('ERROR: Please provide a username for running this job.');
  173. return;
  174. }
  175. if ($parallel) {
  176. drush_print("Tripal Job Launcher (in parallel)");
  177. drush_print("Running as user '$username'");
  178. drush_print("-------------------");
  179. tripal_jobs_launch($parallel, $job_id);
  180. }
  181. else {
  182. drush_print("Tripal Job Launcher");
  183. drush_print("Running as user '$username'");
  184. drush_print("-------------------");
  185. tripal_jobs_launch(0, $job_id);
  186. }
  187. }
  188. /**
  189. * Executes jobs in the Tripal Jobs Queue
  190. *
  191. * NOTE: The following code is executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called
  192. */
  193. function drush_tripal_core_tripal_jobs_rerun($username, $job_id) {
  194. $new_job_id = tripal_jobs_rerun($job_id, FALSE);
  195. drush_tripal_core_tripal_jobs_launch($username, $new_job_id);
  196. }
  197. /**
  198. * Prints details about the current running job
  199. *
  200. * NOTE: The following code is executed when 'drush trpjob-curr' or 'drush tripal-current-job' is called
  201. */
  202. function drush_tripal_core_tripal_jobs_current() {
  203. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  204. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  205. $jobs = db_query($sql);
  206. while ($job = db_fetch_object($jobs)) {
  207. $job_pid = $job->pid;
  208. $output = "Name: " . $job->job_name . "\n"
  209. ."Submitted: " . date(DATE_RFC822, $job->submit_date) . "\n"
  210. ."Started: " . date(DATE_RFC822, $job->start_time) . "\n"
  211. ."Module: " . $job->modulename . "\n"
  212. ."Callback: " . $job->callback . "\n"
  213. ."Process ID: " . $job->pid . "\n"
  214. ."Progress: " . $job->progress . "%\n";
  215. drush_print($output);
  216. }
  217. if (!$job_pid) {
  218. drush_print('There are currently no running jobs.');
  219. }
  220. //log to the command line with an OK status
  221. drush_log('Running tripal-current-job', 'ok');
  222. }
  223. /**
  224. * Updates the specified materialized view
  225. *
  226. * @param $mview_id
  227. * The ID of the materialized view (tripal_mview.mview_id)
  228. * @param $table_name
  229. * The name of the table storing the materialized view (tripal_mview.mv_table)
  230. *
  231. * Note: Either $mview_id OR $table_name is required
  232. */
  233. function drush_tripal_core_tripal_update_mview() {
  234. $mview_id = drush_get_option('mview_id');
  235. $table_name = drush_get_option('table_name');
  236. // Either table_name or mview is required
  237. if (!$mview_id) {
  238. if ($table_name) {
  239. // if table_name supplied use that to get mview_id
  240. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'";
  241. $r = db_fetch_object(db_query($sql, $table_name));
  242. if (!$r->mview_id) {
  243. drush_set_error('No Materialized View associated with that table_name.');
  244. }
  245. $mview_id=$r->mview_id;
  246. }
  247. else {
  248. drush_set_error('Either mview_id OR table_name are required.');
  249. }
  250. }
  251. drush_print('Updating the Materialized View with ID=' . $mview_id);
  252. $status = tripal_update_mview($mview_id);
  253. if ($status) {
  254. drush_log('Materialized View Updated', 'ok');
  255. }
  256. else {
  257. drush_set_error('Update failed.');
  258. }
  259. }
  260. /**
  261. * Returns the current version of chado
  262. */
  263. function drush_tripal_core_tripal_chado_version() {
  264. $version = tripal_core_get_chado_version();
  265. drush_print('Current Chado Version: '.$version);
  266. }
  267. /**
  268. * Returns the Tripal Schema API Description of the given table
  269. *
  270. * @param $table_name
  271. * The name of the table to return the description of
  272. */
  273. function drush_tripal_core_tripal_chadotable_desc($table_name) {
  274. $section = drush_get_option('section');
  275. drush_print("Schema API Description for $table_name:");
  276. $desc = tripal_core_get_chado_table_schema($table_name);
  277. if (!empty($section)) {
  278. drush_print("$section = ".print_r($desc[$section],TRUE));
  279. }
  280. else {
  281. drush_print(print_r($desc,TRUE));
  282. }
  283. }
  284. /**
  285. * Sync's chado records with drupal creating nodes for the given chado-centric module.
  286. *
  287. * @param $module
  288. * The name of a module with nodes associated with it. For example, feature
  289. */
  290. function drush_tripal_core_tripal_node_sync($module) {
  291. switch($module) {
  292. case 'analysis':
  293. call_user_func('tripal_analysis_sync_analyses');
  294. break;
  295. case 'feature':
  296. call_user_func('tripal_feature_sync_features');
  297. break;
  298. case 'featuremap':
  299. call_user_func('tripal_featuremap_sync_featuremaps');
  300. break;
  301. case 'library':
  302. call_user_func('tripal_library_sync_libraries');
  303. break;
  304. case 'organism':
  305. call_user_func('tripal_organism_sync_organisms');
  306. break;
  307. case 'project':
  308. call_user_func('tripal_project_sync_all_projects');
  309. break;
  310. case 'stock':
  311. call_user_func('tripal_stock_sync_stocks');
  312. break;
  313. }
  314. }
  315. /**
  316. * Clean-up orphaned Drupal nodes and chado records.
  317. *
  318. * @param $module
  319. * The name of a module with nodes associated with it. For example, feature
  320. */
  321. function drush_tripal_core_tripal_node_clean($module) {
  322. tripal_core_clean_orphaned_nodes($module, 0);
  323. }