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