24ed20d173be4bc7c5e1b8efe545fcebcfc6164b
[moodle.git] / search / documents / wiki_document.php
1 <?php
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    * */
10   require_once("$CFG->dirroot/search/documents/document.php");
11   require_once("$CFG->dirroot/mod/wiki/lib.php");
12   
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    * */  
19   class WikiSearchDocument extends SearchDocument {  
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;
24       $doc->author    = $page->author;
25       $doc->contents  = $page->content;
26       $doc->url       = wiki_make_link($wiki_id, $page->pagename, $page->version);      
27       
28       // module specific information; optional
29       $data->version  = $page->version;
30       $data->wiki     = $wiki_id;
31       
32       // construct the parent class
33       parent::__construct($doc, $data, SEARCH_WIKI_TYPE, $course_id, $group_id);
34     } //constructor    
35   } //WikiSearchDocument
36   
37   function wiki_name_convert($str) {
38     return str_replace(' ', '+', $str);
39   } //wiki_name_convert
41   function wiki_make_link($wiki_id, $title, $version) {
42     global $CFG;    
43     return $CFG->wwwroot.'/mod/wiki/view.php?wid='.$wiki_id.'&page='.wiki_name_convert($title).'&version='.$version;
44   } //wiki_make_link
45   
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)."'";
50     
51     if ($version > 0 and is_int($version)) {
52       $version = "AND (version=$version)";
53     } else {
54       $version = '';
55     } //else
56   
57     $select = "(pagename=$pagename) AND wiki=".$entry->id." $version ";
58     $sort   = 'version DESC';
59     
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) {
63         $result_obj = $obj;                 
64       } //foreach
65     } //if
66       
67     if (isset($result_obj))  {
68       $result_obj->meta = @unserialize($result_obj->meta);
69       return $result_obj;
70     } else {
71       return false;
72     } //else
73   } //wiki_get_latest_page
74   
75   //fetches all pages, including old versions
76   function wiki_get_pages(&$entry) {   
77     return get_records('wiki_pages', 'wiki', $entry->id);
78   } //wiki_get_pages
79   
80   //fetches all the latest versions of all the pages
81   function wiki_get_latest_pages(&$entry) {
82     //== (My)SQL for this
83     /* select * from wiki_pages
84        inner join
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)) */
89     
90     $pages = array();    
91     
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();
96     //--    
97       foreach ($ids as $id) {      
98         $pages[] = wiki_get_latest_page($entry, $id[0]);
99       } //foreach
100     } else {
101       return false;
102     } //else  
103       
104     return $pages;   
105   } //wiki_get_latest_pages
106   
107   function wiki_iterator() {
108     return get_all_instances_in_courses("wiki", get_courses());  
109   } //wiki_iterator
110   
111   function wiki_get_content_for_index(&$wiki) {
112     $documents = array();
113     
114     $entries = wiki_get_entries($wiki);    
115     foreach($entries as $entry) {
116       //all pages
117       //$pages = wiki_get_pages($entry);
118       
119       //latest pages
120       $pages = wiki_get_latest_pages($entry);
121       
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);
126           } //if
127         } //foreach
128       } //if
129     } //foreach
130     
131     return $documents;
132   } //wiki_get_content_for_index 
133   
134 ?>