tripal_chado.drush.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 = [];
  45. $items['trp-refresh-mview'] = [
  46. 'description' => dt('Refreshes the contents of the specified materialized view.'),
  47. 'arguments' => [],
  48. 'examples' => [
  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' => [
  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'] = [
  58. 'description' => dt('Returns the current installed version of Chado.'),
  59. 'arguments' => [],
  60. 'examples' => [
  61. 'Standard Example' => 'drush trp-get-cversion',
  62. ],
  63. ];
  64. $items['trp-get-table'] = [
  65. 'description' => dt('Returns a table description in Drupal Schema API format.'),
  66. 'arguments' => [],
  67. 'examples' => [
  68. 'By Table Name' => 'drush trp-get-table --table=feature',
  69. 'By Section' => 'drush trp-get-table --table=feature --section=fields',
  70. ],
  71. 'options' => [
  72. 'table' => [
  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. //
  80. // Drush commands for publications
  81. //
  82. $items['trp-import-pubs'] = [
  83. 'description' => dt('Imports publications from remote databases using saved configuration settings.'),
  84. 'options' => [
  85. 'create_contacts' => dt('Create or update contacts for authors. This option is only available with the --dxref option because creation of contacts is otherwise set for each importer.'),
  86. 'dbxref' => dt('An accession number for a publication from a remote database (e.g. PMID:23582642).'),
  87. 'report' => dt("Set to the email address of the recipient who should receive an HTML report of the publications that have been added. Not applicable with the --dbxref option."),
  88. 'update' => dt("Set to 'Y' to update existing pubs. By default only new pubs are inserted."),
  89. 'publish' => dt("Set to 'Y' to publish publications after import into Chado. (default: Y)."),
  90. 'sync' => dt("For Tripal v3 legacy mode only. Set to 'Y' to sync publications after import into Chado. This will create legacy \"nodes\" rather than the newer Tripal v3 entities."),
  91. 'username' => [
  92. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  93. ],
  94. ],
  95. 'examples' => [
  96. 'Standard example' => 'drush trp-import-pubs --username=[username] --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',
  97. 'Import single publication' => 'drush tripal-pub-import --username=[username] --dbxref=PMID:23582642',
  98. ],
  99. ];
  100. $items['trp-update-pubs'] = [
  101. 'description' => dt('Updates publication information for publications with a supported database cross-reference.'),
  102. 'options' => [
  103. 'dbxref' => dt('An accession number for a publication from a remote database (e.g. PMID:23582642)'),
  104. 'db' => dt('The database name (e.g. PMID or AGL)'),
  105. 'publish' => dt("Set to 'Y' to publish publications after import into Chado. (default: Y)."),
  106. 'sync' => dt("For Tripal v3 legacy mode only. Set to 'Y' to sync publications after import into Chado. This will create legacy \"nodes\" rather than the newer Tripal v3 entities."),
  107. 'username' => [
  108. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  109. ],
  110. ],
  111. 'examples' => [
  112. 'Standard example' => 'drush trp-update-pubs --username=[username]',
  113. 'Update a single record' => 'drush trp-update-pubs --username=[username] --dbxref=PMID:23582642',
  114. 'Update all records for a single database' => 'drush trp-update-pubs --username=[username] --db=PMID',
  115. ],
  116. ];
  117. return $items;
  118. }
  119. /**
  120. * Imports publications into Chado
  121. *
  122. * @ingroup tripal_drush
  123. */
  124. function drush_tripal_chado_trp_import_pubs() {
  125. // The create contacts only goes with the dbxref argument
  126. $create_contacts = drush_get_option('create_contacts');
  127. $dbxref = drush_get_option('dbxref');
  128. // The do_report, publish, sync only go with the normal importer.
  129. $do_report = drush_get_option('report');
  130. $publish = drush_get_option('publish');
  131. $sync = drush_get_option('sync');
  132. $update = drush_get_option('update');
  133. $uname = drush_get_option('username');
  134. drush_tripal_set_user($uname);
  135. if ($update == 'Y') {
  136. $update = TRUE;
  137. }
  138. else {
  139. $update = FALSE;
  140. }
  141. if ($publish == 'Y' and $sync == 'Y') {
  142. $publish = 'both';
  143. }
  144. elseif ($publish != 'Y' and $sync == 'Y') {
  145. $publish = 'sync';
  146. }
  147. elseif ($publish == 'Y' and $sync != 'Y') {
  148. $publish = TRUE;
  149. }
  150. else {
  151. $publish = FALSE;
  152. }
  153. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.pub_importers');
  154. if ($dbxref) {
  155. chado_import_pub_by_dbxref($dbxref, $create_contacts, $publish, $update);
  156. }
  157. else {
  158. chado_execute_active_pub_importers($do_report, $publish, $update);
  159. }
  160. }
  161. /**
  162. * Imports publications into Chado
  163. *
  164. * @ingroup tripal_drush
  165. */
  166. function drush_tripal_chado_trp_update_pubs() {
  167. $create_contacts = drush_get_option('create_contacts');
  168. $dbxref = drush_get_option('dbxref');
  169. $db = drush_get_option('db');
  170. $publish = drush_get_option('publish');
  171. $sync = drush_get_option('sync');
  172. $uname = drush_get_option('username');
  173. drush_tripal_set_user($uname);
  174. if ($publish == 'Y' and $sync == 'Y') {
  175. $publish = 'both';
  176. }
  177. elseif ($publish != 'Y' and $sync == 'Y') {
  178. $publish = 'sync';
  179. }
  180. elseif ($publish == 'Y' and $sync != 'Y') {
  181. $publish = TRUE;
  182. }
  183. else {
  184. $publish = FALSE;
  185. }
  186. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.pub_importers');
  187. chado_reimport_publications($create_contacts, $dbxref, $db, $publish);
  188. }
  189. /**
  190. * Set the user to run a drush job.
  191. *
  192. * @ingroup tripal_drush
  193. */
  194. function drush_tripal_chado_set_user($username) {
  195. if ($username) {
  196. $sql = "SELECT uid FROM {users} WHERE name = :name";
  197. $results = db_query($sql, [':name' => $username]);
  198. $u = $results->fetchObject();
  199. if (!$u) {
  200. drush_print('ERROR: Please provide a valid username (--username argument) for running this job.');
  201. exit;
  202. }
  203. global $user;
  204. $user = user_load($u->uid);
  205. return $u->uid;
  206. }
  207. else {
  208. drush_print('ERROR: Please provide a username (--username argument) for running this job.');
  209. exit;
  210. }
  211. }
  212. /**
  213. * Updates the specified materialized view
  214. *
  215. * @ingroup tripal_drush
  216. */
  217. function drush_tripal_chado_trp_refresh_mview() {
  218. $mview_id = drush_get_option('mview');
  219. $table_name = drush_get_option('table');
  220. // Either table_name or mview is required
  221. if (!$mview_id) {
  222. if ($table_name) {
  223. // if table_name supplied use that to get mview_id
  224. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table = :mv_table";
  225. $results = db_query($sql, [':mv_table' => $table_name]);
  226. $r = $resuls->fetchObject();
  227. if (!$r->mview_id) {
  228. drush_set_error('No Materialized View associated with that table_name.');
  229. }
  230. $mview_id = $r->mview_id;
  231. }
  232. else {
  233. drush_set_error('Plese provide one option of --mview or --table.');
  234. }
  235. }
  236. drush_print('Updating the Materialized View with ID=' . $mview_id);
  237. $status = chado_populate_mview($mview_id);
  238. if ($status) {
  239. drush_log('Materialized View Updated', 'ok');
  240. }
  241. else {
  242. drush_set_error('Update failed.');
  243. }
  244. }
  245. /**
  246. * Returns the current version of chado.
  247. *
  248. * @ingroup tripal_drush
  249. */
  250. function drush_tripal_chado_trp_get_cversion() {
  251. $version = $GLOBALS["exact_chado_version"];
  252. drush_print('Current Chado Version: ' . $version);
  253. }
  254. /**
  255. * Returns the Tripal Schema API Description of the given table
  256. *
  257. * @ingroup tripal_drush
  258. */
  259. function drush_tripal_chado_trp_get_table() {
  260. $section = drush_get_option('section');
  261. $table_name = drush_get_option('table');
  262. drush_print("Schema API Description for $table_name:");
  263. $desc = chado_get_schema($table_name);
  264. if (!empty($section)) {
  265. drush_print("$section = " . print_r($desc[$section], TRUE));
  266. }
  267. else {
  268. drush_print(print_r($desc, TRUE));
  269. }
  270. }