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