forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
197 lines (177 loc) · 6.38 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* Global Search Engine for Moodle
*
* @package search
* @category core
* @subpackage search_engine
* @author Michael Champanis (mchampan) [[email protected]], Valery Fremaux [[email protected]] > 1.8
* @date 2008/03/31
* @version prepared for 2.0
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*
* General function library
*
* This file must not contain any PHP 5, because it is used to test for PHP 5
* itself, and needs to be able to be executed on PHP 4 installations.
*
*/
/**
* Constants
*/
define('SEARCH_INDEX_PATH', $CFG->dataroot.'/search');
define('SEARCH_DATABASE_TABLE', 'block_search_documents');
// get document types
include_once $CFG->dirroot.'/search/searchtypes.php';
/**
* collects all searchable items identities
* @param boolean $namelist if true, only returns list of names of searchable items
* @param boolean $verbose if true, prints a discovering status
* @return an array of names or an array of type descriptors
*/
function search_collect_searchables($namelist=false, $verbose=true){
global $CFG, $DB;
$searchables = array();
$searchables_names = array();
/// get all installed modules
if ($mods = $DB->get_records('modules', null, 'name', 'id,name')){
$searchabletypes = array_values(search_get_document_types());
foreach($mods as $mod){
$plugin = new StdClass();
$plugin->name = $mod->name;
$plugin->type = 'mod';
if (in_array($mod->name, $searchabletypes)){
$plugin->location = 'internal';
$searchables[$plugin->name] = $plugin;
$searchables_names[] = $mod->name;
} else {
$documentfile = $CFG->dirroot."/mod/{$mod->name}/search_document.php";
$plugin->location = 'mod';
if (file_exists($documentfile)){
$searchables[$plugin->name] = $plugin;
$searchables_names[] = $mod->name;
}
}
}
if ($verbose) mtrace(count($searchables).' modules to search in / '.count($mods).' modules found.');
}
/// collects blocks as indexable information may be found in blocks either
if ($blocks = $DB->get_records('block', null, 'name', 'id,name')) {
$blocks_searchables = array();
// prepend the "block_" prefix to discriminate document type plugins
foreach($blocks as $block){
$plugin = new StdClass();
$plugin->dirname = $block->name;
$plugin->name = 'block_'.$block->name;
if (in_array('SEARCH_TYPE_'.strtoupper($block->name), $searchabletypes)){
$plugin->location = 'internal';
$plugin->type = 'block';
$blocks_searchables[$plugin->name] = $plugin;
$searchables_names[] = $plugin->name;
} else {
$documentfile = $CFG->dirroot."/blocks/{$plugin->dirname}/search_document.php";
if (file_exists($documentfile)){
$plugin->location = 'blocks';
$plugin->type = 'block';
$blocks_searchables[$plugin->name] = $plugin;
$searchables_names[] = $plugin->name;
}
}
}
if ($verbose) mtrace(count($blocks_searchables).' blocks to search in / '.count($blocks).' blocks found.');
$searchables = array_merge($searchables, $blocks_searchables);
}
/// add virtual modules onto the back of the array
$additional = search_get_additional_modules($searchables_names);
if (!empty($additional)){
if ($verbose) mtrace(count($additional).' additional to search in.');
$searchables = array_merge($searchables, $additional);
}
if ($namelist)
return $searchables_names;
return $searchables;
}
/**
* returns all the document type constants that are known in core implementation
* @param prefix a pattern for recognizing constants
* @return an array of type labels
*/
function search_get_document_types($prefix = 'SEARCH_TYPE_') {
$ret = array();
foreach (get_defined_constants() as $key => $value) {
if (preg_match("/^{$prefix}/", $key)){
$ret[$key] = $value;
}
}
sort($ret);
return $ret;
}
/**
* additional virtual modules to index
*
* By adding 'moo' to the extras array, an additional document type
* documents/moo_document.php will be indexed - this allows for
* virtual modules to be added to the index, i.e. non-module specific
* information.
*/
function search_get_additional_modules(&$searchables_names) {
$extras = array(/* additional keywords go here */);
if (defined('SEARCH_EXTRAS')){
$extras = explode(',', SEARCH_EXTRAS);
}
$ret = array();
$temp = new StdClass;
foreach($extras as $extra) {
$plugin = new StdClass();
$plugin->name = $extra;
$plugin->location = 'internal';
eval('$plugin->type = TYPE_FOR_SEARCH_TYPE_'.strtoupper($extra).';');
$ret[$plugin->name] = $plugin;
$searchables_names[] = $extra;
}
return $ret;
}
/**
* shortens a url so it can fit on the results page
* @param url the url
* @param length the size limit we want
*/
function search_shorten_url($url, $length=30) {
return substr($url, 0, $length)."...";
}
/**
* simple timer function, on first call, records a current microtime stamp, outputs result on 2nd call
* @param cli an output formatting switch
* @return void
*/
function search_stopwatch($cli = false) {
if (!empty($GLOBALS['search_script_start_time'])) {
if (!$cli) print '<em>';
print round(microtime(true) - $GLOBALS['search_script_start_time'], 6).' '.get_string('seconds', 'search');
if (!$cli) print '</em>';
unset($GLOBALS['search_script_start_time']);
} else {
$GLOBALS['search_script_start_time'] = microtime(true);
}
}
/**
* print and exit (for debugging)
* @param str a variable to explore
* @return void
*/
function search_pexit($str = "") {
if (is_array($str) or is_object($str)) {
print_r($str);
} else if ($str) {
print $str."<br/>";
}
exit(0);
}
function search_updatedcallback($name) {
global $CFG, $DB;
// set block to hidden when global search is disabled.
if ($CFG->enableglobalsearch != 1) {
$DB->set_field('block', 'visible', 0, array('name'=>'search')); // Hide block
}
}
?>