]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RecentChanges.php
authorLink uses $Theme->linkExistingWikiWord instead of WikiURL.
[SourceForge/phpwiki.git] / lib / plugin / RecentChanges.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RecentChanges.php,v 1.32 2002-01-25 08:36:09 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         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 true;
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             global $Theme;
113             return $Theme->LinkExistingWikiWord($author);
114         } else
115             return $author;
116     }
117
118     function summaryAsHTML ($rev) {
119         if ( !($summary = $this->summary($rev)) )
120             return '';
121         return  HTML::strong( array('class' => 'wiki-summary'),
122                               "[",
123                               do_transform($summary, 'LinkTransform'),
124                               "]");
125     }
126         
127     function rss_icon () {
128         global $request, $Theme;
129
130         $rss_url = $request->getURLtoSelf(array('format' => 'rss'));
131         return $Theme->makeButton("RSS", $rss_url, 'rssicion');
132     }
133     
134     function description () {
135         extract($this->_args);
136
137         // FIXME: say something about show_all.
138
139         if ($show_major && $show_minor)
140             $edits = _("edits");
141         elseif ($show_major)
142             $edits = _("major edits");
143         else
144             $edits = _("minor edits");
145             
146         if ($limit > 0) {
147             if ($days > 0)
148                 $desc = fmt("The %d most recent %s during the past %.1f days are listed below.",
149                             $limit, $edits, $days);
150             else
151                 $desc = fmt("The %d most recent %s are listed below.",
152                             $limit, $edits);
153         }
154         else {
155             if ($days > 0)
156                 $desc = fmt("The most recent %s during the past %.1f days are listed below.",
157                             $edits, $days);
158             else
159                 $desc = fmt("All %s are listed below.", $edits);
160         }
161         return $desc;
162     }
163
164         
165     function title () {
166         extract($this->_args);
167         return array($show_minor ? _("RecentEdits") : _("RecentChanges"),
168                      ' ',
169                      $this->rss_icon());
170     }
171
172     function format ($changes) {
173         $html[] = HTML::h2(false, $this->title());
174         if (($desc = $this->description()))
175             $html[] = HTML::p(false, $desc);
176         
177         $last_date = '';
178         $lines = false;
179         
180         while ($rev = $changes->next()) {
181             if (($date = $this->date($rev)) != $last_date) {
182                 if ($lines)
183                     $html[] = $lines;
184                 $html[] = HTML::h3($date);
185                 $lines = HTML::ul();
186                 $last_date = $date;
187             }
188             $lines->pushContent($this->format_revision($rev));
189         }
190         if ($lines)
191             $html[] = $lines;
192         return $html;
193     }
194
195     function format_revision ($rev) {
196         $class = 'rc-' . $this->importance($rev);
197         
198         return HTML::li(array('class' => $class),
199                         $this->diffLink($rev), ' ',
200                         $this->pageLink($rev), ' ',
201                         $this->time($rev), ' ',
202                         $this->summaryAsHTML($rev),
203                         ' ... ',
204                         $this->authorLink($rev));
205     }
206 }
207
208
209 class _RecentChanges_RssFormatter
210 extends _RecentChanges_Formatter
211 {
212     var $_absurls = true;
213
214     function time ($rev) {
215         return Iso8601DateTime($rev->get('mtime'));
216     }
217
218     function pageURI ($rev) {
219         $page = $rev->getPage();
220         return WikiURL($page->getName(),
221                        array('version' => $rev->getVersion()),
222                        'absurl');
223     }
224     
225     function format ($changes) {
226         include_once('lib/RssWriter.php');
227         $rss = new RssWriter;
228
229         
230         $rss->channel($this->channel_properties());
231
232         if (($props = $this->image_properties()))
233             $rss->image($props);
234         if (($props = $this->textinput_properties()))
235             $rss->textinput($props);
236
237         while ($rev = $changes->next()) {
238             $rss->addItem($this->item_properties($rev),
239                           $this->pageURI($rev));
240         }
241
242         $rss->finish();
243         printf("\n<!-- Generated by PhpWiki:\n%s-->\n", $GLOBALS['RCS_IDS']);
244
245         global $request;        // FIXME
246         $request->finish();     // 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                      'daylist'          => false,
330                      'caption'          => ''
331                      );
332     }
333
334     function getArgs ($argstr, $request, $defaults = false) {
335         $args = WikiPlugin::getArgs($argstr, $request, $defaults);
336
337         if ($request->getArg('action') != 'browse')
338             $args['format'] = false; // default -> HTML
339         
340         if ($args['format'] == 'rss' && empty($args['limit']))
341             $args['limit'] = 15; // Fix default value for RSS.
342
343         return $args;
344     }
345         
346     function getMostRecentParams ($args) {
347         extract($args);
348
349         $params = array('include_minor_revisions' => $show_minor,
350                         'exclude_major_revisions' => !$show_major,
351                         'include_all_revisions' => !empty($show_all));
352
353         if ($limit > 0)
354             $params['limit'] = $limit;
355
356         if ($days > 0.0)
357             $params['since'] = time() - 24 * 3600 * $days;
358
359         return $params;
360     }
361     
362     function getChanges ($dbi, $args) {
363         return $dbi->mostRecent($this->getMostRecentParams($args));
364     }
365
366     function format ($changes, $args) {
367         global $Theme;
368         $format = $args['format'];
369         
370         $fmt_class = $Theme->getFormatter('RecentChanges', $format);
371         if (!$fmt_class) {
372             if ($format == 'rss')
373                 $fmt_class = '_RecentChanges_RssFormatter';
374             elseif ($format == 'rss091') {
375                 include_once "lib/RSSWriter091.php";
376                 $fmt_class = '_RecentChanges_RssFormatter091';
377             }
378             else
379                 $fmt_class = '_RecentChanges_HtmlFormatter';
380         }
381         
382         $fmt = new $fmt_class($args);
383         return $fmt->format($changes);
384     }
385
386     function run ($dbi, $argstr, $request) {
387         $args = $this->getArgs($argstr, $request);
388         
389         if (! $args['daylist']) {
390             // Display RecentChanges
391             //
392             // Hack alert: format() is a NORETURN for rss formatters.
393             return $this->format($this->getChanges($dbi, $args), $args);
394         } else {
395             // Display days selection buttons
396             extract($args);
397
398             $daysarray = explode(",", $daylist);
399
400             // Defaults
401             $url_show_minor = "";
402             $url_show_all = "";
403
404             // RecentEdits args
405             if (($show_minor == 1)||($show_minor == true))
406                 $url_show_minor = "&show_minor=1";
407             if (($show_all == 1)||($show_all == true))
408                 $url_show_all = "&show_all=1";
409             // Custom caption
410             if (! $caption) {
411                 if ($url_show_minor)
412                     $caption = _("Show changes for:");
413                 else {
414                     if ($url_show_all)
415                         $caption = _("Show all changes for:");
416                     else
417                         $caption = _("Show minor edits for:");
418                 }
419             }
420
421             $b = new buttonSet();
422             $b->caption = $caption;
423
424             foreach ($daysarray as $daynum) {
425
426                 if ($daynum == 1)
427                     $label = _("1 day");
428                 elseif ($daynum < 1)
429                     $label = "..."; //alldays
430                 else
431                     $label = sprintf(_("%s days"), $daynum);
432
433                 // Build the button's url
434                 $b->addButton($label, "RecentChanges?days=" .$daynum
435                                       .$url_show_minor .$url_show_all,
436                               'wikiaction');
437             }
438             return $b->getContent();
439         }
440     }
441 };
442
443
444 class buttonSet {
445     function buttonSet() {
446         $this->caption = "";
447         $this->content = "";
448         $this->_b = array();
449     }
450
451     function addButton($label, $url, $action) {
452         global $Theme;
453         $this->_b[] = $Theme->makeButton($label, $url, $action);
454     }
455
456     function getContent() {
457         if (empty($this->content))
458             $this->_generateContent();
459         return $this->content;
460     }
461
462     function _generateContent() {
463         $this->content = HTML::p($this->caption . " ");
464         // Avoid an extraneous ButtonSeparator
465         $this->content->pushContent(array_shift($this->_b));
466
467         global $Theme;
468         foreach ($this->_b as $button) {
469             $this->content->pushContent($Theme->getButtonSeparator());
470             $this->content->pushContent($button);
471         }
472     }
473
474 };
475
476
477 // (c-file-style: "gnu")
478 // Local Variables:
479 // mode: php
480 // tab-width: 8
481 // c-basic-offset: 4
482 // c-hanging-comment-ender-p: nil
483 // indent-tabs-mode: nil
484 // End:   
485 ?>