MDL-12701:
[moodle-linuxchix.git] / search / lib.php
blob69365d26b95978c5841f946c83540174116d0a24
1 <?php
2 /*
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
11 **/
14 // function reference
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');
46 /**
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_') {
52 $ret = array();
53 foreach (get_defined_constants() as $key => $value) {
54 if (preg_match("/^{$prefix}/", $key)){
55 $ret[$key] = $value;
58 sort($ret);
59 return $ret;
60 } //search_get_document_types
62 /**
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
68 * information.
70 function search_get_additional_modules() {
71 $extras = array(/* additional keywords go here */);
72 $ret = array();
73 foreach($extras as $extra) {
74 $temp->name = $extra;
75 $ret[] = clone($temp);
77 return $ret;
78 } //search_get_additional_modules
80 /**
81 * shortens a url so it can fit on the results page
82 * @param url the url
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
89 /**
90 * a local function for escaping
91 * @param str the string to escape
92 * @return the escaped string
94 function search_escape_string($str) {
95 global $CFG;
97 switch ($CFG->dbfamily) {
98 case 'mysql':
99 $s = mysql_real_escape_string($str);
100 break;
101 case 'postgres':
102 $s = pg_escape_string($str);
103 break;
104 default:
105 $s = addslashes($str);
107 return $s;
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")) {
117 if ($feedback) {
118 print_heading(get_string('versiontoolow', 'search'));
120 return false;
122 else {
123 return true;
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
130 * @return void
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']);
139 else {
140 $GLOBALS['search_script_start_time'] = microtime(true);
142 } //search_stopwatch
145 * print and exit (for debugging)
146 * @param str a variable to explore
147 * @return void
149 function search_pexit($str = "") {
150 if (is_array($str) or is_object($str)) {
151 print_r($str);
152 } else if ($str) {
153 print $str."<br/>";
155 exit(0);
156 } //search_pexit