Rename constructors to __constructor
[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 * @package MediaWiki
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 * @package MediaWiki
18 */
19
20 class PageHistory {
21 const DIR_PREV = 0;
22 const DIR_NEXT = 1;
23
24 var $mArticle, $mTitle, $mSkin;
25 var $lastdate;
26 var $linesonpage;
27 var $mNotificationTimestamp;
28 var $mLatestId = null;
29
30 /**
31 * Construct a new PageHistory.
32 *
33 * @param Article $article
34 * @returns nothing
35 */
36 function __construct($article) {
37 global $wgUser;
38
39 $this->mArticle =& $article;
40 $this->mTitle =& $article->mTitle;
41 $this->mNotificationTimestamp = NULL;
42 $this->mSkin = $wgUser->getSkin();
43 }
44
45 /**
46 * Print the history page for an article.
47 *
48 * @returns nothing
49 */
50 function history() {
51 global $wgOut, $wgRequest, $wgTitle;
52
53 /*
54 * Allow client caching.
55 */
56
57 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) )
58 /* Client cache fresh and headers sent, nothing more to do. */
59 return;
60
61 $fname = 'PageHistory::history';
62 wfProfileIn( $fname );
63
64 /*
65 * Setup page variables.
66 */
67 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
68 $wgOut->setArticleFlag( false );
69 $wgOut->setArticleRelated( true );
70 $wgOut->setRobotpolicy( 'noindex,nofollow' );
71 $wgOut->setSyndicated( true );
72
73 $logPage = SpecialPage::getTitleFor( 'Log' );
74 $logLink = $this->mSkin->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle->getPrefixedUrl() );
75
76 $subtitle = wfMsgHtml( 'revhistory' ) . '<br />' . $logLink;
77 $wgOut->setSubtitle( $subtitle );
78
79 $feedType = $wgRequest->getVal( 'feed' );
80 if( $feedType ) {
81 wfProfileOut( $fname );
82 return $this->feed( $feedType );
83 }
84
85 /*
86 * Fail if article doesn't exist.
87 */
88 if( !$this->mTitle->exists() ) {
89 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
90 wfProfileOut( $fname );
91 return;
92 }
93
94
95 /*
96 * "go=first" means to jump to the last (earliest) history page.
97 * This is deprecated, it no longer appears in the user interface
98 */
99 if ( $wgRequest->getText("go") == 'first' ) {
100 $limit = $wgRequest->getInt( 'limit', 50 );
101 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
102 return;
103 }
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;
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 {
203 $del = $this->mSkin->makeKnownLinkObj( $revdel,
204 wfMsg( 'rev-delundel' ),
205 'target=' . urlencode( $this->mTitle->getPrefixedDbkey() ) .
206 '&oldid=' . urlencode( $rev->getId() ) );
207 }
208 $s .= "(<small>$del</small>) ";
209 }
210
211 $s .= " $link <span class='history-user'>$user</span>";
212
213 if( $row->rev_minor_edit ) {
214 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
215 }
216
217 $s .= $this->mSkin->revComment( $rev );
218 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
219 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
220 }
221 if( $row->rev_deleted & Revision::DELETED_TEXT ) {
222 $s .= ' ' . wfMsgHtml( 'deletedrev' );
223 }
224 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
225 $s .= ' '.$this->mSkin->generateRollback( $rev );
226 }
227 $s .= "</li>\n";
228
229 return $s;
230 }
231
232 /** @todo document */
233 function revLink( $rev ) {
234 global $wgLang;
235 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
236 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
237 $link = $this->mSkin->makeKnownLinkObj(
238 $this->mTitle, $date, "oldid=" . $rev->getId() );
239 } else {
240 $link = $date;
241 }
242 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
243 return '<span class="history-deleted">' . $link . '</span>';
244 }
245 return $link;
246 }
247
248 /** @todo document */
249 function curLink( $rev, $latest ) {
250 $cur = wfMsgExt( 'cur', array( 'escape') );
251 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
252 return $cur;
253 } else {
254 return $this->mSkin->makeKnownLinkObj(
255 $this->mTitle, $cur,
256 'diff=' . $this->getLatestID() .
257 "&oldid=" . $rev->getId() );
258 }
259 }
260
261 /** @todo document */
262 function lastLink( $rev, $next, $counter ) {
263 $last = wfMsgExt( 'last', array( 'escape' ) );
264 if ( is_null( $next ) ) {
265 # Probably no next row
266 return $last;
267 } elseif ( $next === 'unknown' ) {
268 # Next row probably exists but is unknown, use an oldid=prev link
269 return $this->mSkin->makeKnownLinkObj(
270 $this->mTitle,
271 $last,
272 "diff=" . $rev->getId() . "&oldid=prev" );
273 } elseif( !$rev->userCan( Revision::DELETED_TEXT ) ) {
274 return $last;
275 } else {
276 return $this->mSkin->makeKnownLinkObj(
277 $this->mTitle,
278 $last,
279 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
280 /*,
281 '',
282 '',
283 "tabindex={$counter}"*/ );
284 }
285 }
286
287 /** @todo document */
288 function diffButtons( $rev, $firstInList, $counter ) {
289 if( $this->linesonpage > 1) {
290 $radio = array(
291 'type' => 'radio',
292 'value' => $rev->getId(),
293 # do we really need to flood this on every item?
294 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
295 );
296
297 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
298 $radio['disabled'] = 'disabled';
299 }
300
301 /** @todo: move title texts to javascript */
302 if ( $firstInList ) {
303 $first = wfElement( 'input', array_merge(
304 $radio,
305 array(
306 'style' => 'visibility:hidden',
307 'name' => 'oldid' ) ) );
308 $checkmark = array( 'checked' => 'checked' );
309 } else {
310 if( $counter == 2 ) {
311 $checkmark = array( 'checked' => 'checked' );
312 } else {
313 $checkmark = array();
314 }
315 $first = wfElement( 'input', array_merge(
316 $radio,
317 $checkmark,
318 array( 'name' => 'oldid' ) ) );
319 $checkmark = array();
320 }
321 $second = wfElement( 'input', array_merge(
322 $radio,
323 $checkmark,
324 array( 'name' => 'diff' ) ) );
325 return $first . $second;
326 } else {
327 return '';
328 }
329 }
330
331 /** @todo document */
332 function getLatestId() {
333 if( is_null( $this->mLatestId ) ) {
334 $id = $this->mTitle->getArticleID();
335 $db =& wfGetDB(DB_SLAVE);
336 $this->mLatestId = $db->selectField( 'page',
337 "page_latest",
338 array( 'page_id' => $id ),
339 'PageHistory::getLatestID' );
340 }
341 return $this->mLatestId;
342 }
343
344 /**
345 * Fetch an array of revisions, specified by a given limit, offset and
346 * direction. This is now only used by the feeds. It was previously
347 * used by the main UI but that's now handled by the pager.
348 */
349 function fetchRevisions($limit, $offset, $direction) {
350 $fname = 'PageHistory::fetchRevisions';
351
352 $dbr =& wfGetDB( DB_SLAVE );
353
354 if ($direction == PageHistory::DIR_PREV)
355 list($dirs, $oper) = array("ASC", ">=");
356 else /* $direction == PageHistory::DIR_NEXT */
357 list($dirs, $oper) = array("DESC", "<=");
358
359 if ($offset)
360 $offsets = array("rev_timestamp $oper '$offset'");
361 else
362 $offsets = array();
363
364 $page_id = $this->mTitle->getArticleID();
365
366 $res = $dbr->select(
367 'revision',
368 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
369 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
370 array_merge(array("rev_page=$page_id"), $offsets),
371 $fname,
372 array('ORDER BY' => "rev_timestamp $dirs",
373 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
374 );
375
376 $result = array();
377 while (($obj = $dbr->fetchObject($res)) != NULL)
378 $result[] = $obj;
379
380 return $result;
381 }
382
383 /** @todo document */
384 function getNotificationTimestamp() {
385 global $wgUser, $wgShowUpdatedMarker;
386 $fname = 'PageHistory::getNotficationTimestamp';
387
388 if ($this->mNotificationTimestamp !== NULL)
389 return $this->mNotificationTimestamp;
390
391 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
392 return $this->mNotificationTimestamp = false;
393
394 $dbr =& wfGetDB(DB_SLAVE);
395
396 $this->mNotificationTimestamp = $dbr->selectField(
397 'watchlist',
398 'wl_notificationtimestamp',
399 array( 'wl_namespace' => $this->mTitle->getNamespace(),
400 'wl_title' => $this->mTitle->getDBkey(),
401 'wl_user' => $wgUser->getID()
402 ),
403 $fname);
404
405 // Don't use the special value reserved for telling whether the field is filled
406 if ( is_null( $this->mNotificationTimestamp ) ) {
407 $this->mNotificationTimestamp = false;
408 }
409
410 return $this->mNotificationTimestamp;
411 }
412
413 /**
414 * Output a subscription feed listing recent edits to this page.
415 * @param string $type
416 */
417 function feed( $type ) {
418 require_once 'SpecialRecentchanges.php';
419
420 global $wgFeedClasses;
421 if( !isset( $wgFeedClasses[$type] ) ) {
422 global $wgOut;
423 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
424 return;
425 }
426
427 $feed = new $wgFeedClasses[$type](
428 $this->mTitle->getPrefixedText() . ' - ' .
429 wfMsgForContent( 'history-feed-title' ),
430 wfMsgForContent( 'history-feed-description' ),
431 $this->mTitle->getFullUrl( 'action=history' ) );
432
433 $items = $this->fetchRevisions(10, 0, PageHistory::DIR_NEXT);
434 $feed->outHeader();
435 if( $items ) {
436 foreach( $items as $row ) {
437 $feed->outItem( $this->feedItem( $row ) );
438 }
439 } else {
440 $feed->outItem( $this->feedEmpty() );
441 }
442 $feed->outFooter();
443 }
444
445 function feedEmpty() {
446 global $wgOut;
447 return new FeedItem(
448 wfMsgForContent( 'nohistory' ),
449 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
450 $this->mTitle->getFullUrl(),
451 wfTimestamp( TS_MW ),
452 '',
453 $this->mTitle->getTalkPage()->getFullUrl() );
454 }
455
456 /**
457 * Generate a FeedItem object from a given revision table row
458 * Borrows Recent Changes' feed generation functions for formatting;
459 * includes a diff to the previous revision (if any).
460 *
461 * @param $row
462 * @return FeedItem
463 */
464 function feedItem( $row ) {
465 $rev = new Revision( $row );
466 $rev->setTitle( $this->mTitle );
467 $text = rcFormatDiffRow( $this->mTitle,
468 $this->mTitle->getPreviousRevisionID( $rev->getId() ),
469 $rev->getId(),
470 $rev->getTimestamp(),
471 $rev->getComment() );
472
473 if( $rev->getComment() == '' ) {
474 global $wgContLang;
475 $title = wfMsgForContent( 'history-feed-item-nocomment',
476 $rev->getUserText(),
477 $wgContLang->timeanddate( $rev->getTimestamp() ) );
478 } else {
479 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
480 }
481
482 return new FeedItem(
483 $title,
484 $text,
485 $this->mTitle->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
486 $rev->getTimestamp(),
487 $rev->getUserText(),
488 $this->mTitle->getTalkPage()->getFullUrl() );
489 }
490
491 /**
492 * Quickie hack... strip out wikilinks to more legible form from the comment.
493 */
494 function stripComment( $text ) {
495 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
496 }
497 }
498
499
500 class PageHistoryPager extends ReverseChronologicalPager {
501 public $mLastRow = false, $mPageHistory;
502
503 function __construct( $pageHistory ) {
504 parent::__construct();
505 $this->mPageHistory = $pageHistory;
506 }
507
508 function getQueryInfo() {
509 return array(
510 'tables' => 'revision',
511 'fields' => array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
512 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
513 'conds' => array('rev_page' => $this->mPageHistory->mTitle->getArticleID() ),
514 'options' => array( 'USE INDEX' => 'page_timestamp' )
515 );
516 }
517
518 function getIndexField() {
519 return 'rev_timestamp';
520 }
521
522 function formatRow( $row ) {
523 if ( $this->mLastRow ) {
524 $latest = $this->mCounter == 1 && $this->mOffset == '';
525 $firstInList = $this->mCounter == 1;
526 $s = $this->mPageHistory->historyLine( $this->mLastRow, $row, $this->mCounter++,
527 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
528 } else {
529 $s = '';
530 }
531 $this->mLastRow = $row;
532 return $s;
533 }
534
535 function getStartBody() {
536 $this->mLastRow = false;
537 $this->mCounter = 1;
538 return '';
539 }
540
541 function getEndBody() {
542 if ( $this->mLastRow ) {
543 $latest = $this->mCounter == 1 && $this->mOffset == 0;
544 $firstInList = $this->mCounter == 1;
545 if ( $this->mIsBackwards ) {
546 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
547 if ( $this->mOffset == '' ) {
548 $next = null;
549 } else {
550 $next = 'unknown';
551 }
552 } else {
553 # The next row is the past-the-end row
554 $next = $this->mPastTheEndRow;
555 }
556 $s = $this->mPageHistory->historyLine( $this->mLastRow, $next, $this->mCounter++,
557 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
558 } else {
559 $s = '';
560 }
561 return $s;
562 }
563 }
564
565 ?>