]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RecentChanges.php
added difflink and historylink args
[SourceForge/phpwiki.git] / lib / plugin / RecentChanges.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RecentChanges.php,v 1.49 2002-02-01 19:17:21 carstenklapp 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 ($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         $difflink = false;
212         $historylink = false;
213         if ($this->_show_difflinks)
214             $difflink = $this->diffLink($rev);
215         if ($this->_show_historylinks)
216             $historylink = $this->historyLink($rev);
217
218         return HTML::li(array('class' => $class),
219                         $difflink, ' ',
220                         $historylink, ' ',
221                         $this->pageLink($rev), ' ',
222                         $rev->get('is_minor_edit') ? $this->time($rev) : HTML::strong($this->time($rev)), ' ',
223                         $this->summaryAsHTML($rev),
224                         ' ... ',
225                         $this->authorLink($rev));
226     }
227 }
228
229
230 class _RecentChanges_RssFormatter
231 extends _RecentChanges_Formatter
232 {
233     var $_absurls = true;
234
235     function time ($rev) {
236         return Iso8601DateTime($rev->get('mtime'));
237     }
238
239     function pageURI ($rev) {
240         return WikiURL($rev, '', 'absurl');
241     }
242     
243     function format ($changes) {
244         include_once('lib/RssWriter.php');
245         $rss = new RssWriter;
246
247         
248         $rss->channel($this->channel_properties());
249
250         if (($props = $this->image_properties()))
251             $rss->image($props);
252         if (($props = $this->textinput_properties()))
253             $rss->textinput($props);
254
255         while ($rev = $changes->next()) {
256             $rss->addItem($this->item_properties($rev),
257                           $this->pageURI($rev));
258         }
259
260         $rss->finish();
261         printf("\n<!-- Generated by PhpWiki:\n%s-->\n", $GLOBALS['RCS_IDS']);
262
263         global $request;        // FIXME
264         $request->finish();     // NORETURN!!!!
265     }
266     
267     function image_properties () {
268         global $Theme;
269
270         $img_url = $Theme->getImageURL('logo');
271         if (!$img_url)
272             return false;
273         
274         return array('title' => WIKI_NAME,
275                      'link' => WikiURL(HomePage, false, 'absurl'),
276                      'url' => $img_url);
277     }
278
279     function textinput_properties () {
280         return array('title' => _("Search"),
281                      'description' => _("Title Search"),
282                      'name' => 's',
283                      'link' => WikiURL(_("TitleSearch"), false, 'absurl'));
284     }
285     
286     function channel_properties () {
287         global $request;
288
289         $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
290
291         return array('title' => WIKI_NAME,
292                      'dc:description' => _("RecentChanges"),
293                      'link' => $rc_url,
294                      'dc:date' => Iso8601DateTime(time()));
295
296         /* FIXME: other things one might like in <channel>:                   
297          * sy:updateFrequency
298          * sy:updatePeriod
299          * sy:updateBase
300          * dc:subject
301          * dc:publisher
302          * dc:language
303          * dc:rights
304          * rss091:language
305          * rss091:managingEditor
306          * rss091:webmaster
307          * rss091:lastBuildDate
308          * rss091:copyright
309          */
310     }
311     
312
313     
314         
315     function item_properties ($rev) {
316         $page = $rev->getPage();
317         $pagename = $page->getName();
318         
319         return array( 'title'           => split_pagename($pagename),
320                       'description'     => $this->summary($rev),
321                       'link'            => $this->pageURL($rev),
322                       'dc:date'         => $this->time($rev),
323                       'dc:contributor'  => $rev->get('author'),
324                       'wiki:version'    => $rev->getVersion(),
325                       'wiki:importance' => $this->importance($rev),
326                       'wiki:status'     => $this->status($rev),
327                       'wiki:diff'       => $this->diffURL($rev),
328                       'wiki:history'    => $this->historyURL($rev)
329                       );
330     }
331 }
332
333 class NonDeletedRevisionIterator extends WikiDB_PageRevisionIterator
334 {
335     /** Constructor
336      *
337      * @param $revisions object a WikiDB_PageRevisionIterator.
338      */
339     function NonDeletedRevisionIterator ($revisions, $check_current_revision = true) {
340         $this->_revisions = $revisions;
341         $this->_check_current_revision = $check_current_revision;
342     }
343
344     function next () {
345         while (($rev = $this->_revisions->next())) {
346             if ($this->_check_current_revision) {
347                 $page = $rev->getPage();
348                 $check_rev = $page->getCurrentRevision();
349             }
350             else {
351                 $check_rev = $rev;
352             }
353             if (! $check_rev->hasDefaultContents())
354                 return $rev;
355         }
356         $this->free();
357         return false;
358     }
359
360     function free () {
361         $this->_revisions->free();
362     }
363 }
364
365 class WikiPlugin_RecentChanges
366 extends WikiPlugin
367 {
368     function getName () {
369         return _("RecentChanges");
370     }
371
372     function getDefaultArguments() {
373         return array('days'             => 2,
374                      'show_minor'       => false,
375                      'show_major'       => true,
376                      'show_all'         => false,
377                      'show_deleted'     => 'sometimes',
378                      'limit'            => false,
379                      'format'           => false,
380                      'daylist'          => false,
381                      'difflinks'        => true,
382                      'historylinks'     => false,
383                      'caption'          => ''
384                      );
385     }
386
387     function getArgs ($argstr, $request, $defaults = false) {
388         $args = WikiPlugin::getArgs($argstr, $request, $defaults);
389
390         $action = $request->getArg('action');
391         if ($action != 'browse' && ! $request->isActionPage($action))
392             $args['format'] = false; // default -> HTML
393         
394         if ($args['format'] == 'rss' && empty($args['limit']))
395             $args['limit'] = 15; // Fix default value for RSS.
396
397         return $args;
398     }
399         
400     function getMostRecentParams ($args) {
401         extract($args);
402
403         $params = array('include_minor_revisions' => $show_minor,
404                         'exclude_major_revisions' => !$show_major,
405                         'include_all_revisions' => !empty($show_all));
406
407         if ($limit > 0)
408             $params['limit'] = $limit;
409
410         if ($days > 0.0)
411             $params['since'] = time() - 24 * 3600 * $days;
412
413         return $params;
414     }
415     
416     function getChanges ($dbi, $args) {
417         $changes = $dbi->mostRecent($this->getMostRecentParams($args));
418
419         $show_deleted = $args['show_deleted'];
420         if ($show_deleted == 'sometimes')
421             $show_deleted = $args['show_minor'];
422
423         if (!$show_deleted)
424             $changes = new NonDeletedRevisionIterator($changes, !$args['show_all']);
425
426         return $changes;
427     }
428
429     function format ($changes, $args) {
430         global $Theme;
431         $format = $args['format'];
432         
433         $fmt_class = $Theme->getFormatter('RecentChanges', $format);
434         if (!$fmt_class) {
435             if ($format == 'rss')
436                 $fmt_class = '_RecentChanges_RssFormatter';
437             elseif ($format == 'rss091') {
438                 include_once "lib/RSSWriter091.php";
439                 $fmt_class = '_RecentChanges_RssFormatter091';
440             }
441             else
442                 $fmt_class = '_RecentChanges_HtmlFormatter';
443         }
444         
445         $fmt = new $fmt_class($args);
446         $fmt->_show_difflinks = $args['difflinks'];
447         $fmt->_show_historylinks = $args['historylinks'];
448
449         return $fmt->format($changes);
450     }
451
452     function run ($dbi, $argstr, $request) {
453         $args = $this->getArgs($argstr, $request);
454         
455         if (! $args['daylist']) {
456             // Display RecentChanges
457             //
458             // Hack alert: format() is a NORETURN for rss formatters.
459             return $this->format($this->getChanges($dbi, $args), $args);
460         } else {
461             // Display days selection buttons
462             extract($args);
463
464             $daysarray = explode(",", $daylist);
465
466             // Defaults
467             $url_show_minor = "";
468             $url_show_all = "";
469
470             // RecentEdits args
471             if (($show_minor == 1)||($show_minor == true))
472                 $url_show_minor = "&show_minor=1";
473             if (($show_all == 1)||($show_all == true))
474                 $url_show_all = "&show_all=1";
475             // Custom caption
476             if (! $caption) {
477                 if ($url_show_minor)
478                     $caption = _("Show changes for:");
479                 else {
480                     if ($url_show_all)
481                         $caption = _("Show all changes for:");
482                     else
483                         $caption = _("Show minor edits for:");
484                 }
485             }
486
487             $b = new buttonSet();
488             $b->caption = $caption;
489
490             foreach ($daysarray as $daynum) {
491
492                 if ($daynum == 1)
493                     $label = _("1 day");
494                 elseif ($daynum < 1)
495                     $label = "..."; //alldays
496                 else
497                     $label = sprintf(_("%s days"), $daynum);
498
499                 // Build the button's url
500                 $b->addButton($label, "RecentChanges?days=" .$daynum
501                                       .$url_show_minor .$url_show_all,
502                               'wiki-rc-action');
503             }
504             return HTML::div(array('class'=>'wiki-rc-action'), $b->getContent());
505         }
506     }
507 };
508
509
510 class buttonSet {
511     function buttonSet() {
512         $this->caption = "";
513         $this->content = "";
514         $this->_b = array();
515     }
516
517     function addButton($label, $url, $action) {
518         global $Theme;
519         $this->_b[] = $Theme->makeButton($label, $url, $action);
520     }
521
522     function getContent() {
523         if (empty($this->content))
524             $this->_generateContent();
525         return $this->content;
526     }
527
528     function _generateContent() {
529         $this->content = HTML::p($this->caption . " ");
530         // Avoid an extraneous ButtonSeparator
531         $this->content->pushContent(array_shift($this->_b));
532
533         global $Theme;
534         foreach ($this->_b as $button) {
535             $this->content->pushContent($Theme->getButtonSeparator());
536             $this->content->pushContent($button);
537         }
538     }
539
540 };
541
542
543 // (c-file-style: "gnu")
544 // Local Variables:
545 // mode: php
546 // tab-width: 8
547 // c-basic-offset: 4
548 // c-hanging-comment-ender-p: nil
549 // indent-tabs-mode: nil
550 // End:   
551 ?>