MDL-30026 improve session lock - unit tests & minor comment fixup
[moodle.git] / search / stats.php
blob8ecb097f6f008a7c8e5582645be4d61589577d34
1 <?php
2 /**
3 * Global Search Engine for Moodle
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
13 * Prints some basic statistics about the current index.
14 * Does some diagnostics if you are logged in as an administrator.
18 /**
19 * includes and requires
21 require_once('../config.php');
22 require_once($CFG->dirroot.'/search/lib.php');
24 $block_instanceid = required_param('block_instanceid', PARAM_INT);// Block Instance ID
26 /// checks global search is enabled
28 if ($CFG->forcelogin) {
29 require_login();
32 if (empty($CFG->enableglobalsearch)) {
33 print_error('globalsearchdisabled', 'search');
35 //Check user's permissions against the block instance from which the user came
36 if (empty($block_instanceid)) {
37 print_error('searchnotpermitted', 'search');
39 if (!$DB->record_exists('block_instances', array('id' => $block_instanceid, 'blockname' => 'search'))) {
40 print_error('searchnotpermitted', 'search');
42 $contextblock = get_context_instance(CONTEXT_BLOCK, $block_instanceid);
43 require_capability('moodle/block:view', $contextblock);
45 /// check for php5, but don't die yet
47 require_once($CFG->dirroot.'/search/indexlib.php');
49 $indexinfo = new IndexInfo();
51 $site = get_site();
53 $strsearch = get_string('search', 'search');
54 $strquery = get_string('statistics', 'search');
56 $site = get_site();
58 $url = new moodle_url('/search/stats.php');
59 $url->param('block_instanceid', $block_instanceid);
60 $PAGE->set_url($url);
62 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
63 $PAGE->navbar->add($strsearch, new moodle_url('/search/query.php?block_instanceid=' . $block_instanceid));
64 $PAGE->navbar->add($strquery, new moodle_url('/search/stats.php?block_instanceid=' . $block_instanceid));
65 $PAGE->set_title($strsearch);
66 $PAGE->set_heading($site->fullname);
67 echo $OUTPUT->header();
69 /// keep things pretty, even if php5 isn't available
71 echo $OUTPUT->box_start();
72 echo $OUTPUT->heading($strquery);
74 echo $OUTPUT->box_start();
76 $databasestr = get_string('database', 'search');
77 $documentsinindexstr = get_string('documentsinindex', 'search');
78 $deletionsinindexstr = get_string('deletionsinindex', 'search');
79 $documentsindatabasestr = get_string('documentsindatabase', 'search');
80 $databasestatestr = get_string('databasestate', 'search');
82 /// this table is only for admins, shows index directory size and location
84 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
85 $datadirectorystr = get_string('datadirectory', 'search');
86 $inindexdirectorystr = get_string('filesinindexdirectory', 'search');
87 $totalsizestr = get_string('totalsize', 'search');
88 $errorsstr = get_string('errors', 'search');
89 $solutionsstr = get_string('solutions', 'search');
90 $checkdirstr = get_string('checkdir', 'search');
91 $checkdbstr = get_string('checkdb', 'search');
92 $checkdiradvicestr = get_string('checkdiradvice', 'search');
93 $checkdbadvicestr = get_string('checkdbadvice', 'search');
94 $runindexerteststr = get_string('runindexertest', 'search');
95 $runindexerstr = get_string('runindexer', 'search');
97 $admin_table = new html_table();
98 $admin_table->tablealign = 'center';
99 $admin_table->align = array ('right', 'left');
100 $admin_table->wrap = array ('nowrap', 'nowrap');
101 $admin_table->cellpadding = 5;
102 $admin_table->cellspacing = 0;
103 $admin_table->width = '500';
105 $admin_table->data[] = array("<strong>{$datadirectorystr}</strong>", '<em><strong>'.$indexinfo->path.'</strong></em>');
106 $admin_table->data[] = array($inindexdirectorystr, $indexinfo->filecount);
107 $admin_table->data[] = array($totalsizestr, $indexinfo->size);
109 if ($indexinfo->time > 0) {
110 $admin_table->data[] = array(get_string('createdon', 'search'), date('r', $indexinfo->time));
112 else {
113 $admin_table->data[] = array(get_string('createdon', 'search'), '-');
116 if (!$indexinfo->valid($errors)) {
117 $admin_table->data[] = array("<strong>{$errorsstr}</strong>", '&nbsp;');
118 foreach ($errors as $key => $value) {
119 $admin_table->data[] = array($key.' ... ', $value);
123 echo html_writer::table($admin_table);
124 $spacer = array('height'=>20, 'br'=>true);
125 echo $OUTPUT->spacer($spacer); // should be done with CSS instead
126 echo $OUTPUT->heading($solutionsstr);
128 unset($admin_table->data);
129 if (isset($errors['dir'])) {
130 $admin_table->data[] = array($checkdirstr, $checkdiradvicestr);
132 if (isset($errors['db'])) {
133 $admin_table->data[] = array($checkdbstr, $checkdbadvicestr);
136 $admin_table->data[] = array($runindexerteststr, '<a href="tests/index.php" target="_blank">tests/index.php</a>');
137 $admin_table->data[] = array($runindexerstr, '<a href="indexersplash.php" target="_blank">indexersplash.php</a>');
139 echo html_writer::table($admin_table);
140 echo $OUTPUT->spacer($spacer) . '<br />';
143 /// this is the standard summary table for normal users, shows document counts
145 $table = new html_table();
146 $table->tablealign = 'center';
147 $table->align = array ('right', 'left');
148 $table->wrap = array ('nowrap', 'nowrap');
149 $table->cellpadding = 5;
150 $table->cellspacing = 0;
151 $table->width = '500';
153 $table->data[] = array("<strong>{$databasestr}</strong>", "<em><strong>{$CFG->prefix}".SEARCH_DATABASE_TABLE.'</strong></em>');
155 /// add extra fields if we're admin
157 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
158 //don't want to confuse users if the two totals don't match (hint: they should)
159 $table->data[] = array($documentsinindexstr, $indexinfo->indexcount);
161 //*cough* they should match if deletions were actually removed from the index,
162 //as it turns out, they're only marked as deleted and not returned in search results
163 $table->data[] = array($deletionsinindexstr, (int)$indexinfo->indexcount - (int)$indexinfo->dbcount);
166 $table->data[] = array($documentsindatabasestr, $indexinfo->dbcount);
168 foreach($indexinfo->types as $type) {
169 if ($type->type == 'mod'){
170 $table->data[] = array(get_string('documentsfor', 'search') . " '".get_string('modulenameplural', $type->name)."'", $type->records);
171 } else if ($type->type == 'block') {
172 $table->data[] = array(get_string('documentsfor', 'search') . " '".get_string('pluginname', $type->name)."'", $type->records);
173 } else {
174 $table->data[] = array(get_string('documentsfor', 'search') . " '".get_string($type->name)."'", $type->records);
179 echo $OUTPUT->heading($databasestatestr);
180 echo html_writer::table($table);
182 echo $OUTPUT->box_end();
183 echo $OUTPUT->box_end();
184 echo $OUTPUT->footer();