]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RecentChanges.php
sf.net patch by Matt Brown: Add rel=nofollow to more actions
[SourceForge/phpwiki.git] / lib / plugin / RecentChanges.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RecentChanges.php,v 1.109 2006-03-19 14:26:29 rurban Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  */
25 class _RecentChanges_Formatter
26 {
27     var $_absurls = false;
28
29     function _RecentChanges_Formatter ($rc_args) {
30         $this->_args = $rc_args;
31         $this->_diffargs = array('action' => 'diff');
32
33         if ($rc_args['show_minor'] || !$rc_args['show_major'])
34             $this->_diffargs['previous'] = 'minor';
35
36         // PageHistoryPlugin doesn't have a 'daylist' arg.
37         if (!isset($this->_args['daylist']))
38             $this->_args['daylist'] = false;
39     }
40
41     function include_versions_in_URLs() {
42         return (bool) $this->_args['show_all'];
43     }
44
45     function date ($rev) {
46         global $WikiTheme;
47         return $WikiTheme->getDay($rev->get('mtime'));
48     }
49
50     function time ($rev) {
51         global $WikiTheme;
52         return $WikiTheme->formatTime($rev->get('mtime'));
53     }
54
55     function diffURL ($rev) {
56         $args = $this->_diffargs;
57         if ($this->include_versions_in_URLs())
58             $args['version'] = $rev->getVersion();
59         $page = $rev->getPage();
60         return WikiURL($page->getName(), $args, $this->_absurls);
61     }
62
63     function historyURL ($rev) {
64         $page = $rev->getPage();
65         return WikiURL($page, array('action' => _("PageHistory")),
66                        $this->_absurls);
67     }
68
69     function pageURL ($rev) {
70         return WikiURL($this->include_versions_in_URLs() ? $rev : $rev->getPage(),
71                        '', $this->_absurls);
72     }
73
74     function authorHasPage ($author) {
75         global $WikiNameRegexp, $request;
76         $dbi = $request->getDbh();
77         return isWikiWord($author) && $dbi->isWikiPage($author);
78     }
79
80     function authorURL ($author) {
81         return $this->authorHasPage() ? WikiURL($author) : false;
82     }
83
84
85     function status ($rev) {
86         if ($rev->hasDefaultContents())
87             return 'deleted';
88         $page = $rev->getPage();
89         $prev = $page->getRevisionBefore($rev->getVersion());
90         if ($prev->hasDefaultContents())
91             return 'new';
92         return 'updated';
93     }
94
95     function importance ($rev) {
96         return $rev->get('is_minor_edit') ? 'minor' : 'major';
97     }
98
99     function summary($rev) {
100         if ( ($summary = $rev->get('summary')) )
101             return $summary;
102
103         switch ($this->status($rev)) {
104             case 'deleted':
105                 return _("Deleted");
106             case 'new':
107                 return _("New page");
108             default:
109                 return '';
110         }
111     }
112
113     function setValidators($most_recent_rev) {
114         $rev = $most_recent_rev;
115         $validators = array('RecentChanges-top' =>
116                             array($rev->getPageName(), $rev->getVersion()),
117                             '%mtime' => $rev->get('mtime'));
118         global $request;
119         $request->appendValidators($validators);
120     }
121 }
122
123 class _RecentChanges_HtmlFormatter
124 extends _RecentChanges_Formatter
125 {
126     function diffLink ($rev) {
127         global $WikiTheme;
128         $button = $WikiTheme->makeButton(_("(diff)"), $this->diffURL($rev), 'wiki-rc-action');
129         $button->setAttr('rel', 'nofollow');
130         return $button;
131     }
132
133     function historyLink ($rev) {
134         global $WikiTheme;
135         return $WikiTheme->makeButton(_("(hist)"), $this->historyURL($rev), 'wiki-rc-action');
136     }
137
138     function pageLink ($rev, $link_text=false) {
139
140         return WikiLink($this->include_versions_in_URLs() ? $rev : $rev->getPage(),'auto',$link_text);
141         /*
142         $page = $rev->getPage();
143         global $WikiTheme;
144         if ($this->include_versions_in_URLs()) {
145             $version = $rev->getVersion();
146             if ($rev->isCurrent())
147                 $version = false;
148             $exists = !$rev->hasDefaultContents();
149         }
150         else {
151             $version = false;
152             $cur = $page->getCurrentRevision();
153             $exists = !$cur->hasDefaultContents();
154         }
155         if ($exists)
156             return $WikiTheme->linkExistingWikiWord($page->getName(), $link_text, $version);
157         else
158             return $WikiTheme->linkUnknownWikiWord($page->getName(), $link_text);
159         */
160     }
161
162     function authorLink ($rev) {
163         $author = $rev->get('author');
164         if ( $this->authorHasPage($author) ) {
165             return WikiLink($author);
166         } else
167             return $author;
168     }
169
170     function summaryAsHTML ($rev) {
171         if ( !($summary = $this->summary($rev)) )
172             return '';
173         return  HTML::strong( array('class' => 'wiki-summary'),
174                               "[",
175                               TransformLinks($summary, $rev->get('markup'), $rev->getPageName()),
176                               "]");
177     }
178
179     function rss_icon () {
180         global $request, $WikiTheme;
181
182         $rss_url = $request->getURLtoSelf(array('format' => 'rss'));
183         return HTML::small(array('style' => 'font-weight:normal;vertical-align:middle;'), 
184                            $WikiTheme->makeButton("RSS", $rss_url, 'rssicon'));
185     }
186     function rss2_icon () {
187         global $request, $WikiTheme;
188
189         $rss_url = $request->getURLtoSelf(array('format' => 'rss2'));
190         return HTML::small(array('style' => 'font-weight:normal;vertical-align:middle;'), 
191                            $WikiTheme->makeButton("RSS2", $rss_url, 'rssicon'));
192     }
193
194     function pre_description () {
195         extract($this->_args);
196         // FIXME: say something about show_all.
197         if ($show_major && $show_minor)
198             $edits = _("edits");
199         elseif ($show_major)
200             $edits = _("major edits");
201         else
202             $edits = _("minor edits");
203         if (isset($caption) and $caption == _("Recent Comments"))
204             $edits = _("comments");
205
206         if ($timespan = $days > 0) {
207             if (intval($days) != $days)
208                 $days = sprintf("%.1f", $days);
209         }
210         $lmt = abs($limit);
211         /**
212          * Depending how this text is split up it can be tricky or
213          * impossible to translate with good grammar. So the seperate
214          * strings for 1 day and %s days are necessary in this case
215          * for translating to multiple languages, due to differing
216          * overlapping ideal word cutting points.
217          *
218          * en: day/days "The %d most recent %s [during (the past] day) are listed below."
219          * de: 1 Tag    "Die %d jüngste %s [innerhalb (von des letzten] Tages) sind unten aufgelistet."
220          * de: %s days  "Die %d jüngste %s [innerhalb (von] %s Tagen) sind unten aufgelistet."
221          *
222          * en: day/days "The %d most recent %s during [the past] (day) are listed below."
223          * fr: 1 jour   "Les %d %s les plus récentes pendant [le dernier (d'une] jour) sont énumérées ci-dessous."
224          * fr: %s jours "Les %d %s les plus récentes pendant [les derniers (%s] jours) sont énumérées ci-dessous."
225          */
226         if ($limit > 0) {
227             if ($timespan) {
228                 if (intval($days) == 1)
229                     $desc = fmt("The %d most recent %s during the past day are listed below.",
230                                 $limit, $edits);
231                 else
232                     $desc = fmt("The %d most recent %s during the past %s days are listed below.",
233                                 $limit, $edits, $days);
234             } else
235                 $desc = fmt("The %d most recent %s are listed below.",
236                             $limit, $edits);
237         }
238         elseif ($limit < 0) {  //$limit < 0 means we want oldest pages
239             if ($timespan) {
240                 if (intval($days) == 1)
241                     $desc = fmt("The %d oldest %s during the past day are listed below.",
242                                 $lmt, $edits);
243                 else
244                     $desc = fmt("The %d oldest %s during the past %s days are listed below.",
245                                 $lmt, $edits, $days);
246             } else
247                 $desc = fmt("The %d oldest %s are listed below.",
248                             $lmt, $edits);
249         }
250
251         else {
252             if ($timespan) {
253                 if (intval($days) == 1)
254                     $desc = fmt("The most recent %s during the past day are listed below.",
255                                 $edits);
256                 else
257                     $desc = fmt("The most recent %s during the past %s days are listed below.",
258                                 $edits, $days);
259             } else
260                 $desc = fmt("All %s are listed below.", $edits);
261         }
262         return $desc;
263     }    
264
265     function description() {
266         return HTML::p(false, $this->pre_description());
267     }
268
269
270     function title () {
271         extract($this->_args);
272         return array($show_minor ? _("RecentEdits") : _("RecentChanges"),
273                      ' ',
274                      $this->rss_icon(), HTML::raw('&nbsp;'), $this->rss2_icon(),
275                      $this->sidebar_link());
276     }
277
278     function empty_message () {
279         if (isset($this->_args['caption']) and $this->_args['caption'] == _("Recent Comments"))
280             return _("No comments found");
281         else 
282             return _("No changes found");
283     }
284         
285     function sidebar_link() {
286         extract($this->_args);
287         $pagetitle = $show_minor ? _("RecentEdits") : _("RecentChanges");
288
289         global $request;
290         $sidebarurl = WikiURL($pagetitle, array('format' => 'sidebar'), 'absurl');
291
292         $addsidebarjsfunc =
293             "function addPanel() {\n"
294             ."    window.sidebar.addPanel (\"" . sprintf("%s - %s", WIKI_NAME, $pagetitle) . "\",\n"
295             ."       \"$sidebarurl\",\"\");\n"
296             ."}\n";
297         $jsf = JavaScript($addsidebarjsfunc);
298
299         global $WikiTheme;
300         $sidebar_button = $WikiTheme->makeButton("sidebar", 'javascript:addPanel();', 'sidebaricon');
301         $addsidebarjsclick = asXML(HTML::small(array('style' => 'font-weight:normal;vertical-align:middle;'), $sidebar_button));
302         $jsc = JavaScript("if ((typeof window.sidebar == 'object') &&\n"
303                                 ."    (typeof window.sidebar.addPanel == 'function'))\n"
304                                 ."   {\n"
305                                 ."       document.write('$addsidebarjsclick');\n"
306                                 ."   }\n"
307                                 );
308         return HTML(new RawXML("\n"), $jsf, new RawXML("\n"), $jsc);
309     }
310
311     function format ($changes) {
312         include_once('lib/InlineParser.php');
313         
314         $html = HTML(HTML::h2(false, $this->title()));
315         if (($desc = $this->description()))
316             $html->pushContent($desc);
317         
318         if ($this->_args['daylist'])
319             $html->pushContent(new DayButtonBar($this->_args));
320
321         $last_date = '';
322         $lines = false;
323         $first = true;
324
325         while ($rev = $changes->next()) {
326             if (($date = $this->date($rev)) != $last_date) {
327                 if ($lines)
328                     $html->pushContent($lines);
329                 $html->pushContent(HTML::h3($date));
330                 $lines = HTML::ul();
331                 $last_date = $date;
332
333             }
334             // enforce view permission
335             if (mayAccessPage('view', $rev->_pagename)) {
336                 $lines->pushContent($this->format_revision($rev));
337
338                 if ($first)
339                     $this->setValidators($rev);
340                 $first = false;
341             }
342         }
343         if ($lines)
344             $html->pushContent($lines);
345         if ($first)
346             $html->pushContent(HTML::p(array('class' => 'rc-empty'),
347                                        $this->empty_message()));
348         
349         return $html;
350     }
351
352     function format_revision ($rev) {
353         $args = &$this->_args;
354
355         $class = 'rc-' . $this->importance($rev);
356
357         $time = $this->time($rev);
358         if (! $rev->get('is_minor_edit'))
359             $time = HTML::strong(array('class' => 'pageinfo-majoredit'), $time);
360
361         $line = HTML::li(array('class' => $class));
362
363
364         if ($args['difflinks'])
365             $line->pushContent($this->diffLink($rev), ' ');
366
367         if ($args['historylinks'])
368             $line->pushContent($this->historyLink($rev), ' ');
369
370         $line->pushContent($this->pageLink($rev), ' ',
371                            $time, ' ',
372                            $this->summaryAsHTML($rev),
373                            ' ... ',
374                            $this->authorLink($rev));
375         return $line;
376     }
377 }
378
379
380 class _RecentChanges_SideBarFormatter
381 extends _RecentChanges_HtmlFormatter
382 {
383     function rss_icon () {
384         //omit rssicon
385     }
386     function rss2_icon () { }
387     function title () {
388         //title click opens the normal RC or RE page in the main browser frame
389         extract($this->_args);
390         $titlelink = WikiLink($show_minor ? _("RecentEdits") : _("RecentChanges"));
391         $titlelink->setAttr('target', '_content');
392         return HTML($this->logo(), $titlelink);
393     }
394     function logo () {
395         //logo click opens the HomePage in the main browser frame
396         global $WikiTheme;
397         $img = HTML::img(array('src' => $WikiTheme->getImageURL('logo'),
398                                'border' => 0,
399                                'align' => 'right',
400                                'style' => 'height:2.5ex'
401                                ));
402         $linkurl = WikiLink(HOME_PAGE, false, $img);
403         $linkurl->setAttr('target', '_content');
404         return $linkurl;
405     }
406
407     function authorLink ($rev) {
408         $author = $rev->get('author');
409         if ( $this->authorHasPage($author) ) {
410             $linkurl = WikiLink($author);
411             $linkurl->setAttr('target', '_content'); // way to do this using parent::authorLink ??
412             return $linkurl;
413         } else
414             return $author;
415     }
416
417     function diffLink ($rev) {
418         $linkurl = parent::diffLink($rev);
419         $linkurl->setAttr('target', '_content');
420         $linkurl->setAttr('rel', 'nofollow');
421         // FIXME: Smelly hack to get smaller diff buttons in sidebar
422         $linkurl = new RawXML(str_replace('<img ', '<img style="height:2ex" ', asXML($linkurl)));
423         return $linkurl;
424     }
425     function historyLink ($rev) {
426         $linkurl = parent::historyLink($rev);
427         $linkurl->setAttr('target', '_content');
428         // FIXME: Smelly hack to get smaller history buttons in sidebar
429         $linkurl = new RawXML(str_replace('<img ', '<img style="height:2ex" ', asXML($linkurl)));
430         return $linkurl;
431     }
432     function pageLink ($rev) {
433         $linkurl = parent::pageLink($rev);
434         $linkurl->setAttr('target', '_content');
435         return $linkurl;
436     }
437     // Overriding summaryAsHTML, because there is no way yet to
438     // return summary as transformed text with
439     // links setAttr('target', '_content') in Mozilla sidebar.
440     // So for now don't create clickable links inside summary
441     // in the sidebar, or else they target the sidebar and not the
442     // main content window.
443     function summaryAsHTML ($rev) {
444         if ( !($summary = $this->summary($rev)) )
445             return '';
446         return HTML::strong(array('class' => 'wiki-summary'),
447                                 "[",
448                                 /*TransformLinks(*/$summary,/* $rev->get('markup')),*/
449                                 "]");
450     }
451
452
453     function format ($changes) {
454         $this->_args['daylist'] = false; //don't show day buttons in Mozilla sidebar
455         $html = _RecentChanges_HtmlFormatter::format ($changes);
456         $html = HTML::div(array('class' => 'wikitext'), $html);
457         global $request;
458         $request->discardOutput();
459         
460         printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n", $GLOBALS['charset']);
461         printf('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"');
462         printf('  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
463         printf('<html xmlns="http://www.w3.org/1999/xhtml">');
464
465         printf("<head>\n");
466         extract($this->_args);
467         $title = WIKI_NAME . $show_minor ? _("RecentEdits") : _("RecentChanges");
468         printf("<title>" . $title . "</title>\n");
469         global $WikiTheme;
470         $css = $WikiTheme->getCSS();
471         $css->PrintXML();
472         printf("</head>\n");
473
474         printf("<body class=\"sidebar\">\n");
475         $html->PrintXML();
476         echo '<a href="http://www.feedvalidator.org/check.cgi?url=http://phpwiki.org/RecentChanges?format=rss"><img src="themes/default/buttons/valid-rss.png" alt="[Valid RSS]" title="Validate the RSS feed" width="44" height="15" /></a>';
477         printf("\n</body>\n");
478         printf("</html>\n");
479
480         $request->finish(); // cut rest of page processing short
481     }
482 }
483
484 class _RecentChanges_BoxFormatter
485 extends _RecentChanges_HtmlFormatter
486 {
487     function rss_icon () {
488     }
489     function rss2_icon () {
490     }
491     function title () {
492     }
493     function authorLink ($rev) {
494     }
495     function diffLink ($rev) {
496     }
497     function historyLink ($rev) {
498     }
499     function summaryAsHTML ($rev) {
500     }
501     function description () {
502     }
503     function format ($changes) {
504         include_once('lib/InlineParser.php');
505         $last_date = '';
506         $first = true;
507         $html = HTML();
508         $counter = 1;
509         $sp = HTML::Raw("\n&nbsp;&middot;&nbsp;");
510         while ($rev = $changes->next()) {
511             // enforce view permission
512             if (mayAccessPage('view',$rev->_pagename)) {
513                 if ($link = $this->pageLink($rev)) // some entries may be empty 
514                                                    // (/Blog/.. interim pages)
515                     $html->pushContent($sp, $link, HTML::br());
516                 if ($first)
517                     $this->setValidators($rev);
518                 $first = false;
519             }
520         }
521         if ($first)
522             $html->pushContent(HTML::p(array('class' => 'rc-empty'),
523                                        $this->empty_message()));
524         return $html;
525     }
526 }
527
528 class _RecentChanges_RssFormatter
529 extends _RecentChanges_Formatter
530 {
531     var $_absurls = true;
532
533     function time ($rev) {
534         return Iso8601DateTime($rev->get('mtime'));
535     }
536
537     function pageURI ($rev) {
538         return WikiURL($rev, '', 'absurl');
539     }
540
541     function format ($changes) {
542         
543         include_once('lib/RssWriter.php');
544         $rss = new RssWriter;
545
546         $rss->channel($this->channel_properties());
547
548         if (($props = $this->image_properties()))
549             $rss->image($props);
550         if (($props = $this->textinput_properties()))
551             $rss->textinput($props);
552
553         $first = true;
554         while ($rev = $changes->next()) {
555             // enforce view permission
556             if (mayAccessPage('view', $rev->_pagename)) {
557                 $rss->addItem($this->item_properties($rev),
558                               $this->pageURI($rev));
559                 if ($first)
560                     $this->setValidators($rev);
561                 $first = false;
562             }
563         }
564
565         global $request;
566         $request->discardOutput();
567         $rss->finish();
568         printf("\n<!-- Generated by PhpWiki-%s:\n%s-->\n", PHPWIKI_VERSION, $GLOBALS['RCS_IDS']);
569
570         // Flush errors in comment, otherwise it's invalid XML.
571         global $ErrorManager;
572         if (($errors = $ErrorManager->getPostponedErrorsAsHTML()))
573             printf("\n<!-- PHP Warnings:\n%s-->\n", AsXML($errors));
574
575         $request->finish();     // NORETURN!!!!
576     }
577
578     function image_properties () {
579         global $WikiTheme;
580
581         $img_url = AbsoluteURL($WikiTheme->getImageURL('logo'));
582         if (!$img_url)
583             return false;
584
585         return array('title' => WIKI_NAME,
586                      'link' => WikiURL(HOME_PAGE, false, 'absurl'),
587                      'url' => $img_url);
588     }
589
590     function textinput_properties () {
591         return array('title' => _("Search"),
592                      'description' => _("Title Search"),
593                      'name' => 's',
594                      'link' => WikiURL(_("TitleSearch"), false, 'absurl'));
595     }
596
597     function channel_properties () {
598         global $request;
599
600         $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
601         return array('title' => WIKI_NAME,
602                      'link' => $rc_url,
603                      'description' => _("RecentChanges"),
604                      'dc:date' => Iso8601DateTime(time()),
605                      'dc:language' => $GLOBALS['LANG']);
606
607         /* FIXME: other things one might like in <channel>:
608          * sy:updateFrequency
609          * sy:updatePeriod
610          * sy:updateBase
611          * dc:subject
612          * dc:publisher
613          * dc:language
614          * dc:rights
615          * rss091:language
616          * rss091:managingEditor
617          * rss091:webmaster
618          * rss091:lastBuildDate
619          * rss091:copyright
620          */
621     }
622
623     function item_properties ($rev) {
624         $page = $rev->getPage();
625         $pagename = $page->getName();
626
627         return array( 'title'           => SplitPagename($pagename),
628                       'description'     => $this->summary($rev),
629                       'link'            => $this->pageURL($rev),
630                       'dc:date'         => $this->time($rev),
631                       'dc:contributor'  => $rev->get('author'),
632                       'wiki:version'    => $rev->getVersion(),
633                       'wiki:importance' => $this->importance($rev),
634                       'wiki:status'     => $this->status($rev),
635                       'wiki:diff'       => $this->diffURL($rev),
636                       'wiki:history'    => $this->historyURL($rev)
637                       );
638     }
639 }
640
641 /** explicit application/rss+xml Content-Type,
642  * simplified xml structure (no namespace),
643  * support for xml-rpc cloud registerProcedure (not yet)
644  */
645 class _RecentChanges_Rss2Formatter
646 extends _RecentChanges_RssFormatter {
647
648     function format ($changes) {
649         include_once('lib/RssWriter2.php');
650         $rss = new RssWriter2;
651
652         $rss->channel($this->channel_properties());
653         if (($props = $this->cloud_properties()))
654             $rss->cloud($props);
655         if (($props = $this->image_properties()))
656             $rss->image($props);
657         if (($props = $this->textinput_properties()))
658             $rss->textinput($props);
659         $first = true;
660         while ($rev = $changes->next()) {
661             // enforce view permission
662             if (mayAccessPage('view', $rev->_pagename)) {
663                 $rss->addItem($this->item_properties($rev),
664                               $this->pageURI($rev));
665                 if ($first)
666                     $this->setValidators($rev);
667                 $first = false;
668             }
669         }
670
671         global $request;
672         $request->discardOutput();
673         $rss->finish();
674         printf("\n<!-- Generated by PhpWiki-%s:\n%s-->\n", PHPWIKI_VERSION, $GLOBALS['RCS_IDS']);
675         // Flush errors in comment, otherwise it's invalid XML.
676         global $ErrorManager;
677         if (($errors = $ErrorManager->getPostponedErrorsAsHTML()))
678             printf("\n<!-- PHP Warnings:\n%s-->\n", AsXML($errors));
679
680         $request->finish();     // NORETURN!!!!
681     }
682
683     function channel_properties () {
684         $chann_10 = parent::channel_properties();
685         return array_merge($chann_10,
686                            array('generator' => 'PhpWiki-'.PHPWIKI_VERSION,
687                                  //<pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
688                                  //<lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>
689                                  //<docs>http://blogs.law.harvard.edu/tech/rss</docs>
690                                  'copyright' => COPYRIGHTPAGE_URL
691                                  ));
692     }
693
694     function cloud_properties () { return false; } // xml-rpc registerProcedure not yet implemented
695     function cloud_properties_test () {
696         return array('protocol' => 'xml-rpc', // xml-rpc or soap or http-post
697                      'registerProcedure' => 'wiki.rssPleaseNotify',
698                      'path' => DATA_PATH.'/RPC2.php',
699                      'port' => !SERVER_PORT ? '80' : (SERVER_PROTOCOL == 'https' ? '443' : '80'),
700                      'domain' => SERVER_NAME);
701     }
702 }
703
704 class NonDeletedRevisionIterator extends WikiDB_PageRevisionIterator
705 {
706     /** Constructor
707      *
708      * @param $revisions object a WikiDB_PageRevisionIterator.
709      */
710     function NonDeletedRevisionIterator ($revisions, $check_current_revision = true) {
711         $this->_revisions = $revisions;
712         $this->_check_current_revision = $check_current_revision;
713     }
714
715     function next () {
716         while (($rev = $this->_revisions->next())) {
717             if ($this->_check_current_revision) {
718                 $page = $rev->getPage();
719                 $check_rev = $page->getCurrentRevision();
720             }
721             else {
722                 $check_rev = $rev;
723             }
724             if (! $check_rev->hasDefaultContents())
725                 return $rev;
726         }
727         $this->free();
728         return false;
729     }
730
731 }
732
733 class WikiPlugin_RecentChanges
734 extends WikiPlugin
735 {
736     function getName () {
737         return _("RecentChanges");
738     }
739
740     function getVersion() {
741         return preg_replace("/[Revision: $]/", '',
742                             "\$Revision: 1.109 $");
743     }
744
745     function managesValidators() {
746         // Note that this is a bit of a fig.
747         // We set validators based on the most recently changed page,
748         // but this fails when the most-recent page is deleted.
749         // (Consider that the Last-Modified time will decrease
750         // when this happens.)
751
752         // We might be better off, leaving this as false (and junking
753         // the validator logic above) and just falling back to the
754         // default behavior (handled by WikiPlugin) of just using
755         // the WikiDB global timestamp as the mtime.
756
757         // Nevertheless, for now, I leave this here, mostly as an
758         // example for how to use appendValidators() and managesValidators().
759         
760         return true;
761     }
762             
763     function getDefaultArguments() {
764         return array('days'         => 2,
765                      'show_minor'   => false,
766                      'show_major'   => true,
767                      'show_all'     => false,
768                      'show_deleted' => 'sometimes',
769                      'limit'        => false,
770                      'format'       => false,
771                      'daylist'      => false,
772                      'difflinks'    => true,
773                      'historylinks' => false,
774                      'caption'      => ''
775                      );
776     }
777
778     function getArgs ($argstr, $request, $defaults = false) {
779         if (!$defaults) $defaults = $this->getDefaultArguments();
780         $args = WikiPlugin::getArgs($argstr, $request, $defaults);
781
782         $action = $request->getArg('action');
783         if ($action != 'browse' && ! $request->isActionPage($action))
784             $args['format'] = false; // default -> HTML
785
786         if ($args['format'] == 'rss' && empty($args['limit']))
787             $args['limit'] = 15; // Fix default value for RSS.
788         if ($args['format'] == 'rss2' && empty($args['limit']))
789             $args['limit'] = 15; // Fix default value for RSS2.
790
791         if ($args['format'] == 'sidebar' && empty($args['limit']))
792             $args['limit'] = 10; // Fix default value for sidebar.
793
794         return $args;
795     }
796
797     function getMostRecentParams ($args) {
798         extract($args);
799
800         $params = array('include_minor_revisions' => $show_minor,
801                         'exclude_major_revisions' => !$show_major,
802                         'include_all_revisions' => !empty($show_all));
803         if ($limit != 0)
804             $params['limit'] = $limit;
805
806         if ($days > 0.0)
807             $params['since'] = time() - 24 * 3600 * $days;
808         elseif ($days < 0.0)
809             $params['since'] = 24 * 3600 * $days - time();
810
811         return $params;
812     }
813
814     function getChanges ($dbi, $args) {
815         $changes = $dbi->mostRecent($this->getMostRecentParams($args));
816
817         $show_deleted = $args['show_deleted'];
818         if ($show_deleted == 'sometimes')
819             $show_deleted = $args['show_minor'];
820
821         if (!$show_deleted)
822             $changes = new NonDeletedRevisionIterator($changes, !$args['show_all']);
823
824         return $changes;
825     }
826
827     function format ($changes, $args) {
828         global $WikiTheme;
829         $format = $args['format'];
830
831         $fmt_class = $WikiTheme->getFormatter('RecentChanges', $format);
832         if (!$fmt_class) {
833             if ($format == 'rss')
834                 $fmt_class = '_RecentChanges_RssFormatter';
835             elseif ($format == 'rss2')
836                 $fmt_class = '_RecentChanges_Rss2Formatter';
837             elseif ($format == 'rss091') {
838                 include_once "lib/RSSWriter091.php";
839                 $fmt_class = '_RecentChanges_RssFormatter091';
840             }
841             elseif ($format == 'sidebar')
842                 $fmt_class = '_RecentChanges_SideBarFormatter';
843             elseif ($format == 'box')
844                 $fmt_class = '_RecentChanges_BoxFormatter';
845             else
846                 $fmt_class = '_RecentChanges_HtmlFormatter';
847         }
848
849         $fmt = new $fmt_class($args);
850         return $fmt->format($changes);
851     }
852
853     function run($dbi, $argstr, &$request, $basepage) {
854         $args = $this->getArgs($argstr, $request);
855
856         // HACKish: fix for SF bug #622784  (1000 years of RecentChanges ought
857         // to be enough for anyone.)
858         $args['days'] = min($args['days'], 365000);
859         
860         // Hack alert: format() is a NORETURN for rss formatters.
861         return $this->format($this->getChanges($dbi, $args), $args);
862     }
863
864     // box is used to display a fixed-width, narrow version with common header.
865     // just a numbered list of limit pagenames, without date.
866     function box($args = false, $request = false, $basepage = false) {
867         if (!$request) $request =& $GLOBALS['request'];
868         if (!isset($args['limit'])) $args['limit'] = 15;
869         $args['format'] = 'box';
870         $args['show_minor'] = false;
871         $args['show_major'] = true;
872         $args['show_deleted'] = 'sometimes';
873         $args['show_all'] = false;
874         $args['days'] = 90;
875         return $this->makeBox(WikiLink($this->getName(),'',SplitPagename($this->getName())),
876                               $this->format($this->getChanges($request->_dbi, $args), $args));
877     }
878
879 };
880
881
882 class DayButtonBar extends HtmlElement {
883
884     function DayButtonBar ($plugin_args) {
885         $this->__construct('p', array('class' => 'wiki-rc-action'));
886
887         // Display days selection buttons
888         extract($plugin_args);
889
890         // Custom caption
891         if (! $caption) {
892             if ($show_minor)
893                 $caption = _("Show minor edits for:");
894             elseif ($show_all)
895                 $caption = _("Show all changes for:");
896             else
897                 $caption = _("Show changes for:");
898         }
899
900         $this->pushContent($caption, ' ');
901
902         global $WikiTheme;
903         $sep = $WikiTheme->getButtonSeparator();
904
905         $n = 0;
906         foreach (explode(",", $daylist) as $days) {
907             if ($n++)
908                 $this->pushContent($sep);
909             $this->pushContent($this->_makeDayButton($days));
910         }
911     }
912
913     function _makeDayButton ($days) {
914         global $WikiTheme, $request;
915
916         if ($days == 1)
917             $label = _("1 day");
918         elseif ($days < 1)
919             $label = "..."; //alldays
920         else
921             $label = sprintf(_("%s days"), abs($days));
922
923         $url = $request->getURLtoSelf(array('action' => $request->getArg('action'), 'days' => $days));
924
925         return $WikiTheme->makeButton($label, $url, 'wiki-rc-action');
926     }
927 }
928
929 // $Log: not supported by cvs2svn $
930 // Revision 1.108  2005/04/01 16:09:35  rurban
931 // fix defaults in RecentChanges plugins: e.g. invalid pagenames for PageHistory
932 //
933 // Revision 1.107  2005/02/04 13:45:28  rurban
934 // improve box layout a bit
935 //
936 // Revision 1.106  2005/02/02 19:39:10  rurban
937 // honor show_all=false
938 //
939 // Revision 1.105  2005/01/25 03:50:54  uckelman
940 // pre_description is a member function, so call with $this->.
941 //
942 // Revision 1.104  2005/01/24 23:15:16  uckelman
943 // The extra description for RelatedChanges was appearing in RecentChanges
944 // and PageHistory due to a bad test in _RecentChanges_HtmlFormatter. Fixed.
945 //
946 // Revision 1.103  2004/12/15 17:45:09  rurban
947 // fix box method
948 //
949 // Revision 1.102  2004/12/06 19:29:24  rurban
950 // simplify RSS: add RSS2 link (rss tag only, new content-type)
951 //
952 // Revision 1.101  2004/11/10 19:32:24  rurban
953 // * optimize increaseHitCount, esp. for mysql.
954 // * prepend dirs to the include_path (phpwiki_dir for faster searches)
955 // * Pear_DB version logic (awful but needed)
956 // * fix broken ADODB quote
957 // * _extract_page_data simplification
958 //
959 // Revision 1.100  2004/06/28 16:35:12  rurban
960 // prevent from shell commands
961 //
962 // Revision 1.99  2004/06/20 14:42:54  rurban
963 // various php5 fixes (still broken at blockparser)
964 //
965 // Revision 1.98  2004/06/14 11:31:39  rurban
966 // renamed global $Theme to $WikiTheme (gforge nameclash)
967 // inherit PageList default options from PageList
968 //   default sortby=pagename
969 // use options in PageList_Selectable (limit, sortby, ...)
970 // added action revert, with button at action=diff
971 // added option regex to WikiAdminSearchReplace
972 //
973 // Revision 1.97  2004/06/03 18:58:27  rurban
974 // days links requires action=RelatedChanges arg
975 //
976 // Revision 1.96  2004/05/18 16:23:40  rurban
977 // rename split_pagename to SplitPagename
978 //
979 // Revision 1.95  2004/05/16 22:07:35  rurban
980 // check more config-default and predefined constants
981 // various PagePerm fixes:
982 //   fix default PagePerms, esp. edit and view for Bogo and Password users
983 //   implemented Creator and Owner
984 //   BOGOUSERS renamed to BOGOUSER
985 // fixed syntax errors in signin.tmpl
986 //
987 // Revision 1.94  2004/05/14 20:55:03  rurban
988 // simplified RecentComments
989 //
990 // Revision 1.93  2004/05/14 17:33:07  rurban
991 // new plugin RecentChanges
992 //
993 // Revision 1.92  2004/04/21 04:29:10  rurban
994 // Two convenient RecentChanges extensions
995 //   RelatedChanges (only links from current page)
996 //   RecentEdits (just change the default args)
997 //
998 // Revision 1.91  2004/04/19 18:27:46  rurban
999 // Prevent from some PHP5 warnings (ref args, no :: object init)
1000 //   php5 runs now through, just one wrong XmlElement object init missing
1001 // Removed unneccesary UpgradeUser lines
1002 // Changed WikiLink to omit version if current (RecentChanges)
1003 //
1004 // Revision 1.90  2004/04/18 01:11:52  rurban
1005 // more numeric pagename fixes.
1006 // fixed action=upload with merge conflict warnings.
1007 // charset changed from constant to global (dynamic utf-8 switching)
1008 //
1009 // Revision 1.89  2004/04/10 02:30:49  rurban
1010 // Fixed gettext problem with VIRTUAL_PATH scripts (Windows only probably)
1011 // Fixed "cannot setlocale..." (sf.net problem)
1012 //
1013 // Revision 1.88  2004/04/01 15:57:10  rurban
1014 // simplified Sidebar theme: table, not absolute css positioning
1015 // added the new box methods.
1016 // remaining problems: large left margin, how to override _autosplitWikiWords in Template only
1017 //
1018 // Revision 1.87  2004/03/30 02:14:03  rurban
1019 // fixed yet another Prefs bug
1020 // added generic PearDb_iter
1021 // $request->appendValidators no so strict as before
1022 // added some box plugin methods
1023 // PageList commalist for condensed output
1024 //
1025 // Revision 1.86  2004/03/12 13:31:43  rurban
1026 // enforce PagePermissions, errormsg if not Admin
1027 //
1028 // Revision 1.85  2004/02/17 12:11:36  rurban
1029 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
1030 //
1031 // Revision 1.84  2004/02/15 22:29:42  rurban
1032 // revert premature performance fix
1033 //
1034 // Revision 1.83  2004/02/15 21:34:37  rurban
1035 // PageList enhanced and improved.
1036 // fixed new WikiAdmin... plugins
1037 // editpage, Theme with exp. htmlarea framework
1038 //   (htmlarea yet committed, this is really questionable)
1039 // WikiUser... code with better session handling for prefs
1040 // enhanced UserPreferences (again)
1041 // RecentChanges for show_deleted: how should pages be deleted then?
1042 //
1043 // Revision 1.82  2004/01/25 03:58:43  rurban
1044 // use stdlib:isWikiWord()
1045 //
1046 // Revision 1.81  2003/11/28 21:06:31  carstenklapp
1047 // Enhancement: Mozilla RecentChanges sidebar now defaults to 10 changes
1048 // instead of 1. Make diff buttons smaller with css. Added description
1049 // line back in at the top.
1050 //
1051 // Revision 1.80  2003/11/27 15:17:01  carstenklapp
1052 // Theme & appearance tweaks: Converted Mozilla sidebar link into a Theme
1053 // button, to allow an image button for it to be added to Themes. Output
1054 // RSS button in small text size when theme has no button image.
1055 //
1056 // Revision 1.79  2003/04/29 14:34:20  dairiki
1057 // Bug fix: "add sidebar" link didn't work when USE_PATH_INFO was false.
1058 //
1059 // Revision 1.78  2003/03/04 01:55:05  dairiki
1060 // Fix to ensure absolute URL for logo in RSS recent changes.
1061 //
1062 // Revision 1.77  2003/02/27 23:23:38  dairiki
1063 // Fix my breakage of CSS and sidebar RecentChanges output.
1064 //
1065 // Revision 1.76  2003/02/27 22:48:44  dairiki
1066 // Fixes invalid HTML generated by PageHistory plugin.
1067 //
1068 // (<noscript> is block-level and not allowed within <p>.)
1069 //
1070 // Revision 1.75  2003/02/22 21:39:05  dairiki
1071 // Hackish fix for SF bug #622784.
1072 //
1073 // (The root of the problem is clearly a PHP bug.)
1074 //
1075 // Revision 1.74  2003/02/21 22:52:21  dairiki
1076 // Make sure to interpret relative links (like [/Subpage]) in summary
1077 // relative to correct basepage.
1078 //
1079 // Revision 1.73  2003/02/21 04:12:06  dairiki
1080 // Minor fixes for new cached markup.
1081 //
1082 // Revision 1.72  2003/02/17 02:19:01  dairiki
1083 // Fix so that PageHistory will work when the current revision
1084 // of a page has been "deleted".
1085 //
1086 // Revision 1.71  2003/02/16 20:04:48  dairiki
1087 // Refactor the HTTP validator generation/checking code.
1088 //
1089 // This also fixes a number of bugs with yesterdays validator mods.
1090 //
1091 // Revision 1.70  2003/02/16 05:09:43  dairiki
1092 // Starting to fix handling of the HTTP validator headers, Last-Modified,
1093 // and ETag.
1094 //
1095 // Last-Modified was being set incorrectly (but only when DEBUG was not
1096 // defined!)  Setting a Last-Modified without setting an appropriate
1097 // Expires: and/or Cache-Control: header results in browsers caching
1098 // the page unconditionally (for a certain period of time).
1099 // This is generally bad, since it means people don't see updated
1100 // page contents right away --- this is particularly confusing to
1101 // the people who are editing pages since their edits don't show up
1102 // next time they browse the page.
1103 //
1104 // Now, we don't allow caching of pages without revalidation
1105 // (via the If-Modified-Since and/or If-None-Match request headers.)
1106 // (You can allow caching by defining CACHE_CONTROL_MAX_AGE to an
1107 // appropriate value in index.php, but I advise against it.)
1108 //
1109 // Problems:
1110 //
1111 //   o Even when request is aborted due to the content not being
1112 //     modified, we currently still do almost all the work involved
1113 //     in producing the page.  So the only real savings from all
1114 //     this logic is in network bandwidth.
1115 //
1116 //   o Plugins which produce "dynamic" output need to be inspected
1117 //     and made to call $request->addToETag() and
1118 //     $request->setModificationTime() appropriately, otherwise the
1119 //     page can change without the change being detected.
1120 //     This leads to stale pages in cache again...
1121 //
1122 // Revision 1.69  2003/01/18 22:01:43  carstenklapp
1123 // Code cleanup:
1124 // Reformatting & tabs to spaces;
1125 // Added copyleft, getVersion, getDescription, rcs_id.
1126 //
1127
1128 // (c-file-style: "gnu")
1129 // Local Variables:
1130 // mode: php
1131 // tab-width: 8
1132 // c-basic-offset: 4
1133 // c-hanging-comment-ender-p: nil
1134 // indent-tabs-mode: nil
1135 // End:
1136 ?>