MDL-14128, cannot use a full string in the param.
[moodle-linuxchix.git] / search / lib.php
blobaf40f8f1d9ff4faaf809865e11904983e0a45057
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 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 * General function library
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.
20 // function reference
21 function search_get_document_types($prefix = 'SEARCH_TYPE_') {
22 function search_get_additional_modules() {
23 function search_shorten_url($url, $length=30) {
24 function search_escape_string($str) {
25 function search_check_php5($feedback = false) {
26 function search_stopwatch($cli = false) {
27 function search_pexit($str = "") {
30 define('SEARCH_INDEX_PATH', "$CFG->dataroot/search");
31 define('SEARCH_DATABASE_TABLE', 'block_search_documents');
33 //document types that can be searched
34 //define('SEARCH_TYPE_NONE', 'none');
35 define('SEARCH_TYPE_WIKI', 'wiki');
36 define('PATH_FOR_SEARCH_TYPE_WIKI', 'mod/wiki');
37 define('SEARCH_TYPE_FORUM', 'forum');
38 define('PATH_FOR_SEARCH_TYPE_FORUM', 'mod/forum');
39 define('SEARCH_TYPE_GLOSSARY', 'glossary');
40 define('PATH_FOR_SEARCH_TYPE_GLOSSARY', 'mod/glossary');
41 define('SEARCH_TYPE_RESOURCE', 'resource');
42 define('PATH_FOR_SEARCH_TYPE_RESOURCE', 'mod/resource');
43 define('SEARCH_TYPE_TECHPROJECT', 'techproject');
44 define('PATH_FOR_SEARCH_TYPE_TECHPROJECT', 'mod/techproject');
45 define('SEARCH_TYPE_DATA', 'data');
46 define('PATH_FOR_SEARCH_TYPE_DATA', 'mod/data');
47 define('SEARCH_TYPE_CHAT', 'chat');
48 define('PATH_FOR_SEARCH_TYPE_CHAT', 'mod/chat');
49 define('SEARCH_TYPE_LESSON', 'lesson');
50 define('PATH_FOR_SEARCH_TYPE_LESSON', 'mod/lesson');
52 /**
53 * returns all the document type constants
54 * @param prefix a pattern for recognizing constants
55 * @return an array of type labels
57 function search_get_document_types($prefix = 'SEARCH_TYPE_') {
58 $ret = array();
59 foreach (get_defined_constants() as $key => $value) {
60 if (preg_match("/^{$prefix}/", $key)){
61 $ret[$key] = $value;
64 sort($ret);
65 return $ret;
66 } //search_get_document_types
68 /**
69 * additional virtual modules to index
71 * By adding 'moo' to the extras array, an additional document type
72 * documents/moo_document.php will be indexed - this allows for
73 * virtual modules to be added to the index, i.e. non-module specific
74 * information.
76 function search_get_additional_modules() {
77 $extras = array(/* additional keywords go here */);
78 $ret = array();
79 foreach($extras as $extra) {
80 $temp->name = $extra;
81 $ret[] = clone($temp);
83 return $ret;
84 } //search_get_additional_modules
86 /**
87 * shortens a url so it can fit on the results page
88 * @param url the url
89 * @param length the size limit we want
91 function search_shorten_url($url, $length=30) {
92 return substr($url, 0, $length)."...";
93 } //search_shorten_url
95 /**
96 * a local function for escaping
97 * @param str the string to escape
98 * @return the escaped string
100 function search_escape_string($str) {
101 global $CFG;
103 switch ($CFG->dbfamily) {
104 case 'mysql':
105 $s = mysql_real_escape_string($str);
106 break;
107 case 'postgres':
108 $s = pg_escape_string($str);
109 break;
110 default:
111 $s = addslashes($str);
113 return $s;
114 } //search_escape_string
117 * get a real php 5 version number, using 5.0.0 arbitrarily
118 * @param feedback if true, prints a feedback message to output.
119 * @return true if version of PHP is high enough
121 function search_check_php5($feedback = false) {
122 if (!check_php_version("5.0.0")) {
123 if ($feedback) {
124 print_heading(get_string('versiontoolow', 'search'));
126 return false;
128 else {
129 return true;
131 } //search_check_php5
134 * simple timer function, on first call, records a current microtime stamp, outputs result on 2nd call
135 * @param cli an output formatting switch
136 * @return void
138 function search_stopwatch($cli = false) {
139 if (!empty($GLOBALS['search_script_start_time'])) {
140 if (!$cli) print '<em>';
141 print round(microtime(true) - $GLOBALS['search_script_start_time'], 6).' '.get_string('seconds', 'search');
142 if (!$cli) print '</em>';
143 unset($GLOBALS['search_script_start_time']);
145 else {
146 $GLOBALS['search_script_start_time'] = microtime(true);
148 } //search_stopwatch
151 * print and exit (for debugging)
152 * @param str a variable to explore
153 * @return void
155 function search_pexit($str = "") {
156 if (is_array($str) or is_object($str)) {
157 print_r($str);
158 } else if ($str) {
159 print $str."<br/>";
161 exit(0);
162 } //search_pexit