3 * Author: Michael Champanis
5 * Reviewed by: Valery Fremaux (2007)
9 * Used to retrieve information about an index.
10 * Has methods to check for valid database and data directory,
11 * and the index itself.
14 require_once("$CFG->dirroot/search/lib.php");
15 require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
18 private $path, //index data directory
19 $size, //size of directory (i.e. the whole index)
20 $filecount, //number of files
21 $indexcount, //number of docs in index
22 $dbcount, //number of docs in db
23 $types, //array of [document types => count]
24 $complete, //is index completely formed?
25 $time; //date index was generated
27 public function __construct($path = SEARCH_INDEX_PATH
) {
32 //test to see if there is a valid index on disk, at the specified path
34 $test_index = new Zend_Search_Lucene($this->path
, false);
36 } catch(Exception
$e) {
40 //retrieve file system info about the index if it is valid
42 $this->size
= display_size(get_directory_size($this->path
));
43 $index_dir = get_directory_list($this->path
, '', false, false);
44 $this->filecount
= count($index_dir);
45 $this->indexcount
= $test_index->count();
50 $this->indexcount
= 0;
53 $db_exists = false; //for now
55 //get all the current tables in moodle
56 $admin_tables = $db->MetaTables();
58 //TODO: use new IndexDBControl class for database checks?
60 //check if our search table exists
61 if (in_array($CFG->prefix
.SEARCH_DATABASE_TABLE
, $admin_tables)) {
62 //retrieve database information if it does
66 $this->dbcount
= count_records(SEARCH_DATABASE_TABLE
);
68 //individual document types
69 $types = search_get_document_types();
72 foreach($types as $type) {
73 $c = count_records(SEARCH_DATABASE_TABLE
, 'doctype', $type);
74 $this->types
[$type] = (int)$c;
79 $this->types
= array();
82 //check if the busy flag is set
83 if ($CFG->search_indexer_busy
== '1') {
84 $this->complete
= false;
87 $this->complete
= true;
90 //get the last run date for the indexer
91 if ($this->valid() && $CFG->search_indexer_run_date
) {
92 $this->time
= $CFG->search_indexer_run_date
;
100 * returns false on error, and the error message via referenced variable $err
103 public function valid(&$err = null) {
107 if (!$this->is_valid_dir()) {
108 $err['dir'] = get_string('invalidindexerror', 'search');
112 if (!$this->is_valid_db()) {
113 $err['db'] = get_string('emptydatabaseerror', 'search');
117 if (!$this->complete
) {
118 $err['index'] = get_string('uncompleteindexingerror','search');
126 * is the index dir valid
129 public function is_valid_dir() {
130 if ($this->filecount
> 0) {
139 * is the db table valid
142 public function is_valid_db() {
143 if ($this->dbcount
> 0) {
152 * shorthand get method for the class variables
155 public function __get($var) {
156 if (in_array($var, array_keys(get_class_vars(get_class($this))))) {
164 * DB Index control class
166 * Used to control the search index database table
168 class IndexDBControl
{
171 * does the table exist?
174 public function checkTableExists() {
177 $table = SEARCH_DATABASE_TABLE
;
178 $tables = $db->MetaTables();
179 if (in_array($CFG->prefix
.$table, $tables)) {
188 * is our database setup valid?
191 public function checkDB() {
194 $sqlfile = "$CFG->dirroot/blocks/search/db/$CFG->dbtype.sql";
196 if ($this->checkTableExists()) {
197 execute_sql('drop table '.$CFG->prefix
.SEARCH_DATABASE_TABLE
, false);
200 //turn output buffering on - to hide modify_database() output
202 $ret = modify_database($sqlfile, '', false);
204 //chuck the buffer and resume normal operation
210 * add a document record to the table
211 * @param document must be a Lucene SearchDocument instance
213 public function addDocument($document=null) {
216 if ($document == null) {
220 // object to insert into db
221 $doc->doctype
= $document->doctype
;
222 $doc->docid
= $document->docid
;
223 $doc->itemtype
= $document->itemtype
;
224 $doc->title
= search_escape_string($document->title
);
225 $doc->url
= search_escape_string($document->url
);
226 $doc->update
= time();
227 $doc->docdate
= $document->date
;
228 $doc->courseid
= $document->course_id
;
229 $doc->groupid
= $document->group_id
;
231 //insert summary into db
232 $id = insert_record(SEARCH_DATABASE_TABLE
, $doc);
238 * remove a document record from the index
239 * @param document must be a Lucene document instance, or at least a dbid enveloppe
241 public function delDocument($document) {
244 delete_records(SEARCH_DATABASE_TABLE
, 'id', $document->dbid
);