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 * @version prepared for 2.0
11 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
15 * Used to retrieve information about an index.
16 * Has methods to check for valid database and data directory,
17 * and the index itself.
21 * includes and requires
23 require_once($CFG->dirroot
.'/search/lib.php');
24 require_once($CFG->dirroot
.'/search/Zend/Search/Lucene.php');
27 * main class for searchable information in the Lucene index
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
) {
45 //test to see if there is a valid index on disk, at the specified path
47 $test_index = new Zend_Search_Lucene($this->path
, false);
49 } catch(Exception
$e) {
53 //retrieve file system info about the index if it is valid
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();
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
77 $this->dbcount
= $DB->count_records(SEARCH_DATABASE_TABLE
);
79 //individual document types
80 $types = search_collect_searchables(false, false);
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;
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;
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
;
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) {
116 if (!$this->is_valid_dir()) {
117 $err['dir'] = get_string('invalidindexerror', 'search');
121 if (!$this->is_valid_db()) {
122 $err['db'] = get_string('emptydatabaseerror', 'search');
126 if (!$this->complete
) {
127 $err['index'] = get_string('uncompleteindexingerror','search');
135 * is the index dir valid
138 public function is_valid_dir() {
139 if ($this->filecount
> 0) {
147 * is the db table valid
150 public function is_valid_db() {
151 if ($this->dbcount
> 0) {
159 * shorthand get method for the class variables
162 public function __get($var) {
163 if (in_array($var, array_keys(get_class_vars(get_class($this))))) {
171 * DB Index control class
173 * Used to control the search index database table
175 class IndexDBControl
{
178 * does the table exist?
182 public function checkTableExists() {
185 $tables = $DB->get_tables();
186 if (in_array(SEARCH_DATABASE_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 '.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
= $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);
250 * remove a document record from the index
251 * @param document must be a Lucene document instance, or at least a dbid enveloppe
254 public function delDocument($document) {
257 $table = SEARCH_DATABASE_TABLE
;
258 $DB->delete_records($table, array('id' => $document->dbid
));