Bug #6077 - Teacher saving their grades destroys student essay! Merged from MOODLE_16...
[moodle.git] / search / indexer.php
CommitLineData
682d4032 1<?php
abb4ea20 2 /* The indexer logic -
3 * Look through each installed module's lib file for necessary search functions,
4 * and if they're present (and the module search document class file), add the
5 * content to the index. Repeat this for blocks.
6 *
7 * Along with the index data, each document's summary gets stored in the database
8 * and synchronised to the index (flat file) via the primary key ('id') which is mapped
9 * to the 'dbid' field in the index
10 * */
11
682d4032 12 //this'll take some time, set up the environment
13 @set_time_limit(0);
14 @ob_implicit_flush(true);
15 @ob_end_flush();
16
17 require_once('../config.php');
18 require_once("$CFG->dirroot/search/lib.php");
19
abb4ea20 20 //only administrators can index the moodle installation, because access to all pages is required
682d4032 21 require_login();
22
23 if (!isadmin()) {
24 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
25 } //if
26
abb4ea20 27 //confirmation flag to prevent accidental reindexing (indexersplash.php is the correct entry point)
682d4032 28 $sure = strtolower(optional_param('areyousure', '', PARAM_ALPHA));
29
30 if ($sure != 'yes') {
abb4ea20 31 mtrace("<pre>Sorry, you need to confirm indexing via <a href='indexersplash.php'>indexersplash.php</a>"
32 .". (<a href='index.php'>Back to query page</a>).</pre>");
33
682d4032 34 exit(0);
35 } //if
36
37 //check for php5 (lib.php)
38 if (!search_check_php5()) {
39 $phpversion = phpversion();
40 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version $phpversion)");
41 exit(0);
42 } //if
43
abb4ea20 44 //php5 found, continue including php5-only files
682d4032 45 require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
46
47 //begin timer
48 search_stopwatch();
49 mtrace('<pre>Server Time: '.date('r',time())."\n");
50
51 //paths
52 $index_path = $CFG->dataroot.'/search';
53 $index_db_file = "$CFG->dirroot/search/db/$CFG->dbtype.sql";
54
abb4ea20 55 //setup directory in data root
682d4032 56 if (!file_exists($index_path)) {
57 mtrace("Data directory ($index_path) does not exist, attempting to create.");
58 if (!mkdir($index_path)) {
59 search_pexit("Error creating data directory at: $index_path. Please correct.");
60 } else {
61 mtrace("Directory successfully created.");
62 } //else
63 } else {
64 mtrace("Using $index_path as data directory.");
65 } //else
66
682d4032 67 $index = new Zend_Search_Lucene($index_path, true);
68
69 //create the database tables
bef08835 70 $tables = $db->MetaTables();
71
72 if (in_array($CFG->prefix.'search_documents', $tables)) {
73 delete_records('search_documents');
74 } else {
75 ob_start(); //turn output buffering on - to hide modify_database() output
76 modify_database($index_db_file, '', false);
77 ob_end_clean(); //chuck the buffer and resume normal operation
78 } //else
682d4032 79
80 mtrace('Starting activity modules');
abb4ea20 81
82 //the presence of the required search functions -
83 // * mod_iterator
84 // * mod_get_content_for_index
85 //are the sole basis for including a module in the index at the moment.
86
682d4032 87 if ($mods = get_records_select('modules' /*'index this module?' where statement*/)) {
88 foreach ($mods as $mod) {
89 $libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
abb4ea20 90
682d4032 91 if (file_exists($libfile)) {
92 include_once($libfile);
93
94 $iter_function = $mod->name.'_iterator';
95 $index_function = $mod->name.'_get_content_for_index';
abb4ea20 96
97 //specific module search document class
98 $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
99
100 $counter = 0;
682d4032 101 $doc = new stdClass;
102
abb4ea20 103 if (file_exists($class_file) && function_exists($index_function) && function_exists($iter_function)) {
104 include_once($class_file);
682d4032 105
106 mtrace("Processing module function $index_function ...");
107
108 foreach ($iter_function() as $i) {
109 $documents = $index_function($i);
110
111 //begin transaction
112
113 foreach($documents as $document) {
abb4ea20 114 $counter++;
115
682d4032 116 //data object for db
117 $doc->type = $document->type;
abb4ea20 118 $doc->title = search_escape_string($document->title);
119 $doc->update = time();
682d4032 120 $doc->url = 'none';
abb4ea20 121 $doc->courseid = $document->courseid;
682d4032 122 $doc->groupid = $document->groupid;
123
124 //insert summary into db
bef08835 125 $id = insert_record('search_documents', $doc);
682d4032 126
127 //synchronise db with index
128 $document->addField(Zend_Search_Lucene_Field::Keyword('dbid', $id));
abb4ea20 129
130 //add document to index
682d4032 131 $index->addDocument($document);
132
abb4ea20 133 //commit every x new documents, and print a status message
134 if (($counter%200) == 0) {
682d4032 135 $index->commit();
abb4ea20 136 mtrace(".. $counter");
682d4032 137 } //if
138 } //foreach
139
140 //end transaction
141
142 } //foreach
143
144 //commit left over documents, and finish up
145 $index->commit();
abb4ea20 146
147 mtrace("-- $counter documents indexed");
682d4032 148 mtrace('done.');
149 } //if
150 } //if
151 } //foreach
152 } //if
153
abb4ea20 154 //finished modules
682d4032 155 mtrace('Finished activity modules');
156 search_stopwatch();
abb4ea20 157
158 //now blocks...
159 //
160
682d4032 161 mtrace(".<br><a href='index.php'>Back to query page</a>.");
162 mtrace('</pre>');
163
164?>