MDL-14666 "overview report is very inefficient when view detailed marks is selected...
[moodle.git] / search / lib.php
CommitLineData
682d4032 1<?php
3319ef85 2/**
3* Global Search Engine for Moodle
4*
5* @package search
6* @category core
7* @subpackage search_engine
8* @author Michael Champanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
9* @date 2008/03/31
10* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11*
12* General function library
2f338ab5 13*
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.
16*
3319ef85 17*/
2f338ab5 18
19/*
20// function reference
21function search_get_document_types($prefix = 'SEARCH_TYPE_') {
22function search_get_additional_modules() {
23function search_shorten_url($url, $length=30) {
24function search_escape_string($str) {
2f338ab5 25function search_stopwatch($cli = false) {
26function search_pexit($str = "") {
27*/
28
29define('SEARCH_INDEX_PATH', "$CFG->dataroot/search");
690b644d 30define('SEARCH_DATABASE_TABLE', 'block_search_documents');
2f338ab5 31
32//document types that can be searched
33//define('SEARCH_TYPE_NONE', 'none');
34define('SEARCH_TYPE_WIKI', 'wiki');
35define('PATH_FOR_SEARCH_TYPE_WIKI', 'mod/wiki');
36define('SEARCH_TYPE_FORUM', 'forum');
37define('PATH_FOR_SEARCH_TYPE_FORUM', 'mod/forum');
38define('SEARCH_TYPE_GLOSSARY', 'glossary');
39define('PATH_FOR_SEARCH_TYPE_GLOSSARY', 'mod/glossary');
40define('SEARCH_TYPE_RESOURCE', 'resource');
41define('PATH_FOR_SEARCH_TYPE_RESOURCE', 'mod/resource');
42define('SEARCH_TYPE_TECHPROJECT', 'techproject');
43define('PATH_FOR_SEARCH_TYPE_TECHPROJECT', 'mod/techproject');
44define('SEARCH_TYPE_DATA', 'data');
45define('PATH_FOR_SEARCH_TYPE_DATA', 'mod/data');
46define('SEARCH_TYPE_CHAT', 'chat');
47define('PATH_FOR_SEARCH_TYPE_CHAT', 'mod/chat');
9daffe5f 48define('SEARCH_TYPE_LESSON', 'lesson');
49define('PATH_FOR_SEARCH_TYPE_LESSON', 'mod/lesson');
2f338ab5 50
51/**
52* returns all the document type constants
53* @param prefix a pattern for recognizing constants
54* @return an array of type labels
55*/
56function search_get_document_types($prefix = 'SEARCH_TYPE_') {
b585dc5f 57 $ret = array();
2f338ab5 58 foreach (get_defined_constants() as $key => $value) {
59 if (preg_match("/^{$prefix}/", $key)){
60 $ret[$key] = $value;
61 }
62 }
6e780562 63 sort($ret);
b585dc5f 64 return $ret;
2f338ab5 65} //search_get_document_types
66
67/**
68* additional virtual modules to index
69*
70* By adding 'moo' to the extras array, an additional document type
71* documents/moo_document.php will be indexed - this allows for
72* virtual modules to be added to the index, i.e. non-module specific
73* information.
74*/
75function search_get_additional_modules() {
c629cfed 76 $extras = array(/* additional keywords go here */);
77 $ret = array();
c629cfed 78 foreach($extras as $extra) {
2f338ab5 79 $temp->name = $extra;
80 $ret[] = clone($temp);
81 }
c629cfed 82 return $ret;
2f338ab5 83} //search_get_additional_modules
84
85/**
86* shortens a url so it can fit on the results page
87* @param url the url
88* @param length the size limit we want
89*/
90function search_shorten_url($url, $length=30) {
682d4032 91 return substr($url, 0, $length)."...";
2f338ab5 92} //search_shorten_url
93
94/**
95* a local function for escaping
96* @param str the string to escape
97* @return the escaped string
98*/
99function search_escape_string($str) {
abb4ea20 100 global $CFG;
eef868d1 101
38b72419 102 switch ($CFG->dbfamily) {
2f338ab5 103 case 'mysql':
104 $s = mysql_real_escape_string($str);
105 break;
106 case 'postgres':
107 $s = pg_escape_string($str);
108 break;
109 default:
110 $s = addslashes($str);
111 }
abb4ea20 112 return $s;
2f338ab5 113} //search_escape_string
114
2f338ab5 115/**
116* simple timer function, on first call, records a current microtime stamp, outputs result on 2nd call
117* @param cli an output formatting switch
118* @return void
119*/
120function search_stopwatch($cli = false) {
682d4032 121 if (!empty($GLOBALS['search_script_start_time'])) {
2f338ab5 122 if (!$cli) print '<em>';
123 print round(microtime(true) - $GLOBALS['search_script_start_time'], 6).' '.get_string('seconds', 'search');
124 if (!$cli) print '</em>';
125 unset($GLOBALS['search_script_start_time']);
126 }
127 else {
128 $GLOBALS['search_script_start_time'] = microtime(true);
129 }
130} //search_stopwatch
131
132/**
133* print and exit (for debugging)
134* @param str a variable to explore
135* @return void
136*/
137function search_pexit($str = "") {
682d4032 138 if (is_array($str) or is_object($str)) {
2f338ab5 139 print_r($str);
682d4032 140 } else if ($str) {
2f338ab5 141 print $str."<br/>";
142 }
682d4032 143 exit(0);
2f338ab5 144} //search_pexit
682d4032 145
38b72419 146?>