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