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