Split time and date for better i18n
[lhc/web/wiklou.git] / includes / HistoryPage.php
1 <?php
2 /**
3 * Page history
4 *
5 * Split off from Article.php and Skin.php, 2003-12-22
6 * @file
7 */
8
9 /**
10 * This class handles printing the history page for an article. In order to
11 * be efficient, it uses timestamps rather than offsets for paging, to avoid
12 * costly LIMIT,offset queries.
13 *
14 * Construct it by passing in an Article, and call $h->history() to print the
15 * history.
16 *
17 */
18 class HistoryPage {
19 const DIR_PREV = 0;
20 const DIR_NEXT = 1;
21
22 var $article, $title, $skin;
23
24 /**
25 * Construct a new HistoryPage.
26 *
27 * @param Article $article
28 * @returns nothing
29 */
30 function __construct( $article ) {
31 global $wgUser;
32 $this->article = $article;
33 $this->title = $article->getTitle();
34 $this->skin = $wgUser->getSkin();
35 $this->preCacheMessages();
36 }
37
38 function getArticle() {
39 return $this->article;
40 }
41
42 function getTitle() {
43 return $this->title;
44 }
45
46 /**
47 * As we use the same small set of messages in various methods and that
48 * they are called often, we call them once and save them in $this->message
49 */
50 function preCacheMessages() {
51 // Precache various messages
52 if( !isset( $this->message ) ) {
53 $msgs = array( 'cur', 'last', 'rev-delundel', 'pipe-separator' );
54 foreach( $msgs as $msg ) {
55 $this->message[$msg] = wfMsgExt( $msg, array( 'escapenoentities') );
56 }
57 }
58 }
59
60 /**
61 * Print the history page for an article.
62 *
63 * @returns nothing
64 */
65 function history() {
66 global $wgOut, $wgRequest, $wgScript;
67
68 /*
69 * Allow client caching.
70 */
71 if( $wgOut->checkLastModified( $this->article->getTouched() ) )
72 return; // Client cache fresh and headers sent, nothing more to do.
73
74 wfProfileIn( __METHOD__ );
75
76 /*
77 * Setup page variables.
78 */
79 $wgOut->setPageTitle( wfMsg( 'history-title', $this->title->getPrefixedText() ) );
80 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
81 $wgOut->setArticleFlag( false );
82 $wgOut->setArticleRelated( true );
83 $wgOut->setRobotPolicy( 'noindex,nofollow' );
84 $wgOut->setSyndicated( true );
85 $wgOut->setFeedAppendQuery( 'action=history' );
86 $wgOut->addScriptFile( 'history.js' );
87
88 $logPage = SpecialPage::getTitleFor( 'Log' );
89 $logLink = $this->skin->link(
90 $logPage,
91 wfMsgHtml( 'viewpagelogs' ),
92 array(),
93 array( 'page' => $this->title->getPrefixedText() ),
94 array( 'known', 'noclasses' )
95 );
96 $wgOut->setSubtitle( $logLink );
97
98 $feedType = $wgRequest->getVal( 'feed' );
99 if( $feedType ) {
100 wfProfileOut( __METHOD__ );
101 return $this->feed( $feedType );
102 }
103
104 /*
105 * Fail if article doesn't exist.
106 */
107 if( !$this->title->exists() ) {
108 $wgOut->addWikiMsg( 'nohistory' );
109 # show deletion/move log if there is an entry
110 LogEventsList::showLogExtract(
111 $wgOut,
112 array( 'delete', 'move' ),
113 $this->title->getPrefixedText(),
114 '',
115 array( 'lim' => 10,
116 'conds' => array( "log_action != 'revision'" ),
117 'showIfEmpty' => false,
118 'msgKey' => array( 'moveddeleted-notice' )
119 )
120 );
121 wfProfileOut( __METHOD__ );
122 return;
123 }
124
125 /**
126 * Add date selector to quickly get to a certain time
127 */
128 $year = $wgRequest->getInt( 'year' );
129 $month = $wgRequest->getInt( 'month' );
130 $tagFilter = $wgRequest->getVal( 'tagfilter' );
131 $tagSelector = ChangeTags::buildTagFilterSelector( $tagFilter );
132
133 $action = htmlspecialchars( $wgScript );
134 $wgOut->addHTML(
135 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
136 Xml::fieldset(
137 wfMsg( 'history-fieldset-title' ),
138 false,
139 array( 'id' => 'mw-history-search' )
140 ) .
141 Xml::hidden( 'title', $this->title->getPrefixedDBKey() ) . "\n" .
142 Xml::hidden( 'action', 'history' ) . "\n" .
143 xml::dateMenu( $year, $month ) . '&nbsp;' .
144 ( $tagSelector ? ( implode( '&nbsp;', $tagSelector ) . '&nbsp;' ) : '' ) .
145 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
146 '</fieldset></form>'
147 );
148
149 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article ) );
150
151 /**
152 * Do the list
153 */
154 $pager = new HistoryPager( $this, $year, $month, $tagFilter );
155 $wgOut->addHTML(
156 $pager->getNavigationBar() .
157 $pager->getBody() .
158 $pager->getNavigationBar()
159 );
160
161 wfProfileOut( __METHOD__ );
162 }
163
164 /**
165 * Fetch an array of revisions, specified by a given limit, offset and
166 * direction. This is now only used by the feeds. It was previously
167 * used by the main UI but that's now handled by the pager.
168 */
169 function fetchRevisions($limit, $offset, $direction) {
170 $dbr = wfGetDB( DB_SLAVE );
171
172 if( $direction == HistoryPage::DIR_PREV )
173 list($dirs, $oper) = array("ASC", ">=");
174 else /* $direction == HistoryPage::DIR_NEXT */
175 list($dirs, $oper) = array("DESC", "<=");
176
177 if( $offset )
178 $offsets = array("rev_timestamp $oper '$offset'");
179 else
180 $offsets = array();
181
182 $page_id = $this->title->getArticleID();
183
184 return $dbr->select( 'revision',
185 Revision::selectFields(),
186 array_merge(array("rev_page=$page_id"), $offsets),
187 __METHOD__,
188 array( 'ORDER BY' => "rev_timestamp $dirs",
189 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
190 );
191 }
192
193 /**
194 * Output a subscription feed listing recent edits to this page.
195 * @param string $type
196 */
197 function feed( $type ) {
198 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
199 if( !FeedUtils::checkFeedOutput($type) ) {
200 return;
201 }
202
203 $feed = new $wgFeedClasses[$type](
204 $this->title->getPrefixedText() . ' - ' .
205 wfMsgForContent( 'history-feed-title' ),
206 wfMsgForContent( 'history-feed-description' ),
207 $this->title->getFullUrl( 'action=history' )
208 );
209
210 // Get a limit on number of feed entries. Provide a sane default
211 // of 10 if none is defined (but limit to $wgFeedLimit max)
212 $limit = $wgRequest->getInt( 'limit', 10 );
213 if( $limit > $wgFeedLimit || $limit < 1 ) {
214 $limit = 10;
215 }
216 $items = $this->fetchRevisions($limit, 0, HistoryPage::DIR_NEXT);
217
218 $feed->outHeader();
219 if( $items ) {
220 foreach( $items as $row ) {
221 $feed->outItem( $this->feedItem( $row ) );
222 }
223 } else {
224 $feed->outItem( $this->feedEmpty() );
225 }
226 $feed->outFooter();
227 }
228
229 function feedEmpty() {
230 global $wgOut;
231 return new FeedItem(
232 wfMsgForContent( 'nohistory' ),
233 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
234 $this->title->getFullUrl(),
235 wfTimestamp( TS_MW ),
236 '',
237 $this->title->getTalkPage()->getFullUrl()
238 );
239 }
240
241 /**
242 * Generate a FeedItem object from a given revision table row
243 * Borrows Recent Changes' feed generation functions for formatting;
244 * includes a diff to the previous revision (if any).
245 *
246 * @param $row
247 * @return FeedItem
248 */
249 function feedItem( $row ) {
250 $rev = new Revision( $row );
251 $rev->setTitle( $this->title );
252 $text = FeedUtils::formatDiffRow(
253 $this->title,
254 $this->title->getPreviousRevisionID( $rev->getId() ),
255 $rev->getId(),
256 $rev->getTimestamp(),
257 $rev->getComment()
258 );
259 if( $rev->getComment() == '' ) {
260 global $wgContLang;
261 $title = wfMsgForContent( 'history-feed-item-nocomment',
262 $rev->getUserText(),
263 $wgContLang->timeanddate( $rev->getTimestamp() ),
264 $wgContLang->date( $rev->getTimestamp() ),
265 $wgContLang->time( $rev->getTimestamp() )
266 );
267 } else {
268 $title = $rev->getUserText() .
269 wfMsgForContent( 'colon-separator' ) .
270 FeedItem::stripComment( $rev->getComment() );
271 }
272 return new FeedItem(
273 $title,
274 $text,
275 $this->title->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
276 $rev->getTimestamp(),
277 $rev->getUserText(),
278 $this->title->getTalkPage()->getFullUrl()
279 );
280 }
281 }
282
283 /**
284 * @ingroup Pager
285 */
286 class HistoryPager extends ReverseChronologicalPager {
287 public $lastRow = false, $counter, $historyPage, $title, $buttons;
288 protected $oldIdChecked;
289
290 function __construct( $historyPage, $year='', $month='', $tagFilter = '' ) {
291 parent::__construct();
292 $this->historyPage = $historyPage;
293 $this->title = $this->historyPage->title;
294 $this->tagFilter = $tagFilter;
295 $this->getDateCond( $year, $month );
296 }
297
298 // For hook compatibility...
299 function getArticle() {
300 return $this->historyPage->getArticle();
301 }
302
303 function getQueryInfo() {
304 $queryInfo = array(
305 'tables' => array('revision'),
306 'fields' => array_merge( Revision::selectFields(), array('ts_tags') ),
307 'conds' => array('rev_page' => $this->historyPage->title->getArticleID() ),
308 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
309 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
310 );
311 ChangeTags::modifyDisplayQuery(
312 $queryInfo['tables'],
313 $queryInfo['fields'],
314 $queryInfo['conds'],
315 $queryInfo['join_conds'],
316 $queryInfo['options'],
317 $this->tagFilter
318 );
319 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
320 return $queryInfo;
321 }
322
323 function getIndexField() {
324 return 'rev_timestamp';
325 }
326
327 function formatRow( $row ) {
328 if( $this->lastRow ) {
329 $latest = ($this->counter == 1 && $this->mIsFirst);
330 $firstInList = $this->counter == 1;
331 $s = $this->historyLine( $this->lastRow, $row, $this->counter++,
332 $this->title->getNotificationTimestamp(), $latest, $firstInList );
333 } else {
334 $s = '';
335 }
336 $this->lastRow = $row;
337 return $s;
338 }
339
340 /**
341 * Creates begin of history list with a submit button
342 *
343 * @return string HTML output
344 */
345 function getStartBody() {
346 global $wgScript, $wgEnableHtmlDiff, $wgUser, $wgOut, $wgContLang;
347 $this->lastRow = false;
348 $this->counter = 1;
349 $this->oldIdChecked = 0;
350
351 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1</div>", 'histlegend' );
352 $s = Xml::openElement( 'form', array( 'action' => $wgScript,
353 'id' => 'mw-history-compare' ) ) . "\n";
354 $s .= Xml::hidden( 'title', $this->title->getPrefixedDbKey() ) . "\n";
355 $s .= Xml::hidden( 'action', 'historysubmit' ) . "\n";
356
357 $this->buttons = '<div>';
358 if( $wgUser->isAllowed('deleterevision') ) {
359 $float = $wgContLang->alignEnd();
360 # Note bug #20966, <button> is non-standard in IE<8
361 $this->buttons .= Xml::element( 'button',
362 array(
363 'type' => 'submit',
364 'name' => 'revisiondelete',
365 'value' => '1',
366 'style' => "float: $float;",
367 'class' => 'mw-history-revisiondelete-button',
368 ),
369 wfMsg( 'showhideselectedversions' )
370 ) . "\n";
371 }
372 if( $wgEnableHtmlDiff ) {
373 $this->buttons .= Xml::element( 'button',
374 array(
375 'type' => 'submit',
376 'name' => 'htmldiff',
377 'value' => '1',
378 'class' => 'historysubmit',
379 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
380 'title' => wfMsg( 'tooltip-compareselectedversions' ),
381 ),
382 wfMsg( 'visualcomparison')
383 ) . "\n";
384 $this->buttons .= $this->submitButton( wfMsg( 'wikicodecomparison'),
385 array(
386 'class' => 'historysubmit',
387 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
388 'title' => wfMsg( 'tooltip-compareselectedversions' ),
389 )
390 ) . "\n";
391 } else {
392 $this->buttons .= $this->submitButton( wfMsg( 'compareselectedversions'),
393 array(
394 'class' => 'historysubmit',
395 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
396 'title' => wfMsg( 'tooltip-compareselectedversions' ),
397 )
398 ) . "\n";
399 }
400 $this->buttons .= '</div>';
401 $s .= $this->buttons . '<ul id="pagehistory">' . "\n";
402 return $s;
403 }
404
405 function getEndBody() {
406 if( $this->lastRow ) {
407 $latest = $this->counter == 1 && $this->mIsFirst;
408 $firstInList = $this->counter == 1;
409 if( $this->mIsBackwards ) {
410 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
411 if( $this->mOffset == '' ) {
412 $next = null;
413 } else {
414 $next = 'unknown';
415 }
416 } else {
417 # The next row is the past-the-end row
418 $next = $this->mPastTheEndRow;
419 }
420 $s = $this->historyLine( $this->lastRow, $next, $this->counter++,
421 $this->title->getNotificationTimestamp(), $latest, $firstInList );
422 } else {
423 $s = '';
424 }
425 $s .= "</ul>\n";
426 $s .= $this->buttons;
427 $s .= '</form>';
428 return $s;
429 }
430
431 /**
432 * Creates a submit button
433 *
434 * @param array $attributes attributes
435 * @return string HTML output for the submit button
436 */
437 function submitButton($message, $attributes = array() ) {
438 # Disable submit button if history has 1 revision only
439 if( $this->getNumRows() > 1 ) {
440 return Xml::submitButton( $message , $attributes );
441 } else {
442 return '';
443 }
444 }
445
446 /**
447 * Returns a row from the history printout.
448 *
449 * @todo document some more, and maybe clean up the code (some params redundant?)
450 *
451 * @param Row $row The database row corresponding to the previous line.
452 * @param mixed $next The database row corresponding to the next line.
453 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
454 * @param $notificationtimestamp
455 * @param bool $latest Whether this row corresponds to the page's latest revision.
456 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
457 * @return string HTML output for the row
458 */
459 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
460 $latest = false, $firstInList = false )
461 {
462 global $wgUser, $wgLang;
463 $rev = new Revision( $row );
464 $rev->setTitle( $this->title );
465
466 $curlink = $this->curLink( $rev, $latest );
467 $lastlink = $this->lastLink( $rev, $next, $counter );
468 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
469 $histLinks = Html::rawElement(
470 'span',
471 array( 'class' => 'mw-history-histlinks' ),
472 '(' . $curlink . $this->historyPage->message['pipe-separator'] . $lastlink . ') '
473 );
474 $s = $histLinks . $diffButtons;
475
476 $link = $this->revLink( $rev );
477 $classes = array();
478
479 if( $wgUser->isAllowed( 'deleterevision' ) ) {
480 // Don't show useless link to people who cannot hide revisions
481 if( !$rev->getVisibility() && !$wgUser->isAllowed( 'deleterevision' ) ) {
482 $del = Xml::check( 'deleterevisions', false, array('class' => 'mw-revdelundel-hidden') );
483 // If revision was hidden from sysops
484 } else if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
485 $del = Xml::check( 'deleterevisions', false, array('disabled' => 'disabled') );
486 // Otherwise, show the link...
487 } else {
488 $id = $rev->getId();
489 $del = Xml::check( 'showhiderevisions', false, array( 'name' => "ids[$id]" ) );
490 }
491 $s .= " $del ";
492 }
493
494 $s .= " $link";
495 $s .= " <span class='history-user'>" . $this->getSkin()->revUserTools( $rev, true ) . "</span>";
496
497 if( $rev->isMinor() ) {
498 $s .= ' ' . ChangesList::flag( 'minor' );
499 }
500
501 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision::DELETED_TEXT ) ) {
502 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
503 }
504
505 $s .= $this->getSkin()->revComment( $rev, false, true );
506
507 if( $notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp) ) {
508 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
509 }
510
511 $tools = array();
512
513 # Rollback and undo links
514 if( !is_null( $next ) && is_object( $next ) ) {
515 if( $latest && $this->title->userCan( 'rollback' ) && $this->title->userCan( 'edit' ) ) {
516 $tools[] = '<span class="mw-rollback-link">'.
517 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
518 }
519
520 if( $this->title->quickUserCan( 'edit' )
521 && !$rev->isDeleted( Revision::DELETED_TEXT )
522 && !$next->rev_deleted & Revision::DELETED_TEXT )
523 {
524 # Create undo tooltip for the first (=latest) line only
525 $undoTooltip = $latest
526 ? array( 'title' => wfMsg( 'tooltip-undo' ) )
527 : array();
528 $undolink = $this->getSkin()->link(
529 $this->title,
530 wfMsgHtml( 'editundo' ),
531 $undoTooltip,
532 array(
533 'action' => 'edit',
534 'undoafter' => $next->rev_id,
535 'undo' => $rev->getId()
536 ),
537 array( 'known', 'noclasses' )
538 );
539 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
540 }
541 }
542
543 if( $tools ) {
544 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
545 }
546
547 # Tags
548 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'history' );
549 $classes = array_merge( $classes, $newClasses );
550 $s .= " $tagSummary";
551
552 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
553
554 $attribs = array();
555 if ( $classes ) {
556 $attribs['class'] = implode( ' ', $classes );
557 }
558
559 return Xml::tags( 'li', $attribs, $s ) . "\n";
560 }
561
562 /**
563 * Create a link to view this revision of the page
564 * @param Revision $rev
565 * @returns string
566 */
567 function revLink( $rev ) {
568 global $wgLang;
569 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
570 $date = htmlspecialchars( $date );
571 if( !$rev->isDeleted( Revision::DELETED_TEXT ) ) {
572 $link = $this->getSkin()->link(
573 $this->title,
574 $date,
575 array(),
576 array( 'oldid' => $rev->getId() ),
577 array( 'known', 'noclasses' )
578 );
579 } else {
580 $link = "<span class=\"history-deleted\">$date</span>";
581 }
582 return $link;
583 }
584
585 /**
586 * Create a diff-to-current link for this revision for this page
587 * @param Revision $rev
588 * @param Bool $latest, this is the latest revision of the page?
589 * @returns string
590 */
591 function curLink( $rev, $latest ) {
592 $cur = $this->historyPage->message['cur'];
593 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
594 return $cur;
595 } else {
596 return $this->getSkin()->link(
597 $this->title,
598 $cur,
599 array(),
600 array(
601 'diff' => $this->title->getLatestRevID(),
602 'oldid' => $rev->getId()
603 ),
604 array( 'known', 'noclasses' )
605 );
606 }
607 }
608
609 /**
610 * Create a diff-to-previous link for this revision for this page.
611 * @param Revision $prevRev, the previous revision
612 * @param mixed $next, the newer revision
613 * @param int $counter, what row on the history list this is
614 * @returns string
615 */
616 function lastLink( $prevRev, $next, $counter ) {
617 $last = $this->historyPage->message['last'];
618 # $next may either be a Row, null, or "unkown"
619 $nextRev = is_object($next) ? new Revision( $next ) : $next;
620 if( is_null($next) ) {
621 # Probably no next row
622 return $last;
623 } elseif( $next === 'unknown' ) {
624 # Next row probably exists but is unknown, use an oldid=prev link
625 return $this->getSkin()->link(
626 $this->title,
627 $last,
628 array(),
629 array(
630 'diff' => $prevRev->getId(),
631 'oldid' => 'prev'
632 ),
633 array( 'known', 'noclasses' )
634 );
635 } elseif( !$prevRev->userCan(Revision::DELETED_TEXT) || !$nextRev->userCan(Revision::DELETED_TEXT) ) {
636 return $last;
637 } else {
638 return $this->getSkin()->link(
639 $this->title,
640 $last,
641 array(),
642 array(
643 'diff' => $prevRev->getId(),
644 'oldid' => $next->rev_id
645 ),
646 array( 'known', 'noclasses' )
647 );
648 }
649 }
650
651 /**
652 * Create radio buttons for page history
653 *
654 * @param object $rev Revision
655 * @param bool $firstInList Is this version the first one?
656 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
657 * @return string HTML output for the radio buttons
658 */
659 function diffButtons( $rev, $firstInList, $counter ) {
660 if( $this->getNumRows() > 1 ) {
661 $id = $rev->getId();
662 $radio = array( 'type' => 'radio', 'value' => $id );
663 /** @todo: move title texts to javascript */
664 if( $firstInList ) {
665 $first = Xml::element( 'input',
666 array_merge( $radio, array(
667 'style' => 'visibility:hidden',
668 'name' => 'oldid',
669 'id' => 'mw-oldid-null' ) )
670 );
671 $checkmark = array( 'checked' => 'checked' );
672 } else {
673 # Check ility of old revisions
674 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
675 $radio['disabled'] = 'disabled';
676 $checkmark = array(); // We will check the next possible one
677 } else if( $counter == 2 || !$this->oldIdChecked ) {
678 $checkmark = array( 'checked' => 'checked' );
679 $this->oldIdChecked = $id;
680 } else {
681 $checkmark = array();
682 }
683 $first = Xml::element( 'input',
684 array_merge( $radio, $checkmark, array(
685 'name' => 'oldid',
686 'id' => "mw-oldid-$id" ) ) );
687 $checkmark = array();
688 }
689 $second = Xml::element( 'input',
690 array_merge( $radio, $checkmark, array(
691 'name' => 'diff',
692 'id' => "mw-diff-$id" ) ) );
693 return $first . $second;
694 } else {
695 return '';
696 }
697 }
698 }
699
700 /**
701 * Backwards-compatibility aliases
702 */
703 class PageHistory extends HistoryPage {}
704 class PageHistoryPager extends HistoryPager {}