Date field added to documents.
[moodle.git] / search / indexlib.php
CommitLineData
d9e1bf24 1<?php
2 /* Index info class
3 *
4 * Used to retrieve information about an index.
5 * Has methods to check for valid database and data directory,
6 * and the index itself.
7 * */
8
9 require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
10
11 class IndexInfo {
12 private $path, //index data directory
13 $size, //size of directory (i.e. the whole index)
14 $filecount, //number of files
15 $indexcount, //number of docs in index
16 $dbcount, //number of docs in db
17 $types, //array of [document types => count]
18 $complete, //is index completely formed?
19 $time; //date index was generated
20
21 public function __construct($path=SEARCH_INDEX_PATH) {
22 global $CFG, $db;
23
24 $this->path = $path;
25
26 try {
27 $test_index = new Zend_Search_Lucene($this->path, false);
28 $validindex = true;
29 } catch(Exception $e) {
30 $validindex = false;
31 } //catch
32
33 if ($validindex) {
34 $this->size = display_size(get_directory_size($this->path));
35 $index_dir = get_directory_list($this->path, '', false, false);
36 $this->filecount = count($index_dir);
37 $this->indexcount = $test_index->count();
38 } else {
39 $this->size = 0;
40 $this->filecount = 0;
41 $this->indexcount = 0;
42 } //else
43
44 $db_exists = false;
45
46 $admin_tables = $db->MetaTables();
47
48 if (in_array($CFG->prefix.'search_documents', $admin_tables)) {
49 $db_exists = true;
50
51 //total documents
52 $this->dbcount = count_records('search_documents');
53
54 //individual document types
55 $types = search_get_document_types();
56 sort($types);
57
58 foreach($types as $type) {
59 $c = count_records('search_documents', 'doctype', $type);
60 $this->types[$type] = (int)$c;
61 } //foreach
62 } else {
63 $this->dbcount = 0;
64 $this->types = array();
65 } //else
66
67 if ($CFG->search_indexer_busy == '1') {
68 $this->complete = false;
69 } else {
70 $this->complete = true;
71 } //if
72
73 if ($this->valid() && $CFG->search_indexer_run_date) {
74 $this->time = $CFG->search_indexer_run_date;
75 } else {
76 $this->time = 0;
77 } //else
78 } //__construct
79
80 public function valid(&$err=null) {
81 $err = array();
82 $ret = true;
83
84 if (!$this->is_valid_dir()) {
85 $err['dir'] = 'Index directory either contains an invalid index, or nothing at all.';
86 $ret = false;
87 } //if
88
89 if (!$this->is_valid_db()) {
90 $err['db'] = 'Database table is not present, or contains no index records.';
91 $ret = false;
92 } //if
93
94 if (!$this->complete) {
95 $err['index'] = 'Indexing was not successfully completed, please restart it.';
96 $ret = false;
97 } //if
98
99 return $ret;
100 } //valid
101
102 public function is_valid_dir() {
103 if ($this->filecount > 0) {
104 return true;
105 } else {
106 return false;
107 } //else
108 } //is_valid_dir
109
110 public function is_valid_db() {
111 if ($this->dbcount > 0) {
112 return true;
113 } else {
114 return false;
115 } //else
116 } //is_valid_db
117
118 public function __get($var) {
119 if (in_array($var, array_keys(get_class_vars(get_class($this))))) {
120 return $this->$var;
121 } //if
122 } //__get
123 } //IndexInfo
124
125?>