MDL-10680:
[moodle-linuxchix.git] / search / lib.php
blobb9e6f9195c66716a0763900e980a0d46a5651942
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', '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');
44 /**
45 * returns all the document type constants
46 * @param prefix a pattern for recognizing constants
47 * @return an array of type labels
49 function search_get_document_types($prefix = 'SEARCH_TYPE_') {
50 $ret = array();
51 foreach (get_defined_constants() as $key => $value) {
52 if (preg_match("/^{$prefix}/", $key)){
53 $ret[$key] = $value;
56 sort($ret);
57 return $ret;
58 } //search_get_document_types
60 /**
61 * additional virtual modules to index
63 * By adding 'moo' to the extras array, an additional document type
64 * documents/moo_document.php will be indexed - this allows for
65 * virtual modules to be added to the index, i.e. non-module specific
66 * information.
68 function search_get_additional_modules() {
69 $extras = array(/* additional keywords go here */);
70 $ret = array();
71 foreach($extras as $extra) {
72 $temp->name = $extra;
73 $ret[] = clone($temp);
75 return $ret;
76 } //search_get_additional_modules
78 /**
79 * shortens a url so it can fit on the results page
80 * @param url the url
81 * @param length the size limit we want
83 function search_shorten_url($url, $length=30) {
84 return substr($url, 0, $length)."...";
85 } //search_shorten_url
87 /**
88 * a local function for escaping
89 * @param str the string to escape
90 * @return the escaped string
92 function search_escape_string($str) {
93 global $CFG;
95 switch ($CFG->dbfamily) {
96 case 'mysql':
97 $s = mysql_real_escape_string($str);
98 break;
99 case 'postgres':
100 $s = pg_escape_string($str);
101 break;
102 default:
103 $s = addslashes($str);
105 return $s;
106 } //search_escape_string
109 * get a real php 5 version number, using 5.0.0 arbitrarily
110 * @param feedback if true, prints a feedback message to output.
111 * @return true if version of PHP is high enough
113 function search_check_php5($feedback = false) {
114 if (!check_php_version("5.0.0")) {
115 if ($feedback) {
116 print_heading(get_string('versiontoolow', 'search'));
118 return false;
120 else {
121 return true;
123 } //search_check_php5
126 * simple timer function, on first call, records a current microtime stamp, outputs result on 2nd call
127 * @param cli an output formatting switch
128 * @return void
130 function search_stopwatch($cli = false) {
131 if (!empty($GLOBALS['search_script_start_time'])) {
132 if (!$cli) print '<em>';
133 print round(microtime(true) - $GLOBALS['search_script_start_time'], 6).' '.get_string('seconds', 'search');
134 if (!$cli) print '</em>';
135 unset($GLOBALS['search_script_start_time']);
137 else {
138 $GLOBALS['search_script_start_time'] = microtime(true);
140 } //search_stopwatch
143 * print and exit (for debugging)
144 * @param str a variable to explore
145 * @return void
147 function search_pexit($str = "") {
148 if (is_array($str) or is_object($str)) {
149 print_r($str);
150 } else if ($str) {
151 print $str."<br/>";
153 exit(0);
154 } //search_pexit