Slightly more helpful message if no courses can be shown.
[moodle-linuxchix.git] / search / indexlib.php
blobcc479e866270d32d90a94e3498bc478f8afc8f31
1 <?php
2 /* Index info class
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 * */
9 require_once("$CFG->dirroot/search/lib.php");
10 require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
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
22 public function __construct($path=SEARCH_INDEX_PATH) {
23 global $CFG, $db;
25 $this->path = $path;
27 //test to see if there is a valid index on disk, at the specified path
28 try {
29 $test_index = new Zend_Search_Lucene($this->path, false);
30 $validindex = true;
31 } catch(Exception $e) {
32 $validindex = false;
33 } //catch
35 //retrieve file system info about the index if it is valid
36 if ($validindex) {
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
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
57 $db_exists = true;
59 //total documents
60 $this->dbcount = count_records(SEARCH_DATABASE_TABLE);
62 //individual document types
63 $types = search_get_document_types();
64 sort($types);
66 foreach($types as $type) {
67 $c = count_records(SEARCH_DATABASE_TABLE, 'doctype', $type);
68 $this->types[$type] = (int)$c;
69 } //foreach
70 } else {
71 $this->dbcount = 0;
72 $this->types = array();
73 } //else
75 //check if the busy flag is set
76 if ($CFG->search_indexer_busy == '1') {
77 $this->complete = false;
78 } else {
79 $this->complete = true;
80 } //if
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;
85 } else {
86 $this->time = 0;
87 } //else
88 } //__construct
90 //returns false on error, and the error message via referenced variable $err
91 public function valid(&$err=null) {
92 $err = array();
93 $ret = true;
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
100 if (!$this->is_valid_db()) {
101 $err['db'] = 'Database table is not present, or contains no index records.';
102 $ret = false;
103 } //if
105 if (!$this->complete) {
106 $err['index'] = 'Indexing was not successfully completed, please restart it.';
107 $ret = false;
108 } //if
110 return $ret;
111 } //valid
113 //is the index dir valid
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
122 //is the db table valid
123 public function is_valid_db() {
124 if ($this->dbcount > 0) {
125 return true;
126 } else {
127 return false;
128 } //else
129 } //is_valid_db
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))))) {
134 return $this->$var;
135 } //if
136 } //__get
137 } //IndexInfo
140 /* DB Index control class
142 * Used to control the search index database table
143 * */
145 class IndexDBControl {
146 //does the table exist?
147 public function checkTableExists() {
148 global $CFG, $db;
150 $table = SEARCH_DATABASE_TABLE;
151 $tables = $db->MetaTables();
153 if (in_array($CFG->prefix.$table, $tables)) {
154 return true;
155 } else {
156 return false;
157 } //else
158 } //checkTableExists
160 //is our database setup valid?
161 public function checkDB() {
162 global $CFG, $db;
164 $sqlfile = "$CFG->dirroot/search/db/$CFG->dbtype.sql";
165 $ret = false;
167 if ($this->checkTableExists()) {
168 execute_sql('drop table '.$CFG->prefix.SEARCH_DATABASE_TABLE, false);
169 } //if
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
175 return $ret;
176 } //checkDB
178 //add a document record to the table
179 public function addDocument($document=null) {
180 global $db;
182 if ($document == null) {
183 return false;
184 } //if
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);
199 return $id;
200 } //addDocument
202 //remove a document record from the index
203 public function delDocument($document) {
204 global $db;
206 delete_records(SEARCH_DATABASE_TABLE, 'id', $document->dbid);
207 } //delDocument
208 } //IndexControl