when we're at the first or last revision, do not hyperlink "previous" and "next"...
[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 $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 if ($offset)
155 $prevtext = "<a href=\"$prevurl\">".wfMsg("prevn", $limit)."</a>";
156 else $prevtext = wfMsg("prevn", $limit);
157 if ($revs >= $limitplus)
158 $nexttext = "<a href=\"$nexturl\">".wfMsg("nextn", $limit)."</a>";
159 else $nexttext = wfMsg("nextn", $limit);
160
161 $numbar = "$firstlast " . wfMsg("viewprevnext", $prevtext, $nexttext, $bits);
162
163 $s = $numbar;
164 $s .= $this->beginHistoryList();
165 $counter = 1;
166 foreach($pages as $i => $line) {
167 $first = ($counter == 1 && $offset == 0);
168 $next = isset( $pages[$i + 1] ) ? $pages[$i + 1 ] : null;
169 $s .= $this->historyLine( $line, $next, $counter, $notificationtimestamp, $first );
170 $counter++;
171 }
172 $s .= $this->endHistoryList( !$atend );
173 $s .= $numbar;
174
175 # Validation line
176 if ( isset ( $wgUseValidation ) && $wgUseValidation ) {
177 $s .= "<p>" . Validation::link2statistics ( $this->mArticle ) . "</p>" ;
178 }
179
180 $wgOut->addHTML( $s );
181 wfProfileOut( $fname );
182 }
183
184 function beginHistoryList() {
185 global $wgTitle;
186 $this->lastdate = '';
187 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
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">';
193 return $s;
194 }
195
196 function endHistoryList() {
197 $last = wfMsg( 'last' );
198
199 $s = '</ul>';
200 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
201 $s .= '</form>';
202 return $s;
203 }
204
205 function submitButton( $bits = array() ) {
206 return ( $this->linesonpage > 0 )
207 ? wfElement( 'input', array_merge( $bits,
208 array(
209 'class' => 'historysubmit',
210 'type' => 'submit',
211 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
212 'title' => wfMsg( 'tooltip-compareselectedversions' ),
213 'value' => wfMsg( 'compareselectedversions' ),
214 ) ) )
215 : '';
216 }
217
218 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false ) {
219 global $wgLang, $wgContLang;
220
221 static $message;
222 if( !isset( $message ) ) {
223 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
224 $message[$msg] = wfMsg( $msg );
225 }
226 }
227
228 $link = $this->revLink( $row );
229
230 if ( 0 == $row->rev_user ) {
231 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
232 $ul = $this->mSkin->makeKnownLinkObj( $contribsPage,
233 htmlspecialchars( $row->rev_user_text ),
234 'target=' . urlencode( $row->rev_user_text ) );
235 } else {
236 $userPage =& Title::makeTitle( NS_USER, $row->rev_user_text );
237 $ul = $this->mSkin->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text ) );
238 }
239
240 $s = '<li>';
241 if( $row->rev_deleted ) {
242 $s .= '<span class="deleted">';
243 }
244 $curlink = $this->curLink( $row, $latest );
245 $lastlink = $this->lastLink( $row, $next, $counter );
246 $arbitrary = $this->diffButtons( $row, $latest, $counter );
247 $s .= "({$curlink}) ({$lastlink}) $arbitrary {$link} <span class='user'>{$ul}</span>";
248
249 if( $row->rev_minor_edit ) {
250 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), $message['minoreditletter'] );
251 }
252
253
254 $s .= $this->mSkin->commentBlock( $row->rev_comment, $this->mTitle );
255 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
256 $s .= wfMsg( 'updatedmarker' );
257 }
258 if( $row->rev_deleted ) {
259 $s .= "</span> " . htmlspecialchars( wfMsg( 'deletedrev' ) );
260 }
261 $s .= '</li>';
262
263 return $s;
264 }
265
266 function revLink( $row ) {
267 global $wgUser, $wgLang;
268 $date = $wgLang->timeanddate( $row->rev_timestamp, true );
269 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
270 return $date;
271 } else {
272 return $this->mSkin->makeKnownLinkObj(
273 $this->mTitle,
274 $date,
275 'oldid='.$row->rev_id );
276 }
277 }
278
279 function curLink( $row, $latest ) {
280 global $wgUser;
281 $cur = htmlspecialchars( wfMsg( 'cur' ) );
282 if( $latest
283 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
284 return $cur;
285 } else {
286 return $this->mSkin->makeKnownLinkObj(
287 $this->mTitle,
288 $cur,
289 'diff=0&oldid=' . $row->rev_id );
290 }
291 }
292
293 function lastLink( $row, $next, $counter ) {
294 global $wgUser;
295 $last = htmlspecialchars( wfMsg( 'last' ) );
296 if( is_null( $next )
297 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
298 return $last;
299 } else {
300 return $this->mSkin->makeKnownLinkObj(
301 $this->mTitle,
302 $last,
303 "diff={$row->rev_id}&oldid={$next->rev_id}",
304 '',
305 '',
306 ' tabindex="'.$counter.'"' );
307 }
308 }
309
310 function diffButtons( $row, $latest, $counter ) {
311 global $wgUser;
312 if( $this->linesonpage > 1) {
313 $radio = array(
314 'type' => 'radio',
315 'value' => $row->rev_id,
316 'title' => wfMsg( 'selectolderversionfordiff' )
317 );
318 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
319 $radio['disabled'] = 'disabled';
320 }
321
322 # XXX: move title texts to javascript
323 if ( $latest ) {
324 $first = wfElement( 'input', array_merge(
325 $radio,
326 array(
327 'style' => 'visibility:hidden',
328 'name' => 'oldid' ) ) );
329 $checkmark = array( 'checked' => 'checked' );
330 } else {
331 if( $counter == 2 ) {
332 $checkmark = array( 'checked' => 'checked' );
333 } else {
334 $checkmark = array();
335 }
336 $first = wfElement( 'input', array_merge(
337 $radio,
338 $checkmark,
339 array( 'name' => 'oldid' ) ) );
340 $checkmark = array();
341 }
342 $second = wfElement( 'input', array_merge(
343 $radio,
344 $checkmark,
345 array( 'name' => 'diff' ) ) );
346 return $first . $second;
347 } else {
348 return '';
349 }
350 }
351
352 function getLastOffset($id, $step = 50) {
353 $db =& wfGetDB(DB_SLAVE);
354 $sql = "SELECT rev_timestamp FROM revision WHERE rev_page = $id ORDER BY rev_timestamp ASC LIMIT $step";
355 $res = $db->query($sql, "getLastOffset");
356 $n = $db->numRows($res);
357
358 if ($n == 0)
359 return NULL;
360
361 while ($n--)
362 $obj = $db->fetchObject($res);
363 return $obj->rev_timestamp;
364 }
365 }
366
367 ?>