tripal_chado.drush.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_chado_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. break;
  26. // Chado Specific
  27. case 'drush:tripal-chado-version':
  28. return dt('Returns the current version of chado associated with this drupal site.');
  29. break;
  30. case 'drush:tripal-chadotable-desc':
  31. return dt('Returns the table description as specified in the Tripal Schema API for the supplied table.');
  32. break;
  33. }
  34. }
  35. /**
  36. * Registers a drush command and constructs the full help for that command.
  37. *
  38. * @return
  39. * And array of command descriptions
  40. *
  41. * @ingroup tripal_drush
  42. */
  43. function tripal_chado_drush_command() {
  44. $items = array();
  45. $items['trp-refresh-mview'] = array(
  46. 'description' => dt('Refreshes the contents of the specified materialized view.'),
  47. 'arguments' => array(),
  48. 'examples' => array(
  49. 'By Materialized View ID' => 'drush trp-refresh-mview --mview=5',
  50. 'By Table Name' => 'drush trp-refresh-mview --table=organism_feature_count'
  51. ),
  52. 'options' => array(
  53. 'mview' => dt('The ID of the materialized view to update'),
  54. 'table' => dt('The name of the materialized view table to update.'),
  55. ),
  56. );
  57. $items['trp-get-cversion'] = array(
  58. 'description' => dt('Returns the current installed version of Chado.'),
  59. 'arguments' => array(),
  60. 'examples' => array(
  61. 'Standard Example' => 'drush trp-get-cversion',
  62. ),
  63. );
  64. $items['trp-get-table'] = array(
  65. 'description' => dt('Returns a table description in Drupal Schema API format.'),
  66. 'arguments' => array(),
  67. 'examples' => array(
  68. 'By Table Name' => 'drush trp-get-table --table=feature',
  69. 'By Section' => 'drush trp-get-table --table=feature --section=fields'
  70. ),
  71. 'options' => array(
  72. 'table' => array(
  73. 'description' => dt('The name of the table. The table can be a true Chado table or a custom Chado table.'),
  74. 'required' => TRUE,
  75. ),
  76. '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.'),
  77. ),
  78. );
  79. // Drush commands for publications
  80. $items['trp-import-pubs'] = array(
  81. 'description' => dt('Imports publications from remote databases using saved configuration settings.'),
  82. 'options' => array(
  83. 'create_contacts' => dt('provide this option to create or update contacts for authors. By default contacts are not created or updated.'),
  84. 'dbxref' => dt('An accession number for a publication from a remote database (e.g. PMID:23582642).'),
  85. 'report' => dt("Set to the email address of the recipient who should receive an HTML report of the publications that have been added."),
  86. 'update' => dt("Set to 'Y' to update existing pubs. By default only new pubs are inserted."),
  87. 'username' => array(
  88. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  89. ),
  90. ),
  91. 'examples' => array(
  92. 'Standard example' => 'drush tripal-pubs-import',
  93. 'Standard example' => 'drush -l http://[site url] tripal-pubs-import --report=[email]. Where [site url] is the URL of the website and [email] is the email address of the recipient to receive the HTML report',
  94. 'Import single publication' => 'drush tripal-pub-import --dbxref=PMID:23582642',
  95. ),
  96. );
  97. $items['trp-update-pubs'] = array(
  98. 'description' => dt('Updates publication information for publications with a supported database cross-reference.'),
  99. 'options' => array(
  100. 'create_contacts' => dt('provide this option to create or update contacts for authors. By default contacts are not created or updated.'),
  101. 'dbxref' => dt('An accession number for a publication from a remote database (e.g. PMID:23582642)'),
  102. 'db' => dt('The database name (e.g. PMID or AGL)'),
  103. ),
  104. 'examples' => array(
  105. 'Standard example' => 'drush tripal-pubs-update',
  106. 'Create contacts during update' => 'drush tripal-pubs-update --create_contacts=1',
  107. 'Update a single record' => 'drush tripal-pubs-update --dbxref=PMID:23582642',
  108. 'Update all records for a single database' => 'drush tripal-pubs-update --db=PMID'
  109. ),
  110. );
  111. return $items;
  112. }
  113. /**
  114. * Imports publications into Chado
  115. *
  116. * @ingroup tripal_drush
  117. */
  118. function drush_tripal_chado_trp_import_pubs() {
  119. $create_contacts = drush_get_option('create_contacts');
  120. $dbxref = drush_get_option('dbxref');
  121. $do_report = drush_get_option('report');
  122. $update = drush_get_option('update');
  123. $uname = drush_get_option('username');
  124. drush_tripal_set_user($uname);
  125. if($update == 'Y') {
  126. $update = TRUE;
  127. }
  128. else {
  129. $update = FALSE;
  130. }
  131. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.pub_importers');
  132. if ($dbxref) {
  133. chado_import_pub_by_dbxref($dbxref, $create_contacts, $update);
  134. }
  135. else {
  136. chado_execute_active_pub_importers($do_report, $update);
  137. }
  138. }
  139. /**
  140. * Imports publications into Chado
  141. *
  142. * @ingroup tripal_drush
  143. */
  144. function drush_tripal_chado_trp_update_pubs() {
  145. $create_contacts = drush_get_option('create_contacts');
  146. $dbxref = drush_get_option('dbxref');
  147. $db = drush_get_option('db');
  148. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.pub_importers');
  149. chado_reimport_publications($create_contacts, $dbxref, $db);
  150. }
  151. /**
  152. * Set the user to run a drush job.
  153. *
  154. * @ingroup tripal_drush
  155. */
  156. function drush_tripal_chado_set_user($username) {
  157. if ($username) {
  158. $sql = "SELECT uid FROM {users} WHERE name = :name";
  159. $results = db_query($sql, array(':name' => $username));
  160. $u = $results->fetchObject();
  161. if (!$u) {
  162. drush_print('ERROR: Please provide a valid username (--username argument) for running this job.');
  163. exit;
  164. }
  165. global $user;
  166. $user = user_load($u->uid);
  167. return $u->uid;
  168. }
  169. else {
  170. drush_print('ERROR: Please provide a username (--username argument) for running this job.');
  171. exit;
  172. }
  173. }
  174. /**
  175. * Updates the specified materialized view
  176. *
  177. * @ingroup tripal_drush
  178. */
  179. function drush_tripal_chado_trp_refresh_mview() {
  180. $mview_id = drush_get_option('mview');
  181. $table_name = drush_get_option('table');
  182. // Either table_name or mview is required
  183. if (!$mview_id) {
  184. if ($table_name) {
  185. // if table_name supplied use that to get mview_id
  186. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table = :mv_table";
  187. $results = db_query($sql, array(':mv_table' => $table_name));
  188. $r = $resuls->fetchObject();
  189. if (!$r->mview_id) {
  190. drush_set_error('No Materialized View associated with that table_name.');
  191. }
  192. $mview_id=$r->mview_id;
  193. }
  194. else {
  195. drush_set_error('Plese provide one option of --mview or --table.');
  196. }
  197. }
  198. drush_print('Updating the Materialized View with ID=' . $mview_id);
  199. $status = chado_populate_mview($mview_id);
  200. if ($status) {
  201. drush_log('Materialized View Updated', 'ok');
  202. }
  203. else {
  204. drush_set_error('Update failed.');
  205. }
  206. }
  207. /**
  208. * Returns the current version of chado.
  209. *
  210. * @ingroup tripal_drush
  211. */
  212. function drush_tripal_chado_trp_get_cversion() {
  213. $version = $GLOBALS["exact_chado_version"];
  214. drush_print('Current Chado Version: ' . $version);
  215. }
  216. /**
  217. * Returns the Tripal Schema API Description of the given table
  218. *
  219. * @ingroup tripal_drush
  220. */
  221. function drush_tripal_chado_trp_get_table() {
  222. $section = drush_get_option('section');
  223. $table_name = drush_get_option('table');
  224. drush_print("Schema API Description for $table_name:");
  225. $desc = chado_get_schema($table_name);
  226. if (!empty($section)) {
  227. drush_print("$section = " . print_r($desc[$section], TRUE));
  228. }
  229. else {
  230. drush_print(print_r($desc, TRUE));
  231. }
  232. }