[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/search/ -> update.php (source)

   1  <?php
   2  /**
   3  * Global Search Engine for Moodle
   4  *
   5  * @package search
   6  * @category core
   7  * @subpackage search_engine
   8  * @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
   9  * @date 2008/03/31
  10  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  11  *
  12  * Index asynchronous updator
  13  *
  14  * Major chages in this review is passing the xxxx_db_names return to
  15  * multiple arity to handle multiple document types modules
  16  */
  17  
  18  /**
  19  * includes and requires
  20  */
  21  require_once ('../config.php');
  22  require_once("$CFG->dirroot/search/lib.php");
  23  
  24  /// checks global search activation
  25  
  26      require_login();
  27      
  28      if (empty($CFG->enableglobalsearch)) {
  29          error(get_string('globalsearchdisabled', 'search'));
  30      }
  31      
  32      if (!isadmin()) {
  33          error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
  34      } 
  35      
  36  /// check for php5 (lib.php)
  37  
  38      if (!search_check_php5()) {
  39          $phpversion = phpversion();
  40          mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
  41          exit(0);
  42      } 
  43      
  44      require_once("$CFG->dirroot/search/indexlib.php");
  45      
  46      $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
  47      $dbcontrol = new IndexDBControl();
  48      $update_count = 0;
  49      $indexdate = $CFG->search_indexer_update_date;
  50      $startupdatedate = time();
  51  
  52  /// indexing changed resources
  53      
  54      mtrace("Starting index update (updates)...\n");
  55      
  56      if ($mods = get_records_select('modules')) {
  57          $mods = array_merge($mods, search_get_additional_modules());
  58          
  59          foreach ($mods as $mod) {
  60              $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
  61              $get_document_function = $mod->name.'_single_document';
  62              $delete_function = $mod->name.'_delete';
  63              $db_names_function = $mod->name.'_db_names';
  64              $updates = array();
  65              
  66              if (file_exists($class_file)) {
  67                  require_once($class_file);
  68                  
  69                  //if both required functions exist
  70                  if (function_exists($delete_function) and function_exists($db_names_function) and function_exists($get_document_function)) {
  71                      mtrace("Checking $mod->name module for updates.");
  72                      $valuesArray = $db_names_function();
  73                      if ($valuesArray){
  74                          foreach($valuesArray as $values){
  75                          
  76                              $where = (isset($values[5])) ? 'AND ('.$values[5].')' : '';
  77                              $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " AND itemtype = '{$values[4]}' " : '' ;
  78      
  79                              //TODO: check 'in' syntax with other RDBMS' (add and update.php as well)
  80                              $table = SEARCH_DATABASE_TABLE;
  81                              $query = "
  82                                  SELECT 
  83                                      docid,
  84                                      itemtype
  85                                  FROM 
  86                                      {$CFG->prefix}{$table}
  87                                  WHERE
  88                                      doctype = '{$mod->name}'
  89                                      $itemtypes
  90                              ";
  91                              $docIds = get_records_sql_menu($query);
  92                              $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
  93                              
  94                              $query = "
  95                                  SELECT 
  96                                      id, 
  97                                      {$values[0]} as docid
  98                                  FROM 
  99                                      {$CFG->prefix}{$values[1]} 
 100                                  WHERE 
 101                                      {$values[3]} > {$indexdate} AND 
 102                                      id IN ('{$docIdList}')
 103                                      $where
 104                              ";
 105                              $records = get_records_sql($query);
 106                              if (is_array($records)) {
 107                                  foreach($records as $record) {
 108                                      $updates[] = $delete_function($record->docid, $docIds[$record->docid]);
 109                                  } 
 110                              } 
 111                          }
 112                          
 113                          foreach ($updates as $update) {
 114                              ++$update_count;
 115                              
 116                              //delete old document
 117                              $doc = $index->find("+docid:{$update->id} +doctype:{$mod->name} +itemtype:{$update->itemtype}");
 118                              
 119                              //get the record, should only be one
 120                              foreach ($doc as $thisdoc) {
 121                                  mtrace("  Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
 122                                  $dbcontrol->delDocument($thisdoc);
 123                                  $index->delete($thisdoc->id);
 124                              } 
 125                              
 126                              //add new modified document back into index
 127                              $add = $get_document_function($update->id, $update->itemtype);
 128                              
 129                              //object to insert into db
 130                              $dbid = $dbcontrol->addDocument($add);
 131                              
 132                              //synchronise db with index
 133                              $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
 134                              mtrace("  Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
 135                              $index->addDocument($add);
 136                          } 
 137                      }
 138                      else{
 139                          mtrace("No types to update.\n");
 140                      }
 141                      mtrace("Finished $mod->name.\n");
 142                  } 
 143              } 
 144          } 
 145      } 
 146      
 147      //commit changes
 148      $index->commit();
 149      
 150      //update index date
 151      set_config("search_indexer_update_date", $startupdatedate);
 152      
 153      mtrace("Finished $update_count updates");
 154  
 155  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7