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