2 rcs_id('$Id: RecentChanges.php,v 1.72 2003-02-17 02:19:01 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')),
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);
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 = $this->_javascript($addsidebarjsfunc);
275 $addsidebarjsclick = " " . "<small style=\"font-weight:normal;\"><a href=\"javascript:addPanel();\">sidebar</a></small>";
276 $jsc = $this->_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 _javascript($script) {
286 return HTML::script(array('language' => 'JavaScript',
287 'type' => 'text/javascript'),
288 new RawXml("<!-- //\n$script\n// -->"));
291 function format ($changes) {
292 $html = HTML(HTML::h2(false, $this->title()));
293 if (($desc = $this->description()))
294 $html->pushContent(HTML::p(false, $desc));
296 if ($this->_args['daylist'])
297 $html->pushContent(new DayButtonBar($this->_args));
303 while ($rev = $changes->next()) {
304 if (($date = $this->date($rev)) != $last_date) {
306 $html->pushContent($lines);
307 $html->pushContent(HTML::h3($date));
312 $lines->pushContent($this->format_revision($rev));
315 $this->setValidators($rev);
319 $html->pushContent($lines);
321 $html->pushContent(HTML::p(array('class' => 'rc-empty'),
322 $this->empty_message()));
327 function format_revision ($rev) {
328 $args = &$this->_args;
330 $class = 'rc-' . $this->importance($rev);
332 $time = $this->time($rev);
333 if (! $rev->get('is_minor_edit'))
334 $time = HTML::strong(array('class' => 'pageinfo-majoredit'), $time);
336 $line = HTML::li(array('class' => $class));
339 if ($args['difflinks'])
340 $line->pushContent($this->diffLink($rev), ' ');
342 if ($args['historylinks'])
343 $line->pushContent($this->historyLink($rev), ' ');
345 $line->pushContent($this->pageLink($rev), ' ',
347 $this->summaryAsHTML($rev),
349 $this->authorLink($rev));
355 class _RecentChanges_SideBarFormatter
356 extends _RecentChanges_HtmlFormatter
358 function description () {
361 function rss_icon () {
365 //title click opens the normal RC or RE page in the main browser frame
366 extract($this->_args);
367 $titlelink = WikiLink($show_minor ? _("RecentEdits") : _("RecentChanges"));
368 $titlelink->setAttr('target', '_content');
369 return HTML($this->logo(), $titlelink);
372 //logo click opens the HomePage in the main browser frame
374 $img = HTML::img(array('src' => $Theme->getImageURL('logo'),
379 $linkurl = WikiLink(HOME_PAGE, false, $img);
380 $linkurl->setAttr('target', '_content');
384 function authorLink ($rev) {
385 $author = $rev->get('author');
386 if ( $this->authorHasPage($author) ) {
387 $linkurl = WikiLink($author);
388 $linkurl->setAttr('target', '_content'); // way to do this using parent::authorLink ??
393 function diffLink ($rev) {
394 $linkurl = parent::diffLink($rev);
395 $linkurl->setAttr('target', '_content');
398 function historyLink ($rev) {
399 $linkurl = parent::historyLink($rev);
400 $linkurl->setAttr('target', '_content');
403 function pageLink ($rev) {
404 $linkurl = parent::pageLink($rev);
405 $linkurl->setAttr('target', '_content');
408 // Overriding summaryAsHTML, because there is no way yet to
409 // return summary as transformed text with
410 // links setAttr('target', '_content') in Mozilla sidebar.
411 // So for now don't create clickable links inside summary
412 // in the sidebar, or else they target the sidebar and not the
413 // main content window.
414 function summaryAsHTML ($rev) {
415 if ( !($summary = $this->summary($rev)) )
417 return HTML::strong(array('class' => 'wiki-summary'),
419 /*TransformLinks(*/$summary,/* $rev->get('markup')),*/
424 function format ($changes) {
425 $this->_args['daylist'] = false; //only 1 day for Mozilla sidebar
426 $html = _RecentChanges_HtmlFormatter::format ($changes);
427 $html = HTML::div(array('class' => 'wikitext'), $html);
429 printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n", CHARSET);
430 printf('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"');
431 printf(' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
432 printf('<html xmlns="http://www.w3.org/1999/xhtml">');
435 extract($this->_args);
436 $title = WIKI_NAME . $show_minor ? _("RecentEdits") : _("RecentChanges");
437 printf("<title>" . $title . "</title>\n");
439 $css = $Theme->getCSS();
443 printf("<body class=\"sidebar\">\n");
445 printf("\n</body>\n");
451 $request->finish(); // cut rest of page processing short
456 class _RecentChanges_RssFormatter
457 extends _RecentChanges_Formatter
459 var $_absurls = true;
461 function time ($rev) {
462 return Iso8601DateTime($rev->get('mtime'));
465 function pageURI ($rev) {
466 return WikiURL($rev, '', 'absurl');
469 function format ($changes) {
470 include_once('lib/RssWriter.php');
471 $rss = new RssWriter;
474 $rss->channel($this->channel_properties());
476 if (($props = $this->image_properties()))
478 if (($props = $this->textinput_properties()))
479 $rss->textinput($props);
482 while ($rev = $changes->next()) {
483 $rss->addItem($this->item_properties($rev),
484 $this->pageURI($rev));
486 $this->setValidators($rev);
491 printf("\n<!-- Generated by PhpWiki:\n%s-->\n", $GLOBALS['RCS_IDS']);
493 // Flush errors in comment, otherwise it's invalid XML.
494 global $ErrorManager;
495 if (($errors = $ErrorManager->getPostponedErrorsAsHTML()))
496 printf("\n<!-- PHP Warnings:\n%s-->\n", AsXML($errors));
498 global $request; // FIXME
499 $request->finish(); // NORETURN!!!!
502 function image_properties () {
505 $img_url = SERVER_URL . $Theme->getImageURL('logo');
509 return array('title' => WIKI_NAME,
510 'link' => WikiURL(HOME_PAGE, false, 'absurl'),
514 function textinput_properties () {
515 return array('title' => _("Search"),
516 'description' => _("Title Search"),
518 'link' => WikiURL(_("TitleSearch"), false, 'absurl'));
521 function channel_properties () {
524 $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
526 return array('title' => WIKI_NAME,
528 'description' => _("RecentChanges"),
529 'dc:date' => Iso8601DateTime(time()));
531 /* FIXME: other things one might like in <channel>:
540 * rss091:managingEditor
542 * rss091:lastBuildDate
550 function item_properties ($rev) {
551 $page = $rev->getPage();
552 $pagename = $page->getName();
554 return array( 'title' => split_pagename($pagename),
555 'description' => $this->summary($rev),
556 'link' => $this->pageURL($rev),
557 'dc:date' => $this->time($rev),
558 'dc:contributor' => $rev->get('author'),
559 'wiki:version' => $rev->getVersion(),
560 'wiki:importance' => $this->importance($rev),
561 'wiki:status' => $this->status($rev),
562 'wiki:diff' => $this->diffURL($rev),
563 'wiki:history' => $this->historyURL($rev)
568 class NonDeletedRevisionIterator extends WikiDB_PageRevisionIterator
572 * @param $revisions object a WikiDB_PageRevisionIterator.
574 function NonDeletedRevisionIterator ($revisions, $check_current_revision = true) {
575 $this->_revisions = $revisions;
576 $this->_check_current_revision = $check_current_revision;
580 while (($rev = $this->_revisions->next())) {
581 if ($this->_check_current_revision) {
582 $page = $rev->getPage();
583 $check_rev = $page->getCurrentRevision();
588 if (! $check_rev->hasDefaultContents())
596 $this->_revisions->free();
600 class WikiPlugin_RecentChanges
603 function getName () {
604 return _("RecentChanges");
607 function getVersion() {
608 return preg_replace("/[Revision: $]/", '',
609 "\$Revision: 1.72 $");
612 function managesValidators() {
613 // Note that this is a bit of a fig.
614 // We set validators based on the most recently changed page,
615 // but this fails when the most-recent page is deleted.
616 // (Consider that the Last-Modified time will decrease
617 // when this happens.)
619 // We might be better off, leaving this as false (and junking
620 // the validator logic above) and just falling back to the
621 // default behavior (handled by WikiPlugin) of just using
622 // the WikiDB global timestamp as the mtime.
624 // Nevertheless, for now, I leave this here, mostly as an
625 // example for how to use appendValidators() and managesValidators().
630 function getDefaultArguments() {
631 return array('days' => 2,
632 'show_minor' => false,
633 'show_major' => true,
635 'show_deleted' => 'sometimes',
640 'historylinks' => false,
645 function getArgs ($argstr, $request, $defaults = false) {
646 $args = WikiPlugin::getArgs($argstr, $request, $defaults);
648 $action = $request->getArg('action');
649 if ($action != 'browse' && ! $request->isActionPage($action))
650 $args['format'] = false; // default -> HTML
652 if ($args['format'] == 'rss' && empty($args['limit']))
653 $args['limit'] = 15; // Fix default value for RSS.
655 if ($args['format'] == 'sidebar' && empty($args['limit']))
656 $args['limit'] = 1; // Fix default value for sidebar.
661 function getMostRecentParams ($args) {
664 $params = array('include_minor_revisions' => $show_minor,
665 'exclude_major_revisions' => !$show_major,
666 'include_all_revisions' => !empty($show_all));
669 $params['limit'] = $limit;
672 $params['since'] = time() - 24 * 3600 * $days;
674 $params['since'] = 24 * 3600 * $days - time();
680 function getChanges ($dbi, $args) {
681 $changes = $dbi->mostRecent($this->getMostRecentParams($args));
683 $show_deleted = $args['show_deleted'];
684 if ($show_deleted == 'sometimes')
685 $show_deleted = $args['show_minor'];
688 $changes = new NonDeletedRevisionIterator($changes, !$args['show_all']);
693 function format ($changes, $args) {
695 $format = $args['format'];
697 $fmt_class = $Theme->getFormatter('RecentChanges', $format);
699 if ($format == 'rss')
700 $fmt_class = '_RecentChanges_RssFormatter';
701 elseif ($format == 'rss091') {
702 include_once "lib/RSSWriter091.php";
703 $fmt_class = '_RecentChanges_RssFormatter091';
705 elseif ($format == 'sidebar')
706 $fmt_class = '_RecentChanges_SideBarFormatter';
708 $fmt_class = '_RecentChanges_HtmlFormatter';
711 $fmt = new $fmt_class($args);
712 return $fmt->format($changes);
715 function run ($dbi, $argstr, $request) {
716 $args = $this->getArgs($argstr, $request);
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.71 2003/02/16 20:04:48 dairiki
773 // Refactor the HTTP validator generation/checking code.
775 // This also fixes a number of bugs with yesterdays validator mods.
777 // Revision 1.70 2003/02/16 05:09:43 dairiki
778 // Starting to fix handling of the HTTP validator headers, Last-Modified,
781 // Last-Modified was being set incorrectly (but only when DEBUG was not
782 // defined!) Setting a Last-Modified without setting an appropriate
783 // Expires: and/or Cache-Control: header results in browsers caching
784 // the page unconditionally (for a certain period of time).
785 // This is generally bad, since it means people don't see updated
786 // page contents right away --- this is particularly confusing to
787 // the people who are editing pages since their edits don't show up
788 // next time they browse the page.
790 // Now, we don't allow caching of pages without revalidation
791 // (via the If-Modified-Since and/or If-None-Match request headers.)
792 // (You can allow caching by defining CACHE_CONTROL_MAX_AGE to an
793 // appropriate value in index.php, but I advise against it.)
797 // o Even when request is aborted due to the content not being
798 // modified, we currently still do almost all the work involved
799 // in producing the page. So the only real savings from all
800 // this logic is in network bandwidth.
802 // o Plugins which produce "dynamic" output need to be inspected
803 // and made to call $request->addToETag() and
804 // $request->setModificationTime() appropriately, otherwise the
805 // page can change without the change being detected.
806 // This leads to stale pages in cache again...
808 // Revision 1.69 2003/01/18 22:01:43 carstenklapp
810 // Reformatting & tabs to spaces;
811 // Added copyleft, getVersion, getDescription, rcs_id.
814 // (c-file-style: "gnu")
819 // c-hanging-comment-ender-p: nil
820 // indent-tabs-mode: nil