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, $wgRequest, $wgTitle, $wgUseValidation;
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();
173 * Article validation line.
175 if ($wgUseValidation)
176 $s .= '<p>' . Validation
::getStatisticsLink( $this->mArticle
) . '</p>' ;
178 $wgOut->addHTML( $s );
179 wfProfileOut( $fname );
182 /** @todo document */
183 function beginHistoryList() {
185 $this->lastdate
= '';
186 $s = wfMsgWikiHtml( 'histlegend' );
187 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
188 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
190 // The following line is SUPPOSED to have double-quotes around the
191 // $prefixedkey variable, because htmlspecialchars() doesn't escape
194 // On at least two occasions people have changed it to single-quotes,
195 // which creates invalid HTML and incorrect display of the resulting
198 // Please do not break this a third time. Thank you for your kind
199 // consideration and cooperation.
201 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
203 $s .= $this->submitButton();
204 $s .= '<ul id="pagehistory">' . "\n";
208 /** @todo document */
209 function endHistoryList() {
210 $last = wfMsg( 'last' );
213 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
218 /** @todo document */
219 function submitButton( $bits = array() ) {
220 return ( $this->linesonpage
> 0 )
221 ?
wfElement( 'input', array_merge( $bits,
223 'class' => 'historysubmit',
225 'accesskey' => wfMsgHtml( 'accesskey-compareselectedversions' ),
226 'title' => wfMsgHtml( 'tooltip-compareselectedversions' ),
227 'value' => wfMsgHtml( 'compareselectedversions' ),
232 /** @todo document */
233 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
235 if ( 0 == $row->rev_user
) {
236 $contribsPage =& Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
237 $ul = $this->mSkin
->makeKnownLinkObj( $contribsPage,
238 htmlspecialchars( $row->rev_user_text
),
239 'target=' . urlencode( $row->rev_user_text
) );
241 $userPage =& Title
::makeTitle( NS_USER
, $row->rev_user_text
);
242 $ul = $this->mSkin
->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text
) );
246 /* This feature is not yet used according to schema */
247 if( $row->rev_deleted
) {
248 $s .= '<span class="history-deleted">';
250 $curlink = $this->curLink( $row, $latest );
251 $lastlink = $this->lastLink( $row, $next, $counter );
252 $arbitrary = $this->diffButtons( $row, $firstInList, $counter );
253 $link = $this->revLink( $row );
255 $s .= "($curlink) ($lastlink) $arbitrary $link <span class='history-user'>$ul</span>";
257 if( $row->rev_minor_edit
) {
258 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsgHtml( 'minoreditletter') );
261 $s .= $this->mSkin
->commentBlock( $row->rev_comment
, $this->mTitle
);
262 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
263 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
265 if( $row->rev_deleted
) {
266 $s .= '</span> ' . wfMsgHtml( 'deletedrev' );
273 /** @todo document */
274 function revLink( $row ) {
275 global $wgUser, $wgLang;
276 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->rev_timestamp
), true );
277 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
280 return $this->mSkin
->makeKnownLinkObj(
281 $this->mTitle
, $date, "oldid={$row->rev_id}" );
285 /** @todo document */
286 function curLink( $row, $latest ) {
288 $cur = wfMsgHtml( 'cur' );
290 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
293 return $this->mSkin
->makeKnownLinkObj(
295 'diff=' . $this->getLatestID() .
296 "&oldid={$row->rev_id}" );
300 /** @todo document */
301 function lastLink( $row, $next, $counter ) {
303 $last = htmlspecialchars( wfMsg( 'last' ) );
304 if( is_null( $next ) ) {
305 if( $row->rev_timestamp
== $this->getEarliestOffset() ) {
308 // Cut off by paging; there are more behind us...
309 return $this->mSkin
->makeKnownLinkObj(
312 "diff={$row->rev_id}&oldid=prev" );
314 } elseif( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
317 return $this->mSkin
->makeKnownLinkObj(
320 "diff={$row->rev_id}&oldid={$next->rev_id}"
324 "tabindex={$counter}"*/ );
328 /** @todo document */
329 function diffButtons( $row, $firstInList, $counter ) {
331 if( $this->linesonpage
> 1) {
334 'value' => $row->rev_id
,
335 # do we really need to flood this on every item?
336 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
339 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
340 $radio['disabled'] = 'disabled';
343 /** @todo: move title texts to javascript */
344 if ( $firstInList ) {
345 $first = wfElement( 'input', array_merge(
348 'style' => 'visibility:hidden',
349 'name' => 'oldid' ) ) );
350 $checkmark = array( 'checked' => 'checked' );
352 if( $counter == 2 ) {
353 $checkmark = array( 'checked' => 'checked' );
355 $checkmark = array();
357 $first = wfElement( 'input', array_merge(
360 array( 'name' => 'oldid' ) ) );
361 $checkmark = array();
363 $second = wfElement( 'input', array_merge(
366 array( 'name' => 'diff' ) ) );
367 return $first . $second;
373 /** @todo document */
374 function getLatestOffset( $id = null ) {
375 if ( $id === null) $id = $this->mTitle
->getArticleID();
376 return $this->getExtremeOffset( $id, 'max' );
379 /** @todo document */
380 function getEarliestOffset( $id = null ) {
381 if ( $id === null) $id = $this->mTitle
->getArticleID();
382 return $this->getExtremeOffset( $id, 'min' );
385 /** @todo document */
386 function getExtremeOffset( $id, $func ) {
387 $db =& wfGetDB(DB_SLAVE
);
388 return $db->selectField( 'revision',
389 "$func(rev_timestamp)",
390 array( 'rev_page' => $id ),
391 'PageHistory::getExtremeOffset' );
394 /** @todo document */
395 function getLatestId() {
396 if( is_null( $this->mLatestId
) ) {
397 $id = $this->mTitle
->getArticleID();
398 $db =& wfGetDB(DB_SLAVE
);
399 $this->mLatestId
= $db->selectField( 'revision',
401 array( 'rev_page' => $id ),
402 'PageHistory::getLatestID' );
404 return $this->mLatestId
;
407 /** @todo document */
408 function getLastOffsetForPaging( $id, $step ) {
409 $fname = 'PageHistory::getLastOffsetForPaging';
411 $dbr =& wfGetDB(DB_SLAVE
);
417 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
419 $n = $dbr->numRows( $res );
421 while( $obj = $dbr->fetchObject( $res ) ) {
422 $last = $obj->rev_timestamp
;
424 $dbr->freeResult( $res );
429 * @return returns the direction of browsing watchlist
431 function getDirection() {
433 if ($wgRequest->getText("dir") == "prev")
439 /** @todo document */
440 function fetchRevisions($limit, $offset, $direction) {
441 global $wgUser, $wgShowUpdatedMarker;
442 $fname = 'PageHistory::fetchRevisions';
444 $dbr =& wfGetDB( DB_SLAVE
);
446 if ($direction == DIR_PREV
)
447 list($dirs, $oper) = array("ASC", ">=");
448 else /* $direction == DIR_NEXT */
449 list($dirs, $oper) = array("DESC", "<=");
452 $offsets = array("rev_timestamp $oper '$offset'");
456 $page_id = $this->mTitle
->getArticleID();
460 array('rev_id', 'rev_user', 'rev_comment', 'rev_user_text',
461 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
462 array_merge(array("rev_page=$page_id"), $offsets),
464 array('ORDER BY' => "rev_timestamp $dirs",
465 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
469 while (($obj = $dbr->fetchObject($res)) != NULL)
475 /** @todo document */
476 function getNotificationTimestamp() {
477 global $wgUser, $wgShowUpdatedMarker;
478 $fname = 'PageHistory::getNotficationTimestamp';
480 if ($this->mNotificationTimestamp
!== NULL)
481 return $this->mNotificationTimestamp
;
483 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
484 return $this->mNotificationTimestamp
= false;
486 $dbr =& wfGetDB(DB_SLAVE
);
488 $this->mNotificationTimestamp
= $dbr->selectField(
490 'wl_notificationtimestamp',
491 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
492 'wl_title' => $this->mTitle
->getDBkey(),
493 'wl_user' => $wgUser->getID()
497 return $this->mNotificationTimestamp
;
500 /** @todo document */
501 function makeNavbar($revisions, $offset, $limit, $direction) {
502 global $wgTitle, $wgLang;
504 $revisions = array_slice($revisions, 0, $limit);
506 $latestTimestamp = wfTimestamp(TS_MW
, $this->getLatestOffset());
507 $earliestTimestamp = wfTimestamp(TS_MW
, $this->getEarliestOffset());
510 * When we're displaying previous revisions, we need to reverse
511 * the array, because it's queried in reverse order.
513 if ($direction == DIR_PREV
)
514 $revisions = array_reverse($revisions);
517 * lowts is the timestamp of the first revision on this page.
518 * hights is the timestamp of the last revision.
521 $lowts = $hights = 0;
523 if( count( $revisions ) ) {
524 $latestShown = wfTimestamp(TS_MW
, $revisions[0]->rev_timestamp
);
525 $earliestShown = wfTimestamp(TS_MW
, $revisions[count($revisions) - 1]->rev_timestamp
);
528 /* Don't announce the limit everywhere if it's the default */
529 $usefulLimit = $limit == $this->defaultLimit ?
'' : $limit;
532 foreach (array(20, 50, 100, 250, 500) as $num) {
533 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
534 array('offset' => $offset == 0 ?
'' : wfTimestamp(TS_MW
, $offset), 'limit' => $num, ) );
537 $bits = implode($urls, ' | ');
539 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
540 if( $latestShown < $latestTimestamp ) {
541 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
542 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
543 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
544 array( 'limit' => $usefulLimit ) );
546 $prevtext = wfMsgHtml("prevn", $limit);
547 $lasttext = wfMsgHtml('histlast');
550 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
551 if( $earliestShown > $earliestTimestamp ) {
552 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
553 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
554 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
555 array( 'go' => 'first', 'limit' => $usefulLimit ) );
557 $nexttext = wfMsgHtml("nextn", $limit);
558 $firsttext = wfMsgHtml('histfirst');
561 $firstlast = "($lasttext | $firsttext)";
563 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
566 function MakeLink($text, $query = NULL) {
567 if ( $query === null ) return $text;
568 return $this->mSkin
->makeKnownLinkObj(
569 $this->mTitle
, $text,
570 wfArrayToCGI( $query, array( 'action' => 'history' )));