[ Index ]

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

title

Body

[close]

/search/documents/ -> wiki_document.php (source)

   1  <?php
   2  /**
   3  * Global Search Engine for Moodle
   4  *
   5  * @package search
   6  * @category core
   7  * @subpackage document_wrappers
   8  * @author Michael Campanis (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  * document handling for wiki activity module
  13  * This file contains the mapping between a wiki page and it's indexable counterpart,
  14  * e.g. searchdocument->title = wikipage->pagename
  15  *
  16  * Functions for iterating and retrieving the necessary records are now also included
  17  * in this file, rather than mod/wiki/lib.php
  18  */
  19  
  20  /**
  21  * includes and requires
  22  */
  23  require_once("$CFG->dirroot/search/documents/document.php");
  24  require_once("$CFG->dirroot/mod/wiki/lib.php");
  25  
  26  /**
  27  * All the $doc->___ fields are required by the base document class!
  28  * Each and every module that requires search functionality must correctly
  29  * map their internal fields to the five $doc fields (id, title, author, contents
  30  * and url). Any module specific data can be added to the $data object, which is
  31  * serialised into a binary field in the index.
  32  */
  33  class WikiSearchDocument extends SearchDocument {
  34      public function __construct(&$page, $wiki_id, $course_id, $group_id, $user_id, $context_id) {
  35          // generic information; required
  36          $doc->docid         = $page['id'];
  37          $doc->documenttype  = SEARCH_TYPE_WIKI;
  38          $doc->itemtype      = 'standard';
  39          $doc->contextid     = $context_id;
  40  
  41          $doc->title     = $page['pagename'];
  42          $doc->date      = $page['lastmodified'];
  43          //remove '(ip.ip.ip.ip)' from wiki author field
  44          $doc->author    = preg_replace('/\(.*?\)/', '', $page['author']);
  45          $doc->contents  = $page['content'];
  46          $doc->url       = wiki_make_link($wiki_id, $page['pagename'], $page['version']);
  47          
  48          // module specific information; optional
  49          $data->version  = $page['version'];
  50          $data->wiki     = $wiki_id;
  51          
  52          // construct the parent class
  53          parent::__construct($doc, $data, $course_id, $group_id, $user_id, PATH_FOR_SEARCH_TYPE_WIKI);
  54      } 
  55  }
  56  
  57  /**
  58  * converts a page name to cope Wiki constraints. Transforms spaces in plus.
  59  * @param str the name to convert
  60  * @return the converted name
  61  */
  62  function wiki_name_convert($str) {
  63      return str_replace(' ', '+', $str);
  64  }
  65  
  66  /**
  67  * constructs a valid link to a wiki content
  68  * @param int $wikiId
  69  * @param string $title
  70  * @param int $version
  71  * @uses CFG
  72  */
  73  function wiki_make_link($wikiId, $title, $version) {
  74      global $CFG;
  75  
  76      return $CFG->wwwroot.'/mod/wiki/view.php?wid='.$wikiId.'&amp;page='.wiki_name_convert($title).'&amp;version='.$version;
  77  } //wiki_make_link
  78  
  79  /**
  80  * rescued and converted from ewikimoodlelib.php
  81  * retrieves latest version of a page
  82  * @param object $entry the wiki object as a reference
  83  * @param string $pagename the name of the page known by the wiki engine
  84  * @param int $version
  85  */
  86  function wiki_get_latest_page(&$entry, $pagename, $version = 0) {
  87      $pagename = "'".addslashes($pagename)."'";
  88      
  89      if ($version > 0 and is_int($version)) {
  90          $version = "AND (version=$version)";
  91      } else {
  92          $version = '';
  93      } 
  94      
  95      $select = "(pagename=$pagename) AND wiki=".$entry->id." $version ";
  96      $sort   = 'version DESC';
  97      
  98      //change this to recordset_select, as per http://docs.moodle.org/en/Datalib_Notes
  99      if ($result_arr = get_records_select('wiki_pages', $select, $sort, '*', 0, 1)) {
 100          foreach ($result_arr as $obj) {
 101              $result_obj = $obj;
 102          } 
 103      } 
 104      
 105      if (isset($result_obj))  {
 106          $result_obj->meta = @unserialize($result_obj->meta);
 107          return $result_obj;
 108      } else {
 109          return false;
 110      } 
 111  }
 112  
 113  /**
 114  * fetches all pages, including old versions
 115  * @param object $entry the wiki object as a reference
 116  * @return an array of record objects that represents pages of this wiki object
 117  */
 118  function wiki_get_pages(&$entry) {
 119      return get_records('wiki_pages', 'wiki', $entry->id);
 120  }
 121  
 122  /**
 123  * fetches all the latest versions of all the pages
 124  * @param object $entry
 125  */
 126  function wiki_get_latest_pages(&$entry) {
 127    //== (My)SQL for this
 128    /* select * from wiki_pages
 129       inner join
 130      (select wiki_pages.pagename, max(wiki_pages.version) as ver
 131      from wiki_pages group by pagename) as a
 132      on ((wiki_pages.version = a.ver) and
 133      (wiki_pages.pagename like a.pagename)) */
 134  
 135      $pages = array();
 136      
 137      //http://moodle.org/bugs/bug.php?op=show&bugid=5877&pos=0
 138      if ($ids = get_records('wiki_pages', 'wiki', $entry->id, '', 'distinct pagename')) {
 139          if ($pagesets = get_records('wiki_pages', 'wiki', $entry->id, '', 'distinct pagename')) {
 140              foreach ($pagesets as $aPageset) {
 141                  $pages[] = wiki_get_latest_page($entry, $aPageset->pagename);
 142              } 
 143          } else {
 144              return false;
 145          } 
 146      }
 147      return $pages;
 148  }
 149  
 150  /**
 151  * part of search engine API
 152  *
 153  */
 154  function wiki_iterator() {
 155      $wikis = get_records('wiki');
 156      return $wikis;
 157  }
 158  
 159  /**
 160  * part of search engine API
 161  * @param wiki a wiki instance
 162  * @return an array of searchable deocuments
 163  */
 164  function wiki_get_content_for_index(&$wiki) {
 165  
 166      $documents = array();
 167      $entries = wiki_get_entries($wiki);
 168      if ($entries){
 169          $coursemodule = get_field('modules', 'id', 'name', 'wiki');
 170          $cm = get_record('course_modules', 'course', $wiki->course, 'module', $coursemodule, 'instance', $wiki->id);
 171          $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 172          foreach($entries as $entry) {
 173      
 174              //all pages
 175              //$pages = wiki_get_pages($entry);
 176              
 177              //latest pages
 178              $pages = wiki_get_latest_pages($entry);
 179              if (is_array($pages)) {
 180                  foreach($pages as $page) {
 181                      if (strlen($page->content) > 0) {
 182                          $documents[] = new WikiSearchDocument(get_object_vars($page), $entry->wikiid, $entry->course, $entry->groupid, $page->userid, $context->id);
 183                      } 
 184                  } 
 185              } 
 186          } 
 187      }
 188      return $documents;
 189  }
 190  
 191  /**
 192  * returns a single wiki search document based on a wiki_entry id
 193  * @param id the id of the wiki
 194  * @param itemtype the type of information (standard)
 195  * @return a searchable document
 196  */
 197  function wiki_single_document($id, $itemtype) {
 198      $page = get_record('wiki_pages', 'id', $id);
 199      $entry = get_record('wiki_entries', 'id', $page->wiki);
 200      $coursemodule = get_field('modules', 'id', 'name', 'wiki');
 201      $cm = get_record('course_modules', 'course', $entry->course, 'module', $coursemodule, 'instance', $entry->wikiid);
 202      $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 203      return new WikiSearchDocument(get_object_vars($page), $entry->wikiid, $entry->course, $entry->groupid, $page->userid, $context->id);
 204  }
 205  
 206  /**
 207  * dummy delete function that packs id with itemtype.
 208  * this was here for a reason, but I can't remember it at the moment.
 209  *
 210  */
 211  function wiki_delete($info, $itemtype) {
 212      $object->id = $info;
 213      $object->itemtype = $itemtype;
 214      return $object;
 215  }
 216  
 217  //returns the var names needed to build a sql query for addition/deletions
 218  function wiki_db_names() {
 219      //[primary id], [table name], [time created field name], [time modified field name]
 220      return array(array('id', 'wiki_pages', 'created', 'lastmodified', 'standard'));
 221  }
 222  
 223  /**
 224  * this function handles the access policy to contents indexed as searchable documents. If this 
 225  * function does not exist, the search engine assumes access is allowed.
 226  * When this point is reached, we already know that : 
 227  * - user is legitimate in the surrounding context
 228  * - user may be guest and guest access is allowed to the module
 229  * - the function may perform local checks within the module information logic
 230  * @param path the access path to the module script code
 231  * @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
 232  * @param this_id the item id within the information class denoted by itemtype. In wikies, this id 
 233  * points out the indexed wiki page.
 234  * @param user the user record denoting the user who searches
 235  * @param group_id the current group used by the user when searching
 236  * @uses CFG
 237  * @return true if access is allowed, false elsewhere
 238  */
 239  function wiki_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
 240      global $CFG;
 241      
 242      // get the wiki object and all related stuff
 243      $page = get_record('wiki_pages', 'id', $id);
 244      $entry = get_record('wiki_entries', 'id', $page->wiki);
 245      $course = get_record('course', 'id', $entry->course);
 246      $context = get_record('context', 'id', $context_id);
 247      $cm = get_record('course_modules', 'id', $context->instanceid);
 248      // $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
 249      // $context = get_record('context', 'id', $cm->id);
 250      if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
 251          if (!empty($CFG->search_access_debug)) echo "search reject : hidden wiki ";
 252          return false;
 253      }
 254      
 255      //group consistency check : checks the following situations about groups
 256      // trap if user is not same group and groups are separated
 257      $current_group = get_current_group($course->id);
 258      if ((groupmode($course) == SEPARATEGROUPS) && $group_id != $current_group && !has_capability('moodle/site:accessallgroups', $context)) {
 259          if (!empty($CFG->search_access_debug)) echo "search reject : separated group owner wiki ";
 260          return false;
 261      }
 262          
 263      return true;
 264  }
 265  
 266  /**
 267  * this call back is called when displaying the link for some last post processing
 268  *
 269  */
 270  function wiki_link_post_processing($title){
 271       return mb_convert_encoding($title, 'UTF-8', 'auto');
 272  }
 273  
 274  ?>


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