Automatic installer.php lang files by installer_builder (20080623)
[moodle.git] / search / add.php
blob285c9d948c8a7b1a58ac8654a00d70f015bc395f
1 <?php
3 require_once('../config.php');
4 require_once("$CFG->dirroot/search/lib.php");
6 require_login();
8 if (empty($CFG->enableglobalsearch)) {
9 error('Global searching is not enabled.');
12 if (!isadmin()) {
13 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
14 } //if
16 //check for php5 (lib.php)
17 if (!search_check_php5()) {
18 $phpversion = phpversion();
19 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version $phpversion)");
20 exit(0);
21 } //if
23 require_once("$CFG->dirroot/search/indexlib.php");
25 $index = new Zend_Search_Lucene(SEARCH_INDEX_PATH);
26 $dbcontrol = new IndexDBControl();
27 $addition_count = 0;
29 $indexdate = $CFG->search_indexer_run_date;
31 mtrace('<pre>Starting index update (additions)...');
32 mtrace('Index size before: '.$CFG->search_index_size."\n");
34 //get all modules
35 if ($mods = get_records_select('modules')) {
36 //append virtual modules onto array
37 $mods = array_merge($mods, search_get_additional_modules());
39 foreach ($mods as $mod) {
40 //build include file and function names
41 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
42 $db_names_function = $mod->name.'_db_names';
43 $get_document_function = $mod->name.'_single_document';
44 $additions = array();
46 if (file_exists($class_file)) {
47 require_once($class_file);
49 //if both required functions exist
50 if (function_exists($db_names_function) and function_exists($get_document_function)) {
51 mtrace("Checking $mod->name module for additions.");
52 $values = $db_names_function();
53 $where = (isset($values[4])) ? $values[4] : '';
55 //select records in MODULE table, but not in SEARCH_DATABASE_TABLE
56 $sql = "select id, ".$values[0]." as docid from ".$values[1].
57 " where id not in".
58 " (select docid from ".SEARCH_DATABASE_TABLE." where doctype like '$mod->name')".
59 " and ".$values[2]." > $indexdate".
60 " $where";
62 $records = get_records_sql($sql);
64 //foreach record, build a module specific search document using the get_document function
65 if (is_array($records)) {
66 foreach($records as $record) {
67 $additions[] = $get_document_function($record->id);
68 } //foreach
69 } //if
71 //foreach document, add it to the index and database table
72 foreach ($additions as $add) {
73 ++$addition_count;
75 //object to insert into db
76 $dbid = $dbcontrol->addDocument($add);
78 //synchronise db with index
79 $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
81 mtrace(" Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
83 $index->addDocument($add);
84 } //foreach
86 mtrace("Finished $mod->name.\n");
87 } //if
88 } //if
89 } //foreach
90 } //if
92 //commit changes
93 $index->commit();
95 //update index date and size
96 set_config("search_indexer_run_date", time());
97 set_config("search_index_size", (int)$CFG->search_index_size + (int)$addition_count);
99 //print some additional info
100 mtrace("Added $addition_count documents.");
101 mtrace('Index size after: '.$index->count().'</pre>');