]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RecentChanges.php
More infiltration of new object-based HTML generation.
[SourceForge/phpwiki.git] / lib / plugin / RecentChanges.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RecentChanges.php,v 1.26 2002-01-21 06:55:47 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, $dbi;
59
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         ExitWiki();             // NORETURN!!!!
244     }
245     
246     function image_properties () {
247         global $Theme;
248
249         $img_url = $Theme->getImageURL('logo');
250         if (!$img_url)
251             return false;
252         
253         return array('title' => WIKI_NAME,
254                      'link' => WikiURL(_("HomePage"), false, 'absurl'),
255                      'url' => $img_url);
256     }
257
258     function textinput_properties () {
259         return array('title' => _("Search"),
260                      'description' => _("Title Search"),
261                      'name' => 's',
262                      'link' => WikiURL(_("TitleSearch"), false, 'absurl'));
263     }
264     
265     function channel_properties () {
266         global $request;
267
268         $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
269
270         return array('title' => WIKI_NAME,
271                      'dc:description' => _("RecentChanges"),
272                      'link' => $rc_url,
273                      'dc:date' => Iso8601DateTime(time()));
274
275         /* FIXME: other things one might like in <channel>:                   
276          * sy:updateFrequency
277          * sy:updatePeriod
278          * sy:updateBase
279          * dc:subject
280          * dc:publisher
281          * dc:language
282          * dc:rights
283          * rss091:language
284          * rss091:managingEditor
285          * rss091:webmaster
286          * rss091:lastBuildDate
287          * rss091:copyright
288          */
289     }
290     
291
292     
293         
294     function item_properties ($rev) {
295         $page = $rev->getPage();
296         $pagename = $page->getName();
297         
298         return array( 'title'           => split_pagename($pagename),
299                       'description'     => $this->summary($rev),
300                       'link'            => $this->pageURL($rev),
301                       'dc:date'         => $this->time($rev),
302                       'dc:contributor'  => $rev->get('author'),
303                       'wiki:version'    => $rev->getVersion(),
304                       'wiki:importance' => $this->importance($rev),
305                       'wiki:status'     => $this->status($rev),
306                       'wiki:diff'       => $this->diffURL($rev),
307                       'wiki:history'    => $this->historyURL($rev)
308                       );
309     }
310 }
311
312 class WikiPlugin_RecentChanges
313 extends WikiPlugin
314 {
315     function getName () {
316         return _("RecentChanges");
317     }
318
319     function getDefaultArguments() {
320         return array('days'             => 2,
321                      'show_minor'       => false,
322                      'show_major'       => true,
323                      'show_all'         => false,
324                      'limit'            => false,
325                      'format'           => false);
326     }
327
328     function getArgs ($argstr, $request, $defaults = false) {
329         $args = WikiPlugin::getArgs($argstr, $request, $defaults);
330
331         if ($request->getArg('action') != 'browse')
332             $args['format'] = false; // default -> HTML
333         
334         if ($args['format'] == 'rss' && empty($args['limit']))
335             $args['limit'] = 15; // Fix default value for RSS.
336
337         return $args;
338     }
339         
340     function getMostRecentParams ($args) {
341         extract($args);
342
343         $params = array('include_minor_revisions' => $show_minor,
344                         'exclude_major_revisions' => !$show_major,
345                         'include_all_revisions' => !empty($show_all));
346
347         if ($limit > 0)
348             $params['limit'] = $limit;
349
350         if ($days > 0.0)
351             $params['since'] = time() - 24 * 3600 * $days;
352
353         return $params;
354     }
355     
356     function getChanges ($dbi, $args) {
357         return $dbi->mostRecent($this->getMostRecentParams($args));
358     }
359
360     function format ($changes, $args) {
361         global $Theme;
362         $format = $args['format'];
363         
364         $fmt_class = $Theme->getFormatter('RecentChanges', $format);
365         if (!$fmt_class) {
366             if ($format == 'rss')
367                 $fmt_class = '_RecentChanges_RssFormatter';
368             elseif ($format == 'rss091')
369                                                 {
370                                                     include_once "lib/RSSWriter091.php";
371                                                                 $fmt_class = '_RecentChanges_RssFormatter091';
372                                           }
373                                                 else
374                 $fmt_class = '_RecentChanges_HtmlFormatter';
375         }
376         
377         $fmt = new $fmt_class($args);
378         return $fmt->format($changes);
379     }
380     
381         
382     function run ($dbi, $argstr, $request) {
383         $args = $this->getArgs($argstr, $request);
384         // Hack alert: format() is a NORETURN for rss formatters.
385         return $this->format($this->getChanges($dbi, $args), $args);
386     }
387 };
388
389
390 // (c-file-style: "gnu")
391 // Local Variables:
392 // mode: php
393 // tab-width: 8
394 // c-basic-offset: 4
395 // c-hanging-comment-ender-p: nil
396 // indent-tabs-mode: nil
397 // End:   
398 ?>