| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Global Search Engine for Moodle 4 * 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 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License 11 * 12 * Index info class 13 * 14 * Used to retrieve information about an index. 15 * Has methods to check for valid database and data directory, 16 * and the index itself. 17 */ 18 19 /** 20 * includes and requires 21 */ 22 require_once("$CFG->dirroot/search/lib.php"); 23 require_once("$CFG->dirroot/search/Zend/Search/Lucene.php"); 24 25 /** 26 * main class for searchable information in the Lucene index 27 */ 28 class IndexInfo { 29 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 38 39 public function __construct($path = SEARCH_INDEX_PATH) { 40 global $CFG, $db; 41 42 $this->path = $path; 43 44 //test to see if there is a valid index on disk, at the specified path 45 try { 46 $test_index = new Zend_Search_Lucene($this->path, false); 47 $validindex = true; 48 } catch(Exception $e) { 49 $validindex = false; 50 } 51 52 //retrieve file system info about the index if it is valid 53 if ($validindex) { 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(); 58 } 59 else { 60 $this->size = 0; 61 $this->filecount = 0; 62 $this->indexcount = 0; 63 } 64 65 $db_exists = false; //for now 66 67 //get all the current tables in moodle 68 $admin_tables = $db->MetaTables(); 69 70 //TODO: use new IndexDBControl class for database checks? 71 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 75 $db_exists = true; 76 77 //total documents 78 $this->dbcount = count_records(SEARCH_DATABASE_TABLE); 79 80 //individual document types 81 $types = search_get_document_types(); 82 sort($types); 83 84 foreach($types as $type) { 85 $c = count_records(SEARCH_DATABASE_TABLE, 'doctype', $type); 86 $this->types[$type] = (int)$c; 87 } 88 } else { 89 $this->dbcount = 0; 90 $this->types = array(); 91 } 92 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; 98 } 99 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; 105 } 106 } 107 108 /** 109 * returns false on error, and the error message via referenced variable $err 110 * @param array $err array of errors 111 */ 112 public function valid(&$err = null) { 113 $err = array(); 114 $ret = true; 115 116 if (!$this->is_valid_dir()) { 117 $err['dir'] = get_string('invalidindexerror', 'search'); 118 $ret = false; 119 } 120 121 if (!$this->is_valid_db()) { 122 $err['db'] = get_string('emptydatabaseerror', 'search'); 123 $ret = false; 124 } 125 126 if (!$this->complete) { 127 $err['index'] = get_string('uncompleteindexingerror','search'); 128 $ret = false; 129 } 130 131 return $ret; 132 } 133 134 /** 135 * is the index dir valid 136 * 137 */ 138 public function is_valid_dir() { 139 if ($this->filecount > 0) { 140 return true; 141 } else { 142 return false; 143 } 144 } 145 146 /** 147 * is the db table valid 148 * 149 */ 150 public function is_valid_db() { 151 if ($this->dbcount > 0) { 152 return true; 153 } else { 154 return false; 155 } 156 } 157 158 /** 159 * shorthand get method for the class variables 160 * @param object $var 161 */ 162 public function __get($var) { 163 if (in_array($var, array_keys(get_class_vars(get_class($this))))) { 164 return $this->$var; 165 } 166 } 167 } 168 169 170 /** 171 * DB Index control class 172 * 173 * Used to control the search index database table 174 */ 175 class IndexDBControl { 176 177 /** 178 * does the table exist? 179 * @deprecated 180 * @uses CFG, db 181 */ 182 public function checkTableExists() { 183 global $CFG, $db; 184 185 $table = SEARCH_DATABASE_TABLE; 186 $tables = $db->MetaTables(); 187 if (in_array($CFG->prefix.$table, $tables)) { 188 return true; 189 } 190 else { 191 return false; 192 } 193 } //checkTableExists 194 195 /** 196 * is our database setup valid? 197 * @uses db, CFG 198 * @deprecated Database is installed at install and should not be dropped out 199 */ 200 public function checkDB() { 201 global $CFG, $db; 202 203 $sqlfile = "{$CFG->dirroot}/search/db/$CFG->dbtype.sql"; 204 $ret = false; 205 if ($this->checkTableExists()) { 206 execute_sql('drop table '.$CFG->prefix.SEARCH_DATABASE_TABLE, false); 207 } 208 209 //turn output buffering on - to hide modify_database() output 210 ob_start(); 211 $ret = modify_database($sqlfile, '', false); 212 213 //chuck the buffer and resume normal operation 214 ob_end_clean(); 215 return $ret; 216 } //checkDB 217 218 /** 219 * add a document record to the table 220 * @param document must be a Lucene SearchDocument instance 221 * @uses db, CFG 222 */ 223 public function addDocument($document=null) { 224 global $db, $CFG; 225 226 if ($document == null) { 227 return false; 228 } 229 230 // object to insert into db 231 $doc->doctype = $document->doctype; 232 $doc->docid = $document->docid; 233 $doc->itemtype = $document->itemtype; 234 $doc->title = search_escape_string($document->title); 235 $doc->url = search_escape_string($document->url); 236 $doc->updated = time(); 237 $doc->docdate = $document->date; 238 $doc->courseid = $document->course_id; 239 $doc->groupid = $document->group_id; 240 241 //insert summary into db 242 $id = insert_record(SEARCH_DATABASE_TABLE, $doc); 243 244 return $id; 245 } 246 247 /** 248 * remove a document record from the index 249 * @param document must be a Lucene document instance, or at least a dbid enveloppe 250 * @uses db 251 */ 252 public function delDocument($document) { 253 global $db; 254 255 delete_records(SEARCH_DATABASE_TABLE, 'id', $document->dbid); 256 } 257 } 258 259 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |