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