tripal_stock.sync_stocks.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. # This script can be run as a stand-alone script to sync all the stocks from chado to drupal
  7. // Parameter f specifies the stock_id to sync
  8. // -f 0 will sync all stocks
  9. $arguments = getopt("f:t:");
  10. if (isset($arguments['f']) and isset($arguments['t']) and $arguments['t'] == 'chado_stock') {
  11. $drupal_base_url = parse_url('http://www.example.com');
  12. $_SERVER['HTTP_HOST'] = $drupal_base_url['host'];
  13. $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
  14. $_SERVER['REMOTE_ADDR'] = NULL;
  15. $_SERVER['REQUEST_METHOD'] = NULL;
  16. require_once 'includes/bootstrap.inc';
  17. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  18. $stock_id = $arguments['f'];
  19. if ($stock_id > 0 ) {
  20. tripal_stock_sync_stock($stock_id);
  21. }
  22. else{
  23. print "syncing all stocks...\n";
  24. tripal_stock_sync_stocks();
  25. }
  26. }
  27. /**
  28. *
  29. */
  30. function tripal_stock_sync_form() {
  31. $form['description'] = array(
  32. '#type' => 'item',
  33. '#value' => t("Stocks of the types listed ".
  34. "below in the Stock Types box will be synced (leave blank to sync all types). You may limit the ".
  35. "stocks to be synced by a specific organism. Depending on the ".
  36. "number of stocks in the chado database this may take a long ".
  37. "time to complete. "),
  38. );
  39. $form['stock_types'] = array(
  40. '#title' => t('Stock Types'),
  41. '#type' => 'textarea',
  42. '#description' => t("Enter the names of the stock types to sync. " .
  43. "Leave blank to sync all stocks. Pages for these stock ".
  44. "types will be created automatically for stocks that exist in the ".
  45. "chado database. The names listed here should be spearated by ".
  46. "spaces or entered separately on new lines. The names must match ".
  47. "exactly (spelling and case) with terms in the sequence ontology"),
  48. '#default_value' => variable_get('chado_sync_stock_types', ''),
  49. );
  50. // get the list of organisms
  51. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  52. $orgs = tripal_organism_get_synced();
  53. $organisms[] = '';
  54. foreach ($orgs as $organism) {
  55. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  56. }
  57. $form['organism_id'] = array(
  58. '#title' => t('Organism'),
  59. '#type' => t('select'),
  60. '#description' => t("Choose the organism for which stocks types set above will be synced. Only organisms which also have been synced will appear in this list."),
  61. '#options' => $organisms,
  62. );
  63. $form['button'] = array(
  64. '#type' => 'submit',
  65. '#value' => t('Sync all Stocks'),
  66. '#weight' => 3,
  67. );
  68. return $form;
  69. }
  70. /**
  71. *
  72. */
  73. function tripal_stock_sync_form_validate($form, &$form_state) {
  74. $organism_id = $form_state['values']['organism_id'];
  75. $stock_types = $form_state['values']['stock_types'];
  76. // nothing to do
  77. }
  78. /**
  79. *
  80. */
  81. function tripal_stock_sync_form_submit($form, &$form_state) {
  82. global $user;
  83. $organism_id = $form_state['values']['organism_id'];
  84. $stock_types = $form_state['values']['stock_types'];
  85. $job_args = array(0, $organism_id, $stock_types);
  86. if ($organism_id) {
  87. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $organism_id));
  88. $title = "Sync stocks for " . $organism[0]->genus . " " . $organism[0]->species;
  89. }
  90. else {
  91. $title = 'Sync stocks';
  92. }
  93. variable_set('chado_sync_stock_types', $stock_types);
  94. tripal_add_job($title, 'tripal_stock', 'tripal_stock_sync_stocks', $job_args, $user->uid);
  95. }
  96. /**
  97. *
  98. * @param $na
  99. * Tripal expects all jobs to have at least one argument. For this function
  100. * we don't need any, so we have this dummy argument as a filler
  101. * @param $job_id
  102. */
  103. function tripal_stock_set_urls($na = NULL, $job = NULL) {
  104. // begin the transaction
  105. db_query("BEGIN");
  106. print "\nNOTE: Setting of URLs is performed using a database transaction. \n" .
  107. "If the load fails or is terminated prematurely then the entire set of \n" .
  108. "new URLs will be rolled back and no changes will be made\n\n";
  109. // get the number of records we need to set URLs for
  110. $csql = "SELECT count(*) FROM {chado_stock}";
  111. $num_nodes = db_query($csql)->fetchField();
  112. // calculate the interval at which we will print an update on the screen
  113. $num_set = 0;
  114. $num_per_interval = 100;
  115. // prepate the statements which will quickly add url alias. Because these
  116. // are not Chado tables we must manually prepare them
  117. $psql = "
  118. PREPARE del_url_alias_by_src (text) AS
  119. DELETE FROM {url_alias} WHERE src = \$1
  120. ";
  121. db_query($psql);
  122. $psql = "
  123. PREPARE ins_url_alias_nisrds (text, text) AS
  124. INSERT INTO url_alias (src, dst) VALUES (\$1, \$2)
  125. ";
  126. db_query($psql);
  127. // get the URL alias syntax string
  128. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  129. if (!$url_alias) {
  130. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  131. }
  132. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  133. // get the list of stocks that have been synced
  134. $sql = "SELECT * FROM {chado_stock}";
  135. $nodes = db_query($sql);
  136. while ($node = $nodes->fetchObject()) {
  137. // get the URL alias
  138. $src = "node/$node->nid";
  139. $dst = tripal_stock_get_stock_url($node, $url_alias);
  140. if (!$dst) {
  141. db_query('DEALLOCATE "del_url_alias_by_src"');
  142. db_query('DEALLOCATE "ins_url_alias_nisrds"');
  143. db_query("ROLLBACK");
  144. return;
  145. }
  146. // if the src and dst is the same (the URL alias couldn't be set)
  147. // then skip to the next one. There's nothing we can do about this one.
  148. if($src == $dst) {
  149. continue;
  150. }
  151. // remove any previous alias and then add the new one
  152. $success = db_query("EXECUTE del_url_alias_by_src(':src')", array(':src' => $src));
  153. if (!$success) {
  154. db_query('DEALLOCATE "del_url_alias_by_src"');
  155. db_query('DEALLOCATE "ins_url_alias_nisrds"');
  156. db_query("ROLLBACK");
  157. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Failed Removing URL Alias: %src", array('%src' => $src));
  158. return;
  159. }
  160. $success = db_query("EXECUTE ins_url_alias_nisrds(:src, :dst)", array(':src' => $src, ':dst' => $dst));
  161. if (!$success) {
  162. db_query('DEALLOCATE "del_url_alias_by_src"');
  163. db_query('DEALLOCATE "ins_url_alias_nisrds"');
  164. db_query("ROLLBACK");
  165. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Failed Adding URL Alias: %dst", array('%dst' => $dst));
  166. return;
  167. }
  168. // update the job status every 1% stocks
  169. if ($job and $num_set % $num_per_interval == 0) {
  170. $percent = ($num_set / $num_nodes) * 100;
  171. tripal_set_job_progress($job, intval($percent));
  172. $percent = sprintf("%.2f", $percent);
  173. print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  174. }
  175. $num_set++;
  176. }
  177. $percent = ($num_set / $num_nodes) * 100;
  178. tripal_set_job_progress($job, intval($percent));
  179. $percent = sprintf("%.2f", $percent);
  180. print "Setting URLs (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
  181. print "\nDone. Set " . number_format($num_set) . " URLs\n";
  182. // unprepare the statements
  183. db_query('DEALLOCATE "del_url_alias_by_src"');
  184. db_query('DEALLOCATE "ins_url_alias_nisrds"');
  185. db_query("COMMIT");
  186. }
  187. /**
  188. *
  189. * @param $node
  190. * A node object containing at least the stock_id and nid
  191. * @param $url_alias
  192. * Optional. This should be the URL alias syntax string that contains
  193. * placeholders such as [id], [genus], [species], [name], [uniquename],
  194. * and [type]. These placeholders will be substituted for actual values.
  195. * If this parameter is not provided then the value of the
  196. * chado_stock_url_string Drupal variable will be used.
  197. */
  198. function tripal_stock_get_stock_url($node, $url_alias = NULL) {
  199. // get the starting URL alias
  200. if(!$url_alias) {
  201. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  202. if (!$url_alias) {
  203. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  204. }
  205. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  206. }
  207. // get the stock
  208. $values = array('stock_id' => $node->stock_id);
  209. $options = array('statement_name' => 'sel_stock_id');
  210. $stock = chado_select_record('stock', array('*'), $values, $options);
  211. if (!$stock) {
  212. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find stock when setting URL alias for stock: %id", array('%id' => $node->stock_id));
  213. return FALSE;
  214. }
  215. $stock = (object) $stock[0];
  216. // get the organism
  217. $values = array('organism_id' => $stock->organism_id);
  218. $options = array('statement_name' => 'sel_organism_id');
  219. $organism = chado_select_record('organism', array('*'), $values, $options);
  220. if (!$organism) {
  221. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find organism when setting URL alias for stock: %id", array('%id' => $node->stock_id));
  222. return FALSE;
  223. }
  224. $genus = preg_replace('/\s/', '_', strtolower($organism[0]->genus));
  225. $species = preg_replace('/\s/', '_', strtolower($organism[0]->species));
  226. // get the type
  227. $values = array('cvterm_id' => $stock->type_id);
  228. $options = array('statement_name' => 'sel_cvterm_id');
  229. $cvterm = chado_select_record('cvterm', array('name'), $values, $options);
  230. if (!$cvterm) {
  231. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find type when setting URL alias for stock: %id", array('%id' => $node->stock_id));
  232. return FALSE;
  233. }
  234. $type = preg_replace('/\s/', '_', $cvterm[0]->name);
  235. // now substitute in the values
  236. $url_alias = preg_replace('/\[id\]/', $stock->stock_id, $url_alias);
  237. $url_alias = preg_replace('/\[genus\]/', $genus, $url_alias);
  238. $url_alias = preg_replace('/\[species\]/', $species, $url_alias);
  239. $url_alias = preg_replace('/\[type\]/', $type, $url_alias);
  240. $url_alias = preg_replace('/\[name\]/', $stock->name, $url_alias);
  241. $url_alias = preg_replace('/\[uniquename\]/', $stock->uniquename, $url_alias);
  242. // the dst field of the url_alias table is only 128 characters long.
  243. // if this is the case then simply return the node URL, we can't set this one
  244. if (strlen($url_alias) > 128) {
  245. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot set alias longer than 128 characters: %alias.", array('%alias' => $url_alias));
  246. return "node/" . $node->nid;
  247. }
  248. return $url_alias;
  249. }