3 * Global Search Engine for Moodle
7 * @subpackage search_engine
8 * @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
10 * @version prepared for Moodle 2.0
11 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
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
29 //this'll take some time, set up the environment
31 @ob_implicit_flush
(true);
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
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
));
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>");
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");
81 set_config('search_indexer_busy', '1');
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.");
96 mtrace("Directory successfully created.");
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);
108 mtrace('Deleting old index entries.');
109 $DB->delete_records(SEARCH_DATABASE_TABLE
);
114 mtrace("Starting activity modules\n");
116 //the presence of the required search functions -
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();
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");
136 if ($mod->location
== 'internal'){
137 $class_file = $CFG->dirroot
.'/search/documents/'.$mod->name
.'_document.php';
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';
149 if (function_exists($index_function) && function_exists($iter_function)) {
150 mtrace("Processing module function $index_function ...");
151 $sources = $iter_function();
153 foreach ($sources as $i) {
154 $documents = $index_function($i);
158 foreach($documents as $document) {
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) {
173 mtrace(".. $counter");
181 //commit left over documents, and finish up
184 mtrace("-- $counter documents indexed");
188 mtrace ("No search document found for plugin {$mod->name}. Ignoring.");
195 mtrace('Finished activity modules');
198 mtrace(".<br/><a href='index.php'>Back to query page</a>.");
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());