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