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