Check for valid timestamp input, thanks to Brion for pointing this out.
[lhc/web/wiklou.git] / includes / SpecialContributions.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage SpecialPage
5 */
6
7 /** @package MediaWiki */
8 class ContribsFinder {
9 var $username, $offset, $limit, $namespace;
10 var $dbr;
11
12 function ContribsFinder( $username ) {
13 $this->username = $username;
14 $this->namespace = false;
15 $this->dbr =& wfGetDB( DB_SLAVE );
16 }
17
18 function setNamespace( $ns ) {
19 $this->namespace = $ns;
20 }
21
22 function setLimit( $limit ) {
23 $this->limit = $limit;
24 }
25
26 function setOffset( $offset ) {
27 $this->offset = $offset;
28 }
29
30 function getEditLimit( $dir ) {
31 list( $index, $usercond ) = $this->getUserCond();
32 $nscond = $this->getNamespaceCond();
33 $use_index = $this->dbr->useIndexClause( $index );
34 list( $revision, $page) = $this->dbr->tableNamesN( 'revision', 'page' );
35 $sql = "SELECT rev_timestamp " .
36 " FROM $page,$revision $use_index " .
37 " WHERE rev_page=page_id AND $usercond $nscond" .
38 " ORDER BY rev_timestamp $dir LIMIT 1";
39
40 $res = $this->dbr->query( $sql, __METHOD__ );
41 $row = $this->dbr->fetchObject( $res );
42 if ( $row ) {
43 return $row->rev_timestamp;
44 } else {
45 return false;
46 }
47 }
48
49 function getEditLimits() {
50 return array(
51 $this->getEditLimit( "ASC" ),
52 $this->getEditLimit( "DESC" )
53 );
54 }
55
56 function getUserCond() {
57 $condition = '';
58
59 if ( $this->username == 'newbies' ) {
60 $max = $this->dbr->selectField( 'user', 'max(user_id)', false, 'make_sql' );
61 $condition = '>' . (int)($max - $max / 100);
62 }
63
64 if ( $condition == '' ) {
65 $condition = ' rev_user_text=' . $this->dbr->addQuotes( $this->username );
66 $index = 'usertext_timestamp';
67 } else {
68 $condition = ' rev_user '.$condition ;
69 $index = 'user_timestamp';
70 }
71 return array( $index, $condition );
72 }
73
74 function getNamespaceCond() {
75 if ( $this->namespace !== false )
76 return ' AND page_namespace = ' . (int)$this->namespace;
77 return '';
78 }
79
80 function getPreviousOffsetForPaging() {
81 list( $index, $usercond ) = $this->getUserCond();
82 $nscond = $this->getNamespaceCond();
83
84 $use_index = $this->dbr->useIndexClause( $index );
85 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
86
87 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
88 "WHERE page_id = rev_page AND rev_timestamp > '" . $this->offset . "' AND " .
89 $usercond . $nscond;
90 $sql .= " ORDER BY rev_timestamp ASC";
91 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
92 $res = $this->dbr->query( $sql );
93
94 $numRows = $this->dbr->numRows( $res );
95 if ( $numRows ) {
96 $this->dbr->dataSeek( $res, $numRows - 1 );
97 $row = $this->dbr->fetchObject( $res );
98 $offset = $row->rev_timestamp;
99 } else {
100 $offset = false;
101 }
102 $this->dbr->freeResult( $res );
103 return $offset;
104 }
105
106 function getFirstOffsetForPaging() {
107 list( $index, $usercond ) = $this->getUserCond();
108 $use_index = $this->dbr->useIndexClause( $index );
109 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
110 $nscond = $this->getNamespaceCond();
111 $sql = "SELECT rev_timestamp FROM $page, $revision $use_index " .
112 "WHERE page_id = rev_page AND " .
113 $usercond . $nscond;
114 $sql .= " ORDER BY rev_timestamp ASC";
115 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
116 $res = $this->dbr->query( $sql );
117
118 $numRows = $this->dbr->numRows( $res );
119 if ( $numRows ) {
120 $this->dbr->dataSeek( $res, $numRows - 1 );
121 $row = $this->dbr->fetchObject( $res );
122 $offset = $row->rev_timestamp;
123 } else {
124 $offset = false;
125 }
126 $this->dbr->freeResult( $res );
127 return $offset;
128 }
129
130 /* private */ function makeSql() {
131 $offsetQuery = '';
132
133 list( $page, $revision ) = $this->dbr->tableNamesN( 'page', 'revision' );
134 list( $index, $userCond ) = $this->getUserCond();
135
136 if ( $this->offset )
137 $offsetQuery = "AND rev_timestamp <= '{$this->offset}'";
138
139 $nscond = $this->getNamespaceCond();
140 $use_index = $this->dbr->useIndexClause( $index );
141 $sql = "SELECT
142 page_namespace,page_title,page_is_new,page_latest,
143 rev_id,rev_page,rev_text_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user,rev_user_text,
144 rev_deleted
145 FROM $page,$revision $use_index
146 WHERE page_id=rev_page AND $userCond $nscond $offsetQuery
147 ORDER BY rev_timestamp DESC";
148 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
149 return $sql;
150 }
151
152 function find() {
153 $contribs = array();
154 $res = $this->dbr->query( $this->makeSql(), __METHOD__ );
155 while ( $c = $this->dbr->fetchObject( $res ) )
156 $contribs[] = $c;
157 $this->dbr->freeResult( $res );
158 return $contribs;
159 }
160 };
161
162 /**
163 * Special page "user contributions".
164 * Shows a list of the contributions of a user.
165 *
166 * @return none
167 * @param $par String: (optional) user name of the user for which to show the contributions
168 */
169 function wfSpecialContributions( $par = null ) {
170 global $wgUser, $wgOut, $wgLang, $wgRequest;
171
172 $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
173 if ( !strlen( $target ) ) {
174 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
175 return;
176 }
177
178 $nt = Title::newFromURL( $target );
179 if ( !$nt ) {
180 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
181 return;
182 }
183
184 $options = array();
185
186 list( $options['limit'], $options['offset']) = wfCheckLimits();
187 $options['offset'] = $wgRequest->getVal( 'offset' );
188 /* Check that the offset is valid (e.g. integer or timestamp) */
189 if ( !strlen( $options['offset'] ) )
190 $options['offset'] = '';
191 else {
192 $dbr =& wfGetDB( DB_SLAVE );
193 if ( !$dbr->realTimestamps() ) {
194 if (!preg_match( '/^[0-9]+$/', $options['offset'] ) )
195 $options['offset'] = '';
196 }
197 else if ( !preg_match( '/^[0-9\-\+: ]+$/', $options['offset'] ) )
198 $options['offset'] = '';
199 }
200
201 $title = SpecialPage::getTitleFor( 'Contributions' );
202 $options['target'] = $target;
203
204 $nt =& Title::makeTitle( NS_USER, $nt->getDBkey() );
205 $finder = new ContribsFinder( ( $target == 'newbies' ) ? 'newbies' : $nt->getText() );
206 $finder->setLimit( $options['limit'] );
207 $finder->setOffset( $options['offset'] );
208
209 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
210 $options['namespace'] = intval( $ns );
211 $finder->setNamespace( $options['namespace'] );
212 } else {
213 $options['namespace'] = '';
214 }
215
216 if ( $wgUser->isAllowed( 'rollback' ) && $wgRequest->getBool( 'bot' ) ) {
217 $options['bot'] = '1';
218 }
219
220 if ( $wgRequest->getText( 'go' ) == 'prev' ) {
221 $offset = $finder->getPreviousOffsetForPaging();
222 if ( $offset !== false ) {
223 $options['offset'] = $offset;
224 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
225 $wgOut->redirect( $prevurl );
226 return;
227 }
228 }
229
230 if ( $wgRequest->getText( 'go' ) == 'first' && $target != 'newbies') {
231 $offset = $finder->getFirstOffsetForPaging();
232 if ( $offset !== false ) {
233 $options['offset'] = $offset;
234 $prevurl = $title->getLocalURL( wfArrayToCGI( $options ) );
235 $wgOut->redirect( $prevurl );
236 return;
237 }
238 }
239
240 if ( $target == 'newbies' ) {
241 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
242 } else {
243 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
244 }
245
246 $id = User::idFromName( $nt->getText() );
247 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
248
249 $wgOut->addHTML( contributionsForm( $options) );
250
251 $contribs = $finder->find();
252
253 if ( count( $contribs ) == 0) {
254 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
255 return;
256 }
257
258 list( $early, $late ) = $finder->getEditLimits();
259 $lastts = count( $contribs ) ? $contribs[count( $contribs ) - 1]->rev_timestamp : 0;
260 $atstart = ( !count( $contribs ) || $late == $contribs[0]->rev_timestamp );
261 $atend = ( !count( $contribs ) || $early == $lastts );
262
263 // These four are defaults
264 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
265 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
266 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
267 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
268
269 if ( !$atstart ) {
270 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
271 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
272 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
273 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
274 }
275
276 if ( !$atend ) {
277 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
278 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
279 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
280 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
281 }
282
283 if ( $target == 'newbies' ) {
284 $firstlast ="($newestlink)";
285 } else {
286 $firstlast = "($newestlink | $oldestlink)";
287 }
288
289 $urls = array();
290 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
291 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
292 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
293 }
294 $bits = implode( $urls, ' | ' );
295
296 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
297
298 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
299
300 $wgOut->addHTML( "<ul>\n" );
301
302 $sk = $wgUser->getSkin();
303 foreach ( $contribs as $contrib )
304 $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
305
306 $wgOut->addHTML( "</ul>\n" );
307 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
308 }
309
310 /**
311 * Generates the subheading with links
312 * @param $nt @see Title object for the target
313 */
314 function contributionsSub( $nt ) {
315 global $wgSysopUserBans, $wgLang, $wgUser;
316
317 $sk = $wgUser->getSkin();
318 $id = User::idFromName( $nt->getText() );
319
320 if ( 0 == $id ) {
321 $ul = $nt->getText();
322 } else {
323 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
324 }
325 $talk = $nt->getTalkPage();
326 if( $talk ) {
327 # Talk page link
328 $tools[] = $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) );
329 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
330 # Block link
331 if( $wgUser->isAllowed( 'block' ) )
332 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
333 # Block log link
334 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), htmlspecialchars( LogPage::logName( 'block' ) ), 'type=block&page=' . $nt->getPrefixedUrl() );
335 }
336 # Other logs link
337 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
338 $ul .= ' (' . implode( ' | ', $tools ) . ')';
339 }
340 return $ul;
341 }
342
343 /**
344 * Generates the namespace selector form with hidden attributes.
345 * @param $options Array: the options to be included.
346 */
347 function contributionsForm( $options ) {
348 global $wgScript, $wgTitle;
349
350 $options['title'] = $wgTitle->getPrefixedText();
351
352 $f = "<form method='get' action=\"$wgScript\">\n";
353 foreach ( $options as $name => $value ) {
354 if( $name === 'namespace') continue;
355 $f .= "\t" . wfElement( 'input', array(
356 'name' => $name,
357 'type' => 'hidden',
358 'value' => $value ) ) . "\n";
359 }
360
361 $f .= '<p>' . wfMsgHtml( 'namespace' ) . ' ' .
362 HTMLnamespaceselector( $options['namespace'], '' ) .
363 wfElement( 'input', array(
364 'type' => 'submit',
365 'value' => wfMsg( 'allpagessubmit' ) )
366 ) .
367 "</p></form>\n";
368
369 return $f;
370 }
371
372 /**
373 * Generates each row in the contributions list.
374 *
375 * Contributions which are marked "top" are currently on top of the history.
376 * For these contributions, a [rollback] link is shown for users with sysop
377 * privileges. The rollback link restores the most recent version that was not
378 * written by the target user.
379 *
380 * @todo This would probably look a lot nicer in a table.
381 */
382 function ucListEdit( $sk, $row ) {
383 $fname = 'ucListEdit';
384 wfProfileIn( $fname );
385
386 global $wgLang, $wgUser, $wgRequest;
387 static $messages;
388 if( !isset( $messages ) ) {
389 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
390 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
391 }
392 }
393
394 $rev = new Revision( $row );
395
396 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
397 $link = $sk->makeKnownLinkObj( $page );
398 $difftext = $topmarktext = '';
399 if( $row->rev_id == $row->page_latest ) {
400 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
401 if( !$row->page_is_new ) {
402 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
403 } else {
404 $difftext .= $messages['newarticle'];
405 }
406
407 if( $wgUser->isAllowed( 'rollback' ) ) {
408 $topmarktext .= ' '.$sk->generateRollback( $rev );
409 }
410
411 }
412 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
413 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
414 } else {
415 $difftext = '(' . $messages['diff'] . ')';
416 }
417 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
418
419 $comment = $sk->revComment( $rev );
420 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
421
422 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
423 $d = '<span class="history-deleted">' . $d . '</span>';
424 }
425
426 if( $row->rev_minor_edit ) {
427 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
428 } else {
429 $mflag = '';
430 }
431
432 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
433 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
434 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
435 }
436 $ret = "<li>$ret</li>\n";
437 wfProfileOut( $fname );
438 return $ret;
439 }
440
441 ?>