Removed breaking unit test, originally set up to test xhtml validity throughout the...
[moodle-pu.git] / search / lib.php
blob8cdd62c9df925953967a2279224fccfa214988d5
1 <?php
3 /* Move this stuff to lib/searchlib.php?
4 * Author: Michael Champanis
6 * This file must not contain any PHP 5, because it is used to test for PHP 5
7 * itself, and needs to be able to be executed on PHP 4 installations.
8 * */
10 define('SEARCH_INDEX_PATH', "$CFG->dataroot/search");
11 define('SEARCH_DATABASE_TABLE', 'search_documents');
13 //document types that can be searched
14 //define('SEARCH_TYPE_NONE', 'none');
15 define('SEARCH_TYPE_WIKI', 'wiki');
16 define('SEARCH_TYPE_FORUM', 'forum');
17 define('SEARCH_TYPE_GLOSSARY', 'glossary');
18 define('SEARCH_TYPE_RESOURCE', 'resource');
20 //returns all the document type constants
21 function search_get_document_types($prefix='SEARCH_TYPE') {
22 $ret = array();
24 foreach (get_defined_constants() as $key=>$value) {
25 if (substr($key, 0, strlen($prefix)) == $prefix) {
26 $ret[$key] = $value;
27 } //if
28 } //foreach
30 sort($ret);
32 return $ret;
33 } //search_get_document_types
35 // additional virtual modules to index
37 // By adding 'moo' to the extras array, an additional document type
38 // documents/moo_document.php will be indexed - this allows for
39 // virtual modules to be added to the index, i.e. non-module specific
40 // information.
41 function search_get_additional_modules() {
42 $extras = array(/* additional keywords go here */);
43 $ret = array();
45 foreach($extras as $extra) {
46 $temp->name = $extra;
47 $ret[] = clone($temp);
48 } //foreach
50 return $ret;
51 } //search_get_additional_modules
53 //shortens a url so it can fit on the results page
54 function search_shorten_url($url, $length=30) {
55 return substr($url, 0, $length)."...";
56 } //search_shorten_url
58 function search_escape_string($str) {
59 global $CFG;
61 switch ($CFG->dbfamily) {
62 case 'mysql':
63 $s = mysql_real_escape_string($str);
64 break;
65 case 'postgres':
66 $s = pg_escape_string($str);
67 break;
68 default:
69 $s = addslashes($str);
70 } //switch
72 return $s;
73 } //search_escape_string
75 //get a real php 5 version number, using 5.0.0 arbitrarily
76 function search_check_php5($feedback=false) {
77 if (!check_php_version("5.0.0")) {
78 if ($feedback) {
79 $phpversion = phpversion();
80 print_heading("Sorry, global search requires PHP 5.0.0 or later (currently using version $phpversion)");
81 } //if
83 return false;
84 } else {
85 return true;
86 } //else
87 } //search_check_php5
89 //simple timer function, outputs result on 2nd call
90 function search_stopwatch($cli = false) {
91 if (!empty($GLOBALS['search_script_start_time'])) {
92 if (!$cli) print '<em>';
93 print round(microtime(true) - $GLOBALS['search_script_start_time'], 6).' seconds';
94 if (!$cli) print '</em>';
96 unset($GLOBALS['search_script_start_time']);
97 } else {
98 $GLOBALS['search_script_start_time'] = microtime(true);
99 } //else
100 } //search_stopwatch
102 //print and exit (for debugging)
103 function search_pexit($str = "") {
104 if (is_array($str) or is_object($str)) {
105 print_r($str);
106 } else if ($str) {
107 print $str."<br/>";
108 } //if
110 exit(0);
111 } //search_pexit