* Renamed deletedcontent to deletedtext
[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' );
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 } else {
265 $title = $rev->getUserText() .
266 wfMsgForContent( 'colon-separator' ) .
267 FeedItem::stripComment( $rev->getComment() );
268 }
269 return new FeedItem(
270 $title,
271 $text,
272 $this->title->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
273 $rev->getTimestamp(),
274 $rev->getUserText(),
275 $this->title->getTalkPage()->getFullUrl()
276 );
277 }
278 }
279
280 /**
281 * @ingroup Pager
282 */
283 class HistoryPager extends ReverseChronologicalPager {
284 public $lastRow = false, $counter, $historyPage, $title, $buttons;
285 protected $oldIdChecked;
286
287 function __construct( $historyPage, $year='', $month='', $tagFilter = '' ) {
288 parent::__construct();
289 $this->historyPage = $historyPage;
290 $this->title = $this->historyPage->title;
291 $this->tagFilter = $tagFilter;
292 $this->getDateCond( $year, $month );
293 }
294
295 // For hook compatibility...
296 function getArticle() {
297 return $this->historyPage->getArticle();
298 }
299
300 function getQueryInfo() {
301 $queryInfo = array(
302 'tables' => array('revision'),
303 'fields' => array_merge( Revision::selectFields(), array('ts_tags') ),
304 'conds' => array('rev_page' => $this->historyPage->title->getArticleID() ),
305 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
306 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
307 );
308 ChangeTags::modifyDisplayQuery(
309 $queryInfo['tables'],
310 $queryInfo['fields'],
311 $queryInfo['conds'],
312 $queryInfo['join_conds'],
313 $queryInfo['options'],
314 $this->tagFilter
315 );
316 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
317 return $queryInfo;
318 }
319
320 function getIndexField() {
321 return 'rev_timestamp';
322 }
323
324 function formatRow( $row ) {
325 if( $this->lastRow ) {
326 $latest = ($this->counter == 1 && $this->mIsFirst);
327 $firstInList = $this->counter == 1;
328 $s = $this->historyLine( $this->lastRow, $row, $this->counter++,
329 $this->title->getNotificationTimestamp(), $latest, $firstInList );
330 } else {
331 $s = '';
332 }
333 $this->lastRow = $row;
334 return $s;
335 }
336
337 /**
338 * Creates begin of history list with a submit button
339 *
340 * @return string HTML output
341 */
342 function getStartBody() {
343 global $wgScript, $wgEnableHtmlDiff, $wgUser, $wgOut;
344 $this->lastRow = false;
345 $this->counter = 1;
346 $this->oldIdChecked = 0;
347
348 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1</div>", 'histlegend' );
349 $s = Xml::openElement( 'form', array( 'action' => $wgScript,
350 'id' => 'mw-history-compare' ) ) . "\n";
351 $s .= Xml::hidden( 'title', $this->title->getPrefixedDbKey() ) . "\n";
352
353 $this->buttons = '<div>';
354 if( $wgUser->isAllowed('deletedhistory') ) {
355 $this->buttons .= Xml::element( 'button',
356 array(
357 'type' => 'submit',
358 'name' => 'action',
359 'value' => 'revisiondelete',
360 'style' => 'float: right',
361 ),
362 wfMsg( 'showhideselectedversions' )
363 ) . "\n";
364 }
365 if( $wgEnableHtmlDiff ) {
366 $this->buttons .= Xml::element( 'button',
367 array(
368 'type' => 'submit',
369 'name' => 'htmldiff',
370 'value' => '1',
371 'class' => 'historysubmit',
372 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
373 'title' => wfMsg( 'tooltip-compareselectedversions' ),
374 ),
375 wfMsg( 'visualcomparison')
376 ) . "\n";
377 $this->buttons .= $this->submitButton( wfMsg( 'wikicodecomparison'),
378 array(
379 'class' => 'historysubmit',
380 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
381 'title' => wfMsg( 'tooltip-compareselectedversions' ),
382 )
383 ) . "\n";
384 } else {
385 $this->buttons .= $this->submitButton( wfMsg( 'compareselectedversions'),
386 array(
387 'class' => 'historysubmit',
388 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
389 'title' => wfMsg( 'tooltip-compareselectedversions' ),
390 )
391 ) . "\n";
392 }
393 $this->buttons .= '</div>';
394 $s .= $this->buttons . '<ul id="pagehistory">' . "\n";
395 return $s;
396 }
397
398 function getEndBody() {
399 if( $this->lastRow ) {
400 $latest = $this->counter == 1 && $this->mIsFirst;
401 $firstInList = $this->counter == 1;
402 if( $this->mIsBackwards ) {
403 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
404 if( $this->mOffset == '' ) {
405 $next = null;
406 } else {
407 $next = 'unknown';
408 }
409 } else {
410 # The next row is the past-the-end row
411 $next = $this->mPastTheEndRow;
412 }
413 $s = $this->historyLine( $this->lastRow, $next, $this->counter++,
414 $this->title->getNotificationTimestamp(), $latest, $firstInList );
415 } else {
416 $s = '';
417 }
418 $s .= "</ul>\n";
419 $s .= $this->buttons;
420 $s .= '</form>';
421 return $s;
422 }
423
424 /**
425 * Creates a submit button
426 *
427 * @param array $attributes attributes
428 * @return string HTML output for the submit button
429 */
430 function submitButton($message, $attributes = array() ) {
431 # Disable submit button if history has 1 revision only
432 if( $this->getNumRows() > 1 ) {
433 return Xml::submitButton( $message , $attributes );
434 } else {
435 return '';
436 }
437 }
438
439 /**
440 * Returns a row from the history printout.
441 *
442 * @todo document some more, and maybe clean up the code (some params redundant?)
443 *
444 * @param Row $row The database row corresponding to the previous line.
445 * @param mixed $next The database row corresponding to the next line.
446 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
447 * @param $notificationtimestamp
448 * @param bool $latest Whether this row corresponds to the page's latest revision.
449 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
450 * @return string HTML output for the row
451 */
452 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
453 $latest = false, $firstInList = false )
454 {
455 global $wgUser, $wgLang;
456 $rev = new Revision( $row );
457 $rev->setTitle( $this->title );
458
459 $curlink = $this->curLink( $rev, $latest );
460 $lastlink = $this->lastLink( $rev, $next, $counter );
461 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
462 $link = $this->revLink( $rev );
463 $classes = array();
464
465 $s = "($curlink) ($lastlink) $diffButtons";
466
467 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
468 // Don't show useless link to people who cannot hide revisions
469 if( !$rev->getVisibility() && !$wgUser->isAllowed( 'deleterevision' ) ) {
470 $del = Xml::check( 'deleterevisions', false, array('class' => 'mw-revdelundel-hidden') );
471 // If revision was hidden from sysops
472 } else if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
473 $del = Xml::check( 'deleterevisions', false, array('disabled' => 'disabled') );
474 // Otherwise, show the link...
475 } else {
476 $id = $rev->getId();
477 $del = Xml::check( 'showhiderevisions', false, array( 'name' => "ids[$id]" ) );
478 }
479 $s .= " $del ";
480 }
481
482 $s .= " $link";
483 $s .= " <span class='history-user'>" . $this->getSkin()->revUserTools( $rev, true ) . "</span>";
484
485 if( $rev->isMinor() ) {
486 $s .= ' ' . ChangesList::flag( 'minor' );
487 }
488
489 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision::DELETED_TEXT ) ) {
490 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
491 }
492
493 $s .= $this->getSkin()->revComment( $rev, false, true );
494
495 if( $notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp) ) {
496 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
497 }
498
499 $tools = array();
500
501 # Rollback and undo links
502 if( !is_null( $next ) && is_object( $next ) ) {
503 if( $latest && $this->title->userCan( 'rollback' ) && $this->title->userCan( 'edit' ) ) {
504 $tools[] = '<span class="mw-rollback-link">'.
505 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
506 }
507
508 if( $this->title->quickUserCan( 'edit' )
509 && !$rev->isDeleted( Revision::DELETED_TEXT )
510 && !$next->rev_deleted & Revision::DELETED_TEXT )
511 {
512 # Create undo tooltip for the first (=latest) line only
513 $undoTooltip = $latest
514 ? array( 'title' => wfMsg( 'tooltip-undo' ) )
515 : array();
516 $undolink = $this->getSkin()->link(
517 $this->title,
518 wfMsgHtml( 'editundo' ),
519 $undoTooltip,
520 array(
521 'action' => 'edit',
522 'undoafter' => $next->rev_id,
523 'undo' => $rev->getId()
524 ),
525 array( 'known', 'noclasses' )
526 );
527 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
528 }
529 }
530
531 if( $tools ) {
532 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
533 }
534
535 # Tags
536 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'history' );
537 $classes = array_merge( $classes, $newClasses );
538 $s .= " $tagSummary";
539
540 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
541
542 $attribs = array();
543 if ( $classes ) {
544 $attribs['class'] = implode( ' ', $classes );
545 }
546
547 return Xml::tags( 'li', $attribs, $s ) . "\n";
548 }
549
550 /**
551 * Create a link to view this revision of the page
552 * @param Revision $rev
553 * @returns string
554 */
555 function revLink( $rev ) {
556 global $wgLang;
557 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
558 $date = htmlspecialchars( $date );
559 if( !$rev->isDeleted( Revision::DELETED_TEXT ) ) {
560 $link = $this->getSkin()->link(
561 $this->title,
562 $date,
563 array(),
564 array( 'oldid' => $rev->getId() ),
565 array( 'known', 'noclasses' )
566 );
567 } else {
568 $link = "<span class=\"history-deleted\">$date</span>";
569 }
570 return $link;
571 }
572
573 /**
574 * Create a diff-to-current link for this revision for this page
575 * @param Revision $rev
576 * @param Bool $latest, this is the latest revision of the page?
577 * @returns string
578 */
579 function curLink( $rev, $latest ) {
580 $cur = $this->historyPage->message['cur'];
581 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
582 return $cur;
583 } else {
584 return $this->getSkin()->link(
585 $this->title,
586 $cur,
587 array(),
588 array(
589 'diff' => $this->title->getLatestRevID(),
590 'oldid' => $rev->getId()
591 ),
592 array( 'known', 'noclasses' )
593 );
594 }
595 }
596
597 /**
598 * Create a diff-to-previous link for this revision for this page.
599 * @param Revision $prevRev, the previous revision
600 * @param mixed $next, the newer revision
601 * @param int $counter, what row on the history list this is
602 * @returns string
603 */
604 function lastLink( $prevRev, $next, $counter ) {
605 $last = $this->historyPage->message['last'];
606 # $next may either be a Row, null, or "unkown"
607 $nextRev = is_object($next) ? new Revision( $next ) : $next;
608 if( is_null($next) ) {
609 # Probably no next row
610 return $last;
611 } elseif( $next === 'unknown' ) {
612 # Next row probably exists but is unknown, use an oldid=prev link
613 return $this->getSkin()->link(
614 $this->title,
615 $last,
616 array(),
617 array(
618 'diff' => $prevRev->getId(),
619 'oldid' => 'prev'
620 ),
621 array( 'known', 'noclasses' )
622 );
623 } elseif( !$prevRev->userCan(Revision::DELETED_TEXT) || !$nextRev->userCan(Revision::DELETED_TEXT) ) {
624 return $last;
625 } else {
626 return $this->getSkin()->link(
627 $this->title,
628 $last,
629 array(),
630 array(
631 'diff' => $prevRev->getId(),
632 'oldid' => $next->rev_id
633 ),
634 array( 'known', 'noclasses' )
635 );
636 }
637 }
638
639 /**
640 * Create radio buttons for page history
641 *
642 * @param object $rev Revision
643 * @param bool $firstInList Is this version the first one?
644 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
645 * @return string HTML output for the radio buttons
646 */
647 function diffButtons( $rev, $firstInList, $counter ) {
648 if( $this->getNumRows() > 1 ) {
649 $id = $rev->getId();
650 $radio = array( 'type' => 'radio', 'value' => $id );
651 /** @todo: move title texts to javascript */
652 if( $firstInList ) {
653 $first = Xml::element( 'input',
654 array_merge( $radio, array(
655 'style' => 'visibility:hidden',
656 'name' => 'oldid',
657 'id' => 'mw-oldid-null' ) )
658 );
659 $checkmark = array( 'checked' => 'checked' );
660 } else {
661 # Check visibility of old revisions
662 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
663 $radio['disabled'] = 'disabled';
664 $checkmark = array(); // We will check the next possible one
665 } else if( $counter == 2 || !$this->oldIdChecked ) {
666 $checkmark = array( 'checked' => 'checked' );
667 $this->oldIdChecked = $id;
668 } else {
669 $checkmark = array();
670 }
671 $first = Xml::element( 'input',
672 array_merge( $radio, $checkmark, array(
673 'name' => 'oldid',
674 'id' => "mw-oldid-$id" ) ) );
675 $checkmark = array();
676 }
677 $second = Xml::element( 'input',
678 array_merge( $radio, $checkmark, array(
679 'name' => 'diff',
680 'id' => "mw-diff-$id" ) ) );
681 return $first . $second;
682 } else {
683 return '';
684 }
685 }
686 }
687
688 /**
689 * Backwards-compatibility aliases
690 */
691 class PageHistory extends HistoryPage {}
692 class PageHistoryPager extends HistoryPager {}