MDL-15942 - separate data escaped for database entry from unescaped data
[moodle-linuxchix.git] / search / add.php
bloba1edd3ebd8a19e8594e7b223438a3b2e08c46df4
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');
22 require_once("$CFG->dirroot/search/lib.php");
24 /// checks global search activation
26 require_login();
28 if (empty($CFG->enableglobalsearch)) {
29 error(get_string('globalsearchdisabled', 'search'));
32 if (!isadmin()) {
33 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
36 /// check for php5 (lib.php)
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);
44 require_once("$CFG->dirroot/search/indexlib.php");
46 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
47 $dbcontrol = new IndexDBControl();
48 $addition_count = 0;
49 $startindextime = time();
51 $indexdate = $CFG->search_indexer_run_date;
53 mtrace('Starting index update (additions)...');
54 mtrace('Index size before: '.$CFG->search_index_size."\n");
56 /// get all modules
57 if ($mods = get_records_select('modules')) {
59 /// append virtual modules onto array
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();
70 if (file_exists($class_file)) {
71 require_once($class_file);
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]}' " : '' ;
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
94 $docIds = get_records_sql_menu($query);
95 $docIdList = ($docIds) ? implode("','", array_keys($docIds)) : '' ;
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
107 $records = get_records_sql($query);
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;
120 // foreach document, add it to the index and database table
121 foreach ($additions as $add) {
122 ++$addition_count;
124 // object to insert into db
125 $dbid = $dbcontrol->addDocument($add);
127 // synchronise db with index
128 $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
130 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
132 $index->addDocument($add);
135 else{
136 mtrace("No types to add.\n");
138 mtrace("Finished $mod->name.\n");
144 /// commit changes
146 $index->commit();
148 /// update index date and size
150 set_config("search_indexer_run_date", $startindextime);
151 set_config("search_index_size", (int)$CFG->search_index_size + (int)$addition_count);
153 /// print some additional info
155 mtrace("Added $addition_count documents.");
156 mtrace('Index size after: '.$index->count());