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