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