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