Fix a possible race condition in the PaintWeb DML code.
[moodle/mihaisucan.git] / search / add.php
blobfcd82be1230e6faceccc9640067059321dfca41e
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 * Asynchronous adder for new indexable contents
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");
46 */
48 /// check for php5 (lib.php)
50 try {
51 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
52 } catch(LuceneException $e) {
53 mtrace("Could not construct a valid index. Maybe the first indexation was never made, or files might be corrupted. Run complete indexation again.");
54 return;
56 $dbcontrol = new IndexDBControl();
57 $addition_count = 0;
58 $startindextime = time();
60 $indexdate = $CFG->search_indexer_run_date;
62 mtrace('Starting index update (additions)...');
63 mtrace('Index size before: '.$CFG->search_index_size."\n");
65 /// get all modules
66 if ($mods = search_collect_searchables(false, true)){
68 /// append virtual modules onto array
70 foreach ($mods as $mod) {
71 //build include file and function names
72 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
73 $db_names_function = $mod->name.'_db_names';
74 $get_document_function = $mod->name.'_single_document';
75 $get_newrecords_function = $mod->name.'_new_records';
76 $additions = array();
78 if (file_exists($class_file)) {
79 require_once($class_file);
81 //if both required functions exist
82 if (function_exists($db_names_function) and function_exists($get_document_function)) {
83 mtrace("Checking $mod->name module for additions.");
84 $valuesArray = $db_names_function();
85 if ($valuesArray){
86 foreach($valuesArray as $values){
87 $where = (!empty($values[5])) ? 'AND ('.$values[5].')' : '';
88 $itemtypes = ($values[4] != '*' && $values[4] != 'any') ? " AND itemtype = '{$values[4]}' " : '' ;
90 //select records in MODULE table, but not in SEARCH_DATABASE_TABLE
91 $table = SEARCH_DATABASE_TABLE;
92 $query = "
93 SELECT
94 docid,
95 itemtype
96 FROM
97 {$CFG->prefix}{$table}
98 WHERE
99 doctype = '{$values[1]}'
100 $itemtypes
102 $docIds = get_records_sql_menu($query, array($mod->name));
103 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
105 $query = "
106 SELECT id,
107 {$values[0]} as docid
108 FROM
109 {$CFG->prefix}{$values[1]}
110 WHERE
111 id NOT IN ('{$docIdList}') AND
112 {$values[2]} > {$indexdate}
113 $where
115 $records = get_records_sql($query);
117 // foreach record, build a module specific search document using the get_document function
118 if (is_array($records)) {
119 foreach($records as $record) {
120 $add = $get_document_function($record->docid, $values[4]);
121 // some documents may not be indexable
122 if ($add)
123 $additions[] = $add;
128 // foreach document, add it to the index and database table
129 foreach ($additions as $add) {
130 ++$addition_count;
132 // object to insert into db
133 $dbid = $dbcontrol->addDocument($add);
135 // synchronise db with index
136 $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
138 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
140 $index->addDocument($add);
143 else{
144 mtrace("No types to add.\n");
146 mtrace("Finished $mod->name.\n");
152 /// commit changes
154 $index->commit();
156 /// update index date and size
158 set_config("search_indexer_run_date", $startindextime);
159 set_config("search_index_size", (int)$CFG->search_index_size + (int)$addition_count);
161 /// print some additional info
163 mtrace("Added $addition_count documents.");
164 mtrace('Index size after: '.$index->count());