MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / wiki / filter.php
blobd116790a6439a4a5cedee92482f2612a6883530f
1 <?PHP // $Id$
2 //This function provides automatic linking to
3 //wiki pages when its page title is found inside every Moodle text
4 //It's based in the glosssary filter by Williams Castillo
5 //Modifications by mchurch. Enjoy! :-)
7 require_once($CFG->dirroot.'/mod/wiki/lib.php');
9 function wiki_filter($courseid, $text) {
11 global $CFG;
13 // Trivial-cache - keyed on $cachedcourseid
14 static $nothingtodo;
15 static $wikipagelist;
16 static $cachedcourseid;
18 if (empty($courseid)) {
19 $courseid = SITEID;
22 // Initialise/invalidate our trivial cache if dealing with a different course
23 if (!isset($cachedcourseid) || $cachedcourseid !== (int)$courseid) {
24 $wikipagelist = array();
25 $nothingtodo = false;
27 $cachedcourseid = (int)$courseid;
29 if (!empty($nothingtodo)) { // We've been here in this page already
30 return $text;
33 /// Create a list of all the wikis to search for. It may be cached already.
35 if (empty($wikipagelist)) {
37 /// Get all wikis for this course.
38 if (!$wikis = wiki_get_course_wikis($courseid)) {
39 $nothingtodo = true;
40 return $text;
43 $wikipagelist = array();
45 /// Walk through each wiki, and get entries.
46 foreach ($wikis as $wiki) {
47 if ($wiki_entries = wiki_get_entries($wiki)) {
49 /// Walk through each entry and get the pages.
50 foreach ($wiki_entries as $wiki_entry) {
51 if ($wiki_pages = get_records('wiki_pages', 'wiki', $wiki_entry->id, 'pagename, version DESC')) {
52 /// Walk through each page and filter.
53 $wikientries = array();
54 foreach ($wiki_pages as $wiki_page) {
55 if (!in_array($wiki_page->pagename, $wikientries)) {
56 $startlink = '<a class="wiki autolink" title="Wiki" href="'
57 .$CFG->wwwroot.'/mod/wiki/view.php?wid='.$wiki->id
58 .'&amp;userid='.$wiki_entry->userid
59 .'&amp;groupid='.$wiki_entry->groupid
60 .'&amp;page='.$wiki_page->pagename.'">';
61 $wikipagelist[] = new filterobject($wiki_page->pagename, $startlink, '</a>', false, true);
62 $wikientries[] = $wiki_page->pagename;
71 return filter_phrases($text, $wikipagelist);