indexFeatures.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. // This script can be run as a stand-alone script to sync all the features from chado to drupal
  3. //
  4. // To index a single feature
  5. // -i feature_id
  6. // -n node_id
  7. //
  8. // To index all features
  9. // -i 0
  10. $arguments = getopt("i:n:");
  11. if(isset($arguments['i'])){
  12. $drupal_base_url = parse_url('http://www.example.com');
  13. $_SERVER['HTTP_HOST'] = $drupal_base_url['host'];
  14. $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
  15. $_SERVER['REMOTE_ADDR'] = NULL;
  16. $_SERVER['REQUEST_METHOD'] = NULL;
  17. require_once 'includes/bootstrap.inc';
  18. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  19. $feature_id = $arguments['i'];
  20. $nid = $arguments['n'];
  21. # print "\n";
  22. # print "feature id is $feature_id\n";
  23. # print "nid is $nid\n";
  24. # print "\n";
  25. if($feature_id > 0){
  26. # print "indexing feature $feature_id\n";
  27. // We register a shutdown function to ensure that the nodes
  28. // that are indexed will have proper entries in the search_totals
  29. // table. Without these entries, the searching doesn't work
  30. // properly. This function may run for quite a while since
  31. // it must calculate the sum of the scores of all entries in
  32. // the search_index table. In the case of common words like
  33. // 'contig', this will take quite a while
  34. register_shutdown_function('search_update_totals');
  35. tripal_feature_index_feature($feature_id, $nid);
  36. }
  37. else{
  38. print "indexing all features...\n";
  39. tripal_features_reindex(0);
  40. }
  41. }
  42. /**
  43. *
  44. *
  45. * @ingroup tripal_feature
  46. */
  47. function tripal_features_reindex ($max_sync,$job_id = NULL){
  48. $i = 0;
  49. // We register a shutdown function to ensure that the nodes
  50. // that are indexed will have proper entries in the search_totals
  51. // table. Without these entries, the searching doesn't work
  52. // properly. This function may run for quite a while since
  53. // it must calculate the sum of the scores of all entries in
  54. // the search_index table. In the case of common words like
  55. // 'contig', this will take quite a while
  56. register_shutdown_function('search_update_totals');
  57. // use this SQL statement to get the features that we're going to index. This
  58. // SQL statement is derived from the hook_search function in the Drupal API.
  59. // Essentially, this is the SQL statement that finds all nodes that need
  60. // reindexing, but adjusted to include the chado_feature
  61. $sql = "SELECT N.nid, N.title, CF.feature_id ".
  62. "FROM {node} N ".
  63. " INNER JOIN chado_feature CF ON CF.nid = N.nid ";
  64. $results = db_query($sql);
  65. // load into ids array
  66. $count = 0;
  67. $chado_features = array();
  68. while($chado_feature = db_fetch_object($results)){
  69. $chado_features[$count] = $chado_feature;
  70. $count++;
  71. }
  72. // Iterate through features that need to be indexed
  73. $interval = intval($count * 0.01);
  74. if($interval >= 0){
  75. $interval = 1;
  76. }
  77. foreach($chado_features as $chado_feature){
  78. // update the job status every 1% features
  79. if($job_id and $i % $interval == 0){
  80. $prog = intval(($i/$count)*100);
  81. tripal_job_set_progress($job_id,$prog);
  82. print "$prog\n";
  83. }
  84. // sync only the max requested
  85. if($max_sync and $i == $max_sync){
  86. return '';
  87. }
  88. $i++;
  89. # tripal_feature_index_feature ($chado_feature->feature_id,$chado_feature->nid);
  90. # parsing all the features can cause memory overruns
  91. # we are not sure why PHP does not clean up the memory as it goes
  92. # to avoid this problem we will call this script through an
  93. # independent system call
  94. $cmd = "php " . drupal_get_path('module', 'tripal_feature') . "/indexFeatures.php ";
  95. $cmd .= "-i $chado_feature->feature_id -n $chado_feature->nid ";
  96. # print "\t$cmd\n";
  97. # print "\tfeature id is $chado_feature->feature_id\n";
  98. # print "\tnid is $chado_feature->nid\n";
  99. # print "\n";
  100. system($cmd);
  101. }
  102. return '';
  103. }
  104. /**
  105. *
  106. *
  107. * @ingroup tripal_feature
  108. */
  109. function tripal_feature_index_feature ($feature_id,$nid){
  110. #print "\tfeature $feature_id nid $nid\n";
  111. // return if we haven't been provided with a feature_id
  112. if(!$feature_id){
  113. return 0;
  114. }
  115. // if we only have a feature_id then let's find a corresponding
  116. // node. If we can't find a node then return.
  117. if(!$nid){
  118. $nsql = "SELECT N.nid,N.title FROM {chado_feature} CF ".
  119. " INNER JOIN {node} N ON N.nid = CF.nid ".
  120. "WHERE CF.feature_id = %d";
  121. $node = db_fetch_object(db_query($nsql,$feature_id));
  122. if(!$node){
  123. return 0;
  124. }
  125. $node = node_load($node->nid);
  126. } else {
  127. $node = node_load($nid);
  128. }
  129. // node load the noad, the comments and the taxonomy and
  130. // index
  131. $node->build_mode = NODE_BUILD_SEARCH_INDEX;
  132. $node = node_build_content($node, FALSE, FALSE);
  133. $node->body = drupal_render($node->content);
  134. node_invoke_nodeapi($node, 'view', FALSE, FALSE);
  135. // $node->body .= module_invoke('comment', 'nodeapi', $node, 'update index');
  136. // $node->body .= module_invoke('taxonomy','nodeapi', $node, 'update index');
  137. // print "$node->title: $node->body\n";
  138. search_index($node->nid,'node',$node->body);
  139. # $mem = memory_get_usage(TRUE);
  140. # $mb = $mem/1048576;
  141. # print "$mb mb\n";
  142. return 1;
  143. }
  144. ?>