4 * Used to retrieve information about an index.
5 * Has methods to check for valid database and data directory,
6 * and the index itself.
9 require_once("$CFG->dirroot/search/lib.php");
10 require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
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
22 public function __construct($path=SEARCH_INDEX_PATH
) {
27 //test to see if there is a valid index on disk, at the specified path
29 $test_index = new Zend_Search_Lucene($this->path
, false);
31 } catch(Exception
$e) {
35 //retrieve file system info about the index if it is valid
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();
44 $this->indexcount
= 0;
47 $db_exists = false; //for now
49 //get all the current tables in moodle
50 $admin_tables = $db->MetaTables();
52 //TODO: use new IndexDBControl class for database checks?
54 //check if our search table exists
55 if (in_array($CFG->prefix
.SEARCH_DATABASE_TABLE
, $admin_tables)) {
56 //retrieve database information if it does
60 $this->dbcount
= count_records(SEARCH_DATABASE_TABLE
);
62 //individual document types
63 $types = search_get_document_types();
66 foreach($types as $type) {
67 $c = count_records(SEARCH_DATABASE_TABLE
, 'doctype', $type);
68 $this->types
[$type] = (int)$c;
72 $this->types
= array();
75 //check if the busy flag is set
76 if ($CFG->search_indexer_busy
== '1') {
77 $this->complete
= false;
79 $this->complete
= true;
82 //get the last run date for the indexer
83 if ($this->valid() && $CFG->search_indexer_run_date
) {
84 $this->time
= $CFG->search_indexer_run_date
;
90 //returns false on error, and the error message via referenced variable $err
91 public function valid(&$err=null) {
95 if (!$this->is_valid_dir()) {
96 $err['dir'] = 'Index directory either contains an invalid index, or nothing at all.';
100 if (!$this->is_valid_db()) {
101 $err['db'] = 'Database table is not present, or contains no index records.';
105 if (!$this->complete
) {
106 $err['index'] = 'Indexing was not successfully completed, please restart it.';
113 //is the index dir valid
114 public function is_valid_dir() {
115 if ($this->filecount
> 0) {
122 //is the db table valid
123 public function is_valid_db() {
124 if ($this->dbcount
> 0) {
131 //shorthand get method for the class variables
132 public function __get($var) {
133 if (in_array($var, array_keys(get_class_vars(get_class($this))))) {
140 /* DB Index control class
142 * Used to control the search index database table
145 class IndexDBControl
{
146 //does the table exist?
147 public function checkTableExists() {
150 $table = SEARCH_DATABASE_TABLE
;
151 $tables = $db->MetaTables();
153 if (in_array($CFG->prefix
.$table, $tables)) {
160 //is our database setup valid?
161 public function checkDB() {
164 $sqlfile = "$CFG->dirroot/search/db/$CFG->dbtype.sql";
167 if ($this->checkTableExists()) {
168 execute_sql('drop table '.$CFG->prefix
.SEARCH_DATABASE_TABLE
, false);
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
178 //add a document record to the table
179 public function addDocument($document=null) {
182 if ($document == null) {
186 //object to insert into db
187 $doc->doctype
= $document->doctype
;
188 $doc->docid
= $document->docid
;
189 $doc->title
= search_escape_string($document->title
);
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
;
196 //insert summary into db
197 $id = insert_record(SEARCH_DATABASE_TABLE
, $doc);
202 //remove a document record from the index
203 public function delDocument($document) {
206 delete_records(SEARCH_DATABASE_TABLE
, 'id', $document->dbid
);