d077b76c5d419ed0f4692af82dfa7e0f44416702
[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 foreach ( $contribs as $contrib )
295 $wgOut->addHTML( ucListEdit( $contrib ) );
296
297 $wgOut->addHTML( "</ul>\n" );
298 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n" );
299 }
300
301 /**
302 * Generates the subheading with links
303 * @param $nt @see Title object for the target
304 */
305 function contributionsSub( $nt ) {
306 global $wgSysopUserBans, $wgLang, $wgUser;
307
308 $id = User::idFromName( $nt->getText() );
309
310 if ( 0 == $id ) {
311 $ul = $nt->getText();
312 } else {
313 $ul = Linker::makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
314 }
315 $talk = $nt->getTalkPage();
316 if( $talk ) {
317 # Talk page link
318 $tools[] = Linker::makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) );
319 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
320 # Block link
321 if( $wgUser->isAllowed( 'block' ) )
322 $tools[] = Linker::makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
323 # Block log link
324 $tools[] = Linker::makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), htmlspecialchars( LogPage::logName( 'block' ) ), 'type=block&page=' . $nt->getPrefixedUrl() );
325 }
326 # Other logs link
327 $tools[] = Linker::makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
328 $ul .= ' (' . implode( ' | ', $tools ) . ')';
329 }
330 return $ul;
331 }
332
333 /**
334 * Generates the namespace selector form with hidden attributes.
335 * @param $options Array: the options to be included.
336 */
337 function contributionsForm( $options ) {
338 global $wgScript, $wgTitle;
339
340 $options['title'] = $wgTitle->getPrefixedText();
341
342 $f = "<form method='get' action=\"$wgScript\">\n";
343 foreach ( $options as $name => $value ) {
344 if( $name === 'namespace') continue;
345 $f .= "\t" . wfElement( 'input', array(
346 'name' => $name,
347 'type' => 'hidden',
348 'value' => $value ) ) . "\n";
349 }
350
351 $f .= '<p>' . wfMsgHtml( 'namespace' ) . ' ' .
352 HTMLnamespaceselector( $options['namespace'], '' ) .
353 wfElement( 'input', array(
354 'type' => 'submit',
355 'value' => wfMsg( 'allpagessubmit' ) )
356 ) .
357 "</p></form>\n";
358
359 return $f;
360 }
361
362 /**
363 * Generates each row in the contributions list.
364 *
365 * Contributions which are marked "top" are currently on top of the history.
366 * For these contributions, a [rollback] link is shown for users with sysop
367 * privileges. The rollback link restores the most recent version that was not
368 * written by the target user.
369 *
370 * If the contributions page is called with the parameter &bot=1, all rollback
371 * links also get that parameter. It causes the edit itself and the rollback
372 * to be marked as "bot" edits. Bot edits are hidden by default from recent
373 * changes, so this allows sysops to combat a busy vandal without bothering
374 * other users.
375 *
376 * @todo This would probably look a lot nicer in a table.
377 */
378 function ucListEdit( $row ) {
379 $fname = 'ucListEdit';
380 wfProfileIn( $fname );
381
382 global $wgLang, $wgUser, $wgRequest;
383 static $messages;
384 if( !isset( $messages ) ) {
385 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
386 $messages[$msg] = wfMsgExt( $msg, array( 'escape') );
387 }
388 }
389
390 $rev = new Revision( $row );
391
392 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
393 $link = Linker::makeKnownLinkObj( $page );
394 $difftext = $topmarktext = '';
395 if( $row->rev_id == $row->page_latest ) {
396 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
397 if( !$row->page_is_new ) {
398 $difftext .= '(' . Linker::makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
399 } else {
400 $difftext .= $messages['newarticle'];
401 }
402
403 if( $wgUser->isAllowed( 'rollback' ) ) {
404 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
405 $extraRollback .= '&token=' . urlencode(
406 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text ) ) );
407 $topmarktext .= ' ['. Linker::makeKnownLinkObj( $page,
408 $messages['rollbacklink'],
409 'action=rollback&from=' . urlencode( $row->rev_user_text ) . $extraRollback ) .']';
410 }
411
412 }
413 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
414 $difftext = '(' . Linker::makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
415 } else {
416 $difftext = '(' . $messages['diff'] . ')';
417 }
418 $histlink = '(' . Linker::makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
419
420 $comment = Linker::revComment( $rev );
421 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
422
423 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
424 $d = '<span class="history-deleted">' . $d . '</span>';
425 }
426
427 if( $row->rev_minor_edit ) {
428 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
429 } else {
430 $mflag = '';
431 }
432
433 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
434 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
435 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
436 }
437 $ret = "<li>$ret</li>\n";
438 wfProfileOut( $fname );
439 return $ret;
440 }
441
442 ?>