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