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