tripal_core.drush.inc 12 KB

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