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