PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/search/indexlib.php

https://github.com/kpike/moodle
PHP | 262 lines | 132 code | 36 blank | 94 comment | 23 complexity | a52762e25cfbf19d9724fa948fd0a27a MD5 | raw file
  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. * @version prepared for 2.0
  11. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  12. *
  13. * Index info class
  14. *
  15. * Used to retrieve information about an index.
  16. * Has methods to check for valid database and data directory,
  17. * and the index itself.
  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. * main class for searchable information in the Lucene index
  26. */
  27. class IndexInfo {
  28. private $path, //index data directory
  29. $size, //size of directory (i.e. the whole index)
  30. $filecount, //number of files
  31. $indexcount, //number of docs in index
  32. $dbcount, //number of docs in db
  33. $types, //array of [document types => count]
  34. $complete, //is index completely formed?
  35. $time; //date index was generated
  36. public function __construct($path = SEARCH_INDEX_PATH) {
  37. global $CFG, $DB;
  38. $this->path = $path;
  39. //test to see if there is a valid index on disk, at the specified path
  40. try {
  41. $test_index = new Zend_Search_Lucene($this->path, false);
  42. $validindex = true;
  43. } catch(Exception $e) {
  44. $validindex = false;
  45. }
  46. //retrieve file system info about the index if it is valid
  47. if ($validindex) {
  48. $this->size = display_size(get_directory_size($this->path));
  49. $index_dir = get_directory_list($this->path, '', false, false);
  50. $this->filecount = count($index_dir);
  51. $this->indexcount = $test_index->count();
  52. }
  53. else {
  54. $this->size = 0;
  55. $this->filecount = 0;
  56. $this->indexcount = 0;
  57. }
  58. $db_exists = false; //for now
  59. //get all the current tables in moodle
  60. $admin_tables = $DB->get_tables();
  61. //check if our search table exists
  62. if (in_array(SEARCH_DATABASE_TABLE, $admin_tables)) {
  63. //retrieve database information if it does
  64. $db_exists = true;
  65. //total documents
  66. $this->dbcount = $DB->count_records(SEARCH_DATABASE_TABLE);
  67. //individual document types
  68. $types = search_collect_searchables(false, false);
  69. asort($types);
  70. foreach(array_keys($types) as $type) {
  71. $c = $DB->count_records(SEARCH_DATABASE_TABLE, array('doctype' => $type));
  72. $types[$type]->records = (int)$c;
  73. }
  74. $this->types = $types;
  75. } else {
  76. $this->dbcount = 0;
  77. $this->types = array();
  78. }
  79. //check if the busy flag is set
  80. if (isset($CFG->search_indexer_busy) && $CFG->search_indexer_busy == '1') {
  81. $this->complete = false;
  82. } else {
  83. $this->complete = true;
  84. }
  85. //get the last run date for the indexer
  86. if ($this->valid() && $CFG->search_indexer_run_date) {
  87. $this->time = $CFG->search_indexer_run_date;
  88. } else {
  89. $this->time = 0;
  90. }
  91. }
  92. /**
  93. * returns false on error, and the error message via referenced variable $err
  94. * @param array $err array of errors
  95. */
  96. public function valid(&$err = null) {
  97. $err = array();
  98. $ret = true;
  99. if (!$this->is_valid_dir()) {
  100. $err['dir'] = get_string('invalidindexerror', 'search');
  101. $ret = false;
  102. }
  103. if (!$this->is_valid_db()) {
  104. $err['db'] = get_string('emptydatabaseerror', 'search');
  105. $ret = false;
  106. }
  107. if (!$this->complete) {
  108. $err['index'] = get_string('uncompleteindexingerror','search');
  109. $ret = false;
  110. }
  111. return $ret;
  112. }
  113. /**
  114. * is the index dir valid
  115. *
  116. */
  117. public function is_valid_dir() {
  118. if ($this->filecount > 0) {
  119. return true;
  120. } else {
  121. return false;
  122. }
  123. }
  124. /**
  125. * is the db table valid
  126. *
  127. */
  128. public function is_valid_db() {
  129. if ($this->dbcount > 0) {
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }
  135. /**
  136. * shorthand get method for the class variables
  137. * @param object $var
  138. */
  139. public function __get($var) {
  140. if (in_array($var, array_keys(get_class_vars(get_class($this))))) {
  141. return $this->$var;
  142. }
  143. }
  144. }
  145. /**
  146. * DB Index control class
  147. *
  148. * Used to control the search index database table
  149. */
  150. class IndexDBControl {
  151. /**
  152. * does the table exist?
  153. * @deprecated
  154. * @uses $CFG, $DB
  155. */
  156. public function checkTableExists() {
  157. global $CFG, $DB;
  158. $tables = $DB->get_tables();
  159. if (in_array(SEARCH_DATABASE_TABLE, $tables)) {
  160. return true;
  161. }
  162. else {
  163. return false;
  164. }
  165. } //checkTableExists
  166. /**
  167. * NEVER USED
  168. *
  169. * is our database setup valid?
  170. * @uses db, CFG
  171. * @deprecated Database is installed at install and should not be dropped out
  172. *
  173. public function checkDB() {
  174. global $CFG, $db;
  175. $sqlfile = "{$CFG->dirroot}/search/db/$CFG->dbtype.sql";
  176. $ret = false;
  177. if ($this->checkTableExists()) {
  178. execute_sql('drop table '.SEARCH_DATABASE_TABLE, false);
  179. }
  180. //turn output buffering on - to hide modify_database() output
  181. ob_start();
  182. $ret = modify_database($sqlfile, '', false);
  183. //chuck the buffer and resume normal operation
  184. ob_end_clean();
  185. return $ret;
  186. } //checkDB */
  187. /**
  188. * add a document record to the table
  189. * @param document must be a Lucene SearchDocument instance
  190. * @uses $CFG, $DB
  191. */
  192. public function addDocument($document=null) {
  193. global $DB, $CFG;
  194. if ($document == null) {
  195. return false;
  196. }
  197. // object to insert into db
  198. $doc->doctype = $document->doctype;
  199. $doc->docid = $document->docid;
  200. $doc->itemtype = $document->itemtype;
  201. $doc->title = $document->title;
  202. $doc->url = $document->url;
  203. $doc->updated = time();
  204. $doc->docdate = $document->date;
  205. $doc->courseid = $document->course_id;
  206. $doc->groupid = $document->group_id;
  207. //insert summary into db
  208. $table = SEARCH_DATABASE_TABLE;
  209. $id = $DB->insert_record($table, $doc);
  210. return $id;
  211. }
  212. /**
  213. * remove a document record from the index
  214. * @param document must be a Lucene document instance, or at least a dbid enveloppe
  215. * @uses $DB
  216. */
  217. public function delDocument($document) {
  218. global $DB;
  219. $table = SEARCH_DATABASE_TABLE;
  220. $DB->delete_records($table, array('id' => $document->dbid));
  221. }
  222. }
  223. ?>