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