MDL-9506 Refactored grade_tree::get_tree by doing the following:
[moodle-pu.git] / search / indexer.php
blobc1e18abbb67934c861b786e03cfbf1acc117191a
1 <?php
2 /* The indexer logic -
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
15 * */
17 //this'll take some time, set up the environment
18 @set_time_limit(0);
19 @ob_implicit_flush(true);
20 @ob_end_flush();
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
26 require_login();
28 if (empty($CFG->enableglobalsearch)) {
29 error('Global searching is not enabled.');
32 if (!isadmin()) {
33 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
34 } //if
36 //confirmation flag to prevent accidental reindexing (indexersplash.php is the correct entry point)
37 $sure = strtolower(optional_param('areyousure', '', PARAM_ALPHA));
39 if ($sure != 'yes') {
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>");
43 exit(0);
44 } //if
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)");
50 exit(0);
51 } //if
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");
62 } //if
64 //turn on busy flag
65 set_config('search_indexer_busy', '1');
67 //paths
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.");
77 } else {
78 mtrace("Directory successfully created.");
79 } //else
80 } else {
81 mtrace("Using $index_path as data directory.");
82 } //else
84 $index = new Zend_Search_Lucene($index_path, true);
86 if (!$dbcontrol->checkDB()) {
87 search_pexit("Database error. Please check settings/files.");
88 } //if
90 //begin timer
91 search_stopwatch();
92 mtrace("Starting activity modules\n");
94 //the presence of the required search functions -
95 // * mod_iterator
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';
113 $counter = 0;
114 $doc = new stdClass;
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);
122 //begin transaction
124 foreach($documents as $document) {
125 $counter++;
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) {
138 $index->commit();
139 mtrace(".. $counter");
140 } //if
141 } //foreach
143 //end transaction
145 } //foreach
147 //commit left over documents, and finish up
148 $index->commit();
150 mtrace("-- $counter documents indexed");
151 mtrace("done.\n");
152 } //if
153 } //if
154 } //foreach
155 } //if
157 //finished modules
158 mtrace('Finished activity modules');
159 search_stopwatch();
161 //now blocks...
164 mtrace(".<br/><a href='index.php'>Back to query page</a>.");
165 mtrace('</pre>');
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());
173 //and the index size
174 set_config("search_index_size", (int)$index->count());