weekly release 2.0.4+
[moodle.git] / search / indexlib.php
blob3c2075ef291a992620b97b7320f284e40128e086
1 <?php
2 /**
3 * Global Search Engine for Moodle
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 * @version prepared for 2.0
11 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
13 * Index info class
15 * Used to retrieve information about an index.
16 * Has methods to check for valid database and data directory,
17 * and the index itself.
20 /**
21 * includes and requires
23 require_once($CFG->dirroot.'/search/lib.php');
24 require_once($CFG->dirroot.'/search/Zend/Search/Lucene.php');
26 /**
27 * main class for searchable information in the Lucene index
29 class IndexInfo {
31 private $path, //index data directory
32 $size, //size of directory (i.e. the whole index)
33 $filecount, //number of files
34 $indexcount, //number of docs in index
35 $dbcount, //number of docs in db
36 $types, //array of [document types => count]
37 $complete, //is index completely formed?
38 $time; //date index was generated
40 public function __construct($path = SEARCH_INDEX_PATH) {
41 global $CFG, $DB;
43 $this->path = $path;
45 //test to see if there is a valid index on disk, at the specified path
46 try {
47 $test_index = new Zend_Search_Lucene($this->path, false);
48 $validindex = true;
49 } catch(Exception $e) {
50 $validindex = false;
53 //retrieve file system info about the index if it is valid
54 if ($validindex) {
55 $this->size = display_size(get_directory_size($this->path));
56 $index_dir = get_directory_list($this->path, '', false, false);
57 $this->filecount = count($index_dir);
58 $this->indexcount = $test_index->count();
60 else {
61 $this->size = 0;
62 $this->filecount = 0;
63 $this->indexcount = 0;
66 $db_exists = false; //for now
68 //get all the current tables in moodle
69 $admin_tables = $DB->get_tables();
71 //check if our search table exists
72 if (in_array(SEARCH_DATABASE_TABLE, $admin_tables)) {
73 //retrieve database information if it does
74 $db_exists = true;
76 //total documents
77 $this->dbcount = $DB->count_records(SEARCH_DATABASE_TABLE);
79 //individual document types
80 $types = search_collect_searchables(false, false);
81 asort($types);
83 foreach(array_keys($types) as $type) {
84 $c = $DB->count_records(SEARCH_DATABASE_TABLE, array('doctype' => $type));
85 $types[$type]->records = (int)$c;
87 $this->types = $types;
88 } else {
89 $this->dbcount = 0;
90 $this->types = array();
93 //check if the busy flag is set
94 if (isset($CFG->search_indexer_busy) && $CFG->search_indexer_busy == '1') {
95 $this->complete = false;
96 } else {
97 $this->complete = true;
100 //get the last run date for the indexer
101 if ($this->valid() && $CFG->search_indexer_run_date) {
102 $this->time = $CFG->search_indexer_run_date;
103 } else {
104 $this->time = 0;
109 * returns false on error, and the error message via referenced variable $err
110 * @param array $err array of errors
112 public function valid(&$err = null) {
113 $err = array();
114 $ret = true;
116 if (!$this->is_valid_dir()) {
117 $err['dir'] = get_string('invalidindexerror', 'search');
118 $ret = false;
121 if (!$this->is_valid_db()) {
122 $err['db'] = get_string('emptydatabaseerror', 'search');
123 $ret = false;
126 if (!$this->complete) {
127 $err['index'] = get_string('uncompleteindexingerror','search');
128 $ret = false;
131 return $ret;
135 * is the index dir valid
138 public function is_valid_dir() {
139 if ($this->filecount > 0) {
140 return true;
141 } else {
142 return false;
147 * is the db table valid
150 public function is_valid_db() {
151 if ($this->dbcount > 0) {
152 return true;
153 } else {
154 return false;
159 * shorthand get method for the class variables
160 * @param object $var
162 public function __get($var) {
163 if (in_array($var, array_keys(get_class_vars(get_class($this))))) {
164 return $this->$var;
171 * DB Index control class
173 * Used to control the search index database table
175 class IndexDBControl {
178 * does the table exist?
179 * @deprecated
180 * @uses $CFG, $DB
182 public function checkTableExists() {
183 global $CFG, $DB;
185 $tables = $DB->get_tables();
186 if (in_array(SEARCH_DATABASE_TABLE, $tables)) {
187 return true;
189 else {
190 return false;
192 } //checkTableExists
195 * NEVER USED
197 * is our database setup valid?
198 * @uses db, CFG
199 * @deprecated Database is installed at install and should not be dropped out
201 public function checkDB() {
202 global $CFG, $db;
204 $sqlfile = "{$CFG->dirroot}/search/db/$CFG->dbtype.sql";
205 $ret = false;
206 if ($this->checkTableExists()) {
207 execute_sql('drop table '.SEARCH_DATABASE_TABLE, false);
210 //turn output buffering on - to hide modify_database() output
211 ob_start();
212 $ret = modify_database($sqlfile, '', false);
214 //chuck the buffer and resume normal operation
215 ob_end_clean();
216 return $ret;
217 } //checkDB */
220 * add a document record to the table
221 * @param document must be a Lucene SearchDocument instance
222 * @uses $CFG, $DB
224 public function addDocument($document=null) {
225 global $DB, $CFG;
227 if ($document == null) {
228 return false;
231 // object to insert into db
232 $doc->doctype = $document->doctype;
233 $doc->docid = $document->docid;
234 $doc->itemtype = $document->itemtype;
235 $doc->title = $document->title;
236 $doc->url = $document->url;
237 $doc->updated = time();
238 $doc->docdate = $document->date;
239 $doc->courseid = $document->course_id;
240 $doc->groupid = $document->group_id;
242 //insert summary into db
243 $table = SEARCH_DATABASE_TABLE;
244 $id = $DB->insert_record($table, $doc);
246 return $id;
250 * remove a document record from the index
251 * @param document must be a Lucene document instance, or at least a dbid enveloppe
252 * @uses $DB
254 public function delDocument($document) {
255 global $DB;
257 $table = SEARCH_DATABASE_TABLE;
258 $DB->delete_records($table, array('id' => $document->dbid));