]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RecentChanges.php
Fix bug reported by MarkReid on KnownBugs:
[SourceForge/phpwiki.git] / lib / plugin / RecentChanges.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RecentChanges.php,v 1.20 2002-01-11 01:46:47 dairiki 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         return strftime($GLOBALS['dateformat'], $rev->get('mtime'));
26     }
27
28     function time ($rev) {
29         return preg_replace('/^0/', ' ',
30                             strtolower(strftime("%I:%M %p",
31                                                 $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, $dbi;
59
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         return QElement('a', array('href' => $this->diffURL($rev), 'class' => 'wikiaction'),
100                         _("(diff)"));
101     }
102
103     function pageLink ($rev) {
104         $page = $rev->getPage();
105         return QElement('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 QElement('a', array('href' => $url, 'class' => 'wiki'), $author);
113         else
114             return htmlspecialchars($author);
115     }
116
117
118     function rss_icon () {
119         global $request, $rssicon;
120
121         $rss_url = $request->getURLtoSelf(array('format' => 'rss'));
122         if (empty($rssicon))
123             $rssicon = 'images/rss.png';
124         return Element('a', array('href' => $rss_url),
125                        Element('img', array('src' => DataURL($rssicon),
126                                             'alt' => _("RSS available"),
127                                             'class' => 'rssicon')));
128     }
129     
130     function description () {
131         extract($this->_args);
132
133         // FIXME: say something about show_all.
134
135         if ($show_major && $show_minor)
136             $edits = _("edits");
137         elseif ($show_major)
138             $edits = _("major edits");
139         else
140             $edits = _("minor edits");
141             
142         if ($limit > 0) {
143             if ($days > 0)
144                 $desc = sprintf(_("The %d most recent %s during the past %.1f days are listed below."),
145                                $limit, $edits, $days);
146             else
147                 $desc = sprintf(_("The %d most recent %s are listed below."),
148                                $limit, $edits);
149         }
150         else {
151             if ($days > 0)
152                 $desc = sprintf(_("The most recent %s during the past %.1f days are listed below."),
153                                $edits, $days);
154             else
155                 $desc = sprintf(_("All %s are listed below."), $edits);
156         }
157         return htmlspecialchars($desc);
158     }
159
160         
161     function title () {
162         extract($this->_args);
163         return htmlspecialchars( $show_minor ? _("RecentEdits") : _("RecentChanges") ) . "\n" . $this->rss_icon();
164     }
165
166     function format ($changes) {
167         $html[] = Element('h2', $this->title());
168         if (($desc = $this->description()))
169             $html[] = Element('p', $desc);
170         
171         $last_date = '';
172         $lines = array();
173         
174         while ($rev = $changes->next()) {
175             if (($date = $this->date($rev)) != $last_date) {
176                 if ($lines) {
177                     $html[] = Element('ul', join("\n", $lines));
178                     $lines = array();
179                 }
180                 $html[] = QElement('h3', $date);
181                 $last_date = $date;
182             }
183             
184             $lines[] = $this->format_revision($rev);
185         }
186         if ($lines)
187             $html[] = Element('ul', join("\n", $lines));
188         return join("\n", $html) . "\n";
189     }
190     
191     function format_revision ($rev) {
192         if (!defined('RC_SEPARATOR_A')) define('RC_SEPARATOR_A', '');
193         if (!defined('RC_SEPARATOR_B')) define('RC_SEPARATOR_B', '...');
194
195         if ( ($summary = $this->summary($rev)) ) {
196             $summary = do_transform($summary, 'LinkTransform');
197             $summary = Element('strong', array('class' => 'wiki-summary'), "[$summary]");
198         }
199         
200         $class = 'rc-' . $this->importance($rev);
201         
202         return Element('li', array('class' => $class),
203                        implode(' ', array( $this->diffLink($rev),
204                                            $this->pageLink($rev),
205                                            $this->time($rev),
206                                            RC_SEPARATOR_A,
207                                            $summary,
208                                            RC_SEPARATOR_B,
209                                            $this->authorLink($rev) )));
210     }
211 }
212
213
214
215 class _RecentChanges_RssFormatter
216 extends _RecentChanges_Formatter
217 {
218     var $_absurls = true;
219
220     function time ($rev) {
221         return Iso8601DateTime($rev->get('mtime'));
222     }
223
224     function pageURI ($rev) {
225         $page = $rev->getPage();
226         return WikiURL($page->getName(),
227                        array('version' => $rev->getVersion()),
228                        'absurl');
229     }
230     
231     function format ($changes) {
232         include_once('lib/RssWriter.php');
233         $rss = new RssWriter;
234
235         
236         $rss->channel($this->channel_properties());
237
238         if (($props = $this->image_properties()))
239             $rss->image($props);
240         if (($props = $this->textinput_properties()))
241             $rss->textinput($props);
242
243         while ($rev = $changes->next()) {
244             $rss->addItem($this->item_properties($rev),
245                           $this->pageURI($rev));
246         }
247
248         $rss->finish();
249         printf("\n<!-- Generated by PhpWiki:\n%s-->\n", $GLOBALS['RCS_IDS']);
250         ExitWiki();             // NORETURN!!!!
251     }
252     
253     function image_properties () {
254         return array('title' => WIKI_NAME,
255                      'link' => WikiURL(_("HomePage"), false, 'absurl'),
256                      'url' => DataURL($GLOBALS['logo']));
257     }
258
259     function textinput_properties () {
260         return array('title' => _("Search"),
261                      'description' => _("Title Search"),
262                      'name' => 's',
263                      'link' => WikiURL(_("TitleSearch"), false, 'absurl'));
264     }
265     
266     function channel_properties () {
267         global $request;
268
269         $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
270
271         return array('title' => WIKI_NAME,
272                      'dc:description' => _("RecentChanges"),
273                      'link' => $rc_url,
274                      'dc:date' => Iso8601DateTime(time()));
275
276         /* FIXME: other things one might like in <channel>:                   
277          * sy:updateFrequency
278          * sy:updatePeriod
279          * sy:updateBase
280          * dc:subject
281          * dc:publisher
282          * dc:language
283          * dc:rights
284          * rss091:language
285          * rss091:managingEditor
286          * rss091:webmaster
287          * rss091:lastBuildDate
288          * rss091:copyright
289          */
290     }
291     
292
293     
294         
295     function item_properties ($rev) {
296         $page = $rev->getPage();
297         $pagename = $page->getName();
298         
299         return array( 'title'           => split_pagename($pagename),
300                       'description'     => $this->summary($rev),
301                       'link'            => $this->pageURL($rev),
302                       'dc:date'         => $this->time($rev),
303                       'dc:contributor'  => $rev->get('author'),
304                       'wiki:version'    => $rev->getVersion(),
305                       'wiki:importance' => $this->importance($rev),
306                       'wiki:status'     => $this->status($rev),
307                       'wiki:diff'       => $this->diffURL($rev),
308                       'wiki:history'    => $this->historyURL($rev)
309                       );
310     }
311 }
312
313 class WikiPlugin_RecentChanges
314 extends WikiPlugin
315 {
316     function getName () {
317         return _("RecentChanges");
318     }
319
320     function getDefaultArguments() {
321         return array('days'             => 2,
322                      'show_minor'       => false,
323                      'show_major'       => true,
324                      'show_all'         => false,
325                      'limit'            => false,
326                      'format'           => false);
327     }
328
329     function getArgs ($argstr, $request, $defaults = false) {
330         $args = WikiPlugin::getArgs($argstr, $request, $defaults);
331
332         if ($request->getArg('action') != 'browse')
333             $args['format'] = false; // default -> HTML
334         
335         if ($args['format'] == 'rss' && empty($args['limit']))
336             $args['limit'] = 15; // Fix default value for RSS.
337
338         return $args;
339     }
340         
341     function getMostRecentParams ($args) {
342         extract($args);
343
344         $params = array('include_minor_revisions' => $show_minor,
345                         'exclude_major_revisions' => !$show_major,
346                         'include_all_revisions' => !empty($show_all));
347
348         if ($limit > 0)
349             $params['limit'] = $limit;
350
351         if ($days > 0.0)
352             $params['since'] = time() - 24 * 3600 * $days;
353
354         return $params;
355     }
356     
357     function getChanges ($dbi, $args) {
358         return $dbi->mostRecent($this->getMostRecentParams($args));
359     }
360
361     function format ($changes, $args) {
362         if ($args['format'] == 'rss')
363             $fmt = new _RecentChanges_RssFormatter($args);
364         else
365             $fmt = new _RecentChanges_HtmlFormatter($args);
366         return $fmt->format($changes);
367     }
368     
369         
370     function run ($dbi, $argstr, $request) {
371         $args = $this->getArgs($argstr, $request);
372         // Hack alert: format() is a NORETURN for rss formatters.
373         return $this->format($this->getChanges($dbi, $args), $args);
374     }
375 };
376
377
378 // (c-file-style: "gnu")
379 // Local Variables:
380 // mode: php
381 // tab-width: 8
382 // c-basic-offset: 4
383 // c-hanging-comment-ender-p: nil
384 // indent-tabs-mode: nil
385 // End:   
386 ?>