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