2 /* Wiki Search Document class and functions
3 * This file contains the mapping between a wiki page and it's indexable counterpart,
4 * e.g. searchdocument->title = wikipage->pagename
6 * Functions for iterating and retrieving the necessary records are now also included
7 * in this file, rather than mod/wiki/lib.php
10 require_once("$CFG->dirroot/search/documents/document.php");
11 require_once("$CFG->dirroot/mod/wiki/lib.php");
13 /* All the $doc->___ fields are required by the base document class!
14 * Each and every module that requires search functionality must correctly
15 * map their internal fields to the five $doc fields (id, title, author, contents
16 * and url). Any module specific data can be added to the $data object, which is
17 * serialised into a binary field in the index.
19 class WikiSearchDocument extends SearchDocument {
20 public function __construct(&$page, $wiki_id, $course_id, $group_id) {
21 // generic information; required
23 $doc->title = $page->pagename;
24 $doc->author = $page->author;
25 $doc->contents = $page->content;
26 $doc->url = wiki_make_link($wiki_id, $page->pagename, $page->version);
28 // module specific information; optional
29 $data->version = $page->version;
30 $data->wiki = $wiki_id;
32 // construct the parent class
33 parent::__construct($doc, $data, SEARCH_WIKI_TYPE, $course_id, $group_id);
35 } //WikiSearchDocument
37 function wiki_name_convert($str) {
38 return str_replace(' ', '+', $str);
41 function wiki_make_link($wiki_id, $title, $version) {
43 return $CFG->wwwroot.'/mod/wiki/view.php?wid='.$wiki_id.'&page='.wiki_name_convert($title).'&version='.$version;
46 //rescued and converted from ewikimoodlelib.php
47 //retrieves latest version of a page
48 function wiki_get_latest_page(&$entry, $pagename, $version=0) {
49 $pagename = "'".addslashes($pagename)."'";
51 if ($version > 0 and is_int($version)) {
52 $version = "AND (version=$version)";
57 $select = "(pagename=$pagename) AND wiki=".$entry->id." $version ";
58 $sort = 'version DESC';
60 //change this to recordset_select, as per http://docs.moodle.org/en/Datalib_Notes
61 if ($result_arr = get_records_select('wiki_pages', $select, $sort, '*', 0, 1)) {
62 foreach ($result_arr as $obj) {
67 if (isset($result_obj)) {
68 $result_obj->meta = @unserialize($result_obj->meta);
73 } //wiki_get_latest_page
75 //fetches all pages, including old versions
76 function wiki_get_pages(&$entry) {
77 return get_records('wiki_pages', 'wiki', $entry->id);
80 //fetches all the latest versions of all the pages
81 function wiki_get_latest_pages(&$entry) {
83 /* select * from wiki_pages
85 (select wiki_pages.pagename, max(wiki_pages.version) as ver
86 from wiki_pages group by pagename) as a
87 on ((wiki_pages.version = a.ver) and
88 (wiki_pages.pagename like a.pagename)) */
92 //http://moodle.org/bugs/bug.php?op=show&bugid=5877&pos=0
93 //if ($ids = get_records('wiki_pages', 'wiki', $entry->id, '', 'distinct pagename')) {
94 if ($rs = get_recordset('wiki_pages', 'wiki', $entry->id, '', 'distinct pagename')) {
95 $ids = $rs->GetRows();
97 foreach ($ids as $id) {
98 $pages[] = wiki_get_latest_page($entry, $id[0]);
105 } //wiki_get_latest_pages
107 function wiki_iterator() {
108 return get_all_instances_in_courses("wiki", get_courses());
111 function wiki_get_content_for_index(&$wiki) {
112 $documents = array();
114 $entries = wiki_get_entries($wiki);
115 foreach($entries as $entry) {
117 //$pages = wiki_get_pages($entry);
120 $pages = wiki_get_latest_pages($entry);
122 if (is_array($pages)) {
123 foreach($pages as $page) {
124 if (strlen($page->content) > 0) {
125 $documents[] = new WikiSearchDocument($page, $entry->wikiid, $entry->course, $entry->groupid);
132 } //wiki_get_content_for_index