MDL-11719 increased size of idnumber in user table - needed for some enrolment plugin...
[moodle.git] / search / lib.php
CommitLineData
682d4032 1<?php
3319ef85 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* General function library
2f338ab5 13*
14* This file must not contain any PHP 5, because it is used to test for PHP 5
15* itself, and needs to be able to be executed on PHP 4 installations.
16*
3319ef85 17*/
2f338ab5 18
19/*
20// function reference
21function search_get_document_types($prefix = 'SEARCH_TYPE_') {
22function search_get_additional_modules() {
23function search_shorten_url($url, $length=30) {
24function search_escape_string($str) {
8f41cc02 25function search_check_php5($feedback = false) {
2f338ab5 26function search_stopwatch($cli = false) {
27function search_pexit($str = "") {
28*/
29
30define('SEARCH_INDEX_PATH', "$CFG->dataroot/search");
690b644d 31define('SEARCH_DATABASE_TABLE', 'block_search_documents');
2f338ab5 32
8f41cc02 33// get document types
34include "{$CFG->dirroot}/search/searchtypes.php";
35
36/**
37* collects all searchable items identities
38* @param boolean $namelist
39* @param boolean $verbose
40* @return an array of names or an array of type descriptors
41*/
42function search_collect_searchables($namelist=false, $verbose=true){
43 global $CFG;
44
45 $searchables = array();
46 $searchables_names = array();
47
48/// get all installed modules
49 if ($mods = get_records('modules', '', '', 'name', 'id,name')){
50
51 $searchabletypes = array_values(search_get_document_types());
52
53 foreach($mods as $mod){
54 if (in_array($mod->name, $searchabletypes)){
55 $mod->location = 'internal';
56 $searchables[] = $mod;
57 $searchables_names[] = $mod->name;
58 } else {
59 $documentfile = $CFG->dirroot."/mod/{$mod->name}/search_document.php";
60 $mod->location = 'mod';
61 if (file_exists($documentfile)){
62 $searchables[] = $mod;
63 $searchables_names[] = $mod->name;
64 }
65 }
66 }
67 if ($verbose) mtrace(count($searchables).' modules to search in / '.count($mods).' modules found.');
68 }
69
70/// collects blocks as indexable information may be found in blocks either
71 if ($blocks = get_records('block', '', '', 'name', 'id,name')) {
72 $blocks_searchables = array();
73 // prepend the "block_" prefix to discriminate document type plugins
74 foreach($blocks as $block){
75 $block->dirname = $block->name;
76 $block->name = 'block_'.$block->name;
77 if (in_array('SEARCH_TYPE_'.strtoupper($block->name), $searchabletypes)){
78 $mod->location = 'internal';
79 $blocks_searchables[] = $block;
80 $searchables_names[] = $block->name;
81 } else {
82 $documentfile = $CFG->dirroot."/blocks/{$block->dirname}/search_document.php";
83 if (file_exists($documentfile)){
84 $mod->location = 'blocks';
85 $blocks_searchables[] = $block;
86 $searchables_names[] = $block->name;
87 }
88 }
89 }
90 if ($verbose) mtrace(count($blocks_searchables).' blocks to search in / '.count($blocks).' blocks found.');
91 $searchables = array_merge($searchables, $blocks_searchables);
92 }
93
94/// add virtual modules onto the back of the array
95
96 $additional = search_get_additional_modules();
97 if (!empty($additional)){
98 if ($verbose) mtrace(count($additional).' additional to search in.');
99 $searchables = array_merge($searchables, $additional);
100 }
101
102 if ($namelist)
103 return $searchables_names;
104 return $searchables;
105}
2f338ab5 106
107/**
108* returns all the document type constants
109* @param prefix a pattern for recognizing constants
110* @return an array of type labels
111*/
112function search_get_document_types($prefix = 'SEARCH_TYPE_') {
b585dc5f 113 $ret = array();
2f338ab5 114 foreach (get_defined_constants() as $key => $value) {
115 if (preg_match("/^{$prefix}/", $key)){
116 $ret[$key] = $value;
117 }
118 }
6e780562 119 sort($ret);
b585dc5f 120 return $ret;
8f41cc02 121}
2f338ab5 122
123/**
124* additional virtual modules to index
125*
126* By adding 'moo' to the extras array, an additional document type
127* documents/moo_document.php will be indexed - this allows for
128* virtual modules to be added to the index, i.e. non-module specific
129* information.
130*/
131function search_get_additional_modules() {
c629cfed 132 $extras = array(/* additional keywords go here */);
8f41cc02 133 if (defined('SEARCH_EXTRAS')){
134 $extras = explode(',', SEARCH_EXTRAS);
135 }
136
c629cfed 137 $ret = array();
c629cfed 138 foreach($extras as $extra) {
2f338ab5 139 $temp->name = $extra;
8f41cc02 140 $temp->location = 'internal';
2f338ab5 141 $ret[] = clone($temp);
142 }
c629cfed 143 return $ret;
2f338ab5 144} //search_get_additional_modules
145
146/**
147* shortens a url so it can fit on the results page
148* @param url the url
149* @param length the size limit we want
150*/
151function search_shorten_url($url, $length=30) {
682d4032 152 return substr($url, 0, $length)."...";
2f338ab5 153} //search_shorten_url
154
155/**
156* a local function for escaping
157* @param str the string to escape
158* @return the escaped string
159*/
160function search_escape_string($str) {
abb4ea20 161 global $CFG;
eef868d1 162
38b72419 163 switch ($CFG->dbfamily) {
2f338ab5 164 case 'mysql':
165 $s = mysql_real_escape_string($str);
166 break;
167 case 'postgres':
168 $s = pg_escape_string($str);
169 break;
170 default:
171 $s = addslashes($str);
172 }
abb4ea20 173 return $s;
2f338ab5 174} //search_escape_string
175
8f41cc02 176/**
177* get a real php 5 version number, using 5.0.0 arbitrarily
178* @param feedback if true, prints a feedback message to output.
179* @return true if version of PHP is high enough
180*/
181function search_check_php5($feedback = false) {
182 if (!check_php_version("5.0.0")) {
183 if ($feedback) {
184 print_heading(get_string('versiontoolow', 'search'));
185 }
186 return false;
187 }
188 else {
189 return true;
190 }
191} //search_check_php5
192
2f338ab5 193/**
194* simple timer function, on first call, records a current microtime stamp, outputs result on 2nd call
195* @param cli an output formatting switch
196* @return void
197*/
198function search_stopwatch($cli = false) {
682d4032 199 if (!empty($GLOBALS['search_script_start_time'])) {
2f338ab5 200 if (!$cli) print '<em>';
201 print round(microtime(true) - $GLOBALS['search_script_start_time'], 6).' '.get_string('seconds', 'search');
202 if (!$cli) print '</em>';
203 unset($GLOBALS['search_script_start_time']);
204 }
205 else {
206 $GLOBALS['search_script_start_time'] = microtime(true);
207 }
208} //search_stopwatch
209
210/**
211* print and exit (for debugging)
212* @param str a variable to explore
213* @return void
214*/
215function search_pexit($str = "") {
682d4032 216 if (is_array($str) or is_object($str)) {
2f338ab5 217 print_r($str);
682d4032 218 } else if ($str) {
2f338ab5 219 print $str."<br/>";
220 }
682d4032 221 exit(0);
2f338ab5 222} //search_pexit
682d4032 223
38b72419 224?>