Fixed pagination in query page (lines were too long, distorting table),
[moodle.git] / search / documents / wiki_document.php
CommitLineData
682d4032 1<?php
a25a3912 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
5 *
6 * Functions for iterating and retrieving the necessary records are now also included
7 * in this file, rather than mod/wiki/lib.php
8 * */
682d4032 9
10 require_once("$CFG->dirroot/search/documents/document.php");
a25a3912 11 require_once("$CFG->dirroot/mod/wiki/lib.php");
682d4032 12
a25a3912 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.
18 * */
682d4032 19 class WikiSearchDocument extends SearchDocument {
a25a3912 20 public function __construct(&$page, $wiki_id, $course_id, $group_id) {
21 // generic information; required
22 $doc->id = $page->id;
23 $doc->title = $page->pagename;
91ad9557 24
25 //remove '(ip.ip.ip.ip)' from wiki author field
26 $doc->author = preg_replace('/\(.*?\)/', '', $page->author);
a25a3912 27 $doc->contents = $page->content;
28 $doc->url = wiki_make_link($wiki_id, $page->pagename, $page->version);
682d4032 29
a25a3912 30 // module specific information; optional
31 $data->version = $page->version;
32 $data->wiki = $wiki_id;
682d4032 33
a25a3912 34 // construct the parent class
35 parent::__construct($doc, $data, SEARCH_WIKI_TYPE, $course_id, $group_id);
682d4032 36 } //constructor
37 } //WikiSearchDocument
38
39 function wiki_name_convert($str) {
40 return str_replace(' ', '+', $str);
41 } //wiki_name_convert
a25a3912 42
43 function wiki_make_link($wiki_id, $title, $version) {
682d4032 44 global $CFG;
a25a3912 45 return $CFG->wwwroot.'/mod/wiki/view.php?wid='.$wiki_id.'&page='.wiki_name_convert($title).'&version='.$version;
682d4032 46 } //wiki_make_link
47
a25a3912 48 //rescued and converted from ewikimoodlelib.php
49 //retrieves latest version of a page
50 function wiki_get_latest_page(&$entry, $pagename, $version=0) {
51 $pagename = "'".addslashes($pagename)."'";
52
53 if ($version > 0 and is_int($version)) {
54 $version = "AND (version=$version)";
55 } else {
56 $version = '';
57 } //else
58
59 $select = "(pagename=$pagename) AND wiki=".$entry->id." $version ";
60 $sort = 'version DESC';
61
62 //change this to recordset_select, as per http://docs.moodle.org/en/Datalib_Notes
63 if ($result_arr = get_records_select('wiki_pages', $select, $sort, '*', 0, 1)) {
64 foreach ($result_arr as $obj) {
65 $result_obj = $obj;
66 } //foreach
67 } //if
68
69 if (isset($result_obj)) {
70 $result_obj->meta = @unserialize($result_obj->meta);
71 return $result_obj;
72 } else {
73 return false;
74 } //else
75 } //wiki_get_latest_page
76
77 //fetches all pages, including old versions
78 function wiki_get_pages(&$entry) {
79 return get_records('wiki_pages', 'wiki', $entry->id);
80 } //wiki_get_pages
81
82 //fetches all the latest versions of all the pages
83 function wiki_get_latest_pages(&$entry) {
84 //== (My)SQL for this
85 /* select * from wiki_pages
86 inner join
87 (select wiki_pages.pagename, max(wiki_pages.version) as ver
88 from wiki_pages group by pagename) as a
89 on ((wiki_pages.version = a.ver) and
90 (wiki_pages.pagename like a.pagename)) */
91
92 $pages = array();
93
94 //http://moodle.org/bugs/bug.php?op=show&bugid=5877&pos=0
95 //if ($ids = get_records('wiki_pages', 'wiki', $entry->id, '', 'distinct pagename')) {
96 if ($rs = get_recordset('wiki_pages', 'wiki', $entry->id, '', 'distinct pagename')) {
97 $ids = $rs->GetRows();
98 //--
99 foreach ($ids as $id) {
100 $pages[] = wiki_get_latest_page($entry, $id[0]);
101 } //foreach
102 } else {
103 return false;
104 } //else
105
106 return $pages;
107 } //wiki_get_latest_pages
108
109 function wiki_iterator() {
110 return get_all_instances_in_courses("wiki", get_courses());
111 } //wiki_iterator
112
113 function wiki_get_content_for_index(&$wiki) {
114 $documents = array();
115
116 $entries = wiki_get_entries($wiki);
117 foreach($entries as $entry) {
118 //all pages
119 //$pages = wiki_get_pages($entry);
120
121 //latest pages
122 $pages = wiki_get_latest_pages($entry);
123
124 if (is_array($pages)) {
125 foreach($pages as $page) {
126 if (strlen($page->content) > 0) {
127 $documents[] = new WikiSearchDocument($page, $entry->wikiid, $entry->course, $entry->groupid);
128 } //if
129 } //foreach
130 } //if
131 } //foreach
132
133 return $documents;
134 } //wiki_get_content_for_index
135
682d4032 136?>