MDL-11719 increased size of idnumber in user table - needed for some enrolment plugin...
[moodle.git] / search / indexlib.php
CommitLineData
d9e1bf24 1<?php
3319ef85 2/**
3* Global Search Engine for Moodle
2f338ab5 4*
3319ef85 5* @package search
6* @category core
7* @subpackage search_engine
8* @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
9* @date 2008/03/31
10* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
2f338ab5 11*
12* Index info class
13*
14* Used to retrieve information about an index.
15* Has methods to check for valid database and data directory,
16* and the index itself.
3319ef85 17*/
2f338ab5 18
3319ef85 19/**
20* includes and requires
21*/
2f338ab5 22require_once("$CFG->dirroot/search/lib.php");
23require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
24
3319ef85 25/**
26* main class for searchable information in the Lucene index
27*/
2f338ab5 28class IndexInfo {
3319ef85 29
d9e1bf24 30 private $path, //index data directory
31 $size, //size of directory (i.e. the whole index)
32 $filecount, //number of files
33 $indexcount, //number of docs in index
34 $dbcount, //number of docs in db
35 $types, //array of [document types => count]
36 $complete, //is index completely formed?
37 $time; //date index was generated
2f338ab5 38
39 public function __construct($path = SEARCH_INDEX_PATH) {
40 global $CFG, $db;
41
42 $this->path = $path;
43
44 //test to see if there is a valid index on disk, at the specified path
45 try {
46 $test_index = new Zend_Search_Lucene($this->path, false);
47 $validindex = true;
48 } catch(Exception $e) {
49 $validindex = false;
3319ef85 50 }
2f338ab5 51
52 //retrieve file system info about the index if it is valid
53 if ($validindex) {
54 $this->size = display_size(get_directory_size($this->path));
55 $index_dir = get_directory_list($this->path, '', false, false);
56 $this->filecount = count($index_dir);
57 $this->indexcount = $test_index->count();
58 }
59 else {
60 $this->size = 0;
61 $this->filecount = 0;
62 $this->indexcount = 0;
63 }
64
65 $db_exists = false; //for now
66
67 //get all the current tables in moodle
68 $admin_tables = $db->MetaTables();
69
70 //TODO: use new IndexDBControl class for database checks?
71
72 //check if our search table exists
73 if (in_array($CFG->prefix.SEARCH_DATABASE_TABLE, $admin_tables)) {
74 //retrieve database information if it does
75 $db_exists = true;
76
77 //total documents
78 $this->dbcount = count_records(SEARCH_DATABASE_TABLE);
79
80 //individual document types
8f41cc02 81 // $types = search_get_document_types();
82 $types = search_collect_searchables(true, false);
2f338ab5 83 sort($types);
84
85 foreach($types as $type) {
86 $c = count_records(SEARCH_DATABASE_TABLE, 'doctype', $type);
87 $this->types[$type] = (int)$c;
88 }
3319ef85 89 } else {
2f338ab5 90 $this->dbcount = 0;
91 $this->types = array();
92 }
93
94 //check if the busy flag is set
001f3652 95 if (isset($CFG->search_indexer_busy) && $CFG->search_indexer_busy == '1') {
2f338ab5 96 $this->complete = false;
3319ef85 97 } else {
2f338ab5 98 $this->complete = true;
99 }
100
101 //get the last run date for the indexer
102 if ($this->valid() && $CFG->search_indexer_run_date) {
103 $this->time = $CFG->search_indexer_run_date;
3319ef85 104 } else {
2f338ab5 105 $this->time = 0;
106 }
3319ef85 107 }
2f338ab5 108
109 /**
110 * returns false on error, and the error message via referenced variable $err
3319ef85 111 * @param array $err array of errors
2f338ab5 112 */
113 public function valid(&$err = null) {
114 $err = array();
115 $ret = true;
116
117 if (!$this->is_valid_dir()) {
118 $err['dir'] = get_string('invalidindexerror', 'search');
119 $ret = false;
120 }
121
122 if (!$this->is_valid_db()) {
123 $err['db'] = get_string('emptydatabaseerror', 'search');
124 $ret = false;
125 }
126
127 if (!$this->complete) {
128 $err['index'] = get_string('uncompleteindexingerror','search');
129 $ret = false;
130 }
131
132 return $ret;
3319ef85 133 }
2f338ab5 134
135 /**
136 * is the index dir valid
137 *
138 */
d9e1bf24 139 public function is_valid_dir() {
2f338ab5 140 if ($this->filecount > 0) {
141 return true;
3319ef85 142 } else {
2f338ab5 143 return false;
144 }
3319ef85 145 }
2f338ab5 146
147 /**
148 * is the db table valid
149 *
150 */
d9e1bf24 151 public function is_valid_db() {
2f338ab5 152 if ($this->dbcount > 0) {
153 return true;
3319ef85 154 } else {
2f338ab5 155 return false;
156 }
3319ef85 157 }
2f338ab5 158
159 /**
160 * shorthand get method for the class variables
3319ef85 161 * @param object $var
2f338ab5 162 */
eef868d1 163 public function __get($var) {
2f338ab5 164 if (in_array($var, array_keys(get_class_vars(get_class($this))))) {
165 return $this->$var;
166 }
3319ef85 167 }
168}
eef868d1 169
170
3319ef85 171/**
2f338ab5 172* DB Index control class
173*
174* Used to control the search index database table
3319ef85 175*/
2f338ab5 176class IndexDBControl {
eef868d1 177
2f338ab5 178 /**
179 * does the table exist?
3319ef85 180 * @deprecated
181 * @uses CFG, db
2f338ab5 182 */
6f1b1da1 183 public function checkTableExists() {
2f338ab5 184 global $CFG, $db;
185
186 $table = SEARCH_DATABASE_TABLE;
187 $tables = $db->MetaTables();
188 if (in_array($CFG->prefix.$table, $tables)) {
189 return true;
190 }
191 else {
192 return false;
193 }
6f1b1da1 194 } //checkTableExists
eef868d1 195
2f338ab5 196 /**
197 * is our database setup valid?
3319ef85 198 * @uses db, CFG
199 * @deprecated Database is installed at install and should not be dropped out
2f338ab5 200 */
6f1b1da1 201 public function checkDB() {
2f338ab5 202 global $CFG, $db;
203
1bf5043c 204 $sqlfile = "{$CFG->dirroot}/search/db/$CFG->dbtype.sql";
2f338ab5 205 $ret = false;
206 if ($this->checkTableExists()) {
207 execute_sql('drop table '.$CFG->prefix.SEARCH_DATABASE_TABLE, false);
208 }
eef868d1 209
2f338ab5 210 //turn output buffering on - to hide modify_database() output
211 ob_start();
212 $ret = modify_database($sqlfile, '', false);
eef868d1 213
2f338ab5 214 //chuck the buffer and resume normal operation
215 ob_end_clean();
216 return $ret;
6f1b1da1 217 } //checkDB
eef868d1 218
2f338ab5 219 /**
220 * add a document record to the table
221 * @param document must be a Lucene SearchDocument instance
3319ef85 222 * @uses db, CFG
2f338ab5 223 */
6f1b1da1 224 public function addDocument($document=null) {
2f338ab5 225 global $db, $CFG;
226
227 if ($document == null) {
228 return false;
229 }
230
231 // object to insert into db
232 $doc->doctype = $document->doctype;
233 $doc->docid = $document->docid;
234 $doc->itemtype = $document->itemtype;
235 $doc->title = search_escape_string($document->title);
236 $doc->url = search_escape_string($document->url);
7f9c77ca 237 $doc->updated = time();
2f338ab5 238 $doc->docdate = $document->date;
239 $doc->courseid = $document->course_id;
240 $doc->groupid = $document->group_id;
241
242 //insert summary into db
243 $id = insert_record(SEARCH_DATABASE_TABLE, $doc);
244
245 return $id;
3319ef85 246 }
eef868d1 247
2f338ab5 248 /**
249 * remove a document record from the index
250 * @param document must be a Lucene document instance, or at least a dbid enveloppe
3319ef85 251 * @uses db
2f338ab5 252 */
6f1b1da1 253 public function delDocument($document) {
2f338ab5 254 global $db;
255
256 delete_records(SEARCH_DATABASE_TABLE, 'id', $document->dbid);
3319ef85 257 }
258}
eef868d1 259
d9e1bf24 260?>