prettified for rob
[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 'rev_id,rev_page,rev_text_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user,rev_user_text,'.
167 'rev_deleted ' .
168 "FROM $page,$revision $use_index " .
169 "WHERE page_id=rev_page AND $userCond $nscond $offsetQuery " .
170 'ORDER BY rev_timestamp DESC';
171 $sql = $this->dbr->limitResult( $sql, $this->limit, 0 );
172 return $sql;
173 }
174
175 /**
176 * This do the search for the user given when creating the object.
177 * It should probably be the only public function in this class.
178 * @return Array of contributions.
179 */
180 function find() {
181 $contribs = array();
182 $res = $this->dbr->query( $this->makeSql(), __METHOD__ );
183 while ( $c = $this->dbr->fetchObject( $res ) )
184 $contribs[] = $c;
185 $this->dbr->freeResult( $res );
186 return $contribs;
187 }
188 };
189
190 /**
191 * Special page "user contributions".
192 * Shows a list of the contributions of a user.
193 *
194 * @return none
195 * @param $par String: (optional) user name of the user for which to show the contributions
196 */
197 function wfSpecialContributions( $par = null ) {
198 global $wgUser, $wgOut, $wgLang, $wgRequest;
199
200 $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
201 if ( !strlen( $target ) ) {
202 $wgOut->addHTML( contributionsForm( '' ) );
203 return;
204 }
205
206 $nt = Title::newFromURL( $target );
207 if ( !$nt ) {
208 $wgOut->addHTML( contributionsForm( '' ) );
209 return;
210 }
211
212 $options = array();
213
214 list( $options['limit'], $options['offset']) = wfCheckLimits();
215 $options['offset'] = $wgRequest->getVal( 'offset' );
216
217 /* Offset must either be numeric, or a "standard" timestamp */
218 $dbr = wfGetDB( DB_SLAVE );
219 if ( !strlen( $options['offset'] )
220 || ($dbr->realTimestamps()
221 && !preg_match ( '/^\d\d\d\d\-\d\d\-\d\d \d\d:\d\d:\d\d(?:[+-]\d\d)?$/', $options['offset'] )
222 )
223 || (!$dbr->realTimestamps()
224 && !preg_match( '/^[0-9]+$/', $options['offset'] )
225 )
226 )
227 $options['offset'] = '';
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 if ( preg_match( "/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/(24|16)/", $target ) ) {
271 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $target ) );
272 } else {
273 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', contributionsSub( $nt ) ) );
274 }
275
276 $id = User::idFromName( $nt->getText() );
277 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
278
279 $wgOut->addHTML( contributionsForm( $options) );
280
281 $contribs = $finder->find();
282
283 if ( count( $contribs ) == 0) {
284 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
285 return;
286 }
287
288 list( $early, $late ) = $finder->getEditLimits();
289 $lastts = count( $contribs ) ? $contribs[count( $contribs ) - 1]->rev_timestamp : 0;
290 $atstart = ( !count( $contribs ) || $late == $contribs[0]->rev_timestamp );
291 $atend = ( !count( $contribs ) || $early == $lastts );
292
293 // These four are defaults
294 $newestlink = wfMsgHtml( 'sp-contributions-newest' );
295 $oldestlink = wfMsgHtml( 'sp-contributions-oldest' );
296 $newerlink = wfMsgHtml( 'sp-contributions-newer', $options['limit'] );
297 $olderlink = wfMsgHtml( 'sp-contributions-older', $options['limit'] );
298
299 if ( !$atstart ) {
300 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => '' ), $options ) );
301 $newestlink = "<a href=\"$stuff\">$newestlink</a>";
302 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'prev' ), $options ) );
303 $newerlink = "<a href=\"$stuff\">$newerlink</a>";
304 }
305
306 if ( !$atend ) {
307 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'go' => 'first' ), $options ) );
308 $oldestlink = "<a href=\"$stuff\">$oldestlink</a>";
309 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'offset' => $lastts ), $options ) );
310 $olderlink = "<a href=\"$stuff\">$olderlink</a>";
311 }
312
313 if ( $target == 'newbies' ) {
314 $firstlast ="($newestlink)";
315 } else {
316 $firstlast = "($newestlink | $oldestlink)";
317 }
318
319 $urls = array();
320 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
321 $stuff = $title->escapeLocalURL( wfArrayToCGI( array( 'limit' => $num ), $options ) );
322 $urls[] = "<a href=\"$stuff\">".$wgLang->formatNum( $num )."</a>";
323 }
324 $bits = implode( $urls, ' | ' );
325
326 $prevnextbits = $firstlast .' '. wfMsgHtml( 'viewprevnext', $newerlink, $olderlink, $bits );
327
328 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
329
330 $wgOut->addHTML( "<ul>\n" );
331
332 $sk = $wgUser->getSkin();
333 foreach ( $contribs as $contrib )
334 $wgOut->addHTML( ucListEdit( $sk, $contrib ) );
335
336 $wgOut->addHTML( "</ul>\n" );
337 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
338 }
339
340 /**
341 * Generates the subheading with links
342 * @param $nt @see Title object for the target
343 */
344 function contributionsSub( $nt ) {
345 global $wgSysopUserBans, $wgLang, $wgUser;
346
347 $sk = $wgUser->getSkin();
348 $id = User::idFromName( $nt->getText() );
349
350 if ( 0 == $id ) {
351 $ul = $nt->getText();
352 } else {
353 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
354 }
355 $talk = $nt->getTalkPage();
356 if( $talk ) {
357 # Talk page link
358 $tools[] = $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) );
359 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
360 # Block link
361 if( $wgUser->isAllowed( 'block' ) )
362 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
363 # Block log link
364 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
365 }
366 # Other logs link
367 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
368 $ul .= ' (' . implode( ' | ', $tools ) . ')';
369 }
370 return $ul;
371 }
372
373 /**
374 * Generates the namespace selector form with hidden attributes.
375 * @param $options Array: the options to be included.
376 */
377 function contributionsForm( $options ) {
378 global $wgScript, $wgTitle;
379
380 $options['title'] = $wgTitle->getPrefixedText();
381
382 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
383
384 foreach ( $options as $name => $value ) {
385 if( $name === 'namespace') continue;
386 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
387 }
388
389 $f .= '<fieldset>' .
390 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
391 Xml::inputLabel( wfMsg( 'sp-contributions-username' ), 'target', 'target', 20 , $options['target']) . ' ' .
392 wfMsgHtml( 'namespace' ) . ' ' .
393 Xml::namespaceSelector( $options['namespace'], '' ) .
394 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
395 "</fieldset></form>\n";
396 return $f;
397 }
398
399 /**
400 * Generates each row in the contributions list.
401 *
402 * Contributions which are marked "top" are currently on top of the history.
403 * For these contributions, a [rollback] link is shown for users with sysop
404 * privileges. The rollback link restores the most recent version that was not
405 * written by the target user.
406 *
407 * @todo This would probably look a lot nicer in a table.
408 */
409 function ucListEdit( $sk, $row ) {
410 $fname = 'ucListEdit';
411 wfProfileIn( $fname );
412
413 global $wgLang, $wgUser, $wgRequest;
414 static $messages;
415 if( !isset( $messages ) ) {
416 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
417 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
418 }
419 }
420
421 $rev = new Revision( $row );
422
423 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
424 $link = $sk->makeKnownLinkObj( $page );
425 $difftext = $topmarktext = '';
426 if( $row->rev_id == $row->page_latest ) {
427 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
428 if( !$row->page_is_new ) {
429 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
430 } else {
431 $difftext .= $messages['newarticle'];
432 }
433
434 if( $wgUser->isAllowed( 'rollback' ) ) {
435 $topmarktext .= ' '.$sk->generateRollback( $rev );
436 }
437
438 }
439 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
440 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
441 } else {
442 $difftext = '(' . $messages['diff'] . ')';
443 }
444 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
445
446 $comment = $sk->revComment( $rev );
447 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
448
449 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
450 $d = '<span class="history-deleted">' . $d . '</span>';
451 }
452
453 if( $row->rev_minor_edit ) {
454 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
455 } else {
456 $mflag = '';
457 }
458
459 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
460 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
461 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
462 }
463 $ret = "<li>$ret</li>\n";
464 wfProfileOut( $fname );
465 return $ret;
466 }
467
468 ?>