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