]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RecentChanges.php
New: configurable "..." separators for RecentChanges and RecentEdits lists. (Inspired...
[SourceForge/phpwiki.git] / lib / plugin / RecentChanges.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RecentChanges.php,v 1.18 2002-01-05 18:33:51 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 (!defined('RC_SEPARATOR_A')) define('RC_SEPARATOR_A', '');
191         if (!defined('RC_SEPARATOR_B')) define('RC_SEPARATOR_B', '...');
192
193         if ( ($summary = $this->summary($rev)) ) {
194             $summary = do_transform($summary, 'LinkTransform');
195             $summary = Element('b', array('class' => 'wiki-summary'), "[$summary]");
196         }
197         
198         $class = 'rc-' . $this->importance($rev);
199         
200         return Element('li', array('class' => $class),
201                        implode(' ', array( $this->diffLink($rev),
202                                            $this->pageLink($rev),
203                                            $this->time($rev),
204                                            RC_SEPARATOR_A,
205                                            $summary,
206                                            RC_SEPARATOR_B,
207                                            $this->authorLink($rev) )));
208     }
209 }
210
211
212
213 class _RecentChanges_RssFormatter
214 extends _RecentChanges_Formatter
215 {
216     var $_absurls = true;
217
218     function time ($rev) {
219         return Iso8601DateTime($rev->get('mtime'));
220     }
221
222     function pageURI ($rev) {
223         $page = $rev->getPage();
224         return WikiURL($page->getName(),
225                        array('version' => $rev->getVersion()),
226                        'absurl');
227     }
228     
229     function format ($changes) {
230         include_once('lib/RssWriter.php');
231         $rss = new RssWriter;
232
233         
234         $rss->channel($this->channel_properties());
235
236         if (($props = $this->image_properties()))
237             $rss->image($props);
238         if (($props = $this->textinput_properties()))
239             $rss->textinput($props);
240
241         while ($rev = $changes->next()) {
242             $rss->addItem($this->item_properties($rev),
243                           $this->pageURI($rev));
244         }
245
246         $rss->finish();
247         printf("\n<!-- Generated by PhpWiki:\n%s-->\n", $GLOBALS['RCS_IDS']);
248         ExitWiki();             // NORETURN!!!!
249     }
250     
251     function image_properties () {
252         return array('title' => WIKI_NAME,
253                      'link' => WikiURL(_("HomePage"), false, 'absurl'),
254                      'url' => DataURL($GLOBALS['logo']));
255     }
256
257     function textinput_properties () {
258         return array('title' => _("Search"),
259                      'description' => _("Title Search"),
260                      'name' => 's',
261                      'link' => WikiURL(_("TitleSearch"), false, 'absurl'));
262     }
263     
264     function channel_properties () {
265         global $request;
266
267         $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
268
269         return array('title' => WIKI_NAME,
270                      'dc:description' => _("RecentChanges"),
271                      'link' => $rc_url,
272                      'dc:date' => Iso8601DateTime(time()));
273
274         /* FIXME: other things one might like in <channel>:                   
275          * sy:updateFrequency
276          * sy:updatePeriod
277          * sy:updateBase
278          * dc:subject
279          * dc:publisher
280          * dc:language
281          * dc:rights
282          * rss091:language
283          * rss091:managingEditor
284          * rss091:webmaster
285          * rss091:lastBuildDate
286          * rss091:copyright
287          */
288     }
289     
290
291     
292         
293     function item_properties ($rev) {
294         $page = $rev->getPage();
295         $pagename = $page->getName();
296         
297         return array( 'title'           => split_pagename($pagename),
298                       'description'     => $this->summary($rev),
299                       'link'            => $this->pageURL($rev),
300                       'dc:date'         => $this->time($rev),
301                       'dc:contributor'  => $rev->get('author'),
302                       'wiki:version'    => $rev->getVersion(),
303                       'wiki:importance' => $this->importance($rev),
304                       'wiki:status'     => $this->status($rev),
305                       'wiki:diff'       => $this->diffURL($rev),
306                       'wiki:history'    => $this->historyURL($rev)
307                       );
308     }
309 }
310
311 class WikiPlugin_RecentChanges
312 extends WikiPlugin
313 {
314     function getName () {
315         return _("RecentChanges");
316     }
317
318     function getDefaultArguments() {
319         return array('days'             => 2,
320                      'show_minor'       => false,
321                      'show_major'       => true,
322                      'show_all'         => false,
323                      'limit'            => false,
324                      'format'           => false);
325     }
326
327     function getArgs ($argstr, $request, $defaults = false) {
328         $args = WikiPlugin::getArgs($argstr, $request, $defaults);
329
330         if ($request->getArg('action') != 'browse')
331             $args['format'] = false; // default -> HTML
332         
333         if ($args['format'] == 'rss' && empty($args['limit']))
334             $args['limit'] = 15; // Fix default value for RSS.
335
336         return $args;
337     }
338         
339     function getMostRecentParams ($args) {
340         extract($args);
341
342         $params = array('include_minor_revisions' => $show_minor,
343                         'exclude_major_revisions' => !$show_major,
344                         'include_all_revisions' => !empty($show_all));
345
346         if ($limit > 0)
347             $params['limit'] = $limit;
348
349         if ($days > 0.0)
350             $params['since'] = time() - 24 * 3600 * $days;
351
352         return $params;
353     }
354     
355     function getChanges ($dbi, $args) {
356         return $dbi->mostRecent($this->getMostRecentParams($args));
357     }
358
359     function format ($changes, $args) {
360         if ($args['format'] == 'rss')
361             $fmt = new _RecentChanges_RssFormatter($args);
362         else
363             $fmt = new _RecentChanges_HtmlFormatter($args);
364         return $fmt->format($changes);
365     }
366     
367         
368     function run ($dbi, $argstr, $request) {
369         $args = $this->getArgs($argstr, $request);
370         // Hack alert: format() is a NORETURN for rss formatters.
371         return $this->format($this->getChanges($dbi, $args), $args);
372     }
373 };
374
375
376 // (c-file-style: "gnu")
377 // Local Variables:
378 // mode: php
379 // tab-width: 8
380 // c-basic-offset: 4
381 // c-hanging-comment-ender-p: nil
382 // indent-tabs-mode: nil
383 // End:   
384 ?>