fixes MDL-14775
[moodle.git] / search / update.php
CommitLineData
6f1b1da1 1<?php
2f338ab5 2/**
3* Global Search Engine for Moodle
3319ef85 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
2f338ab5 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
3319ef85 18/**
19* includes and requires
20*/
2f338ab5 21require_once('../config.php');
22require_once("$CFG->dirroot/search/lib.php");
23
3319ef85 24/// checks global search activation
2f338ab5 25
3319ef85 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
3319ef85 36 require_once("$CFG->dirroot/search/indexlib.php");
37
38 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
39 $dbcontrol = new IndexDBControl();
40 $update_count = 0;
41 $indexdate = $CFG->search_indexer_update_date;
42 $startupdatedate = time();
2f338ab5 43
3319ef85 44/// indexing changed resources
2f338ab5 45
3319ef85 46 mtrace("Starting index update (updates)...\n");
47
48 if ($mods = get_records_select('modules')) {
49 $mods = array_merge($mods, search_get_additional_modules());
2f338ab5 50
3319ef85 51 foreach ($mods as $mod) {
52 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
53 $get_document_function = $mod->name.'_single_document';
54 $delete_function = $mod->name.'_delete';
55 $db_names_function = $mod->name.'_db_names';
56 $updates = array();
2f338ab5 57
3319ef85 58 if (file_exists($class_file)) {
59 require_once($class_file);
60
61 //if both required functions exist
62 if (function_exists($delete_function) and function_exists($db_names_function) and function_exists($get_document_function)) {
63 mtrace("Checking $mod->name module for updates.");
64 $valuesArray = $db_names_function();
65 if ($valuesArray){
66 foreach($valuesArray as $values){
2f338ab5 67
3319ef85 68 $where = (isset($values[5])) ? 'AND ('.$values[5].')' : '';
69 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " AND itemtype = '{$values[4]}' " : '' ;
70
71 //TODO: check 'in' syntax with other RDBMS' (add and update.php as well)
72 $table = SEARCH_DATABASE_TABLE;
73 $query = "
74 SELECT
75 docid,
76 itemtype
77 FROM
78 {$CFG->prefix}{$table}
79 WHERE
80 doctype = '{$mod->name}'
81 $itemtypes
82 ";
83 $docIds = get_records_sql_menu($query);
84 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
85
86 $query = "
87 SELECT
88 id,
89 {$values[0]} as docid
90 FROM
91 {$CFG->prefix}{$values[1]}
92 WHERE
93 {$values[3]} > {$indexdate} AND
94 id IN ('{$docIdList}')
95 $where
96 ";
97 $records = get_records_sql($query);
98 if (is_array($records)) {
99 foreach($records as $record) {
100 $updates[] = $delete_function($record->docid, $docIds[$record->docid]);
101 }
2f338ab5 102 }
3319ef85 103 }
2f338ab5 104
3319ef85 105 foreach ($updates as $update) {
106 ++$update_count;
107
108 //delete old document
109 $doc = $index->find("+docid:{$update->id} +doctype:{$mod->name} +itemtype:{$update->itemtype}");
110
111 //get the record, should only be one
112 foreach ($doc as $thisdoc) {
113 mtrace(" Delete: $thisdoc->title (database id = $thisdoc->dbid, index id = $thisdoc->id, moodle instance id = $thisdoc->docid)");
114 $dbcontrol->delDocument($thisdoc);
115 $index->delete($thisdoc->id);
116 }
117
118 //add new modified document back into index
119 $add = $get_document_function($update->id, $update->itemtype);
120
121 //object to insert into db
122 $dbid = $dbcontrol->addDocument($add);
123
124 //synchronise db with index
125 $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
126 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
127 $index->addDocument($add);
2f338ab5 128 }
adddd26d 129 } else {
3319ef85 130 mtrace("No types to update.\n");
131 }
132 mtrace("Finished $mod->name.\n");
133 }
2f338ab5 134 }
135 }
136 }
3319ef85 137
138 //commit changes
139 $index->commit();
140
141 //update index date
142 set_config("search_indexer_update_date", $startupdatedate);
143
144 mtrace("Finished $update_count updates");
6f1b1da1 145
146?>