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