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