5 * Split off from Article.php and Skin.php, 2003-12-22
9 include_once ( "SpecialValidate.php" );
11 define("DIR_PREV", 0);
12 define("DIR_NEXT", 1);
15 * This class handles printing the history page for an article. In order to
16 * be efficient, it uses timestamps rather than offsets for paging, to avoid
17 * costly LIMIT,offset queries.
19 * Construct it by passing in an Article, and call $h->history() to print the
26 var $mArticle, $mTitle, $mSkin;
29 var $mNotificationTimestamp;
32 * Construct a new PageHistory.
34 * @param Article $article
37 function PageHistory($article) {
40 $this->mArticle
=& $article;
41 $this->mTitle
=& $article->mTitle
;
42 $this->mNotificationTimestamp
= NULL;
43 $this->mSkin
= $wgUser->getSkin();
47 * Print the history page for an article.
52 global $wgUser, $wgOut, $wgLang, $wgShowUpdatedMarker, $wgRequest,
53 $wgTitle, $wgUseValidation;
56 * Allow client caching.
59 if( $wgOut->checkLastModified( $this->mArticle
->getTimestamp() ) )
60 /* Client cache fresh and headers sent, nothing more to do. */
63 $fname = 'PageHistory::history';
64 wfProfileIn( $fname );
67 * Setup page variables.
69 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
70 $wgOut->setSubtitle( wfMsg( 'revhistory' ) );
71 $wgOut->setArticleFlag( false );
72 $wgOut->setArticleRelated( true );
73 $wgOut->setRobotpolicy( 'noindex,nofollow' );
76 * Fail if article doesn't exist.
78 $id = $this->mTitle
->getArticleID();
80 $wgOut->addWikiText( 'nohistory' );
81 wfProfileOut( $fname );
86 * Extract limit, the number of revisions to show, and
87 * offset, the timestamp to begin at, from the URL.
89 $limit = $wgRequest->getInt('limit', 50);
90 $offset = $wgRequest->getText('offset');
92 /* Offset must be an integral. */
93 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
97 * "go=last" means to jump to the last history page.
99 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
102 if (($lastid = $this->getLastOffsetForPaging($id, $limit)) === NULL)
104 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset={$lastid}");
110 if (!is_null($gourl)) {
111 $wgOut->redirect($gourl);
119 * If the user clicked "previous", we retrieve the revisions backwards,
120 * then reverse them. This is to avoid needing to know the timestamp of
121 * previous revisions when generating the URL.
123 $direction = $this->getDirection();
124 $revisions = $this->fetchRevisions($limit, $offset, $direction);
125 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
128 * We fetch one more revision than needed to get the timestamp of the
129 * one after this page (and to know if it exists).
131 * linesonpage stores the actual number of lines.
133 if (count($revisions) < $limit +
1)
134 $this->linesonpage
= count($revisions);
136 $this->linesonpage
= count($revisions) - 1;
138 /* Un-reverse revisions */
139 if ($direction == DIR_PREV
)
140 $revisions = array_reverse($revisions);
143 * Print the top navbar.
146 $s .= $this->beginHistoryList();
150 * Print each revision, excluding the one-past-the-end, if any.
152 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
153 $first = !$i && $offset == 0;
154 $next = isset( $revisions[$i +
1] ) ?
$revisions[$i +
1 ] : null;
155 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $first);
162 $s .= $this->endHistoryList();
166 * Article validation line.
168 if ($wgUseValidation)
169 $s .= "<p>" . Validation
::link2statistics ( $this->mArticle
) . "</p>" ;
171 $wgOut->addHTML( $s );
172 wfProfileOut( $fname );
175 function beginHistoryList() {
177 $this->lastdate
= '';
178 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
179 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
180 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
181 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
182 $s .= $this->submitButton();
183 $s .= '<ul id="pagehistory">';
187 function endHistoryList() {
188 $last = wfMsg( 'last' );
191 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
196 function submitButton( $bits = array() ) {
197 return ( $this->linesonpage
> 0 )
198 ?
wfElement( 'input', array_merge( $bits,
200 'class' => 'historysubmit',
202 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
203 'title' => wfMsg( 'tooltip-compareselectedversions' ),
204 'value' => wfMsg( 'compareselectedversions' ),
209 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false ) {
210 global $wgLang, $wgContLang;
213 if( !isset( $message ) ) {
214 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
215 $message[$msg] = wfMsg( $msg );
219 $link = $this->revLink( $row );
221 if ( 0 == $row->rev_user
) {
222 $contribsPage =& Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
223 $ul = $this->mSkin
->makeKnownLinkObj( $contribsPage,
224 htmlspecialchars( $row->rev_user_text
),
225 'target=' . urlencode( $row->rev_user_text
) );
227 $userPage =& Title
::makeTitle( NS_USER
, $row->rev_user_text
);
228 $ul = $this->mSkin
->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text
) );
232 if( $row->rev_deleted
) {
233 $s .= '<span class="deleted">';
235 $curlink = $this->curLink( $row, $latest );
236 $lastlink = $this->lastLink( $row, $next, $counter );
237 $arbitrary = $this->diffButtons( $row, $latest, $counter );
238 $s .= "({$curlink}) ({$lastlink}) $arbitrary {$link} <span class='user'>{$ul}</span>";
240 if( $row->rev_minor_edit
) {
241 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), $message['minoreditletter'] );
244 $s .= $this->mSkin
->commentBlock( $row->rev_comment
, $this->mTitle
);
245 if ($this->getNotificationTimestamp() && ($row->rev_timestamp
>= $this->getNotificationTimestamp())) {
246 $s .= wfMsg( 'updatedmarker' );
248 if( $row->rev_deleted
) {
249 $s .= "</span> " . htmlspecialchars( wfMsg( 'deletedrev' ) );
256 function revLink( $row ) {
257 global $wgUser, $wgLang;
258 $date = $wgLang->timeanddate( $row->rev_timestamp
, true );
259 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
262 return $this->mSkin
->makeKnownLinkObj(
265 'oldid='.$row->rev_id
);
269 function curLink( $row, $latest ) {
271 $cur = htmlspecialchars( wfMsg( 'cur' ) );
273 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
276 return $this->mSkin
->makeKnownLinkObj(
279 'diff=0&oldid=' . $row->rev_id
);
283 function lastLink( $row, $next, $counter ) {
285 $last = htmlspecialchars( wfMsg( 'last' ) );
287 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
290 return $this->mSkin
->makeKnownLinkObj(
293 "diff={$row->rev_id}&oldid={$next->rev_id}",
296 ' tabindex="'.$counter.'"' );
300 function diffButtons( $row, $latest, $counter ) {
302 if( $this->linesonpage
> 1) {
305 'value' => $row->rev_id
,
306 'title' => wfMsg( 'selectolderversionfordiff' )
308 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
309 $radio['disabled'] = 'disabled';
312 # XXX: move title texts to javascript
314 $first = wfElement( 'input', array_merge(
317 'style' => 'visibility:hidden',
318 'name' => 'oldid' ) ) );
319 $checkmark = array( 'checked' => 'checked' );
321 if( $counter == 2 ) {
322 $checkmark = array( 'checked' => 'checked' );
324 $checkmark = array();
326 $first = wfElement( 'input', array_merge(
329 array( 'name' => 'oldid' ) ) );
330 $checkmark = array();
332 $second = wfElement( 'input', array_merge(
335 array( 'name' => 'diff' ) ) );
336 return $first . $second;
342 function getLatestOffset($id) {
343 return $this->getExtremeOffset( $id, 'max' );
346 function getEarliestOffset($id) {
347 return $this->getExtremeOffset( $id, 'min' );
350 function getExtremeOffset( $id, $func ) {
351 $db =& wfGetDB(DB_SLAVE
);
352 return $db->selectField( 'revision',
353 "$func(rev_timestamp)",
354 array( 'rev_page' => $id ),
355 'PageHistory::getExtremeOffset' );
358 function getLastOffsetForPaging( $id, $step = 50 ) {
359 $db =& wfGetDB(DB_SLAVE
);
360 $revision = $db->tableName( 'revision' );
361 $sql = "SELECT rev_timestamp FROM $revision WHERE rev_page = $id " .
362 "ORDER BY rev_timestamp ASC LIMIT $step";
363 $res = $db->query( $sql, "PageHistory::getLastOffsetForPaging" );
364 $n = $db->numRows( $res );
367 while( $obj = $db->fetchObject( $res ) ) {
368 $last = $obj->rev_timestamp
;
370 $db->freeResult( $res );
374 function getDirection() {
377 if ($wgRequest->getText("dir") == "prev")
383 function fetchRevisions($limit, $offset, $direction) {
384 global $wgUser, $wgShowUpdatedMarker;
386 /* Check one extra row to see whether we need to show 'next' and diff links */
387 $limitplus = $limit +
1;
389 $namespace = $this->mTitle
->getNamespace();
390 $title = $this->mTitle
->getText();
391 $uid = $wgUser->getID();
392 $db =& wfGetDB( DB_SLAVE
);
394 $use_index = $db->useIndexClause( 'page_timestamp' );
395 $revision = $db->tableName( 'revision' );
397 $limits = $offsets = "";
399 if ($direction == DIR_PREV
)
400 list($dirs, $oper) = array("ASC", ">=");
401 else /* $direction = DIR_NEXT */
402 list($dirs, $oper) = array("DESC", "<=");
405 $offsets .= " AND rev_timestamp $oper '$offset' ";
408 $limits .= " LIMIT $limitplus ";
409 $page_id = $this->mTitle
->getArticleID();
411 $sql = "SELECT rev_id,rev_user," .
412 "rev_comment,rev_user_text,rev_timestamp,rev_minor_edit,rev_deleted ".
413 "FROM $revision $use_index " .
414 "WHERE rev_page=$page_id " .
416 "ORDER BY rev_timestamp $dirs " .
418 $res = $db->query($sql, "PageHistory::fetchRevisions");
421 while (($obj = $db->fetchObject($res)) != NULL)
423 wfdebug("limits=$limits offset=$offsets got=".count($result)."\n");
428 function getNotificationTimestamp() {
429 global $wgUser, $wgShowUpdatedMarker;
431 if ($this->mNotificationTimestamp
!== NULL)
432 return $this->mNotificationTimestamp
;
434 if ($wgUser->getID() == 0 ||
!$wgShowUpdatedMarker)
435 return $this->mNotificationTimestamp
= false;
437 $db =& wfGetDB(DB_SLAVE
);
439 $this->mNotificationTimestamp
= $db->selectField(
441 'wl_notificationtimestamp',
442 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
443 'wl_title' => $this->mTitle
->getDBkey(),
444 'wl_user' => $wgUser->getID()
446 "PageHistory::getNotficationTimestamp");
448 return $this->mNotificationTimestamp
;
451 function makeNavbar($revisions, $offset, $limit, $direction) {
452 global $wgTitle, $wgLang;
454 $revisions = array_slice($revisions, 0, $limit);
456 $pageid = $this->mTitle
->getArticleID();
457 $latestTimestamp = $this->getLatestOffset( $pageid );
458 $earliestTimestamp = $this->getEarliestOffset( $pageid );
461 * When we're displaying previous revisions, we need to reverse
462 * the array, because it's queried in reverse order.
464 if ($direction == DIR_PREV
)
465 $revisions = array_reverse($revisions);
468 * lowts is the timestamp of the first revision on this page.
469 * hights is the timestamp of the last revision.
472 $lowts = $hights = 0;
474 if( count( $revisions ) ) {
475 $latestShown = $revisions[0]->rev_timestamp
;
476 $earliestShown = $revisions[count($revisions) - 1]->rev_timestamp
;
479 $firsturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}&go=first");
480 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
481 $firsttext = wfMsgHtml("histfirst");
482 $lasttext = wfMsgHtml("histlast");
484 $prevurl = $wgTitle->escapeLocalURL("action=history&dir=prev&offset={$latestShown}&limit={$limit}");
485 $nexturl = $wgTitle->escapeLocalURL("action=history&offset={$earliestShown}&limit={$limit}");
488 foreach (array(20, 50, 100, 250, 500) as $num) {
489 $urls[] = "<a href=\"".$wgTitle->escapeLocalURL(
490 "action=history&offset={$offset}&limit={$num}")."\">".$wgLang->formatNum($num)."</a>";
493 $bits = implode($urls, ' | ');
495 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
496 if( $latestShown < $latestTimestamp ) {
497 $prevtext = "<a href=\"$prevurl\">".wfMsgHtml("prevn", $limit)."</a>";
498 $lasttext = "<a href=\"$lasturl\">$lasttext</a>";
500 $prevtext = wfMsgHtml("prevn", $limit);
503 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
504 if( $earliestShown > $earliestTimestamp ) {
505 $nexttext = "<a href=\"$nexturl\">".wfMsgHtml("nextn", $limit)."</a>";
506 $firsttext = "<a href=\"$firsturl\">$firsttext</a>";
508 $nexttext = wfMsgHtml("nextn", $limit);
511 $firstlast = "($lasttext | $firsttext)";
513 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);