MDL-8682 - the last commit caused a regression that stopped it working.
[moodle.git] / search / lib.php
CommitLineData
682d4032 1<?php
2f338ab5 2/*
3* Author: Michael Champanis
4*
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.
7*
8* Reviewed by: Valery Fremaux (2007)
9* - adding techproject search capabilities
10* - adding full internationalization
11**/
12
13/*
14// function reference
15function search_get_document_types($prefix = 'SEARCH_TYPE_') {
16function search_get_additional_modules() {
17function search_shorten_url($url, $length=30) {
18function search_escape_string($str) {
19function search_check_php5($feedback = false) {
20function search_stopwatch($cli = false) {
21function search_pexit($str = "") {
22*/
23
24define('SEARCH_INDEX_PATH', "$CFG->dataroot/search");
690b644d 25define('SEARCH_DATABASE_TABLE', 'block_search_documents');
2f338ab5 26
27//document types that can be searched
28//define('SEARCH_TYPE_NONE', 'none');
29define('SEARCH_TYPE_WIKI', 'wiki');
30define('PATH_FOR_SEARCH_TYPE_WIKI', 'mod/wiki');
31define('SEARCH_TYPE_FORUM', 'forum');
32define('PATH_FOR_SEARCH_TYPE_FORUM', 'mod/forum');
33define('SEARCH_TYPE_GLOSSARY', 'glossary');
34define('PATH_FOR_SEARCH_TYPE_GLOSSARY', 'mod/glossary');
35define('SEARCH_TYPE_RESOURCE', 'resource');
36define('PATH_FOR_SEARCH_TYPE_RESOURCE', 'mod/resource');
37define('SEARCH_TYPE_TECHPROJECT', 'techproject');
38define('PATH_FOR_SEARCH_TYPE_TECHPROJECT', 'mod/techproject');
39define('SEARCH_TYPE_DATA', 'data');
40define('PATH_FOR_SEARCH_TYPE_DATA', 'mod/data');
41define('SEARCH_TYPE_CHAT', 'chat');
42define('PATH_FOR_SEARCH_TYPE_CHAT', 'mod/chat');
9daffe5f 43define('SEARCH_TYPE_LESSON', 'lesson');
44define('PATH_FOR_SEARCH_TYPE_LESSON', 'mod/lesson');
2f338ab5 45
46/**
47* returns all the document type constants
48* @param prefix a pattern for recognizing constants
49* @return an array of type labels
50*/
51function search_get_document_types($prefix = 'SEARCH_TYPE_') {
b585dc5f 52 $ret = array();
2f338ab5 53 foreach (get_defined_constants() as $key => $value) {
54 if (preg_match("/^{$prefix}/", $key)){
55 $ret[$key] = $value;
56 }
57 }
6e780562 58 sort($ret);
b585dc5f 59 return $ret;
2f338ab5 60} //search_get_document_types
61
62/**
63* additional virtual modules to index
64*
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.
69*/
70function search_get_additional_modules() {
c629cfed 71 $extras = array(/* additional keywords go here */);
72 $ret = array();
c629cfed 73 foreach($extras as $extra) {
2f338ab5 74 $temp->name = $extra;
75 $ret[] = clone($temp);
76 }
c629cfed 77 return $ret;
2f338ab5 78} //search_get_additional_modules
79
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
84*/
85function search_shorten_url($url, $length=30) {
682d4032 86 return substr($url, 0, $length)."...";
2f338ab5 87} //search_shorten_url
88
89/**
90* a local function for escaping
91* @param str the string to escape
92* @return the escaped string
93*/
94function search_escape_string($str) {
abb4ea20 95 global $CFG;
eef868d1 96
38b72419 97 switch ($CFG->dbfamily) {
2f338ab5 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);
106 }
abb4ea20 107 return $s;
2f338ab5 108} //search_escape_string
109
110/**
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
114*/
115function search_check_php5($feedback = false) {
682d4032 116 if (!check_php_version("5.0.0")) {
2f338ab5 117 if ($feedback) {
118 print_heading(get_string('versiontoolow', 'search'));
119 }
120 return false;
121 }
122 else {
682d4032 123 return true;
2f338ab5 124 }
125} //search_check_php5
126
127/**
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
131*/
132function search_stopwatch($cli = false) {
682d4032 133 if (!empty($GLOBALS['search_script_start_time'])) {
2f338ab5 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']);
138 }
139 else {
140 $GLOBALS['search_script_start_time'] = microtime(true);
141 }
142} //search_stopwatch
143
144/**
145* print and exit (for debugging)
146* @param str a variable to explore
147* @return void
148*/
149function search_pexit($str = "") {
682d4032 150 if (is_array($str) or is_object($str)) {
2f338ab5 151 print_r($str);
682d4032 152 } else if ($str) {
2f338ab5 153 print $str."<br/>";
154 }
682d4032 155 exit(0);
2f338ab5 156} //search_pexit
682d4032 157
38b72419 158?>