Lucene Zend Implementation update (better handle of UTF8)
[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 * */
eef868d1 9
b585dc5f 10 define('SEARCH_INDEX_PATH', "$CFG->dataroot/search");
0961c4e6 11 define('SEARCH_DATABASE_TABLE', 'search_documents');
eef868d1 12
13 //document types that can be searched
6e780562 14 //define('SEARCH_TYPE_NONE', 'none');
b585dc5f 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');
eef868d1 19
682d4032 20 //returns all the document type constants
b585dc5f 21 function search_get_document_types($prefix='SEARCH_TYPE') {
22 $ret = array();
eef868d1 23
b585dc5f 24 foreach (get_defined_constants() as $key=>$value) {
25 if (substr($key, 0, strlen($prefix)) == $prefix) {
26 $ret[$key] = $value;
27 } //if
28 } //foreach
eef868d1 29
6e780562 30 sort($ret);
eef868d1 31
b585dc5f 32 return $ret;
682d4032 33 } //search_get_document_types
eef868d1 34
c629cfed 35 // additional virtual modules to index
36 //
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();
eef868d1 44
c629cfed 45 foreach($extras as $extra) {
46 $temp->name = $extra;
47 $ret[] = clone($temp);
48 } //foreach
eef868d1 49
c629cfed 50 return $ret;
eef868d1 51 } //search_get_additional_modules
52
682d4032 53 //shortens a url so it can fit on the results page
eef868d1 54 function search_shorten_url($url, $length=30) {
682d4032 55 return substr($url, 0, $length)."...";
56 } //search_shorten_url
eef868d1 57
abb4ea20 58 function search_escape_string($str) {
59 global $CFG;
eef868d1 60
38b72419 61 switch ($CFG->dbfamily) {
abb4ea20 62 case 'mysql':
63 $s = mysql_real_escape_string($str);
64 break;
38b72419 65 case 'postgres':
abb4ea20 66 $s = pg_escape_string($str);
67 break;
68 default:
69 $s = addslashes($str);
70 } //switch
eef868d1 71
abb4ea20 72 return $s;
73 } //search_escape_string
682d4032 74
eef868d1 75 //get a real php 5 version number, using 5.0.0 arbitrarily
682d4032 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
eef868d1 82
682d4032 83 return false;
84 } else {
85 return true;
86 } //else
87 } //search_check_php5
eef868d1 88
682d4032 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>';
eef868d1 95
682d4032 96 unset($GLOBALS['search_script_start_time']);
97 } else {
98 $GLOBALS['search_script_start_time'] = microtime(true);
99 } //else
100 } //search_stopwatch
eef868d1 101
682d4032 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) {
71502268 107 print $str."<br/>";
682d4032 108 } //if
eef868d1 109
682d4032 110 exit(0);
111 } //search_pexit
112
38b72419 113?>