indexFeatures.inc

@todo Add file header description

File

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