don't hyperlink first/last if we're already there
[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 (!strlen($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 /* Check one extra row to see whether we need to show 'next' and diff links */
81 $limitplus = $limit + 1;
82
83 $namespace = $this->mTitle->getNamespace();
84 $title = $this->mTitle->getText();
85 $uid = $wgUser->getID();
86 $db =& wfGetDB( DB_SLAVE );
87 if ($uid && $wgShowUpdatedMarker )
88 $notificationtimestamp = $db->selectField( 'watchlist',
89 'wl_notificationtimestamp',
90 array( 'wl_namespace' => $namespace, 'wl_title' => $this->mTitle->getDBkey(), 'wl_user' => $uid ),
91 $fname );
92 else $notificationtimestamp = false;
93
94 $use_index = $db->useIndexClause( 'page_timestamp' );
95 $revision = $db->tableName( 'revision' );
96
97 $limits = $offsets = "";
98 $dir = 0;
99 if ($wgRequest->getText("dir") == "prev")
100 $dir = 1;
101
102 list($dirs, $oper) = array("DESC", "<");
103 if ($dir) {
104 list($dirs, $oper) = array("ASC", ">");
105 }
106
107 if ($offset)
108 $offsets .= " AND rev_timestamp $oper '$offset' ";
109 if ($limit)
110 $limits .= " LIMIT $limitplus ";
111
112 $sql = "SELECT rev_id,rev_user," .
113 "rev_comment,rev_user_text,rev_timestamp,rev_minor_edit,rev_deleted ".
114 "FROM $revision $use_index " .
115 "WHERE rev_page=$id " .
116 $offsets .
117 "ORDER BY rev_timestamp $dirs " .
118 $limits;
119 $res = $db->query( $sql, $fname );
120
121 $revs = $db->numRows( $res );
122
123 if( $revs < $limitplus ) // the sql above tries to fetch one extra
124 $this->linesonpage = $revs;
125 else
126 $this->linesonpage = $revs - 1;
127
128 $atend = ($revs < $limitplus);
129
130 $this->mSkin = $wgUser->getSkin();
131
132 $pages = array();
133 $lowts = 0;
134 while ($line = $db->fetchObject($res)) {
135 $pages[] = $line;
136 }
137 if ($dir) $pages = array_reverse($pages);
138 if (count($pages) > 1)
139 $lowts = $pages[count($pages) - 2]->rev_timestamp;
140 else
141 $lowts = $pages[count($pages) - 1]->rev_timestamp;
142
143
144 $prevurl = $wgTitle->escapeLocalURL("action=history&dir=prev&offset={$offset}&limit={$limit}");
145 $nexturl = $wgTitle->escapeLocalURL("action=history&offset={$lowts}&limit={$limit}");
146 $urls = array();
147 foreach (array(20, 50, 100, 250, 500) as $num) {
148 $urls[] = "<a href=\"".$wgTitle->escapeLocalURL(
149 "action=history&offset={$offset}&limit={$num}")."\">".$wgLang->formatNum($num)."</a>";
150 }
151 $bits = implode($urls, ' | ');
152 if ($offset) {
153 $prevtext = "<a href=\"$prevurl\">".wfMsg("prevn", $limit)."</a>";
154 $lasttext = "<a href=\"$lasturl\">$lasttext</a>";
155 } else $prevtext = wfMsg("prevn", $limit);
156
157 if ($revs >= $limitplus) {
158 $nexttext = "<a href=\"$nexturl\">".wfMsg("nextn", $limit)."</a>";
159 $firsttext = "<a href=\"$firsturl\">$firsttext</a>";
160 } else $nexttext = wfMsg("nextn", $limit);
161
162 $firstlast = "($firsttext | $lasttext)";
163
164 $numbar = "$firstlast " . wfMsg("viewprevnext", $prevtext, $nexttext, $bits);
165
166 $s = $numbar;
167 $s .= $this->beginHistoryList();
168 $counter = 1;
169 foreach($pages as $i => $line) {
170 $first = ($counter == 1 && $offset == 0);
171 $next = isset( $pages[$i + 1] ) ? $pages[$i + 1 ] : null;
172 $s .= $this->historyLine( $line, $next, $counter, $notificationtimestamp, $first );
173 $counter++;
174 }
175 $s .= $this->endHistoryList( !$atend );
176 $s .= $numbar;
177
178 # Validation line
179 if ( isset ( $wgUseValidation ) && $wgUseValidation ) {
180 $s .= "<p>" . Validation::link2statistics ( $this->mArticle ) . "</p>" ;
181 }
182
183 $wgOut->addHTML( $s );
184 wfProfileOut( $fname );
185 }
186
187 function beginHistoryList() {
188 global $wgTitle;
189 $this->lastdate = '';
190 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
191 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
192 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
193 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
194 $s .= $this->submitButton();
195 $s .= '<ul id="pagehistory">';
196 return $s;
197 }
198
199 function endHistoryList() {
200 $last = wfMsg( 'last' );
201
202 $s = '</ul>';
203 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
204 $s .= '</form>';
205 return $s;
206 }
207
208 function submitButton( $bits = array() ) {
209 return ( $this->linesonpage > 0 )
210 ? wfElement( 'input', array_merge( $bits,
211 array(
212 'class' => 'historysubmit',
213 'type' => 'submit',
214 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
215 'title' => wfMsg( 'tooltip-compareselectedversions' ),
216 'value' => wfMsg( 'compareselectedversions' ),
217 ) ) )
218 : '';
219 }
220
221 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false ) {
222 global $wgLang, $wgContLang;
223
224 static $message;
225 if( !isset( $message ) ) {
226 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
227 $message[$msg] = wfMsg( $msg );
228 }
229 }
230
231 $link = $this->revLink( $row );
232
233 if ( 0 == $row->rev_user ) {
234 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
235 $ul = $this->mSkin->makeKnownLinkObj( $contribsPage,
236 htmlspecialchars( $row->rev_user_text ),
237 'target=' . urlencode( $row->rev_user_text ) );
238 } else {
239 $userPage =& Title::makeTitle( NS_USER, $row->rev_user_text );
240 $ul = $this->mSkin->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text ) );
241 }
242
243 $s = '<li>';
244 if( $row->rev_deleted ) {
245 $s .= '<span class="deleted">';
246 }
247 $curlink = $this->curLink( $row, $latest );
248 $lastlink = $this->lastLink( $row, $next, $counter );
249 $arbitrary = $this->diffButtons( $row, $latest, $counter );
250 $s .= "({$curlink}) ({$lastlink}) $arbitrary {$link} <span class='user'>{$ul}</span>";
251
252 if( $row->rev_minor_edit ) {
253 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), $message['minoreditletter'] );
254 }
255
256
257 $s .= $this->mSkin->commentBlock( $row->rev_comment, $this->mTitle );
258 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
259 $s .= wfMsg( 'updatedmarker' );
260 }
261 if( $row->rev_deleted ) {
262 $s .= "</span> " . htmlspecialchars( wfMsg( 'deletedrev' ) );
263 }
264 $s .= '</li>';
265
266 return $s;
267 }
268
269 function revLink( $row ) {
270 global $wgUser, $wgLang;
271 $date = $wgLang->timeanddate( $row->rev_timestamp, true );
272 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
273 return $date;
274 } else {
275 return $this->mSkin->makeKnownLinkObj(
276 $this->mTitle,
277 $date,
278 'oldid='.$row->rev_id );
279 }
280 }
281
282 function curLink( $row, $latest ) {
283 global $wgUser;
284 $cur = htmlspecialchars( wfMsg( 'cur' ) );
285 if( $latest
286 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
287 return $cur;
288 } else {
289 return $this->mSkin->makeKnownLinkObj(
290 $this->mTitle,
291 $cur,
292 'diff=0&oldid=' . $row->rev_id );
293 }
294 }
295
296 function lastLink( $row, $next, $counter ) {
297 global $wgUser;
298 $last = htmlspecialchars( wfMsg( 'last' ) );
299 if( is_null( $next )
300 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
301 return $last;
302 } else {
303 return $this->mSkin->makeKnownLinkObj(
304 $this->mTitle,
305 $last,
306 "diff={$row->rev_id}&oldid={$next->rev_id}",
307 '',
308 '',
309 ' tabindex="'.$counter.'"' );
310 }
311 }
312
313 function diffButtons( $row, $latest, $counter ) {
314 global $wgUser;
315 if( $this->linesonpage > 1) {
316 $radio = array(
317 'type' => 'radio',
318 'value' => $row->rev_id,
319 'title' => wfMsg( 'selectolderversionfordiff' )
320 );
321 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
322 $radio['disabled'] = 'disabled';
323 }
324
325 # XXX: move title texts to javascript
326 if ( $latest ) {
327 $first = wfElement( 'input', array_merge(
328 $radio,
329 array(
330 'style' => 'visibility:hidden',
331 'name' => 'oldid' ) ) );
332 $checkmark = array( 'checked' => 'checked' );
333 } else {
334 if( $counter == 2 ) {
335 $checkmark = array( 'checked' => 'checked' );
336 } else {
337 $checkmark = array();
338 }
339 $first = wfElement( 'input', array_merge(
340 $radio,
341 $checkmark,
342 array( 'name' => 'oldid' ) ) );
343 $checkmark = array();
344 }
345 $second = wfElement( 'input', array_merge(
346 $radio,
347 $checkmark,
348 array( 'name' => 'diff' ) ) );
349 return $first . $second;
350 } else {
351 return '';
352 }
353 }
354
355 function getLastOffset($id, $step = 50) {
356 $db =& wfGetDB(DB_SLAVE);
357 $sql = "SELECT rev_timestamp FROM revision WHERE rev_page = $id ORDER BY rev_timestamp ASC LIMIT $step";
358 $res = $db->query($sql, "getLastOffset");
359 $n = $db->numRows($res);
360
361 if ($n == 0)
362 return NULL;
363
364 while ($n--)
365 $obj = $db->fetchObject($res);
366 return $obj->rev_timestamp;
367 }
368 }
369
370 ?>