5 * Split off from Article.php and Skin.php, 2003-12-22
10 include_once ( 'SpecialValidate.php' );
12 define('DIR_PREV', 0);
13 define('DIR_NEXT', 1);
16 * This class handles printing the history page for an article. In order to
17 * be efficient, it uses timestamps rather than offsets for paging, to avoid
18 * costly LIMIT,offset queries.
20 * Construct it by passing in an Article, and call $h->history() to print the
27 var $mArticle, $mTitle, $mSkin;
30 var $mNotificationTimestamp;
31 var $mLatestId = null;
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 $wgUser, $wgOut, $wgLang, $wgShowUpdatedMarker, $wgRequest,
57 $wgTitle, $wgUseValidation;
60 * Allow client caching.
63 if( $wgOut->checkLastModified( $this->mArticle
->getTimestamp() ) )
64 /* Client cache fresh and headers sent, nothing more to do. */
67 $fname = 'PageHistory::history';
68 wfProfileIn( $fname );
71 * Setup page variables.
73 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
74 $wgOut->setSubtitle( wfMsg( 'revhistory' ) );
75 $wgOut->setArticleFlag( false );
76 $wgOut->setArticleRelated( true );
77 $wgOut->setRobotpolicy( 'noindex,nofollow' );
80 * Fail if article doesn't exist.
82 if( !$this->mTitle
->exists() ) {
83 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
84 wfProfileOut( $fname );
88 $dbr =& wfGetDB(DB_SLAVE
);
91 * Extract limit, the number of revisions to show, and
92 * offset, the timestamp to begin at, from the URL.
94 $limit = $wgRequest->getInt('limit', $this->defaultLimit
);
95 $offset = $wgRequest->getText('offset');
97 /* Offset must be an integral. */
98 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
100 # $offset = $dbr->timestamp($offset);
101 $dboffset = $offset === 0 ?
0 : $dbr->timestamp($offset);
103 * "go=last" means to jump to the last history page.
105 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
108 if (($lastid = $this->getLastOffsetForPaging($this->mTitle
->getArticleID(), $limit)) === NULL)
110 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset=".
111 wfTimestamp(TS_MW
, $lastid));
117 if (!is_null($gourl)) {
118 $wgOut->redirect($gourl);
126 * If the user clicked "previous", we retrieve the revisions backwards,
127 * then reverse them. This is to avoid needing to know the timestamp of
128 * previous revisions when generating the URL.
130 $direction = $this->getDirection();
131 $revisions = $this->fetchRevisions($limit, $dboffset, $direction);
132 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
135 * We fetch one more revision than needed to get the timestamp of the
136 * one after this page (and to know if it exists).
138 * linesonpage stores the actual number of lines.
140 if (count($revisions) < $limit +
1)
141 $this->linesonpage
= count($revisions);
143 $this->linesonpage
= count($revisions) - 1;
145 /* Un-reverse revisions */
146 if ($direction == DIR_PREV
)
147 $revisions = array_reverse($revisions);
150 * Print the top navbar.
153 $s .= $this->beginHistoryList();
157 * Print each revision, excluding the one-past-the-end, if any.
159 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
160 $latest = !$i && $offset == 0;
162 $next = isset( $revisions[$i +
1] ) ?
$revisions[$i +
1 ] : null;
163 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $latest, $firstInList);
170 $s .= $this->endHistoryList();
174 * Article validation line.
176 if ($wgUseValidation)
177 $s .= '<p>' . Validation
::getStatisticsLink( $this->mArticle
) . '</p>' ;
179 $wgOut->addHTML( $s );
180 wfProfileOut( $fname );
183 /** @todo document */
184 function beginHistoryList() {
186 $this->lastdate
= '';
187 $s = wfMsgWikiHtml( 'histlegend' );
188 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
189 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
191 // The following line is SUPPOSED to have double-quotes around the
192 // $prefixedkey variable, because htmlspecialchars() doesn't escape
195 // On at least two occasions people have changed it to single-quotes,
196 // which creates invalid HTML and incorrect display of the resulting
199 // Please do not break this a third time. Thank you for your kind
200 // consideration and cooperation.
202 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
204 $s .= $this->submitButton();
205 $s .= '<ul id="pagehistory">' . "\n";
209 /** @todo document */
210 function endHistoryList() {
211 $last = wfMsg( 'last' );
214 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
219 /** @todo document */
220 function submitButton( $bits = array() ) {
221 return ( $this->linesonpage
> 0 )
222 ?
wfElement( 'input', array_merge( $bits,
224 'class' => 'historysubmit',
226 'accesskey' => wfMsgHtml( 'accesskey-compareselectedversions' ),
227 'title' => wfMsgHtml( 'tooltip-compareselectedversions' ),
228 'value' => wfMsgHtml( 'compareselectedversions' ),
233 /** @todo document */
234 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
235 global $wgLang, $wgContLang;
237 if ( 0 == $row->rev_user
) {
238 $contribsPage =& Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
239 $ul = $this->mSkin
->makeKnownLinkObj( $contribsPage,
240 htmlspecialchars( $row->rev_user_text
),
241 'target=' . urlencode( $row->rev_user_text
) );
243 $userPage =& Title
::makeTitle( NS_USER
, $row->rev_user_text
);
244 $ul = $this->mSkin
->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text
) );
248 /* This feature is not yet used according to schema */
249 if( $row->rev_deleted
) {
250 $s .= '<span class="history-deleted">';
252 $curlink = $this->curLink( $row, $latest );
253 $lastlink = $this->lastLink( $row, $next, $counter );
254 $arbitrary = $this->diffButtons( $row, $firstInList, $counter );
255 $link = $this->revLink( $row );
257 $s .= "($curlink) ($lastlink) $arbitrary $link <span class='history-user'>$ul</span>";
259 if( $row->rev_minor_edit
) {
260 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsgHtml( 'minoreditletter') );
263 $s .= $this->mSkin
->commentBlock( $row->rev_comment
, $this->mTitle
);
264 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
265 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
267 if( $row->rev_deleted
) {
268 $s .= '</span> ' . wfMsgHtml( 'deletedrev' );
275 /** @todo document */
276 function revLink( $row ) {
277 global $wgUser, $wgLang;
278 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->rev_timestamp
), true );
279 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
282 return $this->mSkin
->makeKnownLinkObj(
283 $this->mTitle
, $date, "oldid={$row->rev_id}" );
287 /** @todo document */
288 function curLink( $row, $latest ) {
290 $cur = wfMsgHtml( 'cur' );
292 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
295 return $this->mSkin
->makeKnownLinkObj(
297 'diff=' . $this->getLatestID() .
298 "&oldid={$row->rev_id}" );
302 /** @todo document */
303 function lastLink( $row, $next, $counter ) {
305 $last = htmlspecialchars( wfMsg( 'last' ) );
306 if( is_null( $next ) ) {
307 if( $row->rev_timestamp
== $this->getEarliestOffset() ) {
310 // Cut off by paging; there are more behind us...
311 return $this->mSkin
->makeKnownLinkObj(
314 "diff={$row->rev_id}&oldid=prev" );
316 } elseif( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
319 return $this->mSkin
->makeKnownLinkObj(
322 "diff={$row->rev_id}&oldid={$next->rev_id}"
326 "tabindex={$counter}"*/ );
330 /** @todo document */
331 function diffButtons( $row, $firstInList, $counter ) {
333 if( $this->linesonpage
> 1) {
336 'value' => $row->rev_id
,
337 # do we really need to flood this on every item?
338 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
341 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
342 $radio['disabled'] = 'disabled';
345 /** @todo: move title texts to javascript */
346 if ( $firstInList ) {
347 $first = wfElement( 'input', array_merge(
350 'style' => 'visibility:hidden',
351 'name' => 'oldid' ) ) );
352 $checkmark = array( 'checked' => 'checked' );
354 if( $counter == 2 ) {
355 $checkmark = array( 'checked' => 'checked' );
357 $checkmark = array();
359 $first = wfElement( 'input', array_merge(
362 array( 'name' => 'oldid' ) ) );
363 $checkmark = array();
365 $second = wfElement( 'input', array_merge(
368 array( 'name' => 'diff' ) ) );
369 return $first . $second;
375 /** @todo document */
376 function getLatestOffset( $id = null ) {
377 if ( $id === null) $id = $this->mTitle
->getArticleID();
378 return $this->getExtremeOffset( $id, 'max' );
381 /** @todo document */
382 function getEarliestOffset( $id = null ) {
383 if ( $id === null) $id = $this->mTitle
->getArticleID();
384 return $this->getExtremeOffset( $id, 'min' );
387 /** @todo document */
388 function getExtremeOffset( $id, $func ) {
389 $db =& wfGetDB(DB_SLAVE
);
390 return $db->selectField( 'revision',
391 "$func(rev_timestamp)",
392 array( 'rev_page' => $id ),
393 'PageHistory::getExtremeOffset' );
396 /** @todo document */
397 function getLatestId() {
398 if( is_null( $this->mLatestId
) ) {
399 $id = $this->mTitle
->getArticleID();
400 $db =& wfGetDB(DB_SLAVE
);
401 $this->mLatestId
= $db->selectField( 'revision',
403 array( 'rev_page' => $id ),
404 'PageHistory::getLatestID' );
406 return $this->mLatestId
;
409 /** @todo document */
410 function getLastOffsetForPaging( $id, $step ) {
411 $fname = 'PageHistory::getLastOffsetForPaging';
413 $dbr =& wfGetDB(DB_SLAVE
);
419 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
421 $n = $dbr->numRows( $res );
423 while( $obj = $dbr->fetchObject( $res ) ) {
424 $last = $obj->rev_timestamp
;
426 $dbr->freeResult( $res );
431 * @return returns the direction of browsing watchlist
433 function getDirection() {
435 if ($wgRequest->getText("dir") == "prev")
441 /** @todo document */
442 function fetchRevisions($limit, $offset, $direction) {
443 global $wgUser, $wgShowUpdatedMarker;
444 $fname = 'PageHistory::fetchRevisions';
446 $dbr =& wfGetDB( DB_SLAVE
);
448 if ($direction == DIR_PREV
)
449 list($dirs, $oper) = array("ASC", ">=");
450 else /* $direction == DIR_NEXT */
451 list($dirs, $oper) = array("DESC", "<=");
454 $offsets = array("rev_timestamp $oper '$offset'");
458 $page_id = $this->mTitle
->getArticleID();
462 array('rev_id', 'rev_user', 'rev_comment', 'rev_user_text',
463 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
464 array_merge(array("rev_page=$page_id"), $offsets),
466 array('ORDER BY' => "rev_timestamp $dirs",
467 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
471 while (($obj = $dbr->fetchObject($res)) != NULL)
477 /** @todo document */
478 function getNotificationTimestamp() {
479 global $wgUser, $wgShowUpdatedMarker;
480 $fname = 'PageHistory::getNotficationTimestamp';
482 if ($this->mNotificationTimestamp
!== NULL)
483 return $this->mNotificationTimestamp
;
485 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
486 return $this->mNotificationTimestamp
= false;
488 $dbr =& wfGetDB(DB_SLAVE
);
490 $this->mNotificationTimestamp
= $dbr->selectField(
492 'wl_notificationtimestamp',
493 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
494 'wl_title' => $this->mTitle
->getDBkey(),
495 'wl_user' => $wgUser->getID()
499 return $this->mNotificationTimestamp
;
502 /** @todo document */
503 function makeNavbar($revisions, $offset, $limit, $direction) {
504 global $wgTitle, $wgLang;
506 $revisions = array_slice($revisions, 0, $limit);
508 $latestTimestamp = wfTimestamp(TS_MW
, $this->getLatestOffset());
509 $earliestTimestamp = wfTimestamp(TS_MW
, $this->getEarliestOffset());
512 * When we're displaying previous revisions, we need to reverse
513 * the array, because it's queried in reverse order.
515 if ($direction == DIR_PREV
)
516 $revisions = array_reverse($revisions);
519 * lowts is the timestamp of the first revision on this page.
520 * hights is the timestamp of the last revision.
523 $lowts = $hights = 0;
525 if( count( $revisions ) ) {
526 $latestShown = wfTimestamp(TS_MW
, $revisions[0]->rev_timestamp
);
527 $earliestShown = wfTimestamp(TS_MW
, $revisions[count($revisions) - 1]->rev_timestamp
);
530 /* Don't announce the limit everywhere if it's the default */
531 $usefulLimit = $limit == $this->defaultLimit ?
'' : $limit;
534 foreach (array(20, 50, 100, 250, 500) as $num) {
535 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
536 array('offset' => $offset == 0 ?
'' : wfTimestamp(TS_MW
, $offset), 'limit' => $num, ) );
539 $bits = implode($urls, ' | ');
541 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
542 if( $latestShown < $latestTimestamp ) {
543 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
544 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
545 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
546 array( 'limit' => $usefulLimit ) );
548 $prevtext = wfMsgHtml("prevn", $limit);
549 $lasttext = wfMsgHtml('histlast');
552 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
553 if( $earliestShown > $earliestTimestamp ) {
554 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
555 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
556 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
557 array( 'go' => 'first', 'limit' => $usefulLimit ) );
559 $nexttext = wfMsgHtml("nextn", $limit);
560 $firsttext = wfMsgHtml('histfirst');
563 $firstlast = "($lasttext | $firsttext)";
565 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
568 function MakeLink($text, $query = NULL) {
569 if ( $query === null ) return $text;
570 return $this->mSkin
->makeKnownLinkObj(
571 $this->mTitle
, $text,
572 wfArrayToCGI( $query, array( 'action' => 'history' )));