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