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