Fix a possible race condition in the PaintWeb DML code.
[moodle/mihaisucan.git] / search / update.php
blob1bd12274e9f91e5d7b6fb9af531e1e0b79216652
1 <?php
2 /**
3 * Global Search Engine for Moodle
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
12 * Index asynchronous updator
14 * Major chages in this review is passing the xxxx_db_names return to
15 * multiple arity to handle multiple document types modules
18 /**
19 * includes and requires
21 require_once('../config.php');
23 if (!defined('MOODLE_INTERNAL')) {
24 die('Direct access to this script is forbidden.'); /// It must be included from the cron script
27 /// makes inclusions of the Zend Engine more reliable
28 ini_set('include_path', $CFG->dirroot.DIRECTORY_SEPARATOR.'search'.PATH_SEPARATOR.ini_get('include_path'));
30 require_once($CFG->dirroot.'/search/lib.php');
31 require_once($CFG->dirroot.'/search/indexlib.php');
33 /// checks global search activation
35 // require_login();
37 if (empty($CFG->enableglobalsearch)) {
38 error(get_string('globalsearchdisabled', 'search'));
42 Obsolete with the MOODLE INTERNAL check
43 if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
44 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
48 try {
49 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
50 } catch(LuceneException $e) {
51 mtrace("Could not construct a valid index. Maybe the first indexation was never made, or files might be corrupted. Run complete indexation again.");
52 return;
54 $dbcontrol = new IndexDBControl();
55 $update_count = 0;
56 $indexdate = 0 + @$CFG->search_indexer_update_date;
57 $startupdatedate = time();
59 /// indexing changed resources
61 mtrace("Starting index update (updates)...\n");
63 if ($mods = search_collect_searchables(false, true)){
65 foreach ($mods as $mod) {
66 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
67 $get_document_function = $mod->name.'_single_document';
68 $delete_function = $mod->name.'_delete';
69 $db_names_function = $mod->name.'_db_names';
70 $updates = array();
72 if (file_exists($class_file)) {
73 require_once($class_file);
75 //if both required functions exist
76 if (function_exists($delete_function) and function_exists($db_names_function) and function_exists($get_document_function)) {
77 mtrace("Checking $mod->name module for updates.");
78 $valuesArray = $db_names_function();
79 if ($valuesArray){
80 foreach($valuesArray as $values){
82 $where = (!empty($values[5])) ? 'AND ('.$values[5].')' : '';
83 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " AND itemtype = '{$values[4]}' " : '' ;
85 //TODO: check 'in' syntax with other RDBMS' (add and update.php as well)
86 $table = SEARCH_DATABASE_TABLE;
87 $query = "
88 SELECT
89 docid,
90 itemtype
91 FROM
92 {$CFG->prefix}{$table}
93 WHERE
94 doctype = '{$values[1]}'
95 $itemtypes
97 $docIds = get_records_sql_menu($query, array($mod->name));
98 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
100 $query = "
101 SELECT
102 id,
103 {$values[0]} as docid
104 FROM
105 {$CFG->prefix}{$values[1]}
106 WHERE
107 {$values[3]} > {$indexdate} AND
108 id IN ('{$docIdList}')
109 $where
111 $records = get_records_sql($query);
112 if (is_array($records)) {
113 foreach($records as $record) {
114 $updates[] = $delete_function($record->docid, $docIds[$record->docid]);
119 foreach ($updates as $update) {
120 ++$update_count;
122 //delete old document
123 $doc = $index->find("+docid:{$update->id} +doctype:{$mod->name} +itemtype:{$update->itemtype}");
125 //get the record, should only be one
126 foreach ($doc as $thisdoc) {
127 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
128 $dbcontrol->delDocument($thisdoc);
129 $index->delete($thisdoc->id);
132 //add new modified document back into index
133 if (!$add = $get_document_function($update->id, $update->itemtype)){
134 // ignore on errors
135 continue;
138 //object to insert into db
139 $dbid = $dbcontrol->addDocument($add);
141 //synchronise db with index
142 $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
143 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
144 $index->addDocument($add);
147 else{
148 mtrace("No types to update.\n");
150 mtrace("Finished $mod->name.\n");
156 //commit changes
157 $index->commit();
159 //update index date
160 set_config("search_indexer_update_date", $startupdatedate);
162 mtrace("Finished $update_count updates");