Automatic installer lang files (20101011)
[moodle.git] / search / indexer.php
blob78fd6bacf581d62489f41165c9f943f452321370
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 * @version prepared for Moodle 2.0
11 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
13 * The indexer logic -
15 * Look through each installed module's or block's search document class file (/search/documents)
16 * for necessary search functions, and if they're present add the content to the index.
17 * Repeat this for blocks.
19 * Because the iterator/retrieval functions are now stored in /search/documents/<mod>_document.php,
20 * /mod/mod/lib.php doesn't have to be modified - and thus the search module becomes quite
21 * self-sufficient. URL's are now stored in the index, stopping us from needing to require
22 * the class files to generate a results page.
24 * Along with the index data, each document's summary gets stored in the database
25 * and synchronised to the index (flat file) via the primary key ('id') which is mapped
26 * to the 'dbid' field in the index
27 * */
29 //this'll take some time, set up the environment
30 @set_time_limit(0);
31 @ob_implicit_flush(true);
32 @ob_end_flush();
34 /**
35 * includes and requires
37 require_once('../config.php');
38 require_once($CFG->dirroot.'/search/lib.php');
40 ini_set('include_path', $CFG->dirroot.DIRECTORY_SEPARATOR.'search'.PATH_SEPARATOR.ini_get('include_path'));
42 /// only administrators can index the moodle installation, because access to all pages is required
44 require_login();
46 if (empty($CFG->enableglobalsearch)) {
47 print_error('globalsearchdisabled', 'search');
50 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
51 print_error('beadmin', 'search', get_login_url());
54 /// confirmation flag to prevent accidental reindexing (indexersplash.php is the correct entry point)
56 $sure = strtolower(optional_param('areyousure', '', PARAM_ALPHA));
58 if ($sure != 'yes') {
59 mtrace("<pre>Sorry, you need to confirm indexing via <a href='indexersplash.php'>indexersplash.php</a>"
60 .". (<a href='index.php'>Back to query page</a>).</pre>");
62 exit(0);
65 /// check for php5 (lib.php)
67 //php5 found, continue including php5-only files
68 //require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
69 require_once($CFG->dirroot.'/search/indexlib.php');
71 mtrace('<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /></head><body>');
72 mtrace('<pre>Server Time: '.date('r',time())."\n");
74 if (isset($CFG->search_indexer_busy) && $CFG->search_indexer_busy == '1') {
75 //means indexing was not finished previously
76 mtrace("Warning: Indexing was not successfully completed last time, restarting.\n");
79 /// turn on busy flag
81 set_config('search_indexer_busy', '1');
83 //paths
84 $index_path = SEARCH_INDEX_PATH;
85 $index_db_file = "{$CFG->dirroot}/search/db/$CFG->dbtype.sql";
86 $dbcontrol = new IndexDBControl();
88 /// setup directory in data root
90 if (!file_exists($index_path)) {
91 mtrace("Data directory ($index_path) does not exist, attempting to create.");
92 if (!mkdir($index_path, $CFG->directorypermissions)) {
93 search_pexit("Error creating data directory at: $index_path. Please correct.");
95 else {
96 mtrace("Directory successfully created.");
99 else {
100 mtrace("Using {$index_path} as data directory.");
103 Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
104 $index = new Zend_Search_Lucene($index_path, true);
106 /// New regeneration
108 mtrace('Deleting old index entries.');
109 $DB->delete_records(SEARCH_DATABASE_TABLE);
111 /// begin timer
113 search_stopwatch();
114 mtrace("Starting activity modules\n");
116 //the presence of the required search functions -
117 // * mod_iterator
118 // * mod_get_content_for_index
119 //are the sole basis for including a module in the index at the moment.
121 $searchables = search_collect_searchables();
123 /// start indexation
125 if ($searchables){
126 foreach ($searchables as $mod) {
128 mtrace("starting indexing {$mod->name}\n");
130 $key = 'search_in_'.$mod->name;
131 if (isset($CFG->$key) && !$CFG->$key) {
132 mtrace("module $key has been administratively disabled. Skipping...\n");
133 continue;
136 if ($mod->location == 'internal'){
137 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
138 } else {
139 $class_file = $CFG->dirroot.'/'.$mod->location.'/'.$mod->name.'/search_document.php';
142 if (file_exists($class_file)) {
143 include_once($class_file);
145 //build function names
146 $iter_function = $mod->name.'_iterator';
147 $index_function = $mod->name.'_get_content_for_index';
148 $counter = 0;
149 if (function_exists($index_function) && function_exists($iter_function)) {
150 mtrace("Processing module function $index_function ...");
151 $sources = $iter_function();
152 if ($sources){
153 foreach ($sources as $i) {
154 $documents = $index_function($i);
156 //begin transaction
157 if ($documents){
158 foreach($documents as $document) {
159 $counter++;
161 //object to insert into db
162 $dbid = $dbcontrol->addDocument($document);
164 //synchronise db with index
165 $document->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
167 //add document to index
168 $index->addDocument($document);
170 //commit every x new documents, and print a status message
171 if (($counter % 2000) == 0) {
172 $index->commit();
173 mtrace(".. $counter");
177 //end transaction
181 //commit left over documents, and finish up
182 $index->commit();
184 mtrace("-- $counter documents indexed");
185 mtrace("done.\n");
187 } else {
188 mtrace ("No search document found for plugin {$mod->name}. Ignoring.");
193 /// finished modules
195 mtrace('Finished activity modules');
196 search_stopwatch();
198 mtrace(".<br/><a href='index.php'>Back to query page</a>.");
199 mtrace('</pre>');
201 /// finished, turn busy flag off
203 set_config('search_indexer_busy', '0');
205 /// mark the time we last updated
207 set_config('search_indexer_run_date', time());
209 /// and the index size
211 set_config('search_index_size', (int)$index->count());