2 rcs_id('$Id: RecentChanges.php,v 1.74 2003-02-21 22:52:21 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);
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 include_once('lib/InlineParser.php');
294 $html = HTML(HTML::h2(false, $this->title()));
295 if (($desc = $this->description()))
296 $html->pushContent(HTML::p(false, $desc));
298 if ($this->_args['daylist'])
299 $html->pushContent(new DayButtonBar($this->_args));
305 while ($rev = $changes->next()) {
306 if (($date = $this->date($rev)) != $last_date) {
308 $html->pushContent($lines);
309 $html->pushContent(HTML::h3($date));
314 $lines->pushContent($this->format_revision($rev));
317 $this->setValidators($rev);
321 $html->pushContent($lines);
323 $html->pushContent(HTML::p(array('class' => 'rc-empty'),
324 $this->empty_message()));
329 function format_revision ($rev) {
330 $args = &$this->_args;
332 $class = 'rc-' . $this->importance($rev);
334 $time = $this->time($rev);
335 if (! $rev->get('is_minor_edit'))
336 $time = HTML::strong(array('class' => 'pageinfo-majoredit'), $time);
338 $line = HTML::li(array('class' => $class));
341 if ($args['difflinks'])
342 $line->pushContent($this->diffLink($rev), ' ');
344 if ($args['historylinks'])
345 $line->pushContent($this->historyLink($rev), ' ');
347 $line->pushContent($this->pageLink($rev), ' ',
349 $this->summaryAsHTML($rev),
351 $this->authorLink($rev));
357 class _RecentChanges_SideBarFormatter
358 extends _RecentChanges_HtmlFormatter
360 function description () {
363 function rss_icon () {
367 //title click opens the normal RC or RE page in the main browser frame
368 extract($this->_args);
369 $titlelink = WikiLink($show_minor ? _("RecentEdits") : _("RecentChanges"));
370 $titlelink->setAttr('target', '_content');
371 return HTML($this->logo(), $titlelink);
374 //logo click opens the HomePage in the main browser frame
376 $img = HTML::img(array('src' => $Theme->getImageURL('logo'),
381 $linkurl = WikiLink(HOME_PAGE, false, $img);
382 $linkurl->setAttr('target', '_content');
386 function authorLink ($rev) {
387 $author = $rev->get('author');
388 if ( $this->authorHasPage($author) ) {
389 $linkurl = WikiLink($author);
390 $linkurl->setAttr('target', '_content'); // way to do this using parent::authorLink ??
395 function diffLink ($rev) {
396 $linkurl = parent::diffLink($rev);
397 $linkurl->setAttr('target', '_content');
400 function historyLink ($rev) {
401 $linkurl = parent::historyLink($rev);
402 $linkurl->setAttr('target', '_content');
405 function pageLink ($rev) {
406 $linkurl = parent::pageLink($rev);
407 $linkurl->setAttr('target', '_content');
410 // Overriding summaryAsHTML, because there is no way yet to
411 // return summary as transformed text with
412 // links setAttr('target', '_content') in Mozilla sidebar.
413 // So for now don't create clickable links inside summary
414 // in the sidebar, or else they target the sidebar and not the
415 // main content window.
416 function summaryAsHTML ($rev) {
417 if ( !($summary = $this->summary($rev)) )
419 return HTML::strong(array('class' => 'wiki-summary'),
421 /*TransformLinks(*/$summary,/* $rev->get('markup')),*/
426 function format ($changes) {
427 $this->_args['daylist'] = false; //only 1 day for Mozilla sidebar
428 $html = _RecentChanges_HtmlFormatter::format ($changes);
429 $html = HTML::div(array('class' => 'wikitext'), $html);
431 printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n", CHARSET);
432 printf('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"');
433 printf(' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
434 printf('<html xmlns="http://www.w3.org/1999/xhtml">');
437 extract($this->_args);
438 $title = WIKI_NAME . $show_minor ? _("RecentEdits") : _("RecentChanges");
439 printf("<title>" . $title . "</title>\n");
441 $css = $Theme->getCSS();
445 printf("<body class=\"sidebar\">\n");
447 printf("\n</body>\n");
453 $request->finish(); // cut rest of page processing short
458 class _RecentChanges_RssFormatter
459 extends _RecentChanges_Formatter
461 var $_absurls = true;
463 function time ($rev) {
464 return Iso8601DateTime($rev->get('mtime'));
467 function pageURI ($rev) {
468 return WikiURL($rev, '', 'absurl');
471 function format ($changes) {
472 include_once('lib/RssWriter.php');
473 $rss = new RssWriter;
476 $rss->channel($this->channel_properties());
478 if (($props = $this->image_properties()))
480 if (($props = $this->textinput_properties()))
481 $rss->textinput($props);
484 while ($rev = $changes->next()) {
485 $rss->addItem($this->item_properties($rev),
486 $this->pageURI($rev));
488 $this->setValidators($rev);
493 printf("\n<!-- Generated by PhpWiki:\n%s-->\n", $GLOBALS['RCS_IDS']);
495 // Flush errors in comment, otherwise it's invalid XML.
496 global $ErrorManager;
497 if (($errors = $ErrorManager->getPostponedErrorsAsHTML()))
498 printf("\n<!-- PHP Warnings:\n%s-->\n", AsXML($errors));
500 global $request; // FIXME
501 $request->finish(); // NORETURN!!!!
504 function image_properties () {
507 $img_url = SERVER_URL . $Theme->getImageURL('logo');
511 return array('title' => WIKI_NAME,
512 'link' => WikiURL(HOME_PAGE, false, 'absurl'),
516 function textinput_properties () {
517 return array('title' => _("Search"),
518 'description' => _("Title Search"),
520 'link' => WikiURL(_("TitleSearch"), false, 'absurl'));
523 function channel_properties () {
526 $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
528 return array('title' => WIKI_NAME,
530 'description' => _("RecentChanges"),
531 'dc:date' => Iso8601DateTime(time()));
533 /* FIXME: other things one might like in <channel>:
542 * rss091:managingEditor
544 * rss091:lastBuildDate
552 function item_properties ($rev) {
553 $page = $rev->getPage();
554 $pagename = $page->getName();
556 return array( 'title' => split_pagename($pagename),
557 'description' => $this->summary($rev),
558 'link' => $this->pageURL($rev),
559 'dc:date' => $this->time($rev),
560 'dc:contributor' => $rev->get('author'),
561 'wiki:version' => $rev->getVersion(),
562 'wiki:importance' => $this->importance($rev),
563 'wiki:status' => $this->status($rev),
564 'wiki:diff' => $this->diffURL($rev),
565 'wiki:history' => $this->historyURL($rev)
570 class NonDeletedRevisionIterator extends WikiDB_PageRevisionIterator
574 * @param $revisions object a WikiDB_PageRevisionIterator.
576 function NonDeletedRevisionIterator ($revisions, $check_current_revision = true) {
577 $this->_revisions = $revisions;
578 $this->_check_current_revision = $check_current_revision;
582 while (($rev = $this->_revisions->next())) {
583 if ($this->_check_current_revision) {
584 $page = $rev->getPage();
585 $check_rev = $page->getCurrentRevision();
590 if (! $check_rev->hasDefaultContents())
598 $this->_revisions->free();
602 class WikiPlugin_RecentChanges
605 function getName () {
606 return _("RecentChanges");
609 function getVersion() {
610 return preg_replace("/[Revision: $]/", '',
611 "\$Revision: 1.74 $");
614 function managesValidators() {
615 // Note that this is a bit of a fig.
616 // We set validators based on the most recently changed page,
617 // but this fails when the most-recent page is deleted.
618 // (Consider that the Last-Modified time will decrease
619 // when this happens.)
621 // We might be better off, leaving this as false (and junking
622 // the validator logic above) and just falling back to the
623 // default behavior (handled by WikiPlugin) of just using
624 // the WikiDB global timestamp as the mtime.
626 // Nevertheless, for now, I leave this here, mostly as an
627 // example for how to use appendValidators() and managesValidators().
632 function getDefaultArguments() {
633 return array('days' => 2,
634 'show_minor' => false,
635 'show_major' => true,
637 'show_deleted' => 'sometimes',
642 'historylinks' => false,
647 function getArgs ($argstr, $request, $defaults = false) {
648 $args = WikiPlugin::getArgs($argstr, $request, $defaults);
650 $action = $request->getArg('action');
651 if ($action != 'browse' && ! $request->isActionPage($action))
652 $args['format'] = false; // default -> HTML
654 if ($args['format'] == 'rss' && empty($args['limit']))
655 $args['limit'] = 15; // Fix default value for RSS.
657 if ($args['format'] == 'sidebar' && empty($args['limit']))
658 $args['limit'] = 1; // Fix default value for sidebar.
663 function getMostRecentParams ($args) {
666 $params = array('include_minor_revisions' => $show_minor,
667 'exclude_major_revisions' => !$show_major,
668 'include_all_revisions' => !empty($show_all));
671 $params['limit'] = $limit;
674 $params['since'] = time() - 24 * 3600 * $days;
676 $params['since'] = 24 * 3600 * $days - time();
682 function getChanges ($dbi, $args) {
683 $changes = $dbi->mostRecent($this->getMostRecentParams($args));
685 $show_deleted = $args['show_deleted'];
686 if ($show_deleted == 'sometimes')
687 $show_deleted = $args['show_minor'];
690 $changes = new NonDeletedRevisionIterator($changes, !$args['show_all']);
695 function format ($changes, $args) {
697 $format = $args['format'];
699 $fmt_class = $Theme->getFormatter('RecentChanges', $format);
701 if ($format == 'rss')
702 $fmt_class = '_RecentChanges_RssFormatter';
703 elseif ($format == 'rss091') {
704 include_once "lib/RSSWriter091.php";
705 $fmt_class = '_RecentChanges_RssFormatter091';
707 elseif ($format == 'sidebar')
708 $fmt_class = '_RecentChanges_SideBarFormatter';
710 $fmt_class = '_RecentChanges_HtmlFormatter';
713 $fmt = new $fmt_class($args);
714 return $fmt->format($changes);
717 function run ($dbi, $argstr, $request) {
718 $args = $this->getArgs($argstr, $request);
720 // Hack alert: format() is a NORETURN for rss formatters.
721 return $this->format($this->getChanges($dbi, $args), $args);
726 class DayButtonBar extends HtmlElement {
728 function DayButtonBar ($plugin_args) {
729 $this->HtmlElement('p', array('class' => 'wiki-rc-action'));
731 // Display days selection buttons
732 extract($plugin_args);
737 $caption = _("Show minor edits for:");
739 $caption = _("Show all changes for:");
741 $caption = _("Show changes for:");
744 $this->pushContent($caption, ' ');
747 $sep = $Theme->getButtonSeparator();
750 foreach (explode(",", $daylist) as $days) {
752 $this->pushContent($sep);
753 $this->pushContent($this->_makeDayButton($days));
757 function _makeDayButton ($days) {
758 global $Theme, $request;
763 $label = "..."; //alldays
765 $label = sprintf(_("%s days"), abs($days));
767 $url = $request->getURLtoSelf(array('action' => 'browse', 'days' => $days));
769 return $Theme->makeButton($label, $url, 'wiki-rc-action');
773 // $Log: not supported by cvs2svn $
774 // Revision 1.73 2003/02/21 04:12:06 dairiki
775 // Minor fixes for new cached markup.
777 // Revision 1.72 2003/02/17 02:19:01 dairiki
778 // Fix so that PageHistory will work when the current revision
779 // of a page has been "deleted".
781 // Revision 1.71 2003/02/16 20:04:48 dairiki
782 // Refactor the HTTP validator generation/checking code.
784 // This also fixes a number of bugs with yesterdays validator mods.
786 // Revision 1.70 2003/02/16 05:09:43 dairiki
787 // Starting to fix handling of the HTTP validator headers, Last-Modified,
790 // Last-Modified was being set incorrectly (but only when DEBUG was not
791 // defined!) Setting a Last-Modified without setting an appropriate
792 // Expires: and/or Cache-Control: header results in browsers caching
793 // the page unconditionally (for a certain period of time).
794 // This is generally bad, since it means people don't see updated
795 // page contents right away --- this is particularly confusing to
796 // the people who are editing pages since their edits don't show up
797 // next time they browse the page.
799 // Now, we don't allow caching of pages without revalidation
800 // (via the If-Modified-Since and/or If-None-Match request headers.)
801 // (You can allow caching by defining CACHE_CONTROL_MAX_AGE to an
802 // appropriate value in index.php, but I advise against it.)
806 // o Even when request is aborted due to the content not being
807 // modified, we currently still do almost all the work involved
808 // in producing the page. So the only real savings from all
809 // this logic is in network bandwidth.
811 // o Plugins which produce "dynamic" output need to be inspected
812 // and made to call $request->addToETag() and
813 // $request->setModificationTime() appropriately, otherwise the
814 // page can change without the change being detected.
815 // This leads to stale pages in cache again...
817 // Revision 1.69 2003/01/18 22:01:43 carstenklapp
819 // Reformatting & tabs to spaces;
820 // Added copyleft, getVersion, getDescription, rcs_id.
823 // (c-file-style: "gnu")
828 // c-hanging-comment-ender-p: nil
829 // indent-tabs-mode: nil