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;
33 * Construct a new PageHistory.
35 * @param Article $article
38 function PageHistory($article) {
41 $this->mArticle
=& $article;
42 $this->mTitle
=& $article->mTitle
;
43 $this->mNotificationTimestamp
= NULL;
44 $this->mSkin
= $wgUser->getSkin();
46 $this->defaultLimit
= 50;
50 * Print the history page for an article.
55 global $wgUser, $wgOut, $wgLang, $wgShowUpdatedMarker, $wgRequest,
56 $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);
101 wfdebug("offset=[$offset] dboffset=[$dboffset]\n");
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());
190 $s .= "<input type='hidden' name='title' value='{$prefixedkey}' />\n";
191 $s .= $this->submitButton();
192 $s .= '<ul id="pagehistory">' . "\n";
196 /** @todo document */
197 function endHistoryList() {
198 $last = wfMsg( 'last' );
201 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
206 /** @todo document */
207 function submitButton( $bits = array() ) {
208 return ( $this->linesonpage
> 0 )
209 ?
wfElement( 'input', array_merge( $bits,
211 'class' => 'historysubmit',
213 'accesskey' => wfMsgHtml( 'accesskey-compareselectedversions' ),
214 'title' => wfMsgHtml( 'tooltip-compareselectedversions' ),
215 'value' => wfMsgHtml( 'compareselectedversions' ),
220 /** @todo document */
221 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
222 global $wgLang, $wgContLang;
224 if ( 0 == $row->rev_user
) {
225 $contribsPage =& Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
226 $ul = $this->mSkin
->makeKnownLinkObj( $contribsPage,
227 htmlspecialchars( $row->rev_user_text
),
228 'target=' . urlencode( $row->rev_user_text
) );
230 $userPage =& Title
::makeTitle( NS_USER
, $row->rev_user_text
);
231 $ul = $this->mSkin
->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text
) );
235 /* This feature is not yet used according to schema */
236 if( $row->rev_deleted
) {
237 $s .= '<span class="history-deleted">';
239 $curlink = $this->curLink( $row, $latest );
240 $lastlink = $this->lastLink( $row, $next, $counter );
241 $arbitrary = $this->diffButtons( $row, $firstInList, $counter );
242 $link = $this->revLink( $row );
244 $s .= "($curlink) ($lastlink) $arbitrary $link <span class='history-user'>$ul</span>";
246 if( $row->rev_minor_edit
) {
247 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsgHtml( 'minoreditletter') );
250 $s .= $this->mSkin
->commentBlock( $row->rev_comment
, $this->mTitle
);
251 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
252 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
254 if( $row->rev_deleted
) {
255 $s .= '</span> ' . wfMsgHtml( 'deletedrev' );
262 /** @todo document */
263 function revLink( $row ) {
264 global $wgUser, $wgLang;
265 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->rev_timestamp
), true );
266 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
269 return $this->mSkin
->makeKnownLinkObj(
270 $this->mTitle
, $date, "oldid={$row->rev_id}" );
274 /** @todo document */
275 function curLink( $row, $latest ) {
277 $cur = wfMsgHtml( 'cur' );
279 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
282 return $this->mSkin
->makeKnownLinkObj(
284 'diff=' . $this->getLatestID() .
285 "&oldid={$row->rev_id}" );
289 /** @todo document */
290 function lastLink( $row, $next, $counter ) {
292 $last = htmlspecialchars( wfMsg( 'last' ) );
294 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
297 return $this->mSkin
->makeKnownLinkObj(
300 "diff={$row->rev_id}&oldid={$next->rev_id}"
304 "tabindex={$counter}"*/ );
308 /** @todo document */
309 function diffButtons( $row, $firstInList, $counter ) {
311 if( $this->linesonpage
> 1) {
314 'value' => $row->rev_id
,
315 # do we really need to flood this on every item?
316 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
319 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
320 $radio['disabled'] = 'disabled';
323 /** @todo: move title texts to javascript */
324 if ( $firstInList ) {
325 $first = wfElement( 'input', array_merge(
328 'style' => 'visibility:hidden',
329 'name' => 'oldid' ) ) );
330 $checkmark = array( 'checked' => 'checked' );
332 if( $counter == 2 ) {
333 $checkmark = array( 'checked' => 'checked' );
335 $checkmark = array();
337 $first = wfElement( 'input', array_merge(
340 array( 'name' => 'oldid' ) ) );
341 $checkmark = array();
343 $second = wfElement( 'input', array_merge(
346 array( 'name' => 'diff' ) ) );
347 return $first . $second;
353 /** @todo document */
354 function getLatestOffset( $id = null ) {
355 if ( $id === null) $id = $this->mTitle
->getArticleID();
356 return $this->getExtremeOffset( $id, 'max' );
359 /** @todo document */
360 function getEarliestOffset( $id = null ) {
361 if ( $id === null) $id = $this->mTitle
->getArticleID();
362 return $this->getExtremeOffset( $id, 'min' );
365 /** @todo document */
366 function getExtremeOffset( $id, $func ) {
367 $db =& wfGetDB(DB_SLAVE
);
368 return $db->selectField( 'revision',
369 "$func(rev_timestamp)",
370 array( 'rev_page' => $id ),
371 'PageHistory::getExtremeOffset' );
374 /** @todo document */
375 function getLatestID( $id = null ) {
376 if ( $id === null) $id = $this->mTitle
->getArticleID();
377 $db =& wfGetDB(DB_SLAVE
);
378 return $db->selectField( 'revision',
380 array( 'rev_page' => $id ),
381 'PageHistory::getLatestID' );
384 /** @todo document */
385 function getLastOffsetForPaging( $id, $step ) {
386 $fname = 'PageHistory::getLastOffsetForPaging';
388 $dbr =& wfGetDB(DB_SLAVE
);
394 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
396 $n = $dbr->numRows( $res );
398 while( $obj = $db->fetchObject( $res ) ) {
399 $last = $obj->rev_timestamp
;
401 $db->freeResult( $res );
406 * @return returns the direction of browsing watchlist
408 function getDirection() {
410 if ($wgRequest->getText("dir") == "prev")
416 /** @todo document */
417 function fetchRevisions($limit, $offset, $direction) {
418 global $wgUser, $wgShowUpdatedMarker;
419 $fname = 'PageHistory::fetchRevisions';
421 $dbr =& wfGetDB( DB_SLAVE
);
423 if ($direction == DIR_PREV
)
424 list($dirs, $oper) = array("ASC", ">=");
425 else /* $direction == DIR_NEXT */
426 list($dirs, $oper) = array("DESC", "<=");
429 $offsets = array("rev_timestamp $oper '$offset'");
433 $page_id = $this->mTitle
->getArticleID();
437 array('rev_id', 'rev_user', 'rev_comment', 'rev_user_text',
438 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
439 array_merge(array("rev_page=$page_id"), $offsets),
441 array('ORDER BY' => "rev_timestamp $dirs",
442 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
446 while (($obj = $dbr->fetchObject($res)) != NULL)
452 /** @todo document */
453 function getNotificationTimestamp() {
454 global $wgUser, $wgShowUpdatedMarker;
455 $fname = 'PageHistory::getNotficationTimestamp';
457 if ($this->mNotificationTimestamp
!== NULL)
458 return $this->mNotificationTimestamp
;
460 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
461 return $this->mNotificationTimestamp
= false;
463 $dbr =& wfGetDB(DB_SLAVE
);
465 $this->mNotificationTimestamp
= $dbr->selectField(
467 'wl_notificationtimestamp',
468 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
469 'wl_title' => $this->mTitle
->getDBkey(),
470 'wl_user' => $wgUser->getID()
474 return $this->mNotificationTimestamp
;
477 /** @todo document */
478 function makeNavbar($revisions, $offset, $limit, $direction) {
479 global $wgTitle, $wgLang;
481 $revisions = array_slice($revisions, 0, $limit);
483 $latestTimestamp = wfTimestamp(TS_MW
, $this->getLatestOffset());
484 $earliestTimestamp = wfTimestamp(TS_MW
, $this->getEarliestOffset());
487 * When we're displaying previous revisions, we need to reverse
488 * the array, because it's queried in reverse order.
490 if ($direction == DIR_PREV
)
491 $revisions = array_reverse($revisions);
494 * lowts is the timestamp of the first revision on this page.
495 * hights is the timestamp of the last revision.
498 $lowts = $hights = 0;
500 if( count( $revisions ) ) {
501 $latestShown = wfTimestamp(TS_MW
, $revisions[0]->rev_timestamp
);
502 $earliestShown = wfTimestamp(TS_MW
, $revisions[count($revisions) - 1]->rev_timestamp
);
505 /* Don't announce the limit everywhere if it's the default */
506 $usefulLimit = $limit == $this->defaultLimit ?
'' : $limit;
509 foreach (array(20, 50, 100, 250, 500) as $num) {
510 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
511 array('offset' => $offset == 0 ?
'' : wfTimestamp(TS_MW
, $offset), 'limit' => $num, ) );
514 $bits = implode($urls, ' | ');
516 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
517 if( $latestShown < $latestTimestamp ) {
518 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
519 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
520 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
521 array( 'limit' => $usefulLimit ) );
523 $prevtext = wfMsgHtml("prevn", $limit);
524 $lasttext = wfMsgHtml('histlast');
527 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
528 if( $earliestShown > $earliestTimestamp ) {
529 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
530 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
531 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
532 array( 'go' => 'first', 'limit' => $usefulLimit ) );
534 $nexttext = wfMsgHtml("nextn", $limit);
535 $firsttext = wfMsgHtml('histfirst');
538 $firstlast = "($lasttext | $firsttext)";
540 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
543 function MakeLink($text, $query = NULL) {
544 if ( $query === null ) return $text;
545 return $this->mSkin
->makeKnownLinkObj(
546 $this->mTitle
, $text,
547 wfArrayToCGI( $query, array( 'action' => 'history' )));