3 * Global Search Engine for Moodle
7 * @subpackage search_engine
8 * @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
14 * Used to retrieve information about an index.
15 * Has methods to check for valid database and data directory,
16 * and the index itself.
20 * includes and requires
22 require_once($CFG->dirroot
.'/search/lib.php');
23 require_once($CFG->dirroot
.'/search/Zend/Search/Lucene.php');
26 * main class for searchable information in the Lucene index
30 private $path, //index data directory
31 $size, //size of directory (i.e. the whole index)
32 $filecount, //number of files
33 $indexcount, //number of docs in index
34 $dbcount, //number of docs in db
35 $types, //array of [document types => count]
36 $complete, //is index completely formed?
37 $time; //date index was generated
39 public function __construct($path = SEARCH_INDEX_PATH
) {
44 //test to see if there is a valid index on disk, at the specified path
46 $test_index = new Zend_Search_Lucene($this->path
, false);
48 } catch(Exception
$e) {
52 //retrieve file system info about the index if it is valid
54 $this->size
= display_size(get_directory_size($this->path
));
55 $index_dir = get_directory_list($this->path
, '', false, false);
56 $this->filecount
= count($index_dir);
57 $this->indexcount
= $test_index->count();
62 $this->indexcount
= 0;
65 $db_exists = false; //for now
67 //get all the current tables in moodle
68 $admin_tables = $db->MetaTables();
70 //TODO: use new IndexDBControl class for database checks?
72 //check if our search table exists
73 if (in_array($CFG->prefix
.SEARCH_DATABASE_TABLE
, $admin_tables)) {
74 //retrieve database information if it does
78 $this->dbcount
= count_records(SEARCH_DATABASE_TABLE
);
80 //individual document types
81 // $types = search_get_document_types();
82 $types = search_collect_searchables(true, false);
85 foreach($types as $type) {
86 $c = count_records(SEARCH_DATABASE_TABLE
, 'doctype', $type);
87 $this->types
[$type] = (int)$c;
91 $this->types
= array();
94 //check if the busy flag is set
95 if (isset($CFG->search_indexer_busy
) && $CFG->search_indexer_busy
== '1') {
96 $this->complete
= false;
98 $this->complete
= true;
101 //get the last run date for the indexer
102 if ($this->valid() && $CFG->search_indexer_run_date
) {
103 $this->time
= $CFG->search_indexer_run_date
;
110 * returns false on error, and the error message via referenced variable $err
111 * @param array $err array of errors
113 public function valid(&$err = null) {
117 if (!$this->is_valid_dir()) {
118 $err['dir'] = get_string('invalidindexerror', 'search');
122 if (!$this->is_valid_db()) {
123 $err['db'] = get_string('emptydatabaseerror', 'search');
127 if (!$this->complete
) {
128 $err['index'] = get_string('uncompleteindexingerror','search');
136 * is the index dir valid
139 public function is_valid_dir() {
140 if ($this->filecount
> 0) {
148 * is the db table valid
151 public function is_valid_db() {
152 if ($this->dbcount
> 0) {
160 * shorthand get method for the class variables
163 public function __get($var) {
164 if (in_array($var, array_keys(get_class_vars(get_class($this))))) {
172 * DB Index control class
174 * Used to control the search index database table
176 class IndexDBControl
{
179 * does the table exist?
183 public function checkTableExists() {
186 $table = SEARCH_DATABASE_TABLE
;
187 $tables = $db->MetaTables();
188 if (in_array($CFG->prefix
.$table, $tables)) {
197 * is our database setup valid?
199 * @deprecated Database is installed at install and should not be dropped out
201 public function checkDB() {
204 $sqlfile = "{$CFG->dirroot}/search/db/$CFG->dbtype.sql";
206 if ($this->checkTableExists()) {
207 execute_sql('drop table '.$CFG->prefix
.SEARCH_DATABASE_TABLE
, false);
210 //turn output buffering on - to hide modify_database() output
212 $ret = modify_database($sqlfile, '', false);
214 //chuck the buffer and resume normal operation
220 * add a document record to the table
221 * @param document must be a Lucene SearchDocument instance
224 public function addDocument($document=null) {
227 if ($document == null) {
231 // object to insert into db
232 $doc->doctype
= $document->doctype
;
233 $doc->docid
= $document->docid
;
234 $doc->itemtype
= $document->itemtype
;
235 $doc->title
= search_escape_string($document->title
);
236 $doc->url
= search_escape_string($document->url
);
237 $doc->updated
= time();
238 $doc->docdate
= $document->date
;
239 $doc->courseid
= $document->course_id
;
240 $doc->groupid
= $document->group_id
;
242 if ($doc->groupid
< 0) $doc->groupid
= 0;
244 //insert summary into db
245 $id = insert_record(SEARCH_DATABASE_TABLE
, $doc);
251 * remove a document record from the index
252 * @param document must be a Lucene document instance, or at least a dbid enveloppe
255 public function delDocument($document) {
258 delete_records(SEARCH_DATABASE_TABLE
, 'id', $document->dbid
);