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