]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RecentChanges.php
Include the "show recent changes for: 1 days | 3 days | ..." button bar as part
[SourceForge/phpwiki.git] / lib / plugin / RecentChanges.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RecentChanges.php,v 1.52 2002-02-01 22:01:42 dairiki Exp $');
3 /**
4  */
5
6         
7 class _RecentChanges_Formatter
8 {
9     var $_absurls = false;
10     
11     function _RecentChanges_Formatter ($rc_args) {
12         $this->_args = $rc_args;
13         $this->_diffargs = array('action' => 'diff');
14
15         if ($rc_args['show_minor'] || !$rc_args['show_major'])
16             $this->_diffargs['previous'] = 'minor';
17     }
18
19     function include_versions_in_URLs() {
20         return (bool) $this->_args['show_all'];
21     }
22     
23     function date ($rev) {
24         global $Theme;
25         return $Theme->formatDate($rev->get('mtime'));
26     }
27
28     function time ($rev) {
29         global $Theme;
30         return $Theme->formatTime($rev->get('mtime'));
31     }
32
33     function diffURL ($rev) {
34         $args = $this->_diffargs;
35         if ($this->include_versions_in_URLs())
36             $args['version'] = $rev->getVersion();
37         $page = $rev->getPage();
38         return WikiURL($page->getName(), $args, $this->_absurls);
39     }
40
41     function historyURL ($rev) {
42         $page = $rev->getPage();
43         return WikiURL($page, array('action' => _("PageHistory")),
44                        $this->_absurls);
45     }
46
47     function pageURL ($rev) {
48         return WikiURL($this->include_versions_in_URLs() ? $rev : $page,
49                        '', $this->_absurls);
50     }
51     
52     function authorHasPage ($author) {
53         global $WikiNameRegexp, $request;
54         $dbi = $request->getDbh();
55         return preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author);
56     }
57
58     function authorURL ($author) {
59         return $this->authorHasPage() ? WikiURL($author) : false;
60     }
61
62
63     function status ($rev) {
64         if ($rev->hasDefaultContents())
65             return 'deleted';
66         $page = $rev->getPage();
67         $prev = $page->getRevisionBefore($rev->getVersion());
68         if ($prev->hasDefaultContents())
69             return 'new';
70         return 'updated';
71     }
72
73     function importance ($rev) {
74         return $rev->get('is_minor_edit') ? 'minor' : 'major';
75     }
76     
77     function summary($rev) {
78         if ( ($summary = $rev->get('summary')) )
79             return $summary;
80
81         switch ($this->status($rev)) {
82         case 'deleted':
83             return _("Deleted.");
84         case 'new':
85             return _("New page.");
86         default:
87             return '';
88         }
89     }
90 }
91
92 class _RecentChanges_HtmlFormatter
93 extends _RecentChanges_Formatter
94 {
95     function diffLink ($rev) {
96         global $Theme;
97         return $Theme->makeButton(_("(diff)"), $this->diffURL($rev), 'wiki-rc-action');
98     }
99
100     function historyLink ($rev) {
101         global $Theme;
102         return $Theme->makeButton(_("(hist)"), $this->historyURL($rev), 'wiki-rc-action');
103     }
104
105     function pageLink ($rev) {
106         $page = $rev->getPage();
107         global $Theme;
108         if ($this->include_versions_in_URLs()) {
109             $version = $rev->getVersion();
110             $exists = !$rev->hasDefaultContents();
111         }
112         else {
113             $version = false;
114             $cur = $page->getCurrentRevision();
115             $exists = !$cur->hasDefaultContents();
116         }
117         if ($exists)
118             return $Theme->linkExistingWikiWord($page->getName(), false, $version);
119         else
120             return $Theme->linkUnknownWikiWord($page->getName(), false, $version);
121     }
122     
123     function authorLink ($rev) {
124         $author = $rev->get('author');
125         if ( $this->authorHasPage($author) ) {
126             return WikiLink($author);
127         } else
128             return $author;
129     }
130
131     function summaryAsHTML ($rev) {
132         if ( !($summary = $this->summary($rev)) )
133             return '';
134         return  HTML::strong( array('class' => 'wiki-summary'),
135                               "[",
136                               do_transform($summary, 'LinkTransform'),
137                               "]");
138     }
139         
140     function rss_icon () {
141         global $request, $Theme;
142
143         $rss_url = $request->getURLtoSelf(array('format' => 'rss'));
144         return $Theme->makeButton("RSS", $rss_url, 'rssicion');
145     }
146     
147     function description () {
148         extract($this->_args);
149
150         // FIXME: say something about show_all.
151
152         if ($show_major && $show_minor)
153             $edits = _("edits");
154         elseif ($show_major)
155             $edits = _("major edits");
156         else
157             $edits = _("minor edits");
158
159         if ($days > 0) {
160             if (intval($days) != $days)
161                 $days = sprintf("%.1f", $days);
162             $timespan = $days == 1 ? _("day") :  sprintf(_("%s days"), $days);
163         }
164             
165         if ($limit > 0) {
166             if (!empty($timespan))
167                 $desc = fmt("The %d most recent %s during the past %s are listed below.",
168                             $limit, $edits, $timespan);
169             else
170                 $desc = fmt("The %d most recent %s are listed below.",
171                             $limit, $edits);
172         }
173         else {
174             if (!empty($timespan))
175                 $desc = fmt("The most recent %s during the past %s are listed below.",
176                             $edits, $timespan);
177             else
178                 $desc = fmt("All %s are listed below.", $edits);
179         }
180         return $desc;
181     }
182
183         
184     function title () {
185         extract($this->_args);
186         return array($show_minor ? _("RecentEdits") : _("RecentChanges"),
187                      ' ',
188                      $this->rss_icon());
189     }
190
191     function format ($changes) {
192         $html = HTML(HTML::h2(false, $this->title()));
193         if (($desc = $this->description()))
194             $html->pushContent(HTML::p(false, $desc));
195
196         if ($this->_args['daylist'])
197             $html->pushContent(new DayButtonBar($this->_args));
198         
199         $last_date = '';
200         $lines = false;
201         
202         while ($rev = $changes->next()) {
203             if (($date = $this->date($rev)) != $last_date) {
204                 if ($lines)
205                     $html->pushContent($lines);
206                 $html->pushContent(HTML::h3($date));
207                 $lines = HTML::ul();
208                 $last_date = $date;
209             }
210             $lines->pushContent($this->format_revision($rev));
211         }
212         if ($lines)
213             $html->pushContent($lines);
214         return $html;
215     }
216
217     function format_revision ($rev) {
218         $args = &$this->_args;
219         
220         $class = 'rc-' . $this->importance($rev);
221
222         $time = $this->time($rev);
223         if (! $rev->get('is_minor_edit'))
224             $time = HTML::strong($time);
225
226         $line = HTML::li(array('class' => $class));
227
228
229         if ($args['difflinks'])
230             $line->pushContent($this->diffLink($rev), ' ');
231
232         if ($args['historylinks'])
233             $line->pushContent($this->historyLink($rev), ' ');
234
235         $line->pushContent($this->pageLink($rev), ' ',
236                            $time, ' ',
237                            $this->summaryAsHTML($rev),
238                            ' ... ',
239                            $this->authorLink($rev));
240         return $line;
241     }
242 }
243
244
245 class _RecentChanges_RssFormatter
246 extends _RecentChanges_Formatter
247 {
248     var $_absurls = true;
249
250     function time ($rev) {
251         return Iso8601DateTime($rev->get('mtime'));
252     }
253
254     function pageURI ($rev) {
255         return WikiURL($rev, '', 'absurl');
256     }
257     
258     function format ($changes) {
259         include_once('lib/RssWriter.php');
260         $rss = new RssWriter;
261
262         
263         $rss->channel($this->channel_properties());
264
265         if (($props = $this->image_properties()))
266             $rss->image($props);
267         if (($props = $this->textinput_properties()))
268             $rss->textinput($props);
269
270         while ($rev = $changes->next()) {
271             $rss->addItem($this->item_properties($rev),
272                           $this->pageURI($rev));
273         }
274
275         $rss->finish();
276         printf("\n<!-- Generated by PhpWiki:\n%s-->\n", $GLOBALS['RCS_IDS']);
277
278         global $request;        // FIXME
279         $request->finish();     // NORETURN!!!!
280     }
281     
282     function image_properties () {
283         global $Theme;
284
285         $img_url = $Theme->getImageURL('logo');
286         if (!$img_url)
287             return false;
288         
289         return array('title' => WIKI_NAME,
290                      'link' => WikiURL(HomePage, false, 'absurl'),
291                      'url' => $img_url);
292     }
293
294     function textinput_properties () {
295         return array('title' => _("Search"),
296                      'description' => _("Title Search"),
297                      'name' => 's',
298                      'link' => WikiURL(_("TitleSearch"), false, 'absurl'));
299     }
300     
301     function channel_properties () {
302         global $request;
303
304         $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
305
306         return array('title' => WIKI_NAME,
307                      'dc:description' => _("RecentChanges"),
308                      'link' => $rc_url,
309                      'dc:date' => Iso8601DateTime(time()));
310
311         /* FIXME: other things one might like in <channel>:                   
312          * sy:updateFrequency
313          * sy:updatePeriod
314          * sy:updateBase
315          * dc:subject
316          * dc:publisher
317          * dc:language
318          * dc:rights
319          * rss091:language
320          * rss091:managingEditor
321          * rss091:webmaster
322          * rss091:lastBuildDate
323          * rss091:copyright
324          */
325     }
326     
327
328     
329         
330     function item_properties ($rev) {
331         $page = $rev->getPage();
332         $pagename = $page->getName();
333         
334         return array( 'title'           => split_pagename($pagename),
335                       'description'     => $this->summary($rev),
336                       'link'            => $this->pageURL($rev),
337                       'dc:date'         => $this->time($rev),
338                       'dc:contributor'  => $rev->get('author'),
339                       'wiki:version'    => $rev->getVersion(),
340                       'wiki:importance' => $this->importance($rev),
341                       'wiki:status'     => $this->status($rev),
342                       'wiki:diff'       => $this->diffURL($rev),
343                       'wiki:history'    => $this->historyURL($rev)
344                       );
345     }
346 }
347
348 class NonDeletedRevisionIterator extends WikiDB_PageRevisionIterator
349 {
350     /** Constructor
351      *
352      * @param $revisions object a WikiDB_PageRevisionIterator.
353      */
354     function NonDeletedRevisionIterator ($revisions, $check_current_revision = true) {
355         $this->_revisions = $revisions;
356         $this->_check_current_revision = $check_current_revision;
357     }
358
359     function next () {
360         while (($rev = $this->_revisions->next())) {
361             if ($this->_check_current_revision) {
362                 $page = $rev->getPage();
363                 $check_rev = $page->getCurrentRevision();
364             }
365             else {
366                 $check_rev = $rev;
367             }
368             if (! $check_rev->hasDefaultContents())
369                 return $rev;
370         }
371         $this->free();
372         return false;
373     }
374
375     function free () {
376         $this->_revisions->free();
377     }
378 }
379
380 class WikiPlugin_RecentChanges
381 extends WikiPlugin
382 {
383     function getName () {
384         return _("RecentChanges");
385     }
386
387     function getDefaultArguments() {
388         return array('days'             => 2,
389                      'show_minor'       => false,
390                      'show_major'       => true,
391                      'show_all'         => false,
392                      'show_deleted'     => 'sometimes',
393                      'limit'            => false,
394                      'format'           => false,
395                      'daylist'          => false,
396                      'difflinks'        => true,
397                      'historylinks'     => false,
398                      'caption'          => ''
399                      );
400     }
401
402     function getArgs ($argstr, $request, $defaults = false) {
403         $args = WikiPlugin::getArgs($argstr, $request, $defaults);
404
405         $action = $request->getArg('action');
406         if ($action != 'browse' && ! $request->isActionPage($action))
407             $args['format'] = false; // default -> HTML
408         
409         if ($args['format'] == 'rss' && empty($args['limit']))
410             $args['limit'] = 15; // Fix default value for RSS.
411
412         return $args;
413     }
414         
415     function getMostRecentParams ($args) {
416         extract($args);
417
418         $params = array('include_minor_revisions' => $show_minor,
419                         'exclude_major_revisions' => !$show_major,
420                         'include_all_revisions' => !empty($show_all));
421
422         if ($limit > 0)
423             $params['limit'] = $limit;
424
425         if ($days > 0.0)
426             $params['since'] = time() - 24 * 3600 * $days;
427
428         return $params;
429     }
430     
431     function getChanges ($dbi, $args) {
432         $changes = $dbi->mostRecent($this->getMostRecentParams($args));
433
434         $show_deleted = $args['show_deleted'];
435         if ($show_deleted == 'sometimes')
436             $show_deleted = $args['show_minor'];
437
438         if (!$show_deleted)
439             $changes = new NonDeletedRevisionIterator($changes, !$args['show_all']);
440
441         return $changes;
442     }
443
444     function format ($changes, $args) {
445         global $Theme;
446         $format = $args['format'];
447         
448         $fmt_class = $Theme->getFormatter('RecentChanges', $format);
449         if (!$fmt_class) {
450             if ($format == 'rss')
451                 $fmt_class = '_RecentChanges_RssFormatter';
452             elseif ($format == 'rss091') {
453                 include_once "lib/RSSWriter091.php";
454                 $fmt_class = '_RecentChanges_RssFormatter091';
455             }
456             else
457                 $fmt_class = '_RecentChanges_HtmlFormatter';
458         }
459         
460         $fmt = new $fmt_class($args);
461         return $fmt->format($changes);
462     }
463
464     function run ($dbi, $argstr, $request) {
465         $args = $this->getArgs($argstr, $request);
466
467         // Hack alert: format() is a NORETURN for rss formatters.
468         return $this->format($this->getChanges($dbi, $args), $args);
469     }
470 };
471
472
473 class DayButtonBar extends HtmlElement {
474
475     function DayButtonBar ($plugin_args) {
476         $this->HtmlElement('p', array('class' => 'wiki-rc-action'));
477         
478         // Display days selection buttons
479         extract($plugin_args);
480         
481         // Custom caption
482         if (! $caption) {
483             if ($show_minor)
484                 $caption = _("Show minor edits for:");
485             elseif ($show_all)
486                 $caption = _("Show all changes for:");
487             else
488                 $caption = _("Show changes for:");
489         }
490
491         $this->pushContent($caption, ' ');
492
493         global $Theme;
494         $sep = $Theme->getButtonSeparator();
495         
496         $n = 0;
497         foreach (explode(",", $daylist) as $days) {
498             if ($n++)
499                 $this->pushContent($sep);
500             $this->pushContent($this->_makeDayButton($days));
501         }
502     }
503
504     function _makeDayButton ($days) {
505         global $Theme, $request;
506         
507         if ($days == 1)
508             $label = _("1 day");
509         elseif ($days < 1)
510             $label = "..."; //alldays
511         else
512             $label = sprintf(_("%s days"), $days);
513
514         $url = $request->getURLtoSelf(array('action' => 'browse', 'days' => $days));
515
516         return $Theme->makeButton($label, $url, 'wiki-rc-action');
517     }
518 }
519
520
521
522 // (c-file-style: "gnu")
523 // Local Variables:
524 // mode: php
525 // tab-width: 8
526 // c-basic-offset: 4
527 // c-hanging-comment-ender-p: nil
528 // indent-tabs-mode: nil
529 // End:   
530 ?>