[ Index ]

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

title

Body

[close]

/search/ -> add.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  * Asynchronous adder for new indexable contents
  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      $addition_count = 0;
  49      $startindextime = time();
  50      
  51      $indexdate = $CFG->search_indexer_run_date;
  52      
  53      mtrace('Starting index update (additions)...');
  54      mtrace('Index size before: '.$CFG->search_index_size."\n");
  55      
  56  /// get all modules
  57      if ($mods = get_records_select('modules')) {
  58      
  59  /// append virtual modules onto array
  60  
  61      $mods = array_merge($mods, search_get_additional_modules());
  62          foreach ($mods as $mod) {
  63              //build include file and function names
  64              $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
  65              $db_names_function = $mod->name.'_db_names';
  66              $get_document_function = $mod->name.'_single_document';
  67              $get_newrecords_function = $mod->name.'_new_records';
  68              $additions = array();
  69              
  70              if (file_exists($class_file)) {
  71                  require_once($class_file);
  72                  
  73                  //if both required functions exist
  74                  if (function_exists($db_names_function) and function_exists($get_document_function)) {
  75                      mtrace("Checking $mod->name module for additions.");
  76                      $valuesArray = $db_names_function();
  77                      if ($valuesArray){
  78                          foreach($valuesArray as $values){
  79                              $where = (isset($values[5])) ? 'AND ('.$values[5].')' : '';
  80                              $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " AND itemtype = '{$values[4]}' " : '' ;
  81                              
  82                              //select records in MODULE table, but not in SEARCH_DATABASE_TABLE
  83                              $table = SEARCH_DATABASE_TABLE;
  84                              $query = "
  85                                  SELECT 
  86                                      docid,
  87                                      itemtype 
  88                                  FROM 
  89                                      {$CFG->prefix}{$table}
  90                                  WHERE 
  91                                      doctype = '{$mod->name}'
  92                                      $itemtypes
  93                              ";
  94                              $docIds = get_records_sql_menu($query);
  95                              $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
  96                              
  97                              $query =  "
  98                                  SELECT id, 
  99                                      {$values[0]} as docid 
 100                                  FROM 
 101                                      {$CFG->prefix}{$values[1]} 
 102                                  WHERE 
 103                                      id NOT IN ('{$docIdList}') and 
 104                                      {$values[2]} > {$indexdate}
 105                                      $where
 106                              ";
 107                              $records = get_records_sql($query);
 108                              
 109                              // foreach record, build a module specific search document using the get_document function
 110                              if (is_array($records)) {
 111                                  foreach($records as $record) {
 112                                      $add = $get_document_function($record->docid, $values[4]);
 113                                      // some documents may not be indexable
 114                                      if ($add)
 115                                          $additions[] = $add;
 116                                  } 
 117                              } 
 118                          } 
 119                          
 120                          // foreach document, add it to the index and database table
 121                          foreach ($additions as $add) {
 122                              ++$addition_count;
 123                              
 124                              // object to insert into db
 125                              $dbid = $dbcontrol->addDocument($add);
 126                              
 127                              // synchronise db with index
 128                              $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
 129                              
 130                              mtrace("  Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
 131                              
 132                              $index->addDocument($add);
 133                          } 
 134                      }
 135                      else{
 136                          mtrace("No types to add.\n");
 137                      }
 138                      mtrace("Finished $mod->name.\n");
 139                  } 
 140              } 
 141          } 
 142      } 
 143      
 144  /// commit changes
 145  
 146      $index->commit();
 147      
 148  /// update index date and size
 149  
 150      set_config("search_indexer_run_date", $startindextime);
 151      set_config("search_index_size", (int)$CFG->search_index_size + (int)$addition_count);
 152      
 153  /// print some additional info
 154  
 155      mtrace("Added $addition_count documents.");
 156      mtrace('Index size after: '.$index->count());
 157  
 158  ?>


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