]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RecentChanges.php
Yet another new css class, wiki-rc-action (right now only used by phpwiki-printer...
[SourceForge/phpwiki.git] / lib / plugin / RecentChanges.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RecentChanges.php,v 1.35 2002-01-26 07:06:44 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 authorHasPage ($author) {
58         global $WikiNameRegexp, $request;
59         $dbi = $request->getDbh();
60         return preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author);
61     }
62
63     function authorURL ($author) {
64         return $this->authorHasPage() ? WikiURL($author) : false;
65     }
66
67
68     function status ($rev) {
69         if ($rev->hasDefaultContents())
70             return 'deleted';
71         $page = $rev->getPage();
72         $prev = $page->getRevisionBefore($rev->getVersion());
73         if ($prev->hasDefaultContents())
74             return 'new';
75         return 'updated';
76     }
77
78     function importance ($rev) {
79         return $rev->get('is_minor_edit') ? 'minor' : 'major';
80     }
81     
82     function summary($rev) {
83         if ( ($summary = $rev->get('summary')) )
84             return $summary;
85
86         switch ($this->status($rev)) {
87         case 'deleted':
88             return _("Deleted.");
89         case 'new':
90             return _("New page.");
91         default:
92             return '';
93         }
94     }
95 }
96
97 class _RecentChanges_HtmlFormatter
98 extends _RecentChanges_Formatter
99 {
100     function diffLink ($rev) {
101         global $Theme;
102         return $Theme->makeButton(_("(diff)"), $this->diffURL($rev), 'wiki-rc-action');
103     }
104
105     function pageLink ($rev) {
106         $page = $rev->getPage();
107         global $Theme;
108         if ($this->include_versions_in_URLs())
109             return $Theme->LinkExistingWikiWord($page->getName(), false, $rev->getVersion());
110         else
111             return $Theme->LinkExistingWikiWord($page->getName());
112     }
113     
114     function authorLink ($rev) {
115         $author = $rev->get('author');
116         if ( $this->authorHasPage($author) ) {
117             global $Theme;
118             return $Theme->LinkExistingWikiWord($author);
119         } else
120             return $author;
121     }
122
123     function summaryAsHTML ($rev) {
124         if ( !($summary = $this->summary($rev)) )
125             return '';
126         return  HTML::strong( array('class' => 'wiki-summary'),
127                               "[",
128                               do_transform($summary, 'LinkTransform'),
129                               "]");
130     }
131         
132     function rss_icon () {
133         global $request, $Theme;
134
135         $rss_url = $request->getURLtoSelf(array('format' => 'rss'));
136         return $Theme->makeButton("RSS", $rss_url, 'rssicion');
137     }
138     
139     function description () {
140         extract($this->_args);
141
142         // FIXME: say something about show_all.
143
144         if ($show_major && $show_minor)
145             $edits = _("edits");
146         elseif ($show_major)
147             $edits = _("major edits");
148         else
149             $edits = _("minor edits");
150             
151         if ($limit > 0) {
152             if ($days > 0)
153                 $desc = fmt("The %d most recent %s during the past %.1f days are listed below.",
154                             $limit, $edits, $days);
155             else
156                 $desc = fmt("The %d most recent %s are listed below.",
157                             $limit, $edits);
158         }
159         else {
160             if ($days > 0)
161                 $desc = fmt("The most recent %s during the past %.1f days are listed below.",
162                             $edits, $days);
163             else
164                 $desc = fmt("All %s are listed below.", $edits);
165         }
166         return $desc;
167     }
168
169         
170     function title () {
171         extract($this->_args);
172         return array($show_minor ? _("RecentEdits") : _("RecentChanges"),
173                      ' ',
174                      $this->rss_icon());
175     }
176
177     function format ($changes) {
178         $html[] = HTML::h2(false, $this->title());
179         if (($desc = $this->description()))
180             $html[] = HTML::p(false, $desc);
181         
182         $last_date = '';
183         $lines = false;
184         
185         while ($rev = $changes->next()) {
186             if (($date = $this->date($rev)) != $last_date) {
187                 if ($lines)
188                     $html[] = $lines;
189                 $html[] = HTML::h3($date);
190                 $lines = HTML::ul();
191                 $last_date = $date;
192             }
193             $lines->pushContent($this->format_revision($rev));
194         }
195         if ($lines)
196             $html[] = $lines;
197         return $html;
198     }
199
200     function format_revision ($rev) {
201         $class = 'rc-' . $this->importance($rev);
202         
203         return HTML::li(array('class' => $class),
204                         $this->diffLink($rev), ' ',
205                         $this->pageLink($rev), ' ',
206                         $this->time($rev), ' ',
207                         $this->summaryAsHTML($rev),
208                         ' ... ',
209                         $this->authorLink($rev));
210     }
211 }
212
213
214 class _RecentChanges_RssFormatter
215 extends _RecentChanges_Formatter
216 {
217     var $_absurls = true;
218
219     function time ($rev) {
220         return Iso8601DateTime($rev->get('mtime'));
221     }
222
223     function pageURI ($rev) {
224         $page = $rev->getPage();
225         return WikiURL($page->getName(),
226                        array('version' => $rev->getVersion()),
227                        'absurl');
228     }
229     
230     function format ($changes) {
231         include_once('lib/RssWriter.php');
232         $rss = new RssWriter;
233
234         
235         $rss->channel($this->channel_properties());
236
237         if (($props = $this->image_properties()))
238             $rss->image($props);
239         if (($props = $this->textinput_properties()))
240             $rss->textinput($props);
241
242         while ($rev = $changes->next()) {
243             $rss->addItem($this->item_properties($rev),
244                           $this->pageURI($rev));
245         }
246
247         $rss->finish();
248         printf("\n<!-- Generated by PhpWiki:\n%s-->\n", $GLOBALS['RCS_IDS']);
249
250         global $request;        // FIXME
251         $request->finish();     // NORETURN!!!!
252     }
253     
254     function image_properties () {
255         global $Theme;
256
257         $img_url = $Theme->getImageURL('logo');
258         if (!$img_url)
259             return false;
260         
261         return array('title' => WIKI_NAME,
262                      'link' => WikiURL(_("HomePage"), false, 'absurl'),
263                      'url' => $img_url);
264     }
265
266     function textinput_properties () {
267         return array('title' => _("Search"),
268                      'description' => _("Title Search"),
269                      'name' => 's',
270                      'link' => WikiURL(_("TitleSearch"), false, 'absurl'));
271     }
272     
273     function channel_properties () {
274         global $request;
275
276         $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
277
278         return array('title' => WIKI_NAME,
279                      'dc:description' => _("RecentChanges"),
280                      'link' => $rc_url,
281                      'dc:date' => Iso8601DateTime(time()));
282
283         /* FIXME: other things one might like in <channel>:                   
284          * sy:updateFrequency
285          * sy:updatePeriod
286          * sy:updateBase
287          * dc:subject
288          * dc:publisher
289          * dc:language
290          * dc:rights
291          * rss091:language
292          * rss091:managingEditor
293          * rss091:webmaster
294          * rss091:lastBuildDate
295          * rss091:copyright
296          */
297     }
298     
299
300     
301         
302     function item_properties ($rev) {
303         $page = $rev->getPage();
304         $pagename = $page->getName();
305         
306         return array( 'title'           => split_pagename($pagename),
307                       'description'     => $this->summary($rev),
308                       'link'            => $this->pageURL($rev),
309                       'dc:date'         => $this->time($rev),
310                       'dc:contributor'  => $rev->get('author'),
311                       'wiki:version'    => $rev->getVersion(),
312                       'wiki:importance' => $this->importance($rev),
313                       'wiki:status'     => $this->status($rev),
314                       'wiki:diff'       => $this->diffURL($rev),
315                       'wiki:history'    => $this->historyURL($rev)
316                       );
317     }
318 }
319
320 class WikiPlugin_RecentChanges
321 extends WikiPlugin
322 {
323     function getName () {
324         return _("RecentChanges");
325     }
326
327     function getDefaultArguments() {
328         return array('days'             => 2,
329                      'show_minor'       => false,
330                      'show_major'       => true,
331                      'show_all'         => false,
332                      'limit'            => false,
333                      'format'           => false,
334                      'daylist'          => false,
335                      'caption'          => ''
336                      );
337     }
338
339     function getArgs ($argstr, $request, $defaults = false) {
340         $args = WikiPlugin::getArgs($argstr, $request, $defaults);
341
342         if ($request->getArg('action') != 'browse')
343             $args['format'] = false; // default -> HTML
344         
345         if ($args['format'] == 'rss' && empty($args['limit']))
346             $args['limit'] = 15; // Fix default value for RSS.
347
348         return $args;
349     }
350         
351     function getMostRecentParams ($args) {
352         extract($args);
353
354         $params = array('include_minor_revisions' => $show_minor,
355                         'exclude_major_revisions' => !$show_major,
356                         'include_all_revisions' => !empty($show_all));
357
358         if ($limit > 0)
359             $params['limit'] = $limit;
360
361         if ($days > 0.0)
362             $params['since'] = time() - 24 * 3600 * $days;
363
364         return $params;
365     }
366     
367     function getChanges ($dbi, $args) {
368         return $dbi->mostRecent($this->getMostRecentParams($args));
369     }
370
371     function format ($changes, $args) {
372         global $Theme;
373         $format = $args['format'];
374         
375         $fmt_class = $Theme->getFormatter('RecentChanges', $format);
376         if (!$fmt_class) {
377             if ($format == 'rss')
378                 $fmt_class = '_RecentChanges_RssFormatter';
379             elseif ($format == 'rss091') {
380                 include_once "lib/RSSWriter091.php";
381                 $fmt_class = '_RecentChanges_RssFormatter091';
382             }
383             else
384                 $fmt_class = '_RecentChanges_HtmlFormatter';
385         }
386         
387         $fmt = new $fmt_class($args);
388         return $fmt->format($changes);
389     }
390
391     function run ($dbi, $argstr, $request) {
392         $args = $this->getArgs($argstr, $request);
393         
394         if (! $args['daylist']) {
395             // Display RecentChanges
396             //
397             // Hack alert: format() is a NORETURN for rss formatters.
398             return $this->format($this->getChanges($dbi, $args), $args);
399         } else {
400             // Display days selection buttons
401             extract($args);
402
403             $daysarray = explode(",", $daylist);
404
405             // Defaults
406             $url_show_minor = "";
407             $url_show_all = "";
408
409             // RecentEdits args
410             if (($show_minor == 1)||($show_minor == true))
411                 $url_show_minor = "&show_minor=1";
412             if (($show_all == 1)||($show_all == true))
413                 $url_show_all = "&show_all=1";
414             // Custom caption
415             if (! $caption) {
416                 if ($url_show_minor)
417                     $caption = _("Show changes for:");
418                 else {
419                     if ($url_show_all)
420                         $caption = _("Show all changes for:");
421                     else
422                         $caption = _("Show minor edits for:");
423                 }
424             }
425
426             $b = new buttonSet();
427             $b->caption = $caption;
428
429             foreach ($daysarray as $daynum) {
430
431                 if ($daynum == 1)
432                     $label = _("1 day");
433                 elseif ($daynum < 1)
434                     $label = "..."; //alldays
435                 else
436                     $label = sprintf(_("%s days"), $daynum);
437
438                 // Build the button's url
439                 $b->addButton($label, "RecentChanges?days=" .$daynum
440                                       .$url_show_minor .$url_show_all,
441                               'wiki-rc-action');
442             }
443             return HTML::div(array('class'=>'wiki-rc-action'), $b->getContent());
444         }
445     }
446 };
447
448
449 class buttonSet {
450     function buttonSet() {
451         $this->caption = "";
452         $this->content = "";
453         $this->_b = array();
454     }
455
456     function addButton($label, $url, $action) {
457         global $Theme;
458         $this->_b[] = $Theme->makeButton($label, $url, $action);
459     }
460
461     function getContent() {
462         if (empty($this->content))
463             $this->_generateContent();
464         return $this->content;
465     }
466
467     function _generateContent() {
468         $this->content = HTML::p($this->caption . " ");
469         // Avoid an extraneous ButtonSeparator
470         $this->content->pushContent(array_shift($this->_b));
471
472         global $Theme;
473         foreach ($this->_b as $button) {
474             $this->content->pushContent($Theme->getButtonSeparator());
475             $this->content->pushContent($button);
476         }
477     }
478
479 };
480
481
482 // (c-file-style: "gnu")
483 // Local Variables:
484 // mode: php
485 // tab-width: 8
486 // c-basic-offset: 4
487 // c-hanging-comment-ender-p: nil
488 // indent-tabs-mode: nil
489 // End:   
490 ?>