Tripal v1.0 (6.x-1.0)
|
00001 <?php 00008 // This script can be run as a stand-alone script to sync all the features from chado to drupal 00009 // 00010 // To index a single feature 00011 // -i feature_id 00012 // -n node_id 00013 // 00014 // To index all features 00015 // -i 0 00016 00017 $arguments = getopt("i:n:"); 00018 00019 if (isset($arguments['i'])) { 00020 $drupal_base_url = parse_url('http://www.example.com'); 00021 $_SERVER['HTTP_HOST'] = $drupal_base_url['host']; 00022 $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF']; 00023 $_SERVER['REMOTE_ADDR'] = NULL; 00024 $_SERVER['REQUEST_METHOD'] = NULL; 00025 00026 require_once 'includes/bootstrap.inc'; 00027 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 00028 00029 $feature_id = $arguments['i']; 00030 $nid = $arguments['n']; 00031 00032 # print "\n"; 00033 # print "feature id is $feature_id\n"; 00034 # print "nid is $nid\n"; 00035 # print "\n"; 00036 00037 if ($feature_id > 0) { 00038 # print "indexing feature $feature_id\n"; 00039 // We register a shutdown function to ensure that the nodes 00040 // that are indexed will have proper entries in the search_totals 00041 // table. Without these entries, the searching doesn't work 00042 // properly. This function may run for quite a while since 00043 // it must calculate the sum of the scores of all entries in 00044 // the search_index table. In the case of common words like 00045 // 'contig', this will take quite a while 00046 register_shutdown_function('search_update_totals'); 00047 tripal_feature_index_feature($feature_id, $nid); 00048 } 00049 else{ 00050 print "indexing all features...\n"; 00051 tripal_features_reindex(0); 00052 } 00053 00054 } 00055 00061 function tripal_features_reindex($max_sync, $job_id = NULL) { 00062 $i = 0; 00063 00064 // We register a shutdown function to ensure that the nodes 00065 // that are indexed will have proper entries in the search_totals 00066 // table. Without these entries, the searching doesn't work 00067 // properly. This function may run for quite a while since 00068 // it must calculate the sum of the scores of all entries in 00069 // the search_index table. In the case of common words like 00070 // 'contig', this will take quite a while 00071 register_shutdown_function('search_update_totals'); 00072 00073 // use this SQL statement to get the features that we're going to index. This 00074 // SQL statement is derived from the hook_search function in the Drupal API. 00075 // Essentially, this is the SQL statement that finds all nodes that need 00076 // reindexing, but adjusted to include the chado_feature 00077 $sql = "SELECT N.nid, N.title, CF.feature_id ". 00078 "FROM {node} N ". 00079 " INNER JOIN chado_feature CF ON CF.nid = N.nid "; 00080 $results = db_query($sql); 00081 00082 // load into ids array 00083 $count = 0; 00084 $chado_features = array(); 00085 while ($chado_feature = db_fetch_object($results)) { 00086 $chado_features[$count] = $chado_feature; 00087 $count++; 00088 } 00089 00090 // Iterate through features that need to be indexed 00091 $interval = intval($count * 0.01); 00092 if ($interval >= 0) { 00093 $interval = 1; 00094 } 00095 foreach ($chado_features as $chado_feature) { 00096 00097 // update the job status every 1% features 00098 if ($job_id and $i % $interval == 0) { 00099 $prog = intval(($i/$count)*100); 00100 tripal_job_set_progress($job_id, $prog); 00101 print "$prog%\n"; 00102 } 00103 00104 // sync only the max requested 00105 if ($max_sync and $i == $max_sync) { 00106 return ''; 00107 } 00108 $i++; 00109 00118 $cmd = "php " . drupal_get_path('module', 'tripal_feature') . "/indexFeatures.php "; 00119 $cmd .= "-i $chado_feature->feature_id -n $chado_feature->nid "; 00120 00121 # print "\t$cmd\n"; 00122 # print "\tfeature id is $chado_feature->feature_id\n"; 00123 # print "\tnid is $chado_feature->nid\n"; 00124 # print "\n"; 00125 00126 system($cmd); 00127 } 00128 00129 return ''; 00130 } 00131 00137 function tripal_feature_index_feature($feature_id, $nid) { 00138 #print "\tfeature $feature_id nid $nid\n"; 00139 // return if we haven't been provided with a feature_id 00140 if (!$feature_id) { 00141 return 0; 00142 } 00143 00144 // if we only have a feature_id then let's find a corresponding 00145 // node. If we can't find a node then return. 00146 if (!$nid) { 00147 $nsql = "SELECT N.nid,N.title FROM {chado_feature} CF ". 00148 " INNER JOIN {node} N ON N.nid = CF.nid ". 00149 "WHERE CF.feature_id = %d"; 00150 $node = db_fetch_object(db_query($nsql, $feature_id)); 00151 if (!$node) { 00152 return 0; 00153 } 00154 $node = node_load($node->nid); 00155 } 00156 else { 00157 $node = node_load($nid); 00158 } 00159 00160 // node load the noad, the comments and the taxonomy and 00161 // index 00162 $node->build_mode = NODE_BUILD_SEARCH_INDEX; 00163 $node = node_build_content($node, FALSE, FALSE); 00164 $node->body = drupal_render($node->content); 00165 node_invoke_nodeapi($node, 'view', FALSE, FALSE); 00166 // $node->body .= module_invoke('comment', 'nodeapi', $node, 'update index'); 00167 // $node->body .= module_invoke('taxonomy','nodeapi', $node, 'update index'); 00168 // print "$node->title: $node->body\n"; 00169 search_index($node->nid, 'node', $node->body); 00170 00171 # $mem = memory_get_usage(TRUE); 00172 # $mb = $mem/1048576; 00173 # print "$mb mb\n"; 00174 00175 return 1; 00176 } 00177