0575fcb5442d917b053bd97f40c38b1d5df4656d
[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 #getComment is safe, but this is better formatted
223 if( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
224 $s .= " <span class=\"history-deleted\"><span class=\"comment\">" .
225 wfMsgHtml( 'rev-deleted-comment' ) . "</span></span>";
226 } else {
227 $s .= $this->mSkin->revComment( $rev );
228 }
229
230 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
231 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
232 }
233 #add blurb about text having been deleted
234 if( $row->rev_deleted & Revision::DELETED_TEXT ) {
235 $s .= ' ' . wfMsgHtml( 'deletedrev' );
236 }
237 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
238 $s .= ' '.$this->mSkin->generateRollback( $rev );
239 }
240 $s .= "</li>\n";
241
242 return $s;
243 }
244
245 /** @todo document */
246 function revLink( $rev ) {
247 global $wgLang;
248 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
249 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
250 $link = $this->mSkin->makeKnownLinkObj(
251 $this->mTitle, $date, "oldid=" . $rev->getId() );
252 } else {
253 $link = $date;
254 }
255 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
256 return '<span class="history-deleted">' . $link . '</span>';
257 }
258 return $link;
259 }
260
261 /** @todo document */
262 function curLink( $rev, $latest ) {
263 $cur = wfMsgExt( 'cur', array( 'escape') );
264 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
265 return $cur;
266 } else {
267 return $this->mSkin->makeKnownLinkObj(
268 $this->mTitle, $cur,
269 'diff=' . $this->getLatestID() .
270 "&oldid=" . $rev->getId() );
271 }
272 }
273
274 /** @todo document */
275 function lastLink( $rev, $next, $counter ) {
276 $last = wfMsgExt( 'last', array( 'escape' ) );
277 if ( is_null( $next ) ) {
278 # Probably no next row
279 return $last;
280 } elseif ( $next === 'unknown' ) {
281 # Next row probably exists but is unknown, use an oldid=prev link
282 return $this->mSkin->makeKnownLinkObj(
283 $this->mTitle,
284 $last,
285 "diff=" . $rev->getId() . "&oldid=prev" );
286 } elseif( !$rev->userCan( Revision::DELETED_TEXT ) ) {
287 return $last;
288 } else {
289 return $this->mSkin->makeKnownLinkObj(
290 $this->mTitle,
291 $last,
292 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
293 /*,
294 '',
295 '',
296 "tabindex={$counter}"*/ );
297 }
298 }
299
300 /** @todo document */
301 function diffButtons( $rev, $firstInList, $counter ) {
302 if( $this->linesonpage > 1) {
303 $radio = array(
304 'type' => 'radio',
305 'value' => $rev->getId(),
306 # do we really need to flood this on every item?
307 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
308 );
309
310 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
311 $radio['disabled'] = 'disabled';
312 }
313
314 /** @todo: move title texts to javascript */
315 if ( $firstInList ) {
316 $first = wfElement( 'input', array_merge(
317 $radio,
318 array(
319 'style' => 'visibility:hidden',
320 'name' => 'oldid' ) ) );
321 $checkmark = array( 'checked' => 'checked' );
322 } else {
323 if( $counter == 2 ) {
324 $checkmark = array( 'checked' => 'checked' );
325 } else {
326 $checkmark = array();
327 }
328 $first = wfElement( 'input', array_merge(
329 $radio,
330 $checkmark,
331 array( 'name' => 'oldid' ) ) );
332 $checkmark = array();
333 }
334 $second = wfElement( 'input', array_merge(
335 $radio,
336 $checkmark,
337 array( 'name' => 'diff' ) ) );
338 return $first . $second;
339 } else {
340 return '';
341 }
342 }
343
344 /** @todo document */
345 function getLatestId() {
346 if( is_null( $this->mLatestId ) ) {
347 $id = $this->mTitle->getArticleID();
348 $db = wfGetDB(DB_SLAVE);
349 $this->mLatestId = $db->selectField( 'page',
350 "page_latest",
351 array( 'page_id' => $id ),
352 'PageHistory::getLatestID' );
353 }
354 return $this->mLatestId;
355 }
356
357 /**
358 * Fetch an array of revisions, specified by a given limit, offset and
359 * direction. This is now only used by the feeds. It was previously
360 * used by the main UI but that's now handled by the pager.
361 */
362 function fetchRevisions($limit, $offset, $direction) {
363 $fname = 'PageHistory::fetchRevisions';
364
365 $dbr = wfGetDB( DB_SLAVE );
366
367 if ($direction == PageHistory::DIR_PREV)
368 list($dirs, $oper) = array("ASC", ">=");
369 else /* $direction == PageHistory::DIR_NEXT */
370 list($dirs, $oper) = array("DESC", "<=");
371
372 if ($offset)
373 $offsets = array("rev_timestamp $oper '$offset'");
374 else
375 $offsets = array();
376
377 $page_id = $this->mTitle->getArticleID();
378
379 $res = $dbr->select(
380 'revision',
381 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
382 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
383 array_merge(array("rev_page=$page_id"), $offsets),
384 $fname,
385 array('ORDER BY' => "rev_timestamp $dirs",
386 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
387 );
388
389 $result = array();
390 while (($obj = $dbr->fetchObject($res)) != NULL)
391 $result[] = $obj;
392
393 return $result;
394 }
395
396 /** @todo document */
397 function getNotificationTimestamp() {
398 global $wgUser, $wgShowUpdatedMarker;
399 $fname = 'PageHistory::getNotficationTimestamp';
400
401 if ($this->mNotificationTimestamp !== NULL)
402 return $this->mNotificationTimestamp;
403
404 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
405 return $this->mNotificationTimestamp = false;
406
407 $dbr = wfGetDB(DB_SLAVE);
408
409 $this->mNotificationTimestamp = $dbr->selectField(
410 'watchlist',
411 'wl_notificationtimestamp',
412 array( 'wl_namespace' => $this->mTitle->getNamespace(),
413 'wl_title' => $this->mTitle->getDBkey(),
414 'wl_user' => $wgUser->getID()
415 ),
416 $fname);
417
418 // Don't use the special value reserved for telling whether the field is filled
419 if ( is_null( $this->mNotificationTimestamp ) ) {
420 $this->mNotificationTimestamp = false;
421 }
422
423 return $this->mNotificationTimestamp;
424 }
425
426 /**
427 * Output a subscription feed listing recent edits to this page.
428 * @param string $type
429 */
430 function feed( $type ) {
431 require_once 'SpecialRecentchanges.php';
432
433 global $wgFeedClasses;
434 if( !isset( $wgFeedClasses[$type] ) ) {
435 global $wgOut;
436 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
437 return;
438 }
439
440 $feed = new $wgFeedClasses[$type](
441 $this->mTitle->getPrefixedText() . ' - ' .
442 wfMsgForContent( 'history-feed-title' ),
443 wfMsgForContent( 'history-feed-description' ),
444 $this->mTitle->getFullUrl( 'action=history' ) );
445
446 $items = $this->fetchRevisions(10, 0, PageHistory::DIR_NEXT);
447 $feed->outHeader();
448 if( $items ) {
449 foreach( $items as $row ) {
450 $feed->outItem( $this->feedItem( $row ) );
451 }
452 } else {
453 $feed->outItem( $this->feedEmpty() );
454 }
455 $feed->outFooter();
456 }
457
458 function feedEmpty() {
459 global $wgOut;
460 return new FeedItem(
461 wfMsgForContent( 'nohistory' ),
462 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
463 $this->mTitle->getFullUrl(),
464 wfTimestamp( TS_MW ),
465 '',
466 $this->mTitle->getTalkPage()->getFullUrl() );
467 }
468
469 /**
470 * Generate a FeedItem object from a given revision table row
471 * Borrows Recent Changes' feed generation functions for formatting;
472 * includes a diff to the previous revision (if any).
473 *
474 * @param $row
475 * @return FeedItem
476 */
477 function feedItem( $row ) {
478 $rev = new Revision( $row );
479 $rev->setTitle( $this->mTitle );
480 $text = rcFormatDiffRow( $this->mTitle,
481 $this->mTitle->getPreviousRevisionID( $rev->getId() ),
482 $rev->getId(),
483 $rev->getTimestamp(),
484 $rev->getComment() );
485
486 if( $rev->getComment() == '' ) {
487 global $wgContLang;
488 $title = wfMsgForContent( 'history-feed-item-nocomment',
489 $rev->getUserText(),
490 $wgContLang->timeanddate( $rev->getTimestamp() ) );
491 } else {
492 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
493 }
494
495 return new FeedItem(
496 $title,
497 $text,
498 $this->mTitle->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
499 $rev->getTimestamp(),
500 $rev->getUserText(),
501 $this->mTitle->getTalkPage()->getFullUrl() );
502 }
503
504 /**
505 * Quickie hack... strip out wikilinks to more legible form from the comment.
506 */
507 function stripComment( $text ) {
508 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
509 }
510 }
511
512
513 class PageHistoryPager extends ReverseChronologicalPager {
514 public $mLastRow = false, $mPageHistory;
515
516 function __construct( $pageHistory ) {
517 parent::__construct();
518 $this->mPageHistory = $pageHistory;
519 }
520
521 function getQueryInfo() {
522 return array(
523 'tables' => 'revision',
524 'fields' => array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
525 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
526 'conds' => array('rev_page' => $this->mPageHistory->mTitle->getArticleID() ),
527 'options' => array( 'USE INDEX' => 'page_timestamp' )
528 );
529 }
530
531 function getIndexField() {
532 return 'rev_timestamp';
533 }
534
535 function formatRow( $row ) {
536 if ( $this->mLastRow ) {
537 $latest = $this->mCounter == 1 && $this->mOffset == '';
538 $firstInList = $this->mCounter == 1;
539 $s = $this->mPageHistory->historyLine( $this->mLastRow, $row, $this->mCounter++,
540 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
541 } else {
542 $s = '';
543 }
544 $this->mLastRow = $row;
545 return $s;
546 }
547
548 function getStartBody() {
549 $this->mLastRow = false;
550 $this->mCounter = 1;
551 return '';
552 }
553
554 function getEndBody() {
555 if ( $this->mLastRow ) {
556 $latest = $this->mCounter == 1 && $this->mOffset == 0;
557 $firstInList = $this->mCounter == 1;
558 if ( $this->mIsBackwards ) {
559 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
560 if ( $this->mOffset == '' ) {
561 $next = null;
562 } else {
563 $next = 'unknown';
564 }
565 } else {
566 # The next row is the past-the-end row
567 $next = $this->mPastTheEndRow;
568 }
569 $s = $this->mPageHistory->historyLine( $this->mLastRow, $next, $this->mCounter++,
570 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
571 } else {
572 $s = '';
573 }
574 return $s;
575 }
576 }
577
578 ?>