3 * Global Search Engine for Moodle
4 * Michael Champanis (mchampan) [cynnical@gmail.com]
5 * review 1.8+ : Valery Fremaux [valery.fremaux@club-internet.fr]
8 * document handling for wiki activity module
9 * This file contains the mapping between a wiki page and it's indexable counterpart,
10 * e.g. searchdocument->title = wikipage->pagename
12 * Functions for iterating and retrieving the necessary records are now also included
13 * in this file, rather than mod/wiki/lib.php
16 require_once("$CFG->dirroot/search/documents/document.php");
17 require_once("$CFG->dirroot/mod/wiki/lib.php");
20 * All the $doc->___ fields are required by the base document class!
21 * Each and every module that requires search functionality must correctly
22 * map their internal fields to the five $doc fields (id, title, author, contents
23 * and url). Any module specific data can be added to the $data object, which is
24 * serialised into a binary field in the index.
26 class WikiSearchDocument
extends SearchDocument
{
27 public function __construct(&$page, $wiki_id, $course_id, $group_id, $user_id, $context_id) {
28 // generic information; required
29 $doc->docid
= $page['id'];
30 $doc->documenttype
= SEARCH_TYPE_WIKI
;
31 $doc->itemtype
= 'standard';
32 $doc->contextid
= $context_id;
34 $doc->title
= $page['pagename'];
35 $doc->date
= $page['timemodified'];
36 //remove '(ip.ip.ip.ip)' from wiki author field
37 $doc->author
= preg_replace('/\(.*?\)/', '', $page['author']);
38 $doc->contents
= $page['content'];
39 $doc->url
= wiki_make_link($wiki_id, $page['pagename'], $page['version']);
41 // module specific information; optional
42 $data->version
= $page['version'];
43 $data->wiki
= $wiki_id;
45 // construct the parent class
46 parent
::__construct($doc, $data, $course_id, $group_id, $user_id, PATH_FOR_SEARCH_TYPE_WIKI
);
48 } //WikiSearchDocument
51 * converts a page name to cope Wiki constraints. Transforms spaces in plus.
52 * @param str the name to convert
53 * @return the converted name
55 function wiki_name_convert($str) {
56 return str_replace(' ', '+', $str);
60 * constructs a valid link to a wiki content
65 function wiki_make_link($wikiId, $title, $version) {
68 return $CFG->wwwroot
.'/mod/wiki/view.php?wid='.$wikiId.'&page='.wiki_name_convert($title).'&version='.$version;
72 * rescued and converted from ewikimoodlelib.php
73 * retrieves latest version of a page
74 * @param entry the wiki object as a reference
75 * @param pagename the name of the page known by the wiki engine
78 function wiki_get_latest_page(&$entry, $pagename, $version = 0) {
79 $pagename = "'".addslashes($pagename)."'";
81 if ($version > 0 and is_int($version)) {
82 $version = "AND (version=$version)";
87 $select = "(pagename=$pagename) AND wiki=".$entry->id
." $version ";
88 $sort = 'version DESC';
90 //change this to recordset_select, as per http://docs.moodle.org/en/Datalib_Notes
91 if ($result_arr = get_records_select('wiki_pages', $select, $sort, '*', 0, 1)) {
92 foreach ($result_arr as $obj) {
97 if (isset($result_obj)) {
98 $result_obj->meta
= @unserialize
($result_obj->meta
);
103 } //wiki_get_latest_page
106 * fetches all pages, including old versions
107 * @param entry the wiki object as a reference
108 * @return an array of record objects that represents pages of this wiki object
110 function wiki_get_pages(&$entry) {
111 return get_records('wiki_pages', 'wiki', $entry->id
);
115 * fetches all the latest versions of all the pages
118 function wiki_get_latest_pages(&$entry) {
119 //== (My)SQL for this
120 /* select * from wiki_pages
122 (select wiki_pages.pagename, max(wiki_pages.version) as ver
123 from wiki_pages group by pagename) as a
124 on ((wiki_pages.version = a.ver) and
125 (wiki_pages.pagename like a.pagename)) */
129 //http://moodle.org/bugs/bug.php?op=show&bugid=5877&pos=0
130 if ($ids = get_records('wiki_pages', 'wiki', $entry->id
, '', 'distinct pagename')) {
131 if ($pagesets = get_records('wiki_pages', 'wiki', $entry->id
, '', 'distinct pagename')) {
132 foreach ($pagesets as $aPageset) {
133 $pages[] = wiki_get_latest_page($entry, $aPageset->id
);
140 } //wiki_get_latest_pages
143 * part of search engine API
146 function wiki_iterator() {
147 $wikis = get_records('wiki');
152 * part of search engine API
153 * @param wiki a wiki instance
154 * @return an array of searchable deocuments
156 function wiki_get_content_for_index(&$wiki) {
158 $documents = array();
159 $entries = wiki_get_entries($wiki);
160 foreach($entries as $entry) {
161 $coursemodule = get_field('modules', 'id', 'name', 'wiki');
162 $cm = get_record('course_modules', 'course', $entry->course
, 'module', $coursemodule, 'instance', $entry->wikiid
);
163 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
166 //$pages = wiki_get_pages($entry);
169 $pages = wiki_get_latest_pages($entry);
170 if (is_array($pages)) {
171 foreach($pages as $page) {
172 if (strlen($page->content
) > 0) {
173 $documents[] = new WikiSearchDocument(get_object_vars($page), $entry->wikiid
, $entry->course
, $entry->groupid
, $page->userid
, $context->id
);
179 } //wiki_get_content_for_index
182 * returns a single wiki search document based on a wiki_entry id
183 * @param id the id of the wiki
184 * @param itemtype the type of information (standard)
185 * @retuen a searchable document
187 function wiki_single_document($id, $itemtype) {
188 $page = get_record('wiki_pages', 'id', $id);
189 $entry = get_record('wiki_entries', 'id', $page->wiki
);
190 $coursemodule = get_field('modules', 'id', 'name', 'wiki');
191 $cm = get_record('course_modules', 'course', $entry->course
, 'module', $coursemodule, 'instance', $entry->wikiid
);
192 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
193 return new WikiSearchDocument(get_object_vars($page), $entry->wikiid
, $entry->course
, $entry->groupid
, $page->userid
, $context->id
);
194 } //wiki_single_document
197 * dummy delete function that packs id with itemtype.
198 * this was here for a reason, but I can't remember it at the moment.
201 function wiki_delete($info, $itemtype) {
203 $object->itemtype
= $itemtype;
207 //returns the var names needed to build a sql query for addition/deletions
208 function wiki_db_names() {
209 //[primary id], [table name], [time created field name], [time modified field name]
210 return array(array('id', 'wiki_pages', 'created', 'lastmodified', 'standard'));
214 * this function handles the access policy to contents indexed as searchable documents. If this
215 * function does not exist, the search engine assumes access is allowed.
216 * When this point is reached, we already know that :
217 * - user is legitimate in the surrounding context
218 * - user may be guest and guest access is allowed to the module
219 * - the function may perform local checks within the module information logic
220 * @param path the access path to the module script code
221 * @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
222 * @param this_id the item id within the information class denoted by itemtype. In wikies, this id
223 * points out the indexed wiki page.
224 * @param user the user record denoting the user who searches
225 * @param group_id the current group used by the user when searching
226 * @return true if access is allowed, false elsewhere
228 function wiki_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
231 // get the wiki object and all related stuff
232 $page = get_record('wiki_pages', 'id', $id);
233 $entry = get_record('wiki_entries', 'id', $page->wiki
);
234 $course = get_record('course', 'id', $entry->course
);
235 $module_context = get_record('context', 'id', $context_id);
236 $cm = get_record('course_modules', 'id', $module_context->instance
);
237 if (!$cm->visible
and !has_capability('moodle/course:viewhiddenactivities', $module_context)) return false;
239 //group consistency check : checks the following situations about groups
240 // trap if user is not same group and groups are separated
241 $current_group = get_current_group($course->id
);
242 if ((groupmode($course) == SEPARATEGROUPS
) && $group_id != $current_group && !has_capability('moodle/site:accessallgroups', $module_context)) return false;
245 } //wiki_check_text_access