Revert Special:Log to r20745 with non-ugly form
[lhc/web/wiklou.git] / includes / PageHistory.php
1 <?php
2 /**
3 * Page history
4 *
5 * Split off from Article.php and Skin.php, 2003-12-22
6 */
7
8 /**
9 * This class handles printing the history page for an article. In order to
10 * be efficient, it uses timestamps rather than offsets for paging, to avoid
11 * costly LIMIT,offset queries.
12 *
13 * Construct it by passing in an Article, and call $h->history() to print the
14 * history.
15 *
16 */
17
18 class PageHistory {
19 const DIR_PREV = 0;
20 const DIR_NEXT = 1;
21
22 var $mArticle, $mTitle, $mSkin;
23 var $lastdate;
24 var $linesonpage;
25 var $mNotificationTimestamp;
26 var $mLatestId = null;
27
28 /**
29 * Construct a new PageHistory.
30 *
31 * @param Article $article
32 * @returns nothing
33 */
34 function __construct($article) {
35 global $wgUser;
36
37 $this->mArticle =& $article;
38 $this->mTitle =& $article->mTitle;
39 $this->mNotificationTimestamp = NULL;
40 $this->mSkin = $wgUser->getSkin();
41 }
42
43 /**
44 * Print the history page for an article.
45 *
46 * @returns nothing
47 */
48 function history() {
49 global $wgOut, $wgRequest, $wgTitle;
50
51 /*
52 * Allow client caching.
53 */
54
55 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) )
56 /* Client cache fresh and headers sent, nothing more to do. */
57 return;
58
59 $fname = 'PageHistory::history';
60 wfProfileIn( $fname );
61
62 /*
63 * Setup page variables.
64 */
65 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
66 $wgOut->setArticleFlag( false );
67 $wgOut->setArticleRelated( true );
68 $wgOut->setRobotpolicy( 'noindex,nofollow' );
69 $wgOut->setSyndicated( true );
70
71 $logPage = SpecialPage::getTitleFor( 'Log' );
72 $logLink = $this->mSkin->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle->getPrefixedUrl() );
73
74 $subtitle = wfMsgHtml( 'revhistory' ) . '<br />' . $logLink;
75 $wgOut->setSubtitle( $subtitle );
76
77 $feedType = $wgRequest->getVal( 'feed' );
78 if( $feedType ) {
79 wfProfileOut( $fname );
80 return $this->feed( $feedType );
81 }
82
83 /*
84 * Fail if article doesn't exist.
85 */
86 if( !$this->mTitle->exists() ) {
87 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
88 wfProfileOut( $fname );
89 return;
90 }
91
92
93 /*
94 * "go=first" means to jump to the last (earliest) history page.
95 * This is deprecated, it no longer appears in the user interface
96 */
97 if ( $wgRequest->getText("go") == 'first' ) {
98 $limit = $wgRequest->getInt( 'limit', 50 );
99 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
100 return;
101 }
102
103 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle ) );
104
105 /**
106 * Do the list
107 */
108 $pager = new PageHistoryPager( $this );
109 $this->linesonpage = $pager->getNumRows();
110 $wgOut->addHTML(
111 $pager->getNavigationBar() .
112 $this->beginHistoryList() .
113 $pager->getBody() .
114 $this->endHistoryList() .
115 $pager->getNavigationBar()
116 );
117 wfProfileOut( $fname );
118 }
119
120 /** @todo document */
121 function beginHistoryList() {
122 global $wgTitle;
123 $this->lastdate = '';
124 $s = wfMsgExt( 'histlegend', array( 'parse') );
125 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
126 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
127
128 // The following line is SUPPOSED to have double-quotes around the
129 // $prefixedkey variable, because htmlspecialchars() doesn't escape
130 // single-quotes.
131 //
132 // On at least two occasions people have changed it to single-quotes,
133 // which creates invalid HTML and incorrect display of the resulting
134 // link.
135 //
136 // Please do not break this a third time. Thank you for your kind
137 // consideration and cooperation.
138 //
139 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
140
141 $s .= $this->submitButton();
142 $s .= '<ul id="pagehistory">' . "\n";
143 return $s;
144 }
145
146 /** @todo document */
147 function endHistoryList() {
148 $s = '</ul>';
149 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
150 $s .= '</form>';
151 return $s;
152 }
153
154 /** @todo document */
155 function submitButton( $bits = array() ) {
156 return ( $this->linesonpage > 0 )
157 ? wfElement( 'input', array_merge( $bits,
158 array(
159 'class' => 'historysubmit',
160 'type' => 'submit',
161 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
162 'title' => wfMsg( 'tooltip-compareselectedversions' ).' ['.wfMsg( 'accesskey-compareselectedversions' ).']',
163 'value' => wfMsg( 'compareselectedversions' ),
164 ) ) )
165 : '';
166 }
167
168 /**
169 * Returns a row from the history printout.
170 *
171 * @todo document some more, and maybe clean up the code (some params redundant?)
172 *
173 * @param object $row The database row corresponding to the line (or is it the previous line?).
174 * @param object $next The database row corresponding to the next line (or is it this one?).
175 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
176 * @param $notificationtimestamp
177 * @param bool $latest Whether this row corresponds to the page's latest revision.
178 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
179 * @return string HTML output for the row
180 */
181 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
182 global $wgUser, $wgLang;
183 $rev = new Revision( $row );
184 $rev->setTitle( $this->mTitle );
185
186 $s = '<li>';
187 $curlink = $this->curLink( $rev, $latest );
188 $lastlink = $this->lastLink( $rev, $next, $counter );
189 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
190 $link = $this->revLink( $rev );
191
192 $user = $this->mSkin->userLink( $rev->getUser(), $rev->getUserText() )
193 . $this->mSkin->userToolLinks( $rev->getUser(), $rev->getUserText() );
194
195 $s .= "($curlink) ($lastlink) $arbitrary";
196
197 if( $wgUser->isAllowed( 'deleterevision' ) ) {
198 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
199 if( $firstInList ) {
200 // We don't currently handle well changing the top revision's settings
201 $del = wfMsgHtml( 'rev-delundel' );
202 } else if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
203 // If revision was hidden from sysops
204 $del = wfMsgHtml( 'rev-delundel' );
205 } else {
206 $del = $this->mSkin->makeKnownLinkObj( $revdel,
207 wfMsg( 'rev-delundel' ),
208 'target=' . urlencode( $this->mTitle->getPrefixedDbkey() ) .
209 '&oldid=' . urlencode( $rev->getId() ) );
210 }
211 $s .= " (<small>$del</small>) ";
212 }
213
214 $s .= " $link";
215 #getUser is safe, but this avoids making the invalid untargeted contribs links
216 if( $row->rev_deleted & Revision::DELETED_USER ) {
217 $user = '<span class="history-deleted">' . wfMsg('rev-deleted-user') . '</span>';
218 }
219 $s .= " <span class='history-user'>$user</span>";
220
221 if( $row->rev_minor_edit ) {
222 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
223 }
224
225 if (!is_null($size = $rev->getSize())) {
226 if ($size == 0)
227 $stxt = wfMsgHtml('historyempty');
228 else
229 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
230 $s .= " <span class=\"history-size\">$stxt</span>";
231 }
232
233 #getComment is safe, but this is better formatted
234 if( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
235 $s .= " <span class=\"history-deleted\"><span class=\"comment\">" .
236 wfMsgHtml( 'rev-deleted-comment' ) . "</span></span>";
237 } else {
238 $s .= $this->mSkin->revComment( $rev );
239 }
240
241 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
242 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
243 }
244 #add blurb about text having been deleted
245 if( $row->rev_deleted & Revision::DELETED_TEXT ) {
246 $s .= ' ' . wfMsgHtml( 'deletedrev' );
247 }
248 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
249 $s .= ' '.$this->mSkin->generateRollback( $rev );
250 }
251
252 wfRunHooks( 'PageHistoryLineEnding', array( &$row , &$s ) );
253
254 $s .= "</li>\n";
255
256 return $s;
257 }
258
259 /** @todo document */
260 function revLink( $rev ) {
261 global $wgLang;
262 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
263 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
264 $link = $this->mSkin->makeKnownLinkObj(
265 $this->mTitle, $date, "oldid=" . $rev->getId() );
266 } else {
267 $link = $date;
268 }
269 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
270 return '<span class="history-deleted">' . $link . '</span>';
271 }
272 return $link;
273 }
274
275 /** @todo document */
276 function curLink( $rev, $latest ) {
277 $cur = wfMsgExt( 'cur', array( 'escape') );
278 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
279 return $cur;
280 } else {
281 return $this->mSkin->makeKnownLinkObj(
282 $this->mTitle, $cur,
283 'diff=' . $this->getLatestID() .
284 "&oldid=" . $rev->getId() );
285 }
286 }
287
288 /** @todo document */
289 function lastLink( $rev, $next, $counter ) {
290 $last = wfMsgExt( 'last', array( 'escape' ) );
291 if ( is_null( $next ) ) {
292 # Probably no next row
293 return $last;
294 } elseif ( $next === 'unknown' ) {
295 # Next row probably exists but is unknown, use an oldid=prev link
296 return $this->mSkin->makeKnownLinkObj(
297 $this->mTitle,
298 $last,
299 "diff=" . $rev->getId() . "&oldid=prev" );
300 } elseif( !$rev->userCan( Revision::DELETED_TEXT ) ) {
301 return $last;
302 } else {
303 return $this->mSkin->makeKnownLinkObj(
304 $this->mTitle,
305 $last,
306 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
307 /*,
308 '',
309 '',
310 "tabindex={$counter}"*/ );
311 }
312 }
313
314 /** @todo document */
315 function diffButtons( $rev, $firstInList, $counter ) {
316 if( $this->linesonpage > 1) {
317 $radio = array(
318 'type' => 'radio',
319 'value' => $rev->getId(),
320 # do we really need to flood this on every item?
321 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
322 );
323
324 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
325 $radio['disabled'] = 'disabled';
326 }
327
328 /** @todo: move title texts to javascript */
329 if ( $firstInList ) {
330 $first = wfElement( 'input', array_merge(
331 $radio,
332 array(
333 'style' => 'visibility:hidden',
334 'name' => 'oldid' ) ) );
335 $checkmark = array( 'checked' => 'checked' );
336 } else {
337 if( $counter == 2 ) {
338 $checkmark = array( 'checked' => 'checked' );
339 } else {
340 $checkmark = array();
341 }
342 $first = wfElement( 'input', array_merge(
343 $radio,
344 $checkmark,
345 array( 'name' => 'oldid' ) ) );
346 $checkmark = array();
347 }
348 $second = wfElement( 'input', array_merge(
349 $radio,
350 $checkmark,
351 array( 'name' => 'diff' ) ) );
352 return $first . $second;
353 } else {
354 return '';
355 }
356 }
357
358 /** @todo document */
359 function getLatestId() {
360 if( is_null( $this->mLatestId ) ) {
361 $id = $this->mTitle->getArticleID();
362 $db = wfGetDB(DB_SLAVE);
363 $this->mLatestId = $db->selectField( 'page',
364 "page_latest",
365 array( 'page_id' => $id ),
366 'PageHistory::getLatestID' );
367 }
368 return $this->mLatestId;
369 }
370
371 /**
372 * Fetch an array of revisions, specified by a given limit, offset and
373 * direction. This is now only used by the feeds. It was previously
374 * used by the main UI but that's now handled by the pager.
375 */
376 function fetchRevisions($limit, $offset, $direction) {
377 $fname = 'PageHistory::fetchRevisions';
378
379 $dbr = wfGetDB( DB_SLAVE );
380
381 if ($direction == PageHistory::DIR_PREV)
382 list($dirs, $oper) = array("ASC", ">=");
383 else /* $direction == PageHistory::DIR_NEXT */
384 list($dirs, $oper) = array("DESC", "<=");
385
386 if ($offset)
387 $offsets = array("rev_timestamp $oper '$offset'");
388 else
389 $offsets = array();
390
391 $page_id = $this->mTitle->getArticleID();
392
393 $res = $dbr->select(
394 'revision',
395 Revision::selectFields(),
396 array_merge(array("rev_page=$page_id"), $offsets),
397 $fname,
398 array('ORDER BY' => "rev_timestamp $dirs",
399 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
400 );
401
402 $result = array();
403 while (($obj = $dbr->fetchObject($res)) != NULL)
404 $result[] = $obj;
405
406 return $result;
407 }
408
409 /** @todo document */
410 function getNotificationTimestamp() {
411 global $wgUser, $wgShowUpdatedMarker;
412 $fname = 'PageHistory::getNotficationTimestamp';
413
414 if ($this->mNotificationTimestamp !== NULL)
415 return $this->mNotificationTimestamp;
416
417 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
418 return $this->mNotificationTimestamp = false;
419
420 $dbr = wfGetDB(DB_SLAVE);
421
422 $this->mNotificationTimestamp = $dbr->selectField(
423 'watchlist',
424 'wl_notificationtimestamp',
425 array( 'wl_namespace' => $this->mTitle->getNamespace(),
426 'wl_title' => $this->mTitle->getDBkey(),
427 'wl_user' => $wgUser->getID()
428 ),
429 $fname);
430
431 // Don't use the special value reserved for telling whether the field is filled
432 if ( is_null( $this->mNotificationTimestamp ) ) {
433 $this->mNotificationTimestamp = false;
434 }
435
436 return $this->mNotificationTimestamp;
437 }
438
439 /**
440 * Output a subscription feed listing recent edits to this page.
441 * @param string $type
442 */
443 function feed( $type ) {
444 require_once 'SpecialRecentchanges.php';
445
446 global $wgFeedClasses;
447 if( !isset( $wgFeedClasses[$type] ) ) {
448 global $wgOut;
449 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
450 return;
451 }
452
453 $feed = new $wgFeedClasses[$type](
454 $this->mTitle->getPrefixedText() . ' - ' .
455 wfMsgForContent( 'history-feed-title' ),
456 wfMsgForContent( 'history-feed-description' ),
457 $this->mTitle->getFullUrl( 'action=history' ) );
458
459 $items = $this->fetchRevisions(10, 0, PageHistory::DIR_NEXT);
460 $feed->outHeader();
461 if( $items ) {
462 foreach( $items as $row ) {
463 $feed->outItem( $this->feedItem( $row ) );
464 }
465 } else {
466 $feed->outItem( $this->feedEmpty() );
467 }
468 $feed->outFooter();
469 }
470
471 function feedEmpty() {
472 global $wgOut;
473 return new FeedItem(
474 wfMsgForContent( 'nohistory' ),
475 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
476 $this->mTitle->getFullUrl(),
477 wfTimestamp( TS_MW ),
478 '',
479 $this->mTitle->getTalkPage()->getFullUrl() );
480 }
481
482 /**
483 * Generate a FeedItem object from a given revision table row
484 * Borrows Recent Changes' feed generation functions for formatting;
485 * includes a diff to the previous revision (if any).
486 *
487 * @param $row
488 * @return FeedItem
489 */
490 function feedItem( $row ) {
491 $rev = new Revision( $row );
492 $rev->setTitle( $this->mTitle );
493 $text = rcFormatDiffRow( $this->mTitle,
494 $this->mTitle->getPreviousRevisionID( $rev->getId() ),
495 $rev->getId(),
496 $rev->getTimestamp(),
497 $rev->getComment() );
498
499 if( $rev->getComment() == '' ) {
500 global $wgContLang;
501 $title = wfMsgForContent( 'history-feed-item-nocomment',
502 $rev->getUserText(),
503 $wgContLang->timeanddate( $rev->getTimestamp() ) );
504 } else {
505 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
506 }
507
508 return new FeedItem(
509 $title,
510 $text,
511 $this->mTitle->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
512 $rev->getTimestamp(),
513 $rev->getUserText(),
514 $this->mTitle->getTalkPage()->getFullUrl() );
515 }
516
517 /**
518 * Quickie hack... strip out wikilinks to more legible form from the comment.
519 */
520 function stripComment( $text ) {
521 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
522 }
523 }
524
525
526 class PageHistoryPager extends ReverseChronologicalPager {
527 public $mLastRow = false, $mPageHistory;
528
529 function __construct( $pageHistory ) {
530 parent::__construct();
531 $this->mPageHistory = $pageHistory;
532 }
533
534 function getQueryInfo() {
535 return array(
536 'tables' => 'revision',
537 'fields' => Revision::selectFields(),
538 'conds' => array('rev_page' => $this->mPageHistory->mTitle->getArticleID() ),
539 'options' => array( 'USE INDEX' => 'page_timestamp' )
540 );
541 }
542
543 function getIndexField() {
544 return 'rev_timestamp';
545 }
546
547 function formatRow( $row ) {
548 if ( $this->mLastRow ) {
549 $latest = $this->mCounter == 1 && $this->mOffset == '';
550 $firstInList = $this->mCounter == 1;
551 $s = $this->mPageHistory->historyLine( $this->mLastRow, $row, $this->mCounter++,
552 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
553 } else {
554 $s = '';
555 }
556 $this->mLastRow = $row;
557 return $s;
558 }
559
560 function getStartBody() {
561 $this->mLastRow = false;
562 $this->mCounter = 1;
563 return '';
564 }
565
566 function getEndBody() {
567 if ( $this->mLastRow ) {
568 $latest = $this->mCounter == 1 && $this->mOffset == 0;
569 $firstInList = $this->mCounter == 1;
570 if ( $this->mIsBackwards ) {
571 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
572 if ( $this->mOffset == '' ) {
573 $next = null;
574 } else {
575 $next = 'unknown';
576 }
577 } else {
578 # The next row is the past-the-end row
579 $next = $this->mPastTheEndRow;
580 }
581 $s = $this->mPageHistory->historyLine( $this->mLastRow, $next, $this->mCounter++,
582 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
583 } else {
584 $s = '';
585 }
586 return $s;
587 }
588 }
589
590 ?>