3 * Global Search Engine for Moodle
7 * @subpackage document_wrappers
8 * @author Michael Campanis (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 * 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
16 * Functions for iterating and retrieving the necessary records are now also included
17 * in this file, rather than mod/wiki/lib.php
21 * includes and requires
23 require_once("$CFG->dirroot/search/documents/document.php");
24 require_once("$CFG->dirroot/mod/wiki/lib.php");
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.
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;
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']);
48 // module specific information; optional
49 $data->version
= $page['version'];
50 $data->wiki
= $wiki_id;
52 // construct the parent class
53 parent
::__construct($doc, $data, $course_id, $group_id, $user_id, PATH_FOR_SEARCH_TYPE_WIKI
);
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
62 function wiki_name_convert($str) {
63 return str_replace(' ', '+', $str);
67 * constructs a valid link to a wiki content
69 * @param string $title
73 function wiki_make_link($wikiId, $title, $version) {
76 return $CFG->wwwroot
.'/mod/wiki/view.php?wid='.$wikiId.'&page='.wiki_name_convert($title).'&version='.$version;
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
86 function wiki_get_latest_page(&$entry, $pagename, $version = 0) {
87 $pagename = "'".addslashes($pagename)."'";
89 if ($version > 0 and is_int($version)) {
90 $version = "AND (version=$version)";
95 $select = "(pagename=$pagename) AND wiki=".$entry->id
." $version ";
96 $sort = 'version DESC';
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) {
105 if (isset($result_obj)) {
106 $result_obj->meta
= @unserialize
($result_obj->meta
);
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
118 function wiki_get_pages(&$entry) {
119 return get_records('wiki_pages', 'wiki', $entry->id
);
123 * fetches all the latest versions of all the pages
124 * @param object $entry
126 function wiki_get_latest_pages(&$entry) {
127 //== (My)SQL for this
128 /* select * from wiki_pages
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)) */
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
);
151 * part of search engine API
154 function wiki_iterator() {
155 $wikis = get_records('wiki');
160 * part of search engine API
161 * @param wiki a wiki instance
162 * @return an array of searchable deocuments
164 function wiki_get_content_for_index(&$wiki) {
166 $documents = array();
167 $entries = wiki_get_entries($wiki);
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) {
175 //$pages = wiki_get_pages($entry);
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
);
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
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
);
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.
211 function wiki_delete($info, $itemtype) {
213 $object->itemtype
= $itemtype;
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'));
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
237 * @return true if access is allowed, false elsewhere
239 function wiki_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
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 ";
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 ";
267 * this call back is called when displaying the link for some last post processing
270 function wiki_link_post_processing($title){
271 return mb_convert_encoding($title, 'UTF-8', 'auto');