MDL-27445 Fixed the deprecated call
[moodle.git] / search / indexer.php
blob8f2b9e31c4d3d81db18b0d8f609a3f95ed1d717e
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 * */
30 /**
31 * includes and requires
34 define('NO_OUTPUT_BUFFERING', true);
36 require_once('../config.php');
37 require_once($CFG->dirroot.'/search/lib.php');
39 //this'll take some time, set up the environment
40 @set_time_limit(0);
42 ini_set('include_path', $CFG->dirroot.DIRECTORY_SEPARATOR.'search'.PATH_SEPARATOR.ini_get('include_path'));
44 /// only administrators can index the moodle installation, because access to all pages is required
46 require_login();
48 if (empty($CFG->enableglobalsearch)) {
49 print_error('globalsearchdisabled', 'search');
52 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
53 print_error('beadmin', 'search', get_login_url());
56 /// confirmation flag to prevent accidental reindexing (indexersplash.php is the correct entry point)
58 $sure = strtolower(optional_param('areyousure', '', PARAM_ALPHA));
60 if ($sure != 'yes') {
61 mtrace("<pre>Sorry, you need to confirm indexing via <a href='indexersplash.php'>indexersplash.php</a>"
62 .". (<a href='index.php'>Back to query page</a>).</pre>");
64 exit(0);
67 /// check for php5 (lib.php)
69 //php5 found, continue including php5-only files
70 //require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
71 require_once($CFG->dirroot.'/search/indexlib.php');
73 mtrace('<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /></head><body>');
74 mtrace('<pre>Server Time: '.date('r',time())."\n");
76 if (isset($CFG->search_indexer_busy) && $CFG->search_indexer_busy == '1') {
77 //means indexing was not finished previously
78 mtrace("Warning: Indexing was not successfully completed last time, restarting.\n");
81 /// turn on busy flag
83 set_config('search_indexer_busy', '1');
85 //paths
86 $index_path = SEARCH_INDEX_PATH;
87 $index_db_file = "{$CFG->dirroot}/search/db/$CFG->dbtype.sql";
88 $dbcontrol = new IndexDBControl();
90 /// setup directory in data root
92 if (!file_exists($index_path)) {
93 mtrace("Data directory ($index_path) does not exist, attempting to create.");
94 if (!mkdir($index_path, $CFG->directorypermissions)) {
95 search_pexit("Error creating data directory at: $index_path. Please correct.");
97 else {
98 mtrace("Directory successfully created.");
101 else {
102 mtrace("Using {$index_path} as data directory.");
105 Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
106 $index = new Zend_Search_Lucene($index_path, true);
108 /// New regeneration
110 mtrace('Deleting old index entries.');
111 $DB->delete_records(SEARCH_DATABASE_TABLE);
113 /// begin timer
115 search_stopwatch();
116 mtrace("Starting activity modules\n");
118 //the presence of the required search functions -
119 // * mod_iterator
120 // * mod_get_content_for_index
121 //are the sole basis for including a module in the index at the moment.
123 $searchables = search_collect_searchables();
125 /// start indexation
127 if ($searchables){
128 foreach ($searchables as $mod) {
130 //mark last update times for mods to now.
131 $indexdatestring = 'search_indexer_update_date_'.$mod->name;
132 set_config($indexdatestring, time());
133 $indexdatestring = 'search_indexer_run_date_'.$mod->name;
134 set_config($indexdatestring, time());
136 mtrace("starting indexing {$mod->name}\n");
138 $key = 'search_in_'.$mod->name;
139 if (isset($CFG->$key) && !$CFG->$key) {
140 mtrace("module $key has been administratively disabled. Skipping...\n");
141 continue;
144 if ($mod->location == 'internal'){
145 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
146 } else {
147 $class_file = $CFG->dirroot.'/'.$mod->location.'/'.$mod->name.'/search_document.php';
150 if (file_exists($class_file)) {
151 include_once($class_file);
153 //build function names
154 $iter_function = $mod->name.'_iterator';
155 $index_function = $mod->name.'_get_content_for_index';
156 $counter = 0;
157 if (function_exists($index_function) && function_exists($iter_function)) {
158 mtrace("Processing module function $index_function ...");
159 $sources = $iter_function();
160 if ($sources){
161 foreach ($sources as $i) {
162 $documents = $index_function($i);
164 //begin transaction
165 if ($documents){
166 foreach($documents as $document) {
167 $counter++;
169 // temporary fix until MDL-24822 is resolved
170 if ($document->group_id == -1 and $mod->name ='forum') {
171 $document->group_id = 0;
173 //object to insert into db
174 $dbid = $dbcontrol->addDocument($document);
176 //synchronise db with index
177 $document->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
179 //add document to index
180 $index->addDocument($document);
182 //commit every x new documents, and print a status message
183 if (($counter % 2000) == 0) {
184 $index->commit();
185 mtrace(".. $counter");
189 //end transaction
193 //commit left over documents, and finish up
194 $index->commit();
196 mtrace("-- $counter documents indexed");
197 mtrace("done.\n");
199 } else {
200 mtrace ("No search document found for plugin {$mod->name}. Ignoring.");
205 /// finished modules
207 mtrace('Finished activity modules');
208 search_stopwatch();
210 mtrace(".<br/><a href='index.php'>Back to query page</a>.");
211 mtrace('</pre>');
213 /// finished, turn busy flag off
215 set_config('search_indexer_busy', '0');
217 /// mark the time we last updated
219 set_config('search_indexer_run_date', time());
221 /// and the index size
223 set_config('search_index_size', (int)$index->count());