3 * Look through each installed module's search document class file (/search/documents)
4 * for necessary search functions, and if they're present add the content to the index.
5 * Repeat this for blocks.
7 * Because the iterator/retrieval functions are now stored in /search/documents/mod_document.php,
8 * /mod/mod/lib.php doesn't have to be modified - and thus the search module becomes quite
9 * self-sufficient. URL's are now stored in the index, stopping us from needing to require
10 * the class files to generate a results page.
12 * Along with the index data, each document's summary gets stored in the database
13 * and synchronised to the index (flat file) via the primary key ('id') which is mapped
14 * to the 'db_id' field in the index
17 //this'll take some time, set up the environment
19 @ob_implicit_flush
(true);
22 require_once('../config.php');
23 require_once("$CFG->dirroot/search/lib.php");
25 //only administrators can index the moodle installation, because access to all pages is required
28 if (empty($CFG->enableglobalsearch
)) {
29 error('Global searching is not enabled.');
33 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
36 //confirmation flag to prevent accidental reindexing (indexersplash.php is the correct entry point)
37 $sure = strtolower(optional_param('areyousure', '', PARAM_ALPHA
));
40 mtrace("<pre>Sorry, you need to confirm indexing via <a href='indexersplash.php'>indexersplash.php</a>"
41 .". (<a href='index.php'>Back to query page</a>).</pre>");
46 //check for php5 (lib.php)
47 if (!search_check_php5()) {
48 $phpversion = phpversion();
49 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version $phpversion)");
53 //php5 found, continue including php5-only files
54 //require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
55 require_once("$CFG->dirroot/search/indexlib.php");
57 mtrace('<pre>Server Time: '.date('r',time())."\n");
59 if ($CFG->search_indexer_busy
== '1') {
60 //means indexing was not finished previously
61 mtrace("Warning: Indexing was not successfully completed last time, restarting.\n");
65 set_config('search_indexer_busy', '1');
68 $index_path = SEARCH_INDEX_PATH
;
69 $index_db_file = "$CFG->dirroot/search/db/$CFG->dbtype.sql";
70 $dbcontrol = new IndexDBControl();
72 //setup directory in data root
73 if (!file_exists($index_path)) {
74 mtrace("Data directory ($index_path) does not exist, attempting to create.");
75 if (!mkdir($index_path)) {
76 search_pexit("Error creating data directory at: $index_path. Please correct.");
78 mtrace("Directory successfully created.");
81 mtrace("Using $index_path as data directory.");
84 $index = new Zend_Search_Lucene($index_path, true);
86 if (!$dbcontrol->checkDB()) {
87 search_pexit("Database error. Please check settings/files.");
92 mtrace("Starting activity modules\n");
94 //the presence of the required search functions -
96 // * mod_get_content_for_index
97 //are the sole basis for including a module in the index at the moment.
99 if ($mods = get_records_select('modules' /*'index this module?' where statement*/)) {
100 //add virtual modules onto the back of the array
101 $mods = array_merge($mods, search_get_additional_modules());
103 foreach ($mods as $mod) {
104 $class_file = $CFG->dirroot
.'/search/documents/'.$mod->name
.'_document.php';
106 if (file_exists($class_file)) {
107 include_once($class_file);
109 //build function names
110 $iter_function = $mod->name
.'_iterator';
111 $index_function = $mod->name
.'_get_content_for_index';
116 if (function_exists($index_function) && function_exists($iter_function)) {
117 mtrace("Processing module function $index_function ...");
119 foreach ($iter_function() as $i) {
120 $documents = $index_function($i);
124 foreach($documents as $document) {
127 //object to insert into db
128 $dbid = $dbcontrol->addDocument($document);
130 //synchronise db with index
131 $document->addField(Zend_Search_Lucene_Field
::Keyword('dbid', $dbid));
133 //add document to index
134 $index->addDocument($document);
136 //commit every x new documents, and print a status message
137 if (($counter%2000
) == 0) {
139 mtrace(".. $counter");
147 //commit left over documents, and finish up
150 mtrace("-- $counter documents indexed");
158 mtrace('Finished activity modules');
164 mtrace(".<br/><a href='index.php'>Back to query page</a>.");
167 //finished, turn busy flag off
168 set_config("search_indexer_busy", "0");
170 //mark the time we last updated
171 set_config("search_indexer_run_date", time());
174 set_config("search_index_size", (int)$index->count());