3 * Author: Michael Champanis
5 * This file must not contain any PHP 5, because it is used to test for PHP 5
6 * itself, and needs to be able to be executed on PHP 4 installations.
8 * Reviewed by: Valery Fremaux (2007)
9 * - adding techproject search capabilities
10 * - adding full internationalization
15 function search_get_document_types($prefix = 'SEARCH_TYPE_') {
16 function search_get_additional_modules() {
17 function search_shorten_url($url, $length=30) {
18 function search_escape_string($str) {
19 function search_check_php5($feedback = false) {
20 function search_stopwatch($cli = false) {
21 function search_pexit($str = "") {
24 define('SEARCH_INDEX_PATH', "$CFG->dataroot/search");
25 define('SEARCH_DATABASE_TABLE', 'block_search_documents');
27 //document types that can be searched
28 //define('SEARCH_TYPE_NONE', 'none');
29 define('SEARCH_TYPE_WIKI', 'wiki');
30 define('PATH_FOR_SEARCH_TYPE_WIKI', 'mod/wiki');
31 define('SEARCH_TYPE_FORUM', 'forum');
32 define('PATH_FOR_SEARCH_TYPE_FORUM', 'mod/forum');
33 define('SEARCH_TYPE_GLOSSARY', 'glossary');
34 define('PATH_FOR_SEARCH_TYPE_GLOSSARY', 'mod/glossary');
35 define('SEARCH_TYPE_RESOURCE', 'resource');
36 define('PATH_FOR_SEARCH_TYPE_RESOURCE', 'mod/resource');
37 define('SEARCH_TYPE_TECHPROJECT', 'techproject');
38 define('PATH_FOR_SEARCH_TYPE_TECHPROJECT', 'mod/techproject');
39 define('SEARCH_TYPE_DATA', 'data');
40 define('PATH_FOR_SEARCH_TYPE_DATA', 'mod/data');
41 define('SEARCH_TYPE_CHAT', 'chat');
42 define('PATH_FOR_SEARCH_TYPE_CHAT', 'mod/chat');
43 define('SEARCH_TYPE_LESSON', 'lesson');
44 define('PATH_FOR_SEARCH_TYPE_LESSON', 'mod/lesson');
47 * returns all the document type constants
48 * @param prefix a pattern for recognizing constants
49 * @return an array of type labels
51 function search_get_document_types($prefix = 'SEARCH_TYPE_') {
53 foreach (get_defined_constants() as $key => $value) {
54 if (preg_match("/^{$prefix}/", $key)){
60 } //search_get_document_types
63 * additional virtual modules to index
65 * By adding 'moo' to the extras array, an additional document type
66 * documents/moo_document.php will be indexed - this allows for
67 * virtual modules to be added to the index, i.e. non-module specific
70 function search_get_additional_modules() {
71 $extras = array(/* additional keywords go here */);
73 foreach($extras as $extra) {
75 $ret[] = clone($temp);
78 } //search_get_additional_modules
81 * shortens a url so it can fit on the results page
83 * @param length the size limit we want
85 function search_shorten_url($url, $length=30) {
86 return substr($url, 0, $length)."...";
87 } //search_shorten_url
90 * a local function for escaping
91 * @param str the string to escape
92 * @return the escaped string
94 function search_escape_string($str) {
97 switch ($CFG->dbfamily
) {
99 $s = mysql_real_escape_string($str);
102 $s = pg_escape_string($str);
105 $s = addslashes($str);
108 } //search_escape_string
111 * get a real php 5 version number, using 5.0.0 arbitrarily
112 * @param feedback if true, prints a feedback message to output.
113 * @return true if version of PHP is high enough
115 function search_check_php5($feedback = false) {
116 if (!check_php_version("5.0.0")) {
118 print_heading(get_string('versiontoolow', 'search'));
125 } //search_check_php5
128 * simple timer function, on first call, records a current microtime stamp, outputs result on 2nd call
129 * @param cli an output formatting switch
132 function search_stopwatch($cli = false) {
133 if (!empty($GLOBALS['search_script_start_time'])) {
134 if (!$cli) print '<em>';
135 print round(microtime(true) - $GLOBALS['search_script_start_time'], 6).' '.get_string('seconds', 'search');
136 if (!$cli) print '</em>';
137 unset($GLOBALS['search_script_start_time']);
140 $GLOBALS['search_script_start_time'] = microtime(true);
145 * print and exit (for debugging)
146 * @param str a variable to explore
149 function search_pexit($str = "") {
150 if (is_array($str) or is_object($str)) {