Bug #5941 - Myriad of problems with Paypal enrollmemts, part 3 - fixed 'login as...
[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
57 ob_start(); //turn output buffering on - to hide modify_database() output
58 modify_database($index_db_file, '', false);
59 ob_end_clean(); //chuck the buffer and resume normal operation
60
61 //empty database table goes here
62 // delete * from search_documents;
63 // set auto_increment back to 1
64
65 //-------- debug stuff
66 /*
67 include_once("$CFG->dirroot/mod/wiki/lib.php");
68
69 $wikis = get_all_instances_in_courses("wiki", get_courses());
70 #search_pexit($wikis[1]);
71 $entries = wiki_get_entries($wikis[1]);
72 #search_pexit($entries);
73
74 #$r = wiki_get_pages($entries[134]);
75 $r = wiki_get_latest_pages($entries[95]);
76
77 search_pexit($r);
78 //ignore me --------*/
79
80 mtrace('Starting activity modules');
81 if ($mods = get_records_select('modules' /*'index this module?' where statement*/)) {
82 foreach ($mods as $mod) {
83 $libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
84 if (file_exists($libfile)) {
85 include_once($libfile);
86
87 $iter_function = $mod->name.'_iterator';
88 $index_function = $mod->name.'_get_content_for_index';
89 $include_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
90 $c = 0;
91 $doc = new stdClass;
92
93 if (function_exists($index_function) && function_exists($iter_function)) {
94 include_once($include_file);
95
96 mtrace("Processing module function $index_function ...");
97
98 foreach ($iter_function() as $i) {
99 $documents = $index_function($i);
100
101 //begin transaction
102
103 foreach($documents as $document) {
104 $c++;
105
106 //db sync increases indexing time from 55 sec to 73 (64 on Saturday?), so ~30%
107 //therefore, let us make a custom insert function for this search module
108
109 //data object for db
110 $doc->type = $document->type;
111 $doc->title = mysql_real_escape_string($document->title); //naughty
112 $doc->update = time();
113 $doc->permissions = 0;
114 $doc->url = 'none';
115 $doc->courseid = $document->courseid;
116 $doc->userid = $document->userid;
117 $doc->groupid = $document->groupid;
118
119 //insert summary into db
120 $id = insert_record($CFG->prefix.'search_documents', $doc);
121
122 //synchronise db with index
123 $document->addField(Zend_Search_Lucene_Field::Keyword('dbid', $id));
124 $index->addDocument($document);
125
126 //commit every 100 new documents, and print a status message
127 if (($c%100) == 0) {
128 $index->commit();
129 mtrace(".. $c");
130 } //if
131 } //foreach
132
133 //end transaction
134
135 } //foreach
136
137 //commit left over documents, and finish up
138 $index->commit();
139 mtrace("-- $c documents indexed");
140 mtrace('done.');
141 } //if
142 } //if
143 } //foreach
144 } //if
145
146 //done modules
147 mtrace('Finished activity modules');
148 search_stopwatch();
149 mtrace(".<br><a href='index.php'>Back to query page</a>.");
150 mtrace('</pre>');
151
152?>