[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/search/ -> lib.php (source)

   1  <?php
   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
  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  *
  17  */
  18  
  19  /*
  20  // function reference
  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 = "") {
  28  */
  29  
  30  define('SEARCH_INDEX_PATH', "$CFG->dataroot/search");
  31  define('SEARCH_DATABASE_TABLE', 'block_search_documents');
  32  
  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');
  51  
  52  /**
  53  * returns all the document type constants
  54  * @param prefix a pattern for recognizing constants
  55  * @return an array of type labels
  56  */
  57  function search_get_document_types($prefix = 'SEARCH_TYPE_') {
  58      $ret = array();
  59      foreach (get_defined_constants() as $key => $value) {
  60          if (preg_match("/^{$prefix}/", $key)){
  61              $ret[$key] = $value;
  62          } 
  63      } 
  64      sort($ret);
  65      return $ret;
  66  } //search_get_document_types
  67  
  68  /**
  69  * additional virtual modules to index
  70  *
  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
  74  * information.
  75  */
  76  function search_get_additional_modules() {
  77      $extras = array(/* additional keywords go here */);
  78      $ret = array();
  79      foreach($extras as $extra) {
  80          $temp->name = $extra;
  81          $ret[] = clone($temp);
  82      } 
  83      return $ret;
  84  } //search_get_additional_modules
  85  
  86  /**
  87  * shortens a url so it can fit on the results page
  88  * @param url the url
  89  * @param length the size limit we want
  90  */
  91  function search_shorten_url($url, $length=30) {
  92      return substr($url, 0, $length)."...";
  93  } //search_shorten_url
  94  
  95  /**
  96  * a local function for escaping
  97  * @param str the string to escape
  98  * @return the escaped string
  99  */
 100  function search_escape_string($str) {
 101      global $CFG;
 102  
 103      switch ($CFG->dbfamily) {
 104          case 'mysql':
 105              $s = mysql_real_escape_string($str);
 106              break;
 107          case 'postgres':
 108              $s = pg_escape_string($str);
 109              break;
 110          default:
 111              $s = addslashes($str);
 112      }
 113      return $s;
 114  } //search_escape_string
 115  
 116  /**
 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
 120  */
 121  function search_check_php5($feedback = false) {
 122      if (!check_php_version("5.0.0")) {
 123          if ($feedback) {
 124              print_heading(get_string('versiontoolow', 'search'));
 125          }
 126          return false;
 127      } 
 128      else {
 129        return true;
 130      } 
 131  } //search_check_php5
 132  
 133  /**
 134  * simple timer function, on first call, records a current microtime stamp, outputs result on 2nd call
 135  * @param cli an output formatting switch
 136  * @return void
 137  */
 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']);
 144      } 
 145      else {
 146          $GLOBALS['search_script_start_time'] = microtime(true);
 147      } 
 148  } //search_stopwatch
 149  
 150  /**
 151  * print and exit (for debugging)
 152  * @param str a variable to explore
 153  * @return void
 154  */
 155  function search_pexit($str = "") {
 156      if (is_array($str) or is_object($str)) {
 157          print_r($str);
 158      } else if ($str) {
 159          print $str."<br/>";
 160      }
 161      exit(0);
 162  } //search_pexit
 163  
 164  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7