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);
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());
189 $s .= "<input type='hidden' name='title' value='{$prefixedkey}' />\n";
190 $s .= $this->submitButton();
191 $s .= '<ul id="pagehistory">' . "\n";
195 /** @todo document */
196 function endHistoryList() {
197 $last = wfMsg( 'last' );
200 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
205 /** @todo document */
206 function submitButton( $bits = array() ) {
207 return ( $this->linesonpage
> 0 )
208 ?
wfElement( 'input', array_merge( $bits,
210 'class' => 'historysubmit',
212 'accesskey' => wfMsgHtml( 'accesskey-compareselectedversions' ),
213 'title' => wfMsgHtml( 'tooltip-compareselectedversions' ),
214 'value' => wfMsgHtml( 'compareselectedversions' ),
219 /** @todo document */
220 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
221 global $wgLang, $wgContLang;
223 if ( 0 == $row->rev_user
) {
224 $contribsPage =& Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
225 $ul = $this->mSkin
->makeKnownLinkObj( $contribsPage,
226 htmlspecialchars( $row->rev_user_text
),
227 'target=' . urlencode( $row->rev_user_text
) );
229 $userPage =& Title
::makeTitle( NS_USER
, $row->rev_user_text
);
230 $ul = $this->mSkin
->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text
) );
234 /* This feature is not yet used according to schema */
235 if( $row->rev_deleted
) {
236 $s .= '<span class="history-deleted">';
238 $curlink = $this->curLink( $row, $latest );
239 $lastlink = $this->lastLink( $row, $next, $counter );
240 $arbitrary = $this->diffButtons( $row, $firstInList, $counter );
241 $link = $this->revLink( $row );
243 $s .= "($curlink) ($lastlink) $arbitrary $link <span class='history-user'>$ul</span>";
245 if( $row->rev_minor_edit
) {
246 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsgHtml( 'minoreditletter') );
249 $s .= $this->mSkin
->commentBlock( $row->rev_comment
, $this->mTitle
);
250 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
251 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
253 if( $row->rev_deleted
) {
254 $s .= '</span> ' . wfMsgHtml( 'deletedrev' );
261 /** @todo document */
262 function revLink( $row ) {
263 global $wgUser, $wgLang;
264 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->rev_timestamp
), true );
265 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
268 return $this->mSkin
->makeKnownLinkObj(
269 $this->mTitle
, $date, "oldid={$row->rev_id}" );
273 /** @todo document */
274 function curLink( $row, $latest ) {
276 $cur = wfMsgHtml( 'cur' );
278 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
281 return $this->mSkin
->makeKnownLinkObj(
283 'diff=' . $this->getLatestID() .
284 "&oldid={$row->rev_id}" );
288 /** @todo document */
289 function lastLink( $row, $next, $counter ) {
291 $last = htmlspecialchars( wfMsg( 'last' ) );
293 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
296 return $this->mSkin
->makeKnownLinkObj(
299 "diff={$row->rev_id}&oldid={$next->rev_id}"
303 "tabindex={$counter}"*/ );
307 /** @todo document */
308 function diffButtons( $row, $firstInList, $counter ) {
310 if( $this->linesonpage
> 1) {
313 'value' => $row->rev_id
,
314 # do we really need to flood this on every item?
315 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
318 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
319 $radio['disabled'] = 'disabled';
322 /** @todo: move title texts to javascript */
323 if ( $firstInList ) {
324 $first = wfElement( 'input', array_merge(
327 'style' => 'visibility:hidden',
328 'name' => 'oldid' ) ) );
329 $checkmark = array( 'checked' => 'checked' );
331 if( $counter == 2 ) {
332 $checkmark = array( 'checked' => 'checked' );
334 $checkmark = array();
336 $first = wfElement( 'input', array_merge(
339 array( 'name' => 'oldid' ) ) );
340 $checkmark = array();
342 $second = wfElement( 'input', array_merge(
345 array( 'name' => 'diff' ) ) );
346 return $first . $second;
352 /** @todo document */
353 function getLatestOffset( $id = null ) {
354 if ( $id === null) $id = $this->mTitle
->getArticleID();
355 return $this->getExtremeOffset( $id, 'max' );
358 /** @todo document */
359 function getEarliestOffset( $id = null ) {
360 if ( $id === null) $id = $this->mTitle
->getArticleID();
361 return $this->getExtremeOffset( $id, 'min' );
364 /** @todo document */
365 function getExtremeOffset( $id, $func ) {
366 $db =& wfGetDB(DB_SLAVE
);
367 return $db->selectField( 'revision',
368 "$func(rev_timestamp)",
369 array( 'rev_page' => $id ),
370 'PageHistory::getExtremeOffset' );
373 /** @todo document */
374 function getLatestID( $id = null ) {
375 if ( $id === null) $id = $this->mTitle
->getArticleID();
376 $db =& wfGetDB(DB_SLAVE
);
377 return $db->selectField( 'revision',
379 array( 'rev_page' => $id ),
380 'PageHistory::getLatestID' );
383 /** @todo document */
384 function getLastOffsetForPaging( $id, $step ) {
385 $fname = 'PageHistory::getLastOffsetForPaging';
387 $dbr =& wfGetDB(DB_SLAVE
);
393 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
395 $n = $dbr->numRows( $res );
397 while( $obj = $dbr->fetchObject( $res ) ) {
398 $last = $obj->rev_timestamp
;
400 $dbr->freeResult( $res );
405 * @return returns the direction of browsing watchlist
407 function getDirection() {
409 if ($wgRequest->getText("dir") == "prev")
415 /** @todo document */
416 function fetchRevisions($limit, $offset, $direction) {
417 global $wgUser, $wgShowUpdatedMarker;
418 $fname = 'PageHistory::fetchRevisions';
420 $dbr =& wfGetDB( DB_SLAVE
);
422 if ($direction == DIR_PREV
)
423 list($dirs, $oper) = array("ASC", ">=");
424 else /* $direction == DIR_NEXT */
425 list($dirs, $oper) = array("DESC", "<=");
428 $offsets = array("rev_timestamp $oper '$offset'");
432 $page_id = $this->mTitle
->getArticleID();
436 array('rev_id', 'rev_user', 'rev_comment', 'rev_user_text',
437 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
438 array_merge(array("rev_page=$page_id"), $offsets),
440 array('ORDER BY' => "rev_timestamp $dirs",
441 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
445 while (($obj = $dbr->fetchObject($res)) != NULL)
451 /** @todo document */
452 function getNotificationTimestamp() {
453 global $wgUser, $wgShowUpdatedMarker;
454 $fname = 'PageHistory::getNotficationTimestamp';
456 if ($this->mNotificationTimestamp
!== NULL)
457 return $this->mNotificationTimestamp
;
459 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
460 return $this->mNotificationTimestamp
= false;
462 $dbr =& wfGetDB(DB_SLAVE
);
464 $this->mNotificationTimestamp
= $dbr->selectField(
466 'wl_notificationtimestamp',
467 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
468 'wl_title' => $this->mTitle
->getDBkey(),
469 'wl_user' => $wgUser->getID()
473 return $this->mNotificationTimestamp
;
476 /** @todo document */
477 function makeNavbar($revisions, $offset, $limit, $direction) {
478 global $wgTitle, $wgLang;
480 $revisions = array_slice($revisions, 0, $limit);
482 $latestTimestamp = wfTimestamp(TS_MW
, $this->getLatestOffset());
483 $earliestTimestamp = wfTimestamp(TS_MW
, $this->getEarliestOffset());
486 * When we're displaying previous revisions, we need to reverse
487 * the array, because it's queried in reverse order.
489 if ($direction == DIR_PREV
)
490 $revisions = array_reverse($revisions);
493 * lowts is the timestamp of the first revision on this page.
494 * hights is the timestamp of the last revision.
497 $lowts = $hights = 0;
499 if( count( $revisions ) ) {
500 $latestShown = wfTimestamp(TS_MW
, $revisions[0]->rev_timestamp
);
501 $earliestShown = wfTimestamp(TS_MW
, $revisions[count($revisions) - 1]->rev_timestamp
);
504 /* Don't announce the limit everywhere if it's the default */
505 $usefulLimit = $limit == $this->defaultLimit ?
'' : $limit;
508 foreach (array(20, 50, 100, 250, 500) as $num) {
509 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
510 array('offset' => $offset == 0 ?
'' : wfTimestamp(TS_MW
, $offset), 'limit' => $num, ) );
513 $bits = implode($urls, ' | ');
515 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
516 if( $latestShown < $latestTimestamp ) {
517 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
518 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
519 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
520 array( 'limit' => $usefulLimit ) );
522 $prevtext = wfMsgHtml("prevn", $limit);
523 $lasttext = wfMsgHtml('histlast');
526 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
527 if( $earliestShown > $earliestTimestamp ) {
528 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
529 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
530 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
531 array( 'go' => 'first', 'limit' => $usefulLimit ) );
533 $nexttext = wfMsgHtml("nextn", $limit);
534 $firsttext = wfMsgHtml('histfirst');
537 $firstlast = "($lasttext | $firsttext)";
539 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
542 function MakeLink($text, $query = NULL) {
543 if ( $query === null ) return $text;
544 return $this->mSkin
->makeKnownLinkObj(
545 $this->mTitle
, $text,
546 wfArrayToCGI( $query, array( 'action' => 'history' )));