[ Index ]

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

title

Body

[close]

/search/ -> delete.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 index cleaner
  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  
  25      require_login();
  26      
  27      if (empty($CFG->enableglobalsearch)) {
  28          error(get_string('globalsearchdisabled', 'search'));
  29      }
  30      
  31      if (!isadmin()) {
  32          error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
  33      } //if
  34      
  35      //check for php5 (lib.php)
  36      if (!search_check_php5()) {
  37          $phpversion = phpversion();
  38          mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
  39          exit(0);
  40      }
  41      
  42      require_once("$CFG->dirroot/search/indexlib.php");
  43      
  44      $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
  45      $dbcontrol = new IndexDBControl();
  46      $deletion_count = 0;
  47      $startcleantime = time();
  48      
  49      mtrace('Starting clean-up of removed records...');
  50      mtrace('Index size before: '.$CFG->search_index_size."\n");
  51      
  52      if ($mods = get_records_select('modules')) {
  53          $mods = array_merge($mods, search_get_additional_modules());
  54          
  55          foreach ($mods as $mod) {
  56              //build function names
  57              $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
  58              $delete_function = $mod->name.'_delete';
  59              $db_names_function = $mod->name.'_db_names';
  60              $deletions = array();
  61              
  62              if (file_exists($class_file)) {
  63                  require_once($class_file);
  64                  
  65                  //if both required functions exist
  66                  if (function_exists($delete_function) and function_exists($db_names_function)) {
  67                      mtrace("Checking $mod->name module for deletions.");
  68                      $valuesArray = $db_names_function();
  69                      if ($valuesArray){
  70                          foreach($valuesArray as $values){
  71                             $where = (isset($values[5])) ? 'WHERE '.$values[5] : '';
  72                             $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " itemtype = '{$values[4]}' AND " : '' ;
  73                             $query = "
  74                                  SELECT 
  75                                      id,
  76                                      {$values[0]}
  77                                  FROM 
  78                                      {$CFG->prefix}{$values[1]}
  79                                      $where
  80                              ";
  81                              $docIds = get_records_sql($query);
  82                              $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
  83                              
  84                              $table = SEARCH_DATABASE_TABLE;
  85                              $query = "
  86                                  SELECT 
  87                                      id, 
  88                                      docid 
  89                                  FROM 
  90                                      {$CFG->prefix}{$table}
  91                                  WHERE 
  92                                      doctype = '{$mod->name}' AND 
  93                                      $itemtypes
  94                                      docid not in ('{$docIdList}')
  95                              ";
  96                              $records = get_records_sql($query);
  97                              
  98                              // build an array of all the deleted records
  99                              if (is_array($records)) {
 100                                  foreach($records as $record) {
 101                                      $deletions[] = $delete_function($record->docid, $values[4]);
 102                                  }
 103                              }
 104                          }
 105                          
 106                          foreach ($deletions as $delete) {
 107                              // find the specific document in the index, using it's docid and doctype as keys
 108                              $doc = $index->find("+docid:{$delete->id} +doctype:$mod->name +itemtype:{$delete->itemtype}");
 109                              
 110                              // get the record, should only be one
 111                              foreach ($doc as $thisdoc) {
 112                                  ++$deletion_count;
 113                                  mtrace("  Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
 114                                  
 115                                  //remove it from index and database table
 116                                  $dbcontrol->delDocument($thisdoc);
 117                                  $index->delete($thisdoc->id);
 118                              }
 119                          }
 120                      }
 121                      else{
 122                          mtrace("No types to delete.\n");
 123                      }
 124                      mtrace("Finished $mod->name.\n");
 125                  }
 126              }
 127          }
 128      }
 129      
 130  /// commit changes
 131  
 132      $index->commit();
 133      
 134  /// update index date and index size
 135  
 136      set_config("search_indexer_cleanup_date", $startcleantime);
 137      set_config("search_index_size", (int)$CFG->search_index_size - (int)$deletion_count);
 138      
 139      mtrace("Finished $deletion_count removals.");
 140      mtrace('Index size after: '.$index->count());
 141  
 142  ?>


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