3 * Global Search Engine for Moodle
7 * @subpackage search_engine
8 * @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 * General function library
14 * This file must not contain any PHP 5, because it is used to test for PHP 5
15 * itself, and needs to be able to be executed on PHP 4 installations.
21 function search_get_document_types($prefix = 'SEARCH_TYPE_') {
22 function search_get_additional_modules() {
23 function search_shorten_url($url, $length=30) {
24 function search_escape_string($str) {
25 function search_check_php5($feedback = false) {
26 function search_stopwatch($cli = false) {
27 function search_pexit($str = "") {
30 define('SEARCH_INDEX_PATH', "$CFG->dataroot/search");
31 define('SEARCH_DATABASE_TABLE', 'block_search_documents');
33 //document types that can be searched
34 //define('SEARCH_TYPE_NONE', 'none');
35 define('SEARCH_TYPE_WIKI', 'wiki');
36 define('PATH_FOR_SEARCH_TYPE_WIKI', 'mod/wiki');
37 define('SEARCH_TYPE_FORUM', 'forum');
38 define('PATH_FOR_SEARCH_TYPE_FORUM', 'mod/forum');
39 define('SEARCH_TYPE_GLOSSARY', 'glossary');
40 define('PATH_FOR_SEARCH_TYPE_GLOSSARY', 'mod/glossary');
41 define('SEARCH_TYPE_RESOURCE', 'resource');
42 define('PATH_FOR_SEARCH_TYPE_RESOURCE', 'mod/resource');
43 define('SEARCH_TYPE_TECHPROJECT', 'techproject');
44 define('PATH_FOR_SEARCH_TYPE_TECHPROJECT', 'mod/techproject');
45 define('SEARCH_TYPE_DATA', 'data');
46 define('PATH_FOR_SEARCH_TYPE_DATA', 'mod/data');
47 define('SEARCH_TYPE_CHAT', 'chat');
48 define('PATH_FOR_SEARCH_TYPE_CHAT', 'mod/chat');
49 define('SEARCH_TYPE_LESSON', 'lesson');
50 define('PATH_FOR_SEARCH_TYPE_LESSON', 'mod/lesson');
53 * returns all the document type constants
54 * @param prefix a pattern for recognizing constants
55 * @return an array of type labels
57 function search_get_document_types($prefix = 'SEARCH_TYPE_') {
59 foreach (get_defined_constants() as $key => $value) {
60 if (preg_match("/^{$prefix}/", $key)){
66 } //search_get_document_types
69 * additional virtual modules to index
71 * By adding 'moo' to the extras array, an additional document type
72 * documents/moo_document.php will be indexed - this allows for
73 * virtual modules to be added to the index, i.e. non-module specific
76 function search_get_additional_modules() {
77 $extras = array(/* additional keywords go here */);
79 foreach($extras as $extra) {
81 $ret[] = clone($temp);
84 } //search_get_additional_modules
87 * shortens a url so it can fit on the results page
89 * @param length the size limit we want
91 function search_shorten_url($url, $length=30) {
92 return substr($url, 0, $length)."...";
93 } //search_shorten_url
96 * a local function for escaping
97 * @param str the string to escape
98 * @return the escaped string
100 function search_escape_string($str) {
103 switch ($CFG->dbfamily
) {
105 $s = mysql_real_escape_string($str);
108 $s = pg_escape_string($str);
111 $s = addslashes($str);
114 } //search_escape_string
117 * get a real php 5 version number, using 5.0.0 arbitrarily
118 * @param feedback if true, prints a feedback message to output.
119 * @return true if version of PHP is high enough
121 function search_check_php5($feedback = false) {
122 if (!check_php_version("5.0.0")) {
124 print_heading(get_string('versiontoolow', 'search'));
131 } //search_check_php5
134 * simple timer function, on first call, records a current microtime stamp, outputs result on 2nd call
135 * @param cli an output formatting switch
138 function search_stopwatch($cli = false) {
139 if (!empty($GLOBALS['search_script_start_time'])) {
140 if (!$cli) print '<em>';
141 print round(microtime(true) - $GLOBALS['search_script_start_time'], 6).' '.get_string('seconds', 'search');
142 if (!$cli) print '</em>';
143 unset($GLOBALS['search_script_start_time']);
146 $GLOBALS['search_script_start_time'] = microtime(true);
151 * print and exit (for debugging)
152 * @param str a variable to explore
155 function search_pexit($str = "") {
156 if (is_array($str) or is_object($str)) {