2 rcs_id('$Id: RecentChanges.php,v 1.76 2003-02-27 22:48:44 dairiki Exp $');
4 Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 class _RecentChanges_Formatter
27 var $_absurls = false;
29 function _RecentChanges_Formatter ($rc_args) {
30 $this->_args = $rc_args;
31 $this->_diffargs = array('action' => 'diff');
33 if ($rc_args['show_minor'] || !$rc_args['show_major'])
34 $this->_diffargs['previous'] = 'minor';
36 // PageHistoryPlugin doesn't have a 'daylist' arg.
37 if (!isset($this->_args['daylist']))
38 $this->_args['daylist'] = false;
41 function include_versions_in_URLs() {
42 return (bool) $this->_args['show_all'];
45 function date ($rev) {
47 return $Theme->getDay($rev->get('mtime'));
50 function time ($rev) {
52 return $Theme->formatTime($rev->get('mtime'));
55 function diffURL ($rev) {
56 $args = $this->_diffargs;
57 if ($this->include_versions_in_URLs())
58 $args['version'] = $rev->getVersion();
59 $page = $rev->getPage();
60 return WikiURL($page->getName(), $args, $this->_absurls);
63 function historyURL ($rev) {
64 $page = $rev->getPage();
65 return WikiURL($page, array('action' => _("PageHistory")),
69 function pageURL ($rev) {
70 return WikiURL($this->include_versions_in_URLs() ? $rev : $rev->getPage(),
74 function authorHasPage ($author) {
75 global $WikiNameRegexp, $request;
76 $dbi = $request->getDbh();
77 return preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author);
80 function authorURL ($author) {
81 return $this->authorHasPage() ? WikiURL($author) : false;
85 function status ($rev) {
86 if ($rev->hasDefaultContents())
88 $page = $rev->getPage();
89 $prev = $page->getRevisionBefore($rev->getVersion());
90 if ($prev->hasDefaultContents())
95 function importance ($rev) {
96 return $rev->get('is_minor_edit') ? 'minor' : 'major';
99 function summary($rev) {
100 if ( ($summary = $rev->get('summary')) )
103 switch ($this->status($rev)) {
105 return _("Deleted.");
107 return _("New page.");
113 function setValidators($most_recent_rev) {
114 $rev = $most_recent_rev;
115 $validators = array('RecentChanges-top' =>
116 array($rev->getPageName(), $rev->getVersion()),
117 '%mtime' => $rev->get('mtime'));
119 $request->appendValidators($validators);
123 class _RecentChanges_HtmlFormatter
124 extends _RecentChanges_Formatter
126 function diffLink ($rev) {
128 return $Theme->makeButton(_("(diff)"), $this->diffURL($rev), 'wiki-rc-action');
131 function historyLink ($rev) {
133 return $Theme->makeButton(_("(hist)"), $this->historyURL($rev), 'wiki-rc-action');
136 function pageLink ($rev, $link_text=false) {
137 $page = $rev->getPage();
139 if ($this->include_versions_in_URLs()) {
140 $version = $rev->getVersion();
141 $exists = !$rev->hasDefaultContents();
145 $cur = $page->getCurrentRevision();
146 $exists = !$cur->hasDefaultContents();
149 return $Theme->linkExistingWikiWord($page->getName(), $link_text, $version);
151 return $Theme->linkUnknownWikiWord($page->getName(), $link_text);
154 function authorLink ($rev) {
155 $author = $rev->get('author');
156 if ( $this->authorHasPage($author) ) {
157 return WikiLink($author);
162 function summaryAsHTML ($rev) {
163 if ( !($summary = $this->summary($rev)) )
165 return HTML::strong( array('class' => 'wiki-summary'),
167 TransformLinks($summary, $rev->get('markup'), $rev->getPageName()),
171 function rss_icon () {
172 global $request, $Theme;
174 $rss_url = $request->getURLtoSelf(array('format' => 'rss'));
175 return $Theme->makeButton("RSS", $rss_url, 'rssicon');
178 function description () {
179 extract($this->_args);
180 // FIXME: say something about show_all.
181 if ($show_major && $show_minor)
184 $edits = _("major edits");
186 $edits = _("minor edits");
188 if ($timespan = $days > 0) {
189 if (intval($days) != $days)
190 $days = sprintf("%.1f", $days);
194 * Depending how this text is split up it can be tricky or
195 * impossible to translate with good grammar. So the seperate
196 * strings for 1 day and %s days are necessary in this case
197 * for translating to multiple languages, due to differing
198 * overlapping ideal word cutting points.
200 * en: day/days "The %d most recent %s [during (the past] day) are listed below."
201 * de: 1 Tag "Die %d jüngste %s [innerhalb (von des letzten] Tages) sind unten aufgelistet."
202 * de: %s days "Die %d jüngste %s [innerhalb (von] %s Tagen) sind unten aufgelistet."
204 * en: day/days "The %d most recent %s during [the past] (day) are listed below."
205 * fr: 1 jour "Les %d %s les plus récentes pendant [le dernier (d'une] jour) sont énumérées ci-dessous."
206 * fr: %s jours "Les %d %s les plus récentes pendant [les derniers (%s] jours) sont énumérées ci-dessous."
210 if (intval($days) == 1)
211 $desc = fmt("The %d most recent %s during the past day are listed below.",
214 $desc = fmt("The %d most recent %s during the past %s days are listed below.",
215 $limit, $edits, $days);
217 $desc = fmt("The %d most recent %s are listed below.",
220 elseif ($limit < 0) { //$limit < 0 means we want oldest pages
222 if (intval($days) == 1)
223 $desc = fmt("The %d oldest %s during the past day are listed below.",
226 $desc = fmt("The %d oldest %s during the past %s days are listed below.",
227 $lmt, $edits, $days);
229 $desc = fmt("The %d oldest %s are listed below.",
235 if (intval($days) == 1)
236 $desc = fmt("The most recent %s during the past day are listed below.",
239 $desc = fmt("The most recent %s during the past %s days are listed below.",
242 $desc = fmt("All %s are listed below.", $edits);
244 return HTML::p(false, $desc);
249 extract($this->_args);
250 return array($show_minor ? _("RecentEdits") : _("RecentChanges"),
253 $this->sidebar_link());
256 function empty_message () {
257 return _("No changes found");
261 function sidebar_link() {
262 extract($this->_args);
263 $pagetitle = $show_minor ? _("RecentEdits") : _("RecentChanges");
266 $sidebarurl = WikiURL($pagetitle, false, 'absurl') . "?format=sidebar";
269 "function addPanel() {\n"
270 ." window.sidebar.addPanel (\"" . sprintf("%s - %s", WIKI_NAME, $pagetitle) . "\",\n"
271 ." \"$sidebarurl\",\"\");\n"
273 $jsf = JavaScript($addsidebarjsfunc);
275 $addsidebarjsclick = " " . "<small style=\"font-weight:normal;\"><a href=\"javascript:addPanel();\">sidebar</a></small>";
276 $jsc = JavaScript("if ((typeof window.sidebar == 'object') &&\n"
277 ." (typeof window.sidebar.addPanel == 'function'))\n"
279 ." document.write('$addsidebarjsclick');\n"
282 return HTML(new RawXML("\n"), $jsf, new RawXML("\n"), $jsc);
285 function format ($changes) {
286 include_once('lib/InlineParser.php');
288 $html = HTML(HTML::h2(false, $this->title()));
289 if (($desc = $this->description()))
290 $html->pushContent($desc);
292 if ($this->_args['daylist'])
293 $html->pushContent(new DayButtonBar($this->_args));
299 while ($rev = $changes->next()) {
300 if (($date = $this->date($rev)) != $last_date) {
302 $html->pushContent($lines);
303 $html->pushContent(HTML::h3($date));
308 $lines->pushContent($this->format_revision($rev));
311 $this->setValidators($rev);
315 $html->pushContent($lines);
317 $html->pushContent(HTML::p(array('class' => 'rc-empty'),
318 $this->empty_message()));
323 function format_revision ($rev) {
324 $args = &$this->_args;
326 $class = 'rc-' . $this->importance($rev);
328 $time = $this->time($rev);
329 if (! $rev->get('is_minor_edit'))
330 $time = HTML::strong(array('class' => 'pageinfo-majoredit'), $time);
332 $line = HTML::li(array('class' => $class));
335 if ($args['difflinks'])
336 $line->pushContent($this->diffLink($rev), ' ');
338 if ($args['historylinks'])
339 $line->pushContent($this->historyLink($rev), ' ');
341 $line->pushContent($this->pageLink($rev), ' ',
343 $this->summaryAsHTML($rev),
345 $this->authorLink($rev));
351 class _RecentChanges_SideBarFormatter
352 extends _RecentChanges_HtmlFormatter
354 function description () {
357 function rss_icon () {
361 //title click opens the normal RC or RE page in the main browser frame
362 extract($this->_args);
363 $titlelink = WikiLink($show_minor ? _("RecentEdits") : _("RecentChanges"));
364 $titlelink->setAttr('target', '_content');
365 return HTML($this->logo(), $titlelink);
368 //logo click opens the HomePage in the main browser frame
370 $img = HTML::img(array('src' => $Theme->getImageURL('logo'),
375 $linkurl = WikiLink(HOME_PAGE, false, $img);
376 $linkurl->setAttr('target', '_content');
380 function authorLink ($rev) {
381 $author = $rev->get('author');
382 if ( $this->authorHasPage($author) ) {
383 $linkurl = WikiLink($author);
384 $linkurl->setAttr('target', '_content'); // way to do this using parent::authorLink ??
389 function diffLink ($rev) {
390 $linkurl = parent::diffLink($rev);
391 $linkurl->setAttr('target', '_content');
394 function historyLink ($rev) {
395 $linkurl = parent::historyLink($rev);
396 $linkurl->setAttr('target', '_content');
399 function pageLink ($rev) {
400 $linkurl = parent::pageLink($rev);
401 $linkurl->setAttr('target', '_content');
404 // Overriding summaryAsHTML, because there is no way yet to
405 // return summary as transformed text with
406 // links setAttr('target', '_content') in Mozilla sidebar.
407 // So for now don't create clickable links inside summary
408 // in the sidebar, or else they target the sidebar and not the
409 // main content window.
410 function summaryAsHTML ($rev) {
411 if ( !($summary = $this->summary($rev)) )
413 return HTML::strong(array('class' => 'wiki-summary'),
415 /*TransformLinks(*/$summary,/* $rev->get('markup')),*/
420 function format ($changes) {
421 $this->_args['daylist'] = false; //only 1 day for Mozilla sidebar
422 $html = _RecentChanges_HtmlFormatter::format ($changes);
423 $html = HTML::div(array('class' => 'wikitext'), $html);
425 printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n", CHARSET);
426 printf('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"');
427 printf(' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
428 printf('<html xmlns="http://www.w3.org/1999/xhtml">');
431 extract($this->_args);
432 $title = WIKI_NAME . $show_minor ? _("RecentEdits") : _("RecentChanges");
433 printf("<title>" . $title . "</title>\n");
435 $css = $Theme->getCSS();
439 printf("<body class=\"sidebar\">\n");
441 printf("\n</body>\n");
447 $request->finish(); // cut rest of page processing short
452 class _RecentChanges_RssFormatter
453 extends _RecentChanges_Formatter
455 var $_absurls = true;
457 function time ($rev) {
458 return Iso8601DateTime($rev->get('mtime'));
461 function pageURI ($rev) {
462 return WikiURL($rev, '', 'absurl');
465 function format ($changes) {
466 include_once('lib/RssWriter.php');
467 $rss = new RssWriter;
470 $rss->channel($this->channel_properties());
472 if (($props = $this->image_properties()))
474 if (($props = $this->textinput_properties()))
475 $rss->textinput($props);
478 while ($rev = $changes->next()) {
479 $rss->addItem($this->item_properties($rev),
480 $this->pageURI($rev));
482 $this->setValidators($rev);
487 printf("\n<!-- Generated by PhpWiki:\n%s-->\n", $GLOBALS['RCS_IDS']);
489 // Flush errors in comment, otherwise it's invalid XML.
490 global $ErrorManager;
491 if (($errors = $ErrorManager->getPostponedErrorsAsHTML()))
492 printf("\n<!-- PHP Warnings:\n%s-->\n", AsXML($errors));
494 global $request; // FIXME
495 $request->finish(); // NORETURN!!!!
498 function image_properties () {
501 $img_url = SERVER_URL . $Theme->getImageURL('logo');
505 return array('title' => WIKI_NAME,
506 'link' => WikiURL(HOME_PAGE, false, 'absurl'),
510 function textinput_properties () {
511 return array('title' => _("Search"),
512 'description' => _("Title Search"),
514 'link' => WikiURL(_("TitleSearch"), false, 'absurl'));
517 function channel_properties () {
520 $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
522 return array('title' => WIKI_NAME,
524 'description' => _("RecentChanges"),
525 'dc:date' => Iso8601DateTime(time()));
527 /* FIXME: other things one might like in <channel>:
536 * rss091:managingEditor
538 * rss091:lastBuildDate
546 function item_properties ($rev) {
547 $page = $rev->getPage();
548 $pagename = $page->getName();
550 return array( 'title' => split_pagename($pagename),
551 'description' => $this->summary($rev),
552 'link' => $this->pageURL($rev),
553 'dc:date' => $this->time($rev),
554 'dc:contributor' => $rev->get('author'),
555 'wiki:version' => $rev->getVersion(),
556 'wiki:importance' => $this->importance($rev),
557 'wiki:status' => $this->status($rev),
558 'wiki:diff' => $this->diffURL($rev),
559 'wiki:history' => $this->historyURL($rev)
564 class NonDeletedRevisionIterator extends WikiDB_PageRevisionIterator
568 * @param $revisions object a WikiDB_PageRevisionIterator.
570 function NonDeletedRevisionIterator ($revisions, $check_current_revision = true) {
571 $this->_revisions = $revisions;
572 $this->_check_current_revision = $check_current_revision;
576 while (($rev = $this->_revisions->next())) {
577 if ($this->_check_current_revision) {
578 $page = $rev->getPage();
579 $check_rev = $page->getCurrentRevision();
584 if (! $check_rev->hasDefaultContents())
592 $this->_revisions->free();
596 class WikiPlugin_RecentChanges
599 function getName () {
600 return _("RecentChanges");
603 function getVersion() {
604 return preg_replace("/[Revision: $]/", '',
605 "\$Revision: 1.76 $");
608 function managesValidators() {
609 // Note that this is a bit of a fig.
610 // We set validators based on the most recently changed page,
611 // but this fails when the most-recent page is deleted.
612 // (Consider that the Last-Modified time will decrease
613 // when this happens.)
615 // We might be better off, leaving this as false (and junking
616 // the validator logic above) and just falling back to the
617 // default behavior (handled by WikiPlugin) of just using
618 // the WikiDB global timestamp as the mtime.
620 // Nevertheless, for now, I leave this here, mostly as an
621 // example for how to use appendValidators() and managesValidators().
626 function getDefaultArguments() {
627 return array('days' => 2,
628 'show_minor' => false,
629 'show_major' => true,
631 'show_deleted' => 'sometimes',
636 'historylinks' => false,
641 function getArgs ($argstr, $request, $defaults = false) {
642 $args = WikiPlugin::getArgs($argstr, $request, $defaults);
644 $action = $request->getArg('action');
645 if ($action != 'browse' && ! $request->isActionPage($action))
646 $args['format'] = false; // default -> HTML
648 if ($args['format'] == 'rss' && empty($args['limit']))
649 $args['limit'] = 15; // Fix default value for RSS.
651 if ($args['format'] == 'sidebar' && empty($args['limit']))
652 $args['limit'] = 1; // Fix default value for sidebar.
657 function getMostRecentParams ($args) {
660 $params = array('include_minor_revisions' => $show_minor,
661 'exclude_major_revisions' => !$show_major,
662 'include_all_revisions' => !empty($show_all));
665 $params['limit'] = $limit;
668 $params['since'] = time() - 24 * 3600 * $days;
670 $params['since'] = 24 * 3600 * $days - time();
676 function getChanges ($dbi, $args) {
677 $changes = $dbi->mostRecent($this->getMostRecentParams($args));
679 $show_deleted = $args['show_deleted'];
680 if ($show_deleted == 'sometimes')
681 $show_deleted = $args['show_minor'];
684 $changes = new NonDeletedRevisionIterator($changes, !$args['show_all']);
689 function format ($changes, $args) {
691 $format = $args['format'];
693 $fmt_class = $Theme->getFormatter('RecentChanges', $format);
695 if ($format == 'rss')
696 $fmt_class = '_RecentChanges_RssFormatter';
697 elseif ($format == 'rss091') {
698 include_once "lib/RSSWriter091.php";
699 $fmt_class = '_RecentChanges_RssFormatter091';
701 elseif ($format == 'sidebar')
702 $fmt_class = '_RecentChanges_SideBarFormatter';
704 $fmt_class = '_RecentChanges_HtmlFormatter';
707 $fmt = new $fmt_class($args);
708 return $fmt->format($changes);
711 function run ($dbi, $argstr, $request) {
712 $args = $this->getArgs($argstr, $request);
714 // HACKish: fix for SF bug #622784 (1000 years of RecentChanges ought
715 // to be enough for anyone.)
716 $args['days'] = min($args['days'], 365000);
718 // Hack alert: format() is a NORETURN for rss formatters.
719 return $this->format($this->getChanges($dbi, $args), $args);
724 class DayButtonBar extends HtmlElement {
726 function DayButtonBar ($plugin_args) {
727 $this->HtmlElement('p', array('class' => 'wiki-rc-action'));
729 // Display days selection buttons
730 extract($plugin_args);
735 $caption = _("Show minor edits for:");
737 $caption = _("Show all changes for:");
739 $caption = _("Show changes for:");
742 $this->pushContent($caption, ' ');
745 $sep = $Theme->getButtonSeparator();
748 foreach (explode(",", $daylist) as $days) {
750 $this->pushContent($sep);
751 $this->pushContent($this->_makeDayButton($days));
755 function _makeDayButton ($days) {
756 global $Theme, $request;
761 $label = "..."; //alldays
763 $label = sprintf(_("%s days"), abs($days));
765 $url = $request->getURLtoSelf(array('action' => 'browse', 'days' => $days));
767 return $Theme->makeButton($label, $url, 'wiki-rc-action');
771 // $Log: not supported by cvs2svn $
772 // Revision 1.75 2003/02/22 21:39:05 dairiki
773 // Hackish fix for SF bug #622784.
775 // (The root of the problem is clearly a PHP bug.)
777 // Revision 1.74 2003/02/21 22:52:21 dairiki
778 // Make sure to interpret relative links (like [/Subpage]) in summary
779 // relative to correct basepage.
781 // Revision 1.73 2003/02/21 04:12:06 dairiki
782 // Minor fixes for new cached markup.
784 // Revision 1.72 2003/02/17 02:19:01 dairiki
785 // Fix so that PageHistory will work when the current revision
786 // of a page has been "deleted".
788 // Revision 1.71 2003/02/16 20:04:48 dairiki
789 // Refactor the HTTP validator generation/checking code.
791 // This also fixes a number of bugs with yesterdays validator mods.
793 // Revision 1.70 2003/02/16 05:09:43 dairiki
794 // Starting to fix handling of the HTTP validator headers, Last-Modified,
797 // Last-Modified was being set incorrectly (but only when DEBUG was not
798 // defined!) Setting a Last-Modified without setting an appropriate
799 // Expires: and/or Cache-Control: header results in browsers caching
800 // the page unconditionally (for a certain period of time).
801 // This is generally bad, since it means people don't see updated
802 // page contents right away --- this is particularly confusing to
803 // the people who are editing pages since their edits don't show up
804 // next time they browse the page.
806 // Now, we don't allow caching of pages without revalidation
807 // (via the If-Modified-Since and/or If-None-Match request headers.)
808 // (You can allow caching by defining CACHE_CONTROL_MAX_AGE to an
809 // appropriate value in index.php, but I advise against it.)
813 // o Even when request is aborted due to the content not being
814 // modified, we currently still do almost all the work involved
815 // in producing the page. So the only real savings from all
816 // this logic is in network bandwidth.
818 // o Plugins which produce "dynamic" output need to be inspected
819 // and made to call $request->addToETag() and
820 // $request->setModificationTime() appropriately, otherwise the
821 // page can change without the change being detected.
822 // This leads to stale pages in cache again...
824 // Revision 1.69 2003/01/18 22:01:43 carstenklapp
826 // Reformatting & tabs to spaces;
827 // Added copyleft, getVersion, getDescription, rcs_id.
830 // (c-file-style: "gnu")
835 // c-hanging-comment-ender-p: nil
836 // indent-tabs-mode: nil