]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RecentChanges.php
Eliminate DataURL() in favor of Theme::getImageURL().
[SourceForge/phpwiki.git] / lib / plugin / RecentChanges.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RecentChanges.php,v 1.23 2002-01-17 22:52:03 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         return QElement('a', array('href' => $this->diffURL($rev), 'class' => 'wikiaction'),
100                         _("(diff)"));
101     }
102
103     function pageLink ($rev) {
104         $page = $rev->getPage();
105         return QElement('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 QElement('a', array('href' => $url, 'class' => 'wiki'), $author);
113         else
114             return htmlspecialchars($author);
115     }
116
117
118     function rss_icon () {
119         global $request, $Theme;
120
121         $rss_url = $request->getURLtoSelf(array('format' => 'rss'));
122         $button = $Theme->makeButton("RSS", $rss_url, 'rssicion');
123         return $button->asHTML();
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 (!defined('RC_SEPARATOR_A')) define('RC_SEPARATOR_A', '');
189         if (!defined('RC_SEPARATOR_B')) define('RC_SEPARATOR_B', '...');
190
191         if ( ($summary = $this->summary($rev)) ) {
192             $summary = do_transform($summary, 'LinkTransform');
193             $summary = Element('strong', array('class' => 'wiki-summary'), "[$summary]");
194         }
195         
196         $class = 'rc-' . $this->importance($rev);
197         
198         return Element('li', array('class' => $class),
199                        implode(' ', array( $this->diffLink($rev),
200                                            $this->pageLink($rev),
201                                            $this->time($rev),
202                                            RC_SEPARATOR_A,
203                                            $summary,
204                                            RC_SEPARATOR_B,
205                                            $this->authorLink($rev) )));
206     }
207 }
208
209
210
211 class _RecentChanges_RssFormatter
212 extends _RecentChanges_Formatter
213 {
214     var $_absurls = true;
215
216     function time ($rev) {
217         return Iso8601DateTime($rev->get('mtime'));
218     }
219
220     function pageURI ($rev) {
221         $page = $rev->getPage();
222         return WikiURL($page->getName(),
223                        array('version' => $rev->getVersion()),
224                        'absurl');
225     }
226     
227     function format ($changes) {
228         include_once('lib/RssWriter.php');
229         $rss = new RssWriter;
230
231         
232         $rss->channel($this->channel_properties());
233
234         if (($props = $this->image_properties()))
235             $rss->image($props);
236         if (($props = $this->textinput_properties()))
237             $rss->textinput($props);
238
239         while ($rev = $changes->next()) {
240             $rss->addItem($this->item_properties($rev),
241                           $this->pageURI($rev));
242         }
243
244         $rss->finish();
245         printf("\n<!-- Generated by PhpWiki:\n%s-->\n", $GLOBALS['RCS_IDS']);
246         ExitWiki();             // NORETURN!!!!
247     }
248     
249     function image_properties () {
250         global $Theme;
251
252         $img_url = $Theme->getImageURL('logo');
253         if (!$img_url)
254             return false;
255         
256         return array('title' => WIKI_NAME,
257                      'link' => WikiURL(_("HomePage"), false, 'absurl'),
258                      'url' => $img_url);
259     }
260
261     function textinput_properties () {
262         return array('title' => _("Search"),
263                      'description' => _("Title Search"),
264                      'name' => 's',
265                      'link' => WikiURL(_("TitleSearch"), false, 'absurl'));
266     }
267     
268     function channel_properties () {
269         global $request;
270
271         $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
272
273         return array('title' => WIKI_NAME,
274                      'dc:description' => _("RecentChanges"),
275                      'link' => $rc_url,
276                      'dc:date' => Iso8601DateTime(time()));
277
278         /* FIXME: other things one might like in <channel>:                   
279          * sy:updateFrequency
280          * sy:updatePeriod
281          * sy:updateBase
282          * dc:subject
283          * dc:publisher
284          * dc:language
285          * dc:rights
286          * rss091:language
287          * rss091:managingEditor
288          * rss091:webmaster
289          * rss091:lastBuildDate
290          * rss091:copyright
291          */
292     }
293     
294
295     
296         
297     function item_properties ($rev) {
298         $page = $rev->getPage();
299         $pagename = $page->getName();
300         
301         return array( 'title'           => split_pagename($pagename),
302                       'description'     => $this->summary($rev),
303                       'link'            => $this->pageURL($rev),
304                       'dc:date'         => $this->time($rev),
305                       'dc:contributor'  => $rev->get('author'),
306                       'wiki:version'    => $rev->getVersion(),
307                       'wiki:importance' => $this->importance($rev),
308                       'wiki:status'     => $this->status($rev),
309                       'wiki:diff'       => $this->diffURL($rev),
310                       'wiki:history'    => $this->historyURL($rev)
311                       );
312     }
313 }
314
315 class WikiPlugin_RecentChanges
316 extends WikiPlugin
317 {
318     function getName () {
319         return _("RecentChanges");
320     }
321
322     function getDefaultArguments() {
323         return array('days'             => 2,
324                      'show_minor'       => false,
325                      'show_major'       => true,
326                      'show_all'         => false,
327                      'limit'            => false,
328                      'format'           => false);
329     }
330
331     function getArgs ($argstr, $request, $defaults = false) {
332         $args = WikiPlugin::getArgs($argstr, $request, $defaults);
333
334         if ($request->getArg('action') != 'browse')
335             $args['format'] = false; // default -> HTML
336         
337         if ($args['format'] == 'rss' && empty($args['limit']))
338             $args['limit'] = 15; // Fix default value for RSS.
339
340         return $args;
341     }
342         
343     function getMostRecentParams ($args) {
344         extract($args);
345
346         $params = array('include_minor_revisions' => $show_minor,
347                         'exclude_major_revisions' => !$show_major,
348                         'include_all_revisions' => !empty($show_all));
349
350         if ($limit > 0)
351             $params['limit'] = $limit;
352
353         if ($days > 0.0)
354             $params['since'] = time() - 24 * 3600 * $days;
355
356         return $params;
357     }
358     
359     function getChanges ($dbi, $args) {
360         return $dbi->mostRecent($this->getMostRecentParams($args));
361     }
362
363     function format ($changes, $args) {
364         if ($args['format'] == 'rss')
365             $fmt = new _RecentChanges_RssFormatter($args);
366         else
367             $fmt = new _RecentChanges_HtmlFormatter($args);
368         return $fmt->format($changes);
369     }
370     
371         
372     function run ($dbi, $argstr, $request) {
373         $args = $this->getArgs($argstr, $request);
374         // Hack alert: format() is a NORETURN for rss formatters.
375         return $this->format($this->getChanges($dbi, $args), $args);
376     }
377 };
378
379
380 // (c-file-style: "gnu")
381 // Local Variables:
382 // mode: php
383 // tab-width: 8
384 // c-basic-offset: 4
385 // c-hanging-comment-ender-p: nil
386 // indent-tabs-mode: nil
387 // End:   
388 ?>