Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / search / indexer.php
blobbd231d7ae267610821d0df463a4368385817abea
1 <?php
2 /**
3 * Global Search Engine for Moodle
4 * Michael Champanis (mchampan) [cynnical@gmail.com]
5 * review 1.8+ : Valery Fremaux [valery.fremaux@club-internet.fr]
6 * 2007/08/02
8 * The indexer logic -
10 * Look through each installed module's or block's search document class file (/search/documents)
11 * for necessary search functions, and if they're present add the content to the index.
12 * Repeat this for blocks.
14 * Because the iterator/retrieval functions are now stored in /search/documents/<mod>_document.php,
15 * /mod/mod/lib.php doesn't have to be modified - and thus the search module becomes quite
16 * self-sufficient. URL's are now stored in the index, stopping us from needing to require
17 * the class files to generate a results page.
19 * Along with the index data, each document's summary gets stored in the database
20 * and synchronised to the index (flat file) via the primary key ('id') which is mapped
21 * to the 'dbid' field in the index
22 * */
24 //this'll take some time, set up the environment
25 @set_time_limit(0);
26 @ob_implicit_flush(true);
27 @ob_end_flush();
29 require_once('../config.php');
30 require_once("$CFG->dirroot/search/lib.php");
32 //only administrators can index the moodle installation, because access to all pages is required
33 require_login();
35 if (empty($CFG->enableglobalsearch)) {
36 error(get_string('globalsearchdisabled', 'search'));
39 if (!isadmin()) {
40 error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
41 } //if
43 //confirmation flag to prevent accidental reindexing (indexersplash.php is the correct entry point)
44 $sure = strtolower(optional_param('areyousure', '', PARAM_ALPHA));
46 if ($sure != 'yes') {
47 mtrace("<pre>Sorry, you need to confirm indexing via <a href='indexersplash.php'>indexersplash.php</a>"
48 .". (<a href='index.php'>Back to query page</a>).</pre>");
50 exit(0);
51 } //if
53 //check for php5 (lib.php)
54 if (!search_check_php5()) {
55 $phpversion = phpversion();
56 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version $phpversion)");
57 exit(0);
60 //php5 found, continue including php5-only files
61 //require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
62 require_once("$CFG->dirroot/search/indexlib.php");
64 mtrace('<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /></head><body>');
65 mtrace('<pre>Server Time: '.date('r',time())."\n");
67 if ($CFG->search_indexer_busy == '1') {
68 //means indexing was not finished previously
69 mtrace("Warning: Indexing was not successfully completed last time, restarting.\n");
72 //turn on busy flag
73 set_config('search_indexer_busy', '1');
75 //paths
76 $index_path = SEARCH_INDEX_PATH;
77 $index_db_file = "{$CFG->dirroot}/search/db/$CFG->dbtype.sql";
78 $dbcontrol = new IndexDBControl();
80 //setup directory in data root
81 if (!file_exists($index_path)) {
82 mtrace("Data directory ($index_path) does not exist, attempting to create.");
83 if (!mkdir($index_path)) {
84 search_pexit("Error creating data directory at: $index_path. Please correct.");
86 else {
87 mtrace("Directory successfully created.");
90 else {
91 mtrace("Using $index_path as data directory.");
94 $index = new Zend_Search_Lucene($index_path, true);
96 if (!$dbcontrol->checkDB()) {
97 search_pexit("Database error. Please check settings/files.");
100 //begin timer
101 search_stopwatch();
102 mtrace("Starting activity modules\n");
104 //the presence of the required search functions -
105 // * mod_iterator
106 // * mod_get_content_for_index
107 //are the sole basis for including a module in the index at the moment.
108 $searchables = array();
110 // collects modules
111 if ($mods = get_records('modules', '', '', '', 'id,name')) {
112 $searchables = array_merge($searchables, $mods);
114 mtrace(count($searchables).' modules found.');
116 // collects blocks as indexable information may be found in blocks either
117 if ($blocks = get_records('block', '', '', '', 'id,name')) {
118 // prepend the "block_" prefix to discriminate document type plugins
119 foreach(array_keys($blocks) as $aBlockId){
120 $blocks[$aBlockId]->name = 'block_'.$blocks[$aBlockId]->name;
122 $searchables = array_merge($searchables, $blocks);
123 mtrace(count($blocks).' blocks found.');
126 //add virtual modules onto the back of the array
127 $searchables = array_merge($searchables, search_get_additional_modules());
128 if ($searchables){
129 foreach ($searchables as $mod) {
130 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
132 if (file_exists($class_file)) {
133 include_once($class_file);
135 //build function names
136 $iter_function = $mod->name.'_iterator';
137 $index_function = $mod->name.'_get_content_for_index';
138 $counter = 0;
139 if (function_exists($index_function) && function_exists($iter_function)) {
140 mtrace("Processing module function $index_function ...");
141 $sources = $iter_function();
142 if ($sources){
143 foreach ($sources as $i) {
144 $documents = $index_function($i);
146 //begin transaction
147 if ($documents){
148 foreach($documents as $document) {
149 $counter++;
151 //object to insert into db
152 $dbid = $dbcontrol->addDocument($document);
154 //synchronise db with index
155 $document->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
157 //add document to index
158 $index->addDocument($document);
160 //commit every x new documents, and print a status message
161 if (($counter % 2000) == 0) {
162 $index->commit();
163 mtrace(".. $counter");
167 //end transaction
171 //commit left over documents, and finish up
172 $index->commit();
174 mtrace("-- $counter documents indexed");
175 mtrace("done.\n");
181 //finished modules
182 mtrace('Finished activity modules');
183 search_stopwatch();
185 mtrace(".<br/><a href='index.php'>Back to query page</a>.");
186 mtrace('</pre>');
188 //finished, turn busy flag off
189 set_config("search_indexer_busy", "0");
191 //mark the time we last updated
192 set_config("search_indexer_run_date", time());
194 //and the index size
195 set_config("search_index_size", (int)$index->count());