5 * Split off from Article.php and Skin.php, 2003-12-22
10 define('DIR_NEXT', 1);
13 * This class handles printing the history page for an article. In order to
14 * be efficient, it uses timestamps rather than offsets for paging, to avoid
15 * costly LIMIT,offset queries.
17 * Construct it by passing in an Article, and call $h->history() to print the
29 $mNotificationTimestamp,
34 * Construct a new PageHistory.
36 * @param Article $article
39 function PageHistory($article) {
42 $this->mArticle
=& $article;
43 $this->mTitle
=& $article->mTitle
;
44 $this->mNotificationTimestamp
= NULL;
45 $this->mSkin
= $wgUser->getSkin();
47 $this->defaultLimit
= 50;
51 * Print the history page for an article.
56 global $wgOut, $wgRequest, $wgTitle;
59 * Allow client caching.
62 if( $wgOut->checkLastModified( $this->mArticle
->getTimestamp() ) )
63 /* Client cache fresh and headers sent, nothing more to do. */
66 $fname = 'PageHistory::history';
67 wfProfileIn( $fname );
70 * Setup page variables.
72 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
73 $wgOut->setSubtitle( wfMsg( 'revhistory' ) );
74 $wgOut->setArticleFlag( false );
75 $wgOut->setArticleRelated( true );
76 $wgOut->setRobotpolicy( 'noindex,nofollow' );
79 * Fail if article doesn't exist.
81 if( !$this->mTitle
->exists() ) {
82 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
83 wfProfileOut( $fname );
87 $dbr =& wfGetDB(DB_SLAVE
);
90 * Extract limit, the number of revisions to show, and
91 * offset, the timestamp to begin at, from the URL.
93 $limit = $wgRequest->getInt('limit', $this->defaultLimit
);
94 $offset = $wgRequest->getText('offset');
96 /* Offset must be an integral. */
97 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
99 # $offset = $dbr->timestamp($offset);
100 $dboffset = $offset === 0 ?
0 : $dbr->timestamp($offset);
102 * "go=last" means to jump to the last history page.
104 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
107 if (($lastid = $this->getLastOffsetForPaging($this->mTitle
->getArticleID(), $limit)) === NULL)
109 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset=".
110 wfTimestamp(TS_MW
, $lastid));
116 if (!is_null($gourl)) {
117 $wgOut->redirect($gourl);
125 * If the user clicked "previous", we retrieve the revisions backwards,
126 * then reverse them. This is to avoid needing to know the timestamp of
127 * previous revisions when generating the URL.
129 $direction = $this->getDirection();
130 $revisions = $this->fetchRevisions($limit, $dboffset, $direction);
131 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
134 * We fetch one more revision than needed to get the timestamp of the
135 * one after this page (and to know if it exists).
137 * linesonpage stores the actual number of lines.
139 if (count($revisions) < $limit +
1)
140 $this->linesonpage
= count($revisions);
142 $this->linesonpage
= count($revisions) - 1;
144 /* Un-reverse revisions */
145 if ($direction == DIR_PREV
)
146 $revisions = array_reverse($revisions);
149 * Print the top navbar.
152 $s .= $this->beginHistoryList();
156 * Print each revision, excluding the one-past-the-end, if any.
158 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
159 $latest = !$i && $offset == 0;
161 $next = isset( $revisions[$i +
1] ) ?
$revisions[$i +
1 ] : null;
162 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $latest, $firstInList);
169 $s .= $this->endHistoryList();
172 $wgOut->addHTML( $s );
173 wfProfileOut( $fname );
176 /** @todo document */
177 function beginHistoryList() {
179 $this->lastdate
= '';
180 $s = wfMsgExt( 'histlegend', array( 'parse') );
181 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
182 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
184 // The following line is SUPPOSED to have double-quotes around the
185 // $prefixedkey variable, because htmlspecialchars() doesn't escape
188 // On at least two occasions people have changed it to single-quotes,
189 // which creates invalid HTML and incorrect display of the resulting
192 // Please do not break this a third time. Thank you for your kind
193 // consideration and cooperation.
195 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
197 $s .= $this->submitButton();
198 $s .= '<ul id="pagehistory">' . "\n";
202 /** @todo document */
203 function endHistoryList() {
205 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
210 /** @todo document */
211 function submitButton( $bits = array() ) {
212 return ( $this->linesonpage
> 0 )
213 ?
wfElement( 'input', array_merge( $bits,
215 'class' => 'historysubmit',
217 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
218 'title' => wfMsg( 'tooltip-compareselectedversions' ),
219 'value' => wfMsg( 'compareselectedversions' ),
224 /** @todo document */
225 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
227 $rev = new Revision( $row );
230 $curlink = $this->curLink( $rev, $latest );
231 $lastlink = $this->lastLink( $rev, $next, $counter );
232 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
233 $link = $this->revLink( $rev );
234 $user = $this->mSkin
->revUserLink( $rev );
236 $s .= "($curlink) ($lastlink) $arbitrary";
238 if( $wgUser->isAllowed( 'deleterevision' ) ) {
239 $revdel = Title
::makeTitle( NS_SPECIAL
, 'Revisiondelete' );
241 // We don't currently handle well changing the top revision's settings
242 $del = wfMsgHtml( 'rev-delundel' );
244 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
245 wfMsg( 'rev-delundel' ),
246 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
247 '&oldid=' . urlencode( $rev->getId() ) );
249 $s .= "(<small>$del</small>) ";
252 $s .= " $link <span class='history-user'>$user</span>";
254 if( $row->rev_minor_edit
) {
255 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
258 $s .= $this->mSkin
->revComment( $rev );
259 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
260 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
262 if( $row->rev_deleted
& MW_REV_DELETED_TEXT
) {
263 $s .= ' ' . wfMsgHtml( 'deletedrev' );
270 /** @todo document */
271 function revLink( $rev ) {
273 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
274 if( $rev->userCan( MW_REV_DELETED_TEXT
) ) {
275 $link = $this->mSkin
->makeKnownLinkObj(
276 $this->mTitle
, $date, "oldid=" . $rev->getId() );
280 if( $rev->isDeleted( MW_REV_DELETED_TEXT
) ) {
281 return '<span class="history-deleted">' . $link . '</span>';
286 /** @todo document */
287 function curLink( $rev, $latest ) {
288 $cur = wfMsgExt( 'cur', array( 'escape') );
289 if( $latest ||
!$rev->userCan( MW_REV_DELETED_TEXT
) ) {
292 return $this->mSkin
->makeKnownLinkObj(
294 'diff=' . $this->getLatestID() .
295 "&oldid=" . $rev->getId() );
299 /** @todo document */
300 function lastLink( $rev, $next, $counter ) {
301 $last = wfMsgExt( 'last', array( 'escape' ) );
302 if( is_null( $next ) ) {
303 if( $rev->getTimestamp() == $this->getEarliestOffset() ) {
306 // Cut off by paging; there are more behind us...
307 return $this->mSkin
->makeKnownLinkObj(
310 "diff=" . $rev->getId() . "&oldid=prev" );
312 } elseif( !$rev->userCan( MW_REV_DELETED_TEXT
) ) {
315 return $this->mSkin
->makeKnownLinkObj(
318 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
322 "tabindex={$counter}"*/ );
326 /** @todo document */
327 function diffButtons( $rev, $firstInList, $counter ) {
328 if( $this->linesonpage
> 1) {
331 'value' => $rev->getId(),
332 # do we really need to flood this on every item?
333 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
336 if( !$rev->userCan( MW_REV_DELETED_TEXT
) ) {
337 $radio['disabled'] = 'disabled';
340 /** @todo: move title texts to javascript */
341 if ( $firstInList ) {
342 $first = wfElement( 'input', array_merge(
345 'style' => 'visibility:hidden',
346 'name' => 'oldid' ) ) );
347 $checkmark = array( 'checked' => 'checked' );
349 if( $counter == 2 ) {
350 $checkmark = array( 'checked' => 'checked' );
352 $checkmark = array();
354 $first = wfElement( 'input', array_merge(
357 array( 'name' => 'oldid' ) ) );
358 $checkmark = array();
360 $second = wfElement( 'input', array_merge(
363 array( 'name' => 'diff' ) ) );
364 return $first . $second;
370 /** @todo document */
371 function getLatestOffset( $id = null ) {
372 if ( $id === null) $id = $this->mTitle
->getArticleID();
373 return $this->getExtremeOffset( $id, 'max' );
376 /** @todo document */
377 function getEarliestOffset( $id = null ) {
378 if ( $id === null) $id = $this->mTitle
->getArticleID();
379 return $this->getExtremeOffset( $id, 'min' );
382 /** @todo document */
383 function getExtremeOffset( $id, $func ) {
384 $db =& wfGetDB(DB_SLAVE
);
385 return $db->selectField( 'revision',
386 "$func(rev_timestamp)",
387 array( 'rev_page' => $id ),
388 'PageHistory::getExtremeOffset' );
391 /** @todo document */
392 function getLatestId() {
393 if( is_null( $this->mLatestId
) ) {
394 $id = $this->mTitle
->getArticleID();
395 $db =& wfGetDB(DB_SLAVE
);
396 $this->mLatestId
= $db->selectField( 'revision',
398 array( 'rev_page' => $id ),
399 'PageHistory::getLatestID' );
401 return $this->mLatestId
;
404 /** @todo document */
405 function getLastOffsetForPaging( $id, $step ) {
406 $fname = 'PageHistory::getLastOffsetForPaging';
408 $dbr =& wfGetDB(DB_SLAVE
);
414 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
416 $n = $dbr->numRows( $res );
418 while( $obj = $dbr->fetchObject( $res ) ) {
419 $last = $obj->rev_timestamp
;
421 $dbr->freeResult( $res );
426 * @return returns the direction of browsing watchlist
428 function getDirection() {
430 if ($wgRequest->getText("dir") == "prev")
436 /** @todo document */
437 function fetchRevisions($limit, $offset, $direction) {
438 $fname = 'PageHistory::fetchRevisions';
440 $dbr =& wfGetDB( DB_SLAVE
);
442 if ($direction == DIR_PREV
)
443 list($dirs, $oper) = array("ASC", ">=");
444 else /* $direction == DIR_NEXT */
445 list($dirs, $oper) = array("DESC", "<=");
448 $offsets = array("rev_timestamp $oper '$offset'");
452 $page_id = $this->mTitle
->getArticleID();
456 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
457 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
458 array_merge(array("rev_page=$page_id"), $offsets),
460 array('ORDER BY' => "rev_timestamp $dirs",
461 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
465 while (($obj = $dbr->fetchObject($res)) != NULL)
471 /** @todo document */
472 function getNotificationTimestamp() {
473 global $wgUser, $wgShowUpdatedMarker;
474 $fname = 'PageHistory::getNotficationTimestamp';
476 if ($this->mNotificationTimestamp
!== NULL)
477 return $this->mNotificationTimestamp
;
479 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
480 return $this->mNotificationTimestamp
= false;
482 $dbr =& wfGetDB(DB_SLAVE
);
484 $this->mNotificationTimestamp
= $dbr->selectField(
486 'wl_notificationtimestamp',
487 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
488 'wl_title' => $this->mTitle
->getDBkey(),
489 'wl_user' => $wgUser->getID()
493 return $this->mNotificationTimestamp
;
496 /** @todo document */
497 function makeNavbar($revisions, $offset, $limit, $direction) {
500 $revisions = array_slice($revisions, 0, $limit);
502 $latestTimestamp = wfTimestamp(TS_MW
, $this->getLatestOffset());
503 $earliestTimestamp = wfTimestamp(TS_MW
, $this->getEarliestOffset());
506 * When we're displaying previous revisions, we need to reverse
507 * the array, because it's queried in reverse order.
509 if ($direction == DIR_PREV
)
510 $revisions = array_reverse($revisions);
513 * lowts is the timestamp of the first revision on this page.
514 * hights is the timestamp of the last revision.
517 $lowts = $hights = 0;
519 if( count( $revisions ) ) {
520 $latestShown = wfTimestamp(TS_MW
, $revisions[0]->rev_timestamp
);
521 $earliestShown = wfTimestamp(TS_MW
, $revisions[count($revisions) - 1]->rev_timestamp
);
524 /* Don't announce the limit everywhere if it's the default */
525 $usefulLimit = $limit == $this->defaultLimit ?
'' : $limit;
528 foreach (array(20, 50, 100, 250, 500) as $num) {
529 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
530 array('offset' => $offset == 0 ?
'' : wfTimestamp(TS_MW
, $offset), 'limit' => $num, ) );
533 $bits = implode($urls, ' | ');
535 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
536 if( $latestShown < $latestTimestamp ) {
537 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
538 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
539 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
540 array( 'limit' => $usefulLimit ) );
542 $prevtext = wfMsgHtml("prevn", $limit);
543 $lasttext = wfMsgHtml('histlast');
546 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
547 if( $earliestShown > $earliestTimestamp ) {
548 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
549 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
550 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
551 array( 'go' => 'first', 'limit' => $usefulLimit ) );
553 $nexttext = wfMsgHtml("nextn", $limit);
554 $firsttext = wfMsgHtml('histfirst');
557 $firstlast = "($lasttext | $firsttext)";
559 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
562 function MakeLink($text, $query = NULL) {
563 if ( $query === null ) return $text;
564 return $this->mSkin
->makeKnownLinkObj(
565 $this->mTitle
, $text,
566 wfArrayToCGI( $query, array( 'action' => 'history' )));