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