* (bug 2595) Show "Earlier" and "Latest" links on history go to the first/last
[lhc/web/wiklou.git] / includes / PageHistory.php
1 <?php
2 /**
3 * Page history
4 *
5 * Split off from Article.php and Skin.php, 2003-12-22
6 * @package MediaWiki
7 */
8
9 include_once ( "SpecialValidate.php" );
10
11 /**
12 * @todo document
13 * @package MediaWiki
14 */
15
16 class PageHistory {
17 var $mArticle, $mTitle, $mSkin;
18 var $lastdate;
19 var $linesonpage;
20 function PageHistory( $article ) {
21 $this->mArticle =& $article;
22 $this->mTitle =& $article->mTitle;
23 }
24
25 # This shares a lot of issues (and code) with Recent Changes
26
27 function history() {
28 global $wgUser, $wgOut, $wgLang, $wgShowUpdatedMarker, $wgRequest,
29 $wgTitle, $wgUseValidation ;
30
31 # If page hasn't changed, client can cache this
32
33 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) ){
34 # Client cache fresh and headers sent, nothing more to do.
35 return;
36 }
37 $fname = 'PageHistory::history';
38 wfProfileIn( $fname );
39
40 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
41 $wgOut->setSubtitle( wfMsg( 'revhistory' ) );
42 $wgOut->setArticleFlag( false );
43 $wgOut->setArticleRelated( true );
44 $wgOut->setRobotpolicy( 'noindex,nofollow' );
45
46 $id = $this->mTitle->getArticleID();
47 if( $id == 0 ) {
48 $wgOut->addHTML( wfMsg( 'nohistory' ) );
49 wfProfileOut( $fname );
50 return;
51 }
52
53 $limit = $wgRequest->getInt('limit');
54 if (!$limit) $limit = 50;
55 $offset = $wgRequest->getText('offset');
56 if (!isset($offset) || !preg_match("/^[0-9]+$/", $offset)) $offset = 0;
57
58 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
59 switch ($gowhere) {
60 case "first":
61 if (($lastid = $this->getLastOffset($id, $limit)) === NULL)
62 break;
63 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset={$lastid}");
64 break;
65 default:
66 $gourl = NULL;
67 }
68
69 if (!is_null($gourl)) {
70 $wgOut->redirect($gourl);
71 return;
72 }
73 }
74
75 $firsturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}&go=first");
76 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
77 $firsttext = wfMsg("histfirst");
78 $lasttext = wfMsg("histlast");
79
80 $firstlast = "(<a href=\"$firsturl\">$firsttext</a> | <a href=\"$lasturl\">$lasttext</a>)";
81
82 /* Check one extra row to see whether we need to show 'next' and diff links */
83 $limitplus = $limit + 1;
84
85 $namespace = $this->mTitle->getNamespace();
86 $title = $this->mTitle->getText();
87 $uid = $wgUser->getID();
88 $db =& wfGetDB( DB_SLAVE );
89 if ($uid && $wgShowUpdatedMarker )
90 $notificationtimestamp = $db->selectField( 'watchlist',
91 'wl_notificationtimestamp',
92 array( 'wl_namespace' => $namespace, 'wl_title' => $this->mTitle->getDBkey(), 'wl_user' => $uid ),
93 $fname );
94 else $notificationtimestamp = false;
95
96 $use_index = $db->useIndexClause( 'page_timestamp' );
97 $revision = $db->tableName( 'revision' );
98
99 $limits = $offsets = "";
100 $dir = 0;
101 if ($wgRequest->getText("dir") == "prev")
102 $dir = 1;
103
104 list($dirs, $oper) = array("DESC", "<");
105 if ($dir) {
106 list($dirs, $oper) = array("ASC", ">");
107 }
108
109 if ($offset)
110 $offsets .= " AND rev_timestamp $oper '$offset' ";
111 if ($limit)
112 $limits .= " LIMIT $limitplus ";
113
114 $sql = "SELECT rev_id,rev_user," .
115 "rev_comment,rev_user_text,rev_timestamp,rev_minor_edit,rev_deleted ".
116 "FROM $revision $use_index " .
117 "WHERE rev_page=$id " .
118 $offsets .
119 "ORDER BY rev_timestamp $dirs " .
120 $limits;
121 $res = $db->query( $sql, $fname );
122
123 $revs = $db->numRows( $res );
124
125 if( $revs < $limitplus ) // the sql above tries to fetch one extra
126 $this->linesonpage = $revs;
127 else
128 $this->linesonpage = $revs - 1;
129
130 $atend = ($revs < $limitplus);
131
132 $this->mSkin = $wgUser->getSkin();
133
134 $pages = array();
135 $lowts = 0;
136 while ($line = $db->fetchObject($res)) {
137 $pages[] = $line;
138 }
139 if ($dir) $pages = array_reverse($pages);
140 if (count($pages) > 1)
141 $lowts = $pages[count($pages) - 2]->rev_timestamp;
142 else
143 $lowts = $pages[count($pages) - 1]->rev_timestamp;
144
145
146 $prevurl = $wgTitle->escapeLocalURL("action=history&dir=prev&offset={$offset}&limit={$limit}");
147 $nexturl = $wgTitle->escapeLocalURL("action=history&offset={$lowts}&limit={$limit}");
148 $urls = array();
149 foreach (array(20, 50, 100, 250, 500) as $num) {
150 $urls[] = "<a href=\"".$wgTitle->escapeLocalURL(
151 "action=history&offset={$offset}&limit={$num}")."\">".$wgLang->formatNum($num)."</a>";
152 }
153 $bits = implode($urls, ' | ');
154 $numbar = "$firstlast " . wfMsg("viewprevnext",
155 "<a href=\"$prevurl\">".wfMsg("prevn", $limit)."</a>",
156 "<a href=\"$nexturl\">".wfMsg("nextn", $limit)."</a>",
157 $bits);
158
159 $s = $numbar;
160 $s .= $this->beginHistoryList();
161 $counter = 1;
162 foreach($pages as $i => $line) {
163 $first = ($counter == 1 && $offset == 0);
164 $next = isset( $pages[$i + 1] ) ? $pages[$i + 1 ] : null;
165 $s .= $this->historyLine( $line, $next, $counter, $notificationtimestamp, $first );
166 $counter++;
167 }
168 $s .= $this->endHistoryList( !$atend );
169 $s .= $numbar;
170
171 # Validation line
172 if ( isset ( $wgUseValidation ) && $wgUseValidation ) {
173 $s .= "<p>" . Validation::link2statistics ( $this->mArticle ) . "</p>" ;
174 }
175
176 $wgOut->addHTML( $s );
177 wfProfileOut( $fname );
178 }
179
180 function beginHistoryList() {
181 global $wgTitle;
182 $this->lastdate = '';
183 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
184 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
185 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
186 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
187 $s .= $this->submitButton();
188 $s .= '<ul id="pagehistory">';
189 return $s;
190 }
191
192 function endHistoryList() {
193 $last = wfMsg( 'last' );
194
195 $s = '</ul>';
196 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
197 $s .= '</form>';
198 return $s;
199 }
200
201 function submitButton( $bits = array() ) {
202 return ( $this->linesonpage > 0 )
203 ? wfElement( 'input', array_merge( $bits,
204 array(
205 'class' => 'historysubmit',
206 'type' => 'submit',
207 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
208 'title' => wfMsg( 'tooltip-compareselectedversions' ),
209 'value' => wfMsg( 'compareselectedversions' ),
210 ) ) )
211 : '';
212 }
213
214 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false ) {
215 global $wgLang, $wgContLang;
216
217 static $message;
218 if( !isset( $message ) ) {
219 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
220 $message[$msg] = wfMsg( $msg );
221 }
222 }
223
224 $link = $this->revLink( $row );
225
226 if ( 0 == $row->rev_user ) {
227 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
228 $ul = $this->mSkin->makeKnownLinkObj( $contribsPage,
229 htmlspecialchars( $row->rev_user_text ),
230 'target=' . urlencode( $row->rev_user_text ) );
231 } else {
232 $userPage =& Title::makeTitle( NS_USER, $row->rev_user_text );
233 $ul = $this->mSkin->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text ) );
234 }
235
236 $s = '<li>';
237 if( $row->rev_deleted ) {
238 $s .= '<span class="deleted">';
239 }
240 $curlink = $this->curLink( $row, $latest );
241 $lastlink = $this->lastLink( $row, $next, $counter );
242 $arbitrary = $this->diffButtons( $row, $latest, $counter );
243 $s .= "({$curlink}) ({$lastlink}) $arbitrary {$link} <span class='user'>{$ul}</span>";
244
245 if( $row->rev_minor_edit ) {
246 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), $message['minoreditletter'] );
247 }
248
249
250 $s .= $this->mSkin->commentBlock( $row->rev_comment, $this->mTitle );
251 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
252 $s .= wfMsg( 'updatedmarker' );
253 }
254 if( $row->rev_deleted ) {
255 $s .= "</span> " . htmlspecialchars( wfMsg( 'deletedrev' ) );
256 }
257 $s .= '</li>';
258
259 return $s;
260 }
261
262 function revLink( $row ) {
263 global $wgUser, $wgLang;
264 $date = $wgLang->timeanddate( $row->rev_timestamp, true );
265 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
266 return $date;
267 } else {
268 return $this->mSkin->makeKnownLinkObj(
269 $this->mTitle,
270 $date,
271 'oldid='.$row->rev_id );
272 }
273 }
274
275 function curLink( $row, $latest ) {
276 global $wgUser;
277 $cur = htmlspecialchars( wfMsg( 'cur' ) );
278 if( $latest
279 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
280 return $cur;
281 } else {
282 return $this->mSkin->makeKnownLinkObj(
283 $this->mTitle,
284 $cur,
285 'diff=0&oldid=' . $row->rev_id );
286 }
287 }
288
289 function lastLink( $row, $next, $counter ) {
290 global $wgUser;
291 $last = htmlspecialchars( wfMsg( 'last' ) );
292 if( is_null( $next )
293 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
294 return $last;
295 } else {
296 return $this->mSkin->makeKnownLinkObj(
297 $this->mTitle,
298 $last,
299 "diff={$row->rev_id}&oldid={$next->rev_id}",
300 '',
301 '',
302 ' tabindex="'.$counter.'"' );
303 }
304 }
305
306 function diffButtons( $row, $latest, $counter ) {
307 global $wgUser;
308 if( $this->linesonpage > 1) {
309 $radio = array(
310 'type' => 'radio',
311 'value' => $row->rev_id,
312 'title' => wfMsg( 'selectolderversionfordiff' )
313 );
314 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
315 $radio['disabled'] = 'disabled';
316 }
317
318 # XXX: move title texts to javascript
319 if ( $latest ) {
320 $first = wfElement( 'input', array_merge(
321 $radio,
322 array(
323 'style' => 'visibility:hidden',
324 'name' => 'oldid' ) ) );
325 $checkmark = array( 'checked' => 'checked' );
326 } else {
327 if( $counter == 2 ) {
328 $checkmark = array( 'checked' => 'checked' );
329 } else {
330 $checkmark = array();
331 }
332 $first = wfElement( 'input', array_merge(
333 $radio,
334 $checkmark,
335 array( 'name' => 'oldid' ) ) );
336 $checkmark = array();
337 }
338 $second = wfElement( 'input', array_merge(
339 $radio,
340 $checkmark,
341 array( 'name' => 'diff' ) ) );
342 return $first . $second;
343 } else {
344 return '';
345 }
346 }
347
348 function getLastOffset($id, $step = 50) {
349 $db =& wfGetDB(DB_SLAVE);
350 $sql = "SELECT rev_timestamp FROM revision WHERE rev_page = $id ORDER BY rev_timestamp ASC LIMIT $step";
351 $res = $db->query($sql, "getLastOffset");
352 $n = $db->numRows($res);
353
354 if ($n == 0)
355 return NULL;
356
357 while ($n--)
358 $obj = $db->fetchObject($res);
359 return $obj->rev_timestamp;
360 }
361 }
362
363 ?>