Lucene Zend Implementation update (better handle of UTF8)
[moodle.git] / search / indexlib.php
CommitLineData
d9e1bf24 1<?php
2 /* Index info class
3 *
4 * Used to retrieve information about an index.
5 * Has methods to check for valid database and data directory,
6 * and the index itself.
7 * */
8
6f1b1da1 9 require_once("$CFG->dirroot/search/lib.php");
eef868d1 10 require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
d9e1bf24 11
12 class IndexInfo {
13 private $path, //index data directory
14 $size, //size of directory (i.e. the whole index)
15 $filecount, //number of files
16 $indexcount, //number of docs in index
17 $dbcount, //number of docs in db
18 $types, //array of [document types => count]
19 $complete, //is index completely formed?
20 $time; //date index was generated
eef868d1 21
d9e1bf24 22 public function __construct($path=SEARCH_INDEX_PATH) {
23 global $CFG, $db;
eef868d1 24
d9e1bf24 25 $this->path = $path;
eef868d1 26
791a4cec 27 //test to see if there is a valid index on disk, at the specified path
d9e1bf24 28 try {
29 $test_index = new Zend_Search_Lucene($this->path, false);
eef868d1 30 $validindex = true;
31 } catch(Exception $e) {
d9e1bf24 32 $validindex = false;
33 } //catch
791a4cec 34
eef868d1 35 //retrieve file system info about the index if it is valid
36 if ($validindex) {
d9e1bf24 37 $this->size = display_size(get_directory_size($this->path));
38 $index_dir = get_directory_list($this->path, '', false, false);
39 $this->filecount = count($index_dir);
40 $this->indexcount = $test_index->count();
41 } else {
42 $this->size = 0;
43 $this->filecount = 0;
44 $this->indexcount = 0;
45 } //else
46
791a4cec 47 $db_exists = false; //for now
eef868d1 48
791a4cec 49 //get all the current tables in moodle
d9e1bf24 50 $admin_tables = $db->MetaTables();
eef868d1 51
791a4cec 52 //TODO: use new IndexDBControl class for database checks?
eef868d1 53
791a4cec 54 //check if our search table exists
6f1b1da1 55 if (in_array($CFG->prefix.SEARCH_DATABASE_TABLE, $admin_tables)) {
791a4cec 56 //retrieve database information if it does
d9e1bf24 57 $db_exists = true;
eef868d1 58
d9e1bf24 59 //total documents
6f1b1da1 60 $this->dbcount = count_records(SEARCH_DATABASE_TABLE);
d9e1bf24 61
62 //individual document types
63 $types = search_get_document_types();
64 sort($types);
eef868d1 65
d9e1bf24 66 foreach($types as $type) {
6f1b1da1 67 $c = count_records(SEARCH_DATABASE_TABLE, 'doctype', $type);
d9e1bf24 68 $this->types[$type] = (int)$c;
69 } //foreach
70 } else {
71 $this->dbcount = 0;
72 $this->types = array();
eef868d1 73 } //else
74
791a4cec 75 //check if the busy flag is set
d9e1bf24 76 if ($CFG->search_indexer_busy == '1') {
77 $this->complete = false;
78 } else {
79 $this->complete = true;
80 } //if
eef868d1 81
791a4cec 82 //get the last run date for the indexer
d9e1bf24 83 if ($this->valid() && $CFG->search_indexer_run_date) {
84 $this->time = $CFG->search_indexer_run_date;
85 } else {
86 $this->time = 0;
87 } //else
88 } //__construct
eef868d1 89
791a4cec 90 //returns false on error, and the error message via referenced variable $err
d9e1bf24 91 public function valid(&$err=null) {
92 $err = array();
93 $ret = true;
eef868d1 94
d9e1bf24 95 if (!$this->is_valid_dir()) {
96 $err['dir'] = 'Index directory either contains an invalid index, or nothing at all.';
97 $ret = false;
98 } //if
eef868d1 99
d9e1bf24 100 if (!$this->is_valid_db()) {
101 $err['db'] = 'Database table is not present, or contains no index records.';
102 $ret = false;
103 } //if
eef868d1 104
d9e1bf24 105 if (!$this->complete) {
106 $err['index'] = 'Indexing was not successfully completed, please restart it.';
107 $ret = false;
108 } //if
eef868d1 109
110 return $ret;
d9e1bf24 111 } //valid
eef868d1 112
791a4cec 113 //is the index dir valid
d9e1bf24 114 public function is_valid_dir() {
115 if ($this->filecount > 0) {
116 return true;
117 } else {
118 return false;
119 } //else
120 } //is_valid_dir
eef868d1 121
791a4cec 122 //is the db table valid
d9e1bf24 123 public function is_valid_db() {
124 if ($this->dbcount > 0) {
125 return true;
126 } else {
127 return false;
128 } //else
eef868d1 129 } //is_valid_db
130
791a4cec 131 //shorthand get method for the class variables
eef868d1 132 public function __get($var) {
d9e1bf24 133 if (in_array($var, array_keys(get_class_vars(get_class($this))))) {
134 return $this->$var;
135 } //if
eef868d1 136 } //__get
d9e1bf24 137 } //IndexInfo
eef868d1 138
139
140 /* DB Index control class
141 *
791a4cec 142 * Used to control the search index database table
6f1b1da1 143 * */
eef868d1 144
145 class IndexDBControl {
791a4cec 146 //does the table exist?
6f1b1da1 147 public function checkTableExists() {
148 global $CFG, $db;
eef868d1 149
6f1b1da1 150 $table = SEARCH_DATABASE_TABLE;
151 $tables = $db->MetaTables();
eef868d1 152
6f1b1da1 153 if (in_array($CFG->prefix.$table, $tables)) {
154 return true;
155 } else {
156 return false;
157 } //else
158 } //checkTableExists
eef868d1 159
791a4cec 160 //is our database setup valid?
6f1b1da1 161 public function checkDB() {
162 global $CFG, $db;
eef868d1 163
6f1b1da1 164 $sqlfile = "$CFG->dirroot/search/db/$CFG->dbtype.sql";
eef868d1 165 $ret = false;
166
167 if ($this->checkTableExists()) {
6f1b1da1 168 execute_sql('drop table '.$CFG->prefix.SEARCH_DATABASE_TABLE, false);
169 } //if
eef868d1 170
6f1b1da1 171 ob_start(); //turn output buffering on - to hide modify_database() output
172 $ret = modify_database($sqlfile, '', false);
173 ob_end_clean(); //chuck the buffer and resume normal operation
eef868d1 174
6f1b1da1 175 return $ret;
176 } //checkDB
eef868d1 177
791a4cec 178 //add a document record to the table
6f1b1da1 179 public function addDocument($document=null) {
180 global $db;
eef868d1 181
6f1b1da1 182 if ($document == null) {
183 return false;
184 } //if
eef868d1 185
186 //object to insert into db
6f1b1da1 187 $doc->doctype = $document->doctype;
188 $doc->docid = $document->docid;
189 $doc->title = search_escape_string($document->title);
eef868d1 190 $doc->url = search_escape_string($document->url);
191 $doc->update = time();
192 $doc->docdate = $document->date;
193 $doc->courseid = $document->course_id;
194 $doc->groupid = $document->group_id;
195
6f1b1da1 196 //insert summary into db
197 $id = insert_record(SEARCH_DATABASE_TABLE, $doc);
eef868d1 198
6f1b1da1 199 return $id;
200 } //addDocument
eef868d1 201
791a4cec 202 //remove a document record from the index
6f1b1da1 203 public function delDocument($document) {
204 global $db;
eef868d1 205
6f1b1da1 206 delete_records(SEARCH_DATABASE_TABLE, 'id', $document->dbid);
207 } //delDocument
208 } //IndexControl
eef868d1 209
d9e1bf24 210?>