merged from STABLE
[moodle.git] / search / indexer.php
CommitLineData
682d4032 1<?php
2 //this'll take some time, set up the environment
3 @set_time_limit(0);
4 @ob_implicit_flush(true);
5 @ob_end_flush();
6
7 require_once('../config.php');
8 require_once("$CFG->dirroot/search/lib.php");
9
10 require_login();
11
12 if (!isadmin()) {
13 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
14 } //if
15
16 $sure = strtolower(optional_param('areyousure', '', PARAM_ALPHA));
17
18 if ($sure != 'yes') {
19 mtrace("Sorry, you weren't sure enough (<a href='index.php'>back to query page</a>).");
20 exit(0);
21 } //if
22
23 //check for php5 (lib.php)
24 if (!search_check_php5()) {
25 $phpversion = phpversion();
26 mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version $phpversion)");
27 exit(0);
28 } //if
29
30 require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
31
32 //begin timer
33 search_stopwatch();
34 mtrace('<pre>Server Time: '.date('r',time())."\n");
35
36 //paths
37 $index_path = $CFG->dataroot.'/search';
38 $index_db_file = "$CFG->dirroot/search/db/$CFG->dbtype.sql";
39
40 if (!file_exists($index_path)) {
41 mtrace("Data directory ($index_path) does not exist, attempting to create.");
42 if (!mkdir($index_path)) {
43 search_pexit("Error creating data directory at: $index_path. Please correct.");
44 } else {
45 mtrace("Directory successfully created.");
46 } //else
47 } else {
48 mtrace("Using $index_path as data directory.");
49 } //else
50
51 //stop accidental re-indexing (zzz)
52 //search_pexit("Not indexing at this time.");
53
54 $index = new Zend_Search_Lucene($index_path, true);
55
56 //create the database tables
bef08835 57 $tables = $db->MetaTables();
58
59 if (in_array($CFG->prefix.'search_documents', $tables)) {
60 delete_records('search_documents');
61 } else {
62 ob_start(); //turn output buffering on - to hide modify_database() output
63 modify_database($index_db_file, '', false);
64 ob_end_clean(); //chuck the buffer and resume normal operation
65 } //else
682d4032 66
67 //empty database table goes here
68 // delete * from search_documents;
69 // set auto_increment back to 1
70
71 //-------- debug stuff
72 /*
73 include_once("$CFG->dirroot/mod/wiki/lib.php");
74
75 $wikis = get_all_instances_in_courses("wiki", get_courses());
76 #search_pexit($wikis[1]);
77 $entries = wiki_get_entries($wikis[1]);
78 #search_pexit($entries);
79
80 #$r = wiki_get_pages($entries[134]);
81 $r = wiki_get_latest_pages($entries[95]);
82
83 search_pexit($r);
84 //ignore me --------*/
85
86 mtrace('Starting activity modules');
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";
90 if (file_exists($libfile)) {
91 include_once($libfile);
92
93 $iter_function = $mod->name.'_iterator';
94 $index_function = $mod->name.'_get_content_for_index';
95 $include_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
96 $c = 0;
97 $doc = new stdClass;
98
99 if (function_exists($index_function) && function_exists($iter_function)) {
100 include_once($include_file);
101
102 mtrace("Processing module function $index_function ...");
103
104 foreach ($iter_function() as $i) {
105 $documents = $index_function($i);
106
107 //begin transaction
108
109 foreach($documents as $document) {
110 $c++;
111
112 //db sync increases indexing time from 55 sec to 73 (64 on Saturday?), so ~30%
113 //therefore, let us make a custom insert function for this search module
114
115 //data object for db
116 $doc->type = $document->type;
117 $doc->title = mysql_real_escape_string($document->title); //naughty
118 $doc->update = time();
119 $doc->permissions = 0;
120 $doc->url = 'none';
121 $doc->courseid = $document->courseid;
122 $doc->userid = $document->userid;
123 $doc->groupid = $document->groupid;
124
125 //insert summary into db
bef08835 126 $id = insert_record('search_documents', $doc);
682d4032 127
128 //synchronise db with index
129 $document->addField(Zend_Search_Lucene_Field::Keyword('dbid', $id));
130 $index->addDocument($document);
131
132 //commit every 100 new documents, and print a status message
133 if (($c%100) == 0) {
134 $index->commit();
135 mtrace(".. $c");
136 } //if
137 } //foreach
138
139 //end transaction
140
141 } //foreach
142
143 //commit left over documents, and finish up
144 $index->commit();
145 mtrace("-- $c documents indexed");
146 mtrace('done.');
147 } //if
148 } //if
149 } //foreach
150 } //if
151
152 //done modules
153 mtrace('Finished activity modules');
154 search_stopwatch();
155 mtrace(".<br><a href='index.php'>Back to query page</a>.");
156 mtrace('</pre>');
157
158?>