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