tripal_chado.drush.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. return $items;
  80. }
  81. /**
  82. * Set the user to run a drush job.
  83. *
  84. * @ingroup tripal_drush
  85. */
  86. function drush_tripal_chado_set_user($username) {
  87. if ($username) {
  88. $sql = "SELECT uid FROM {users} WHERE name = :name";
  89. $results = db_query($sql, array(':name' => $username));
  90. $u = $results->fetchObject();
  91. if (!$u) {
  92. drush_print('ERROR: Please provide a valid username (--username argument) for running this job.');
  93. exit;
  94. }
  95. global $user;
  96. $user = user_load($u->uid);
  97. return $u->uid;
  98. }
  99. else {
  100. drush_print('ERROR: Please provide a username (--username argument) for running this job.');
  101. exit;
  102. }
  103. }
  104. /**
  105. * Updates the specified materialized view
  106. *
  107. * @ingroup tripal_drush
  108. */
  109. function drush_tripal_chado_trp_refresh_mview() {
  110. $mview_id = drush_get_option('mview');
  111. $table_name = drush_get_option('table');
  112. // Either table_name or mview is required
  113. if (!$mview_id) {
  114. if ($table_name) {
  115. // if table_name supplied use that to get mview_id
  116. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table = :mv_table";
  117. $results = db_query($sql, array(':mv_table' => $table_name));
  118. $r = $resuls->fetchObject();
  119. if (!$r->mview_id) {
  120. drush_set_error('No Materialized View associated with that table_name.');
  121. }
  122. $mview_id=$r->mview_id;
  123. }
  124. else {
  125. drush_set_error('Plese provide one option of --mview or --table.');
  126. }
  127. }
  128. drush_print('Updating the Materialized View with ID=' . $mview_id);
  129. $status = tripal_populate_mview($mview_id);
  130. if ($status) {
  131. drush_log('Materialized View Updated', 'ok');
  132. }
  133. else {
  134. drush_set_error('Update failed.');
  135. }
  136. }
  137. /**
  138. * Returns the current version of chado.
  139. *
  140. * @ingroup tripal_drush
  141. */
  142. function drush_tripal_chado_trp_get_cversion() {
  143. $version = $GLOBALS["exact_chado_version"];
  144. drush_print('Current Chado Version: ' . $version);
  145. }
  146. /**
  147. * Returns the Tripal Schema API Description of the given table
  148. *
  149. * @ingroup tripal_drush
  150. */
  151. function drush_tripal_chado_trp_get_table() {
  152. $section = drush_get_option('section');
  153. $table_name = drush_get_option('table');
  154. drush_print("Schema API Description for $table_name:");
  155. $desc = chado_get_schema($table_name);
  156. if (!empty($section)) {
  157. drush_print("$section = " . print_r($desc[$section], TRUE));
  158. }
  159. else {
  160. drush_print(print_r($desc, TRUE));
  161. }
  162. }
  163. /**
  164. * Clean-up orphaned Drupal nodes and chado records.
  165. *
  166. * @ingroup tripal_drush
  167. */
  168. function drush_tripal_chado_trp_clean_nodes() {
  169. $table = drush_get_option('table');
  170. chado_cleanup_orphaned_nodes($table, 0);
  171. }