Log to DBPerformance log when many LB connections are acquired
[lhc/web/wiklou.git] / includes / specials / SpecialDeletedContributions.php
1 <?php
2 /**
3 * Implements Special:DeletedContributions
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Implements Special:DeletedContributions to display archived revisions
26 * @ingroup SpecialPage
27 */
28 class DeletedContribsPager extends IndexPager {
29 public $mDefaultDirection = IndexPager::DIR_DESCENDING;
30 public $messages;
31 public $target;
32 public $namespace = '';
33 public $mDb;
34
35 /**
36 * @var string Navigation bar with paging links.
37 */
38 protected $mNavigationBar;
39
40 function __construct( IContextSource $context, $target, $namespace = false ) {
41 parent::__construct( $context );
42 $msgs = array( 'deletionlog', 'undeleteviewlink', 'diff' );
43 foreach ( $msgs as $msg ) {
44 $this->messages[$msg] = $this->msg( $msg )->escaped();
45 }
46 $this->target = $target;
47 $this->namespace = $namespace;
48 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
49 }
50
51 function getDefaultQuery() {
52 $query = parent::getDefaultQuery();
53 $query['target'] = $this->target;
54
55 return $query;
56 }
57
58 function getQueryInfo() {
59 list( $index, $userCond ) = $this->getUserCond();
60 $conds = array_merge( $userCond, $this->getNamespaceCond() );
61 $user = $this->getUser();
62 // Paranoia: avoid brute force searches (bug 17792)
63 if ( !$user->isAllowed( 'deletedhistory' ) ) {
64 $conds[] = $this->mDb->bitAnd( 'ar_deleted', Revision::DELETED_USER ) . ' = 0';
65 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
66 $conds[] = $this->mDb->bitAnd( 'ar_deleted', Revision::SUPPRESSED_USER ) .
67 ' != ' . Revision::SUPPRESSED_USER;
68 }
69
70 return array(
71 'tables' => array( 'archive' ),
72 'fields' => array(
73 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp', 'ar_comment',
74 'ar_minor_edit', 'ar_user', 'ar_user_text', 'ar_deleted'
75 ),
76 'conds' => $conds,
77 'options' => array( 'USE INDEX' => $index )
78 );
79 }
80
81 function getUserCond() {
82 $condition = array();
83
84 $condition['ar_user_text'] = $this->target;
85 $index = 'usertext_timestamp';
86
87 return array( $index, $condition );
88 }
89
90 function getIndexField() {
91 return 'ar_timestamp';
92 }
93
94 function getStartBody() {
95 return "<ul>\n";
96 }
97
98 function getEndBody() {
99 return "</ul>\n";
100 }
101
102 function getNavigationBar() {
103 if ( isset( $this->mNavigationBar ) ) {
104 return $this->mNavigationBar;
105 }
106
107 $linkTexts = array(
108 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
109 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
110 'first' => $this->msg( 'histlast' )->escaped(),
111 'last' => $this->msg( 'histfirst' )->escaped()
112 );
113
114 $pagingLinks = $this->getPagingLinks( $linkTexts );
115 $limitLinks = $this->getLimitLinks();
116 $lang = $this->getLanguage();
117 $limits = $lang->pipeList( $limitLinks );
118
119 $firstLast = $lang->pipeList( array( $pagingLinks['first'], $pagingLinks['last'] ) );
120 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
121 $prevNext = $this->msg( 'viewprevnext' )
122 ->rawParams(
123 $pagingLinks['prev'],
124 $pagingLinks['next'],
125 $limits
126 )->escaped();
127 $separator = $this->msg( 'word-separator' )->escaped();
128 $this->mNavigationBar = $firstLast . $separator . $prevNext;
129
130 return $this->mNavigationBar;
131 }
132
133 function getNamespaceCond() {
134 if ( $this->namespace !== '' ) {
135 return array( 'ar_namespace' => (int)$this->namespace );
136 } else {
137 return array();
138 }
139 }
140
141 /**
142 * Generates each row in the contributions list.
143 *
144 * Contributions which are marked "top" are currently on top of the history.
145 * For these contributions, a [rollback] link is shown for users with sysop
146 * privileges. The rollback link restores the most recent version that was not
147 * written by the target user.
148 *
149 * @todo This would probably look a lot nicer in a table.
150 * @param stdClass $row
151 * @return string
152 */
153 function formatRow( $row ) {
154
155 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
156
157 $rev = new Revision( array(
158 'title' => $page,
159 'id' => $row->ar_rev_id,
160 'comment' => $row->ar_comment,
161 'user' => $row->ar_user,
162 'user_text' => $row->ar_user_text,
163 'timestamp' => $row->ar_timestamp,
164 'minor_edit' => $row->ar_minor_edit,
165 'deleted' => $row->ar_deleted,
166 ) );
167
168 $undelete = SpecialPage::getTitleFor( 'Undelete' );
169
170 $logs = SpecialPage::getTitleFor( 'Log' );
171 $dellog = Linker::linkKnown(
172 $logs,
173 $this->messages['deletionlog'],
174 array(),
175 array(
176 'type' => 'delete',
177 'page' => $page->getPrefixedText()
178 )
179 );
180
181 $reviewlink = Linker::linkKnown(
182 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
183 $this->messages['undeleteviewlink']
184 );
185
186 $user = $this->getUser();
187
188 if ( $user->isAllowed( 'deletedtext' ) ) {
189 $last = Linker::linkKnown(
190 $undelete,
191 $this->messages['diff'],
192 array(),
193 array(
194 'target' => $page->getPrefixedText(),
195 'timestamp' => $rev->getTimestamp(),
196 'diff' => 'prev'
197 )
198 );
199 } else {
200 $last = $this->messages['diff'];
201 }
202
203 $comment = Linker::revComment( $rev );
204 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
205 $date = htmlspecialchars( $date );
206
207 if ( !$user->isAllowed( 'undelete' ) || !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
208 $link = $date; // unusable link
209 } else {
210 $link = Linker::linkKnown(
211 $undelete,
212 $date,
213 array( 'class' => 'mw-changeslist-date' ),
214 array(
215 'target' => $page->getPrefixedText(),
216 'timestamp' => $rev->getTimestamp()
217 )
218 );
219 }
220 // Style deleted items
221 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
222 $link = '<span class="history-deleted">' . $link . '</span>';
223 }
224
225 $pagelink = Linker::link(
226 $page,
227 null,
228 array( 'class' => 'mw-changeslist-title' )
229 );
230
231 if ( $rev->isMinor() ) {
232 $mflag = ChangesList::flag( 'minor' );
233 } else {
234 $mflag = '';
235 }
236
237 // Revision delete link
238 $del = Linker::getRevDeleteLink( $user, $rev, $page );
239 if ( $del ) {
240 $del .= ' ';
241 }
242
243 $tools = Html::rawElement(
244 'span',
245 array( 'class' => 'mw-deletedcontribs-tools' ),
246 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
247 array( $last, $dellog, $reviewlink ) ) )->escaped()
248 );
249
250 $separator = '<span class="mw-changeslist-separator">. .</span>';
251 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
252
253 # Denote if username is redacted for this edit
254 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
255 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
256 }
257
258 $ret = Html::rawElement( 'li', array(), $ret ) . "\n";
259
260
261 return $ret;
262 }
263
264 /**
265 * Get the Database object in use
266 *
267 * @return IDatabase
268 */
269 public function getDatabase() {
270 return $this->mDb;
271 }
272 }
273
274 class DeletedContributionsPage extends SpecialPage {
275 function __construct() {
276 parent::__construct( 'DeletedContributions', 'deletedhistory',
277 /*listed*/true, /*function*/false, /*file*/false );
278 }
279
280 /**
281 * Special page "deleted user contributions".
282 * Shows a list of the deleted contributions of a user.
283 *
284 * @param string $par (optional) user name of the user for which to show the contributions
285 */
286 function execute( $par ) {
287 $this->setHeaders();
288 $this->outputHeader();
289
290 $user = $this->getUser();
291
292 if ( !$this->userCanExecute( $user ) ) {
293 $this->displayRestrictionError();
294
295 return;
296 }
297
298 $request = $this->getRequest();
299 $out = $this->getOutput();
300 $out->setPageTitle( $this->msg( 'deletedcontributions-title' ) );
301
302 $options = array();
303
304 if ( $par !== null ) {
305 $target = $par;
306 } else {
307 $target = $request->getVal( 'target' );
308 }
309
310 if ( !strlen( $target ) ) {
311 $out->addHTML( $this->getForm( '' ) );
312
313 return;
314 }
315
316 $options['limit'] = $request->getInt( 'limit',
317 $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
318 $options['target'] = $target;
319
320 $userObj = User::newFromName( $target, false );
321 if ( !$userObj ) {
322 $out->addHTML( $this->getForm( '' ) );
323
324 return;
325 }
326 $this->getSkin()->setRelevantUser( $userObj );
327
328 $target = $userObj->getName();
329 $out->addSubtitle( $this->getSubTitle( $userObj ) );
330
331 if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
332 $options['namespace'] = intval( $ns );
333 } else {
334 $options['namespace'] = '';
335 }
336
337 $out->addHTML( $this->getForm( $options ) );
338
339 $pager = new DeletedContribsPager( $this->getContext(), $target, $options['namespace'] );
340 if ( !$pager->getNumRows() ) {
341 $out->addWikiMsg( 'nocontribs' );
342
343 return;
344 }
345
346 # Show a message about slave lag, if applicable
347 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
348 if ( $lag > 0 ) {
349 $out->showLagWarning( $lag );
350 }
351
352 $out->addHTML(
353 '<p>' . $pager->getNavigationBar() . '</p>' .
354 $pager->getBody() .
355 '<p>' . $pager->getNavigationBar() . '</p>' );
356
357 # If there were contributions, and it was a valid user or IP, show
358 # the appropriate "footer" message - WHOIS tools, etc.
359 if ( $target != 'newbies' ) {
360 $message = IP::isIPAddress( $target ) ?
361 'sp-contributions-footer-anon' :
362 'sp-contributions-footer';
363
364 if ( !$this->msg( $message )->isDisabled() ) {
365 $out->wrapWikiMsg(
366 "<div class='mw-contributions-footer'>\n$1\n</div>",
367 array( $message, $target )
368 );
369 }
370 }
371 }
372
373 /**
374 * Generates the subheading with links
375 * @param User $userObj User object for the target
376 * @return string Appropriately-escaped HTML to be output literally
377 * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
378 */
379 function getSubTitle( $userObj ) {
380 if ( $userObj->isAnon() ) {
381 $user = htmlspecialchars( $userObj->getName() );
382 } else {
383 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
384 }
385 $links = '';
386 $nt = $userObj->getUserPage();
387 $id = $userObj->getID();
388 $talk = $nt->getTalkPage();
389 if ( $talk ) {
390 # Talk page link
391 $tools[] = Linker::link( $talk, $this->msg( 'sp-contributions-talk' )->escaped() );
392 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
393 # Block / Change block / Unblock links
394 if ( $this->getUser()->isAllowed( 'block' ) ) {
395 if ( $userObj->isBlocked() ) {
396 $tools[] = Linker::linkKnown( # Change block link
397 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
398 $this->msg( 'change-blocklink' )->escaped()
399 );
400 $tools[] = Linker::linkKnown( # Unblock link
401 SpecialPage::getTitleFor( 'BlockList' ),
402 $this->msg( 'unblocklink' )->escaped(),
403 array(),
404 array(
405 'action' => 'unblock',
406 'ip' => $nt->getDBkey()
407 )
408 );
409 } else {
410 # User is not blocked
411 $tools[] = Linker::linkKnown( # Block link
412 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
413 $this->msg( 'blocklink' )->escaped()
414 );
415 }
416 }
417 # Block log link
418 $tools[] = Linker::linkKnown(
419 SpecialPage::getTitleFor( 'Log' ),
420 $this->msg( 'sp-contributions-blocklog' )->escaped(),
421 array(),
422 array(
423 'type' => 'block',
424 'page' => $nt->getPrefixedText()
425 )
426 );
427 # Suppression log link (bug 59120)
428 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
429 $tools[] = Linker::linkKnown(
430 SpecialPage::getTitleFor( 'Log', 'suppress' ),
431 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
432 array(),
433 array( 'offender' => $userObj->getName() )
434 );
435 }
436 }
437
438 # Uploads
439 $tools[] = Linker::linkKnown(
440 SpecialPage::getTitleFor( 'Listfiles', $userObj->getName() ),
441 $this->msg( 'sp-contributions-uploads' )->escaped()
442 );
443
444 # Other logs link
445 $tools[] = Linker::linkKnown(
446 SpecialPage::getTitleFor( 'Log' ),
447 $this->msg( 'sp-contributions-logs' )->escaped(),
448 array(),
449 array( 'user' => $nt->getText() )
450 );
451 # Link to contributions
452 $tools[] = Linker::linkKnown(
453 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
454 $this->msg( 'sp-deletedcontributions-contribs' )->escaped()
455 );
456
457 # Add a link to change user rights for privileged users
458 $userrightsPage = new UserrightsPage();
459 $userrightsPage->setContext( $this->getContext() );
460 if ( $userrightsPage->userCanChangeRights( $userObj ) ) {
461 $tools[] = Linker::linkKnown(
462 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
463 $this->msg( 'sp-contributions-userrights' )->escaped()
464 );
465 }
466
467 Hooks::run( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
468
469 $links = $this->getLanguage()->pipeList( $tools );
470
471 // Show a note if the user is blocked and display the last block log entry.
472 $block = Block::newFromTarget( $userObj, $userObj );
473 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
474 if ( $block->getType() == Block::TYPE_RANGE ) {
475 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
476 }
477
478 // LogEventsList::showLogExtract() wants the first parameter by ref
479 $out = $this->getOutput();
480 LogEventsList::showLogExtract(
481 $out,
482 'block',
483 $nt,
484 '',
485 array(
486 'lim' => 1,
487 'showIfEmpty' => false,
488 'msgKey' => array(
489 'sp-contributions-blocked-notice',
490 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
491 ),
492 'offset' => '' # don't use $this->getRequest() parameter offset
493 )
494 );
495 }
496 }
497
498 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
499 }
500
501 /**
502 * Generates the namespace selector form with hidden attributes.
503 * @param array $options The options to be included.
504 * @return string
505 */
506 function getForm( $options ) {
507 $options['title'] = $this->getPageTitle()->getPrefixedText();
508 if ( !isset( $options['target'] ) ) {
509 $options['target'] = '';
510 } else {
511 $options['target'] = str_replace( '_', ' ', $options['target'] );
512 }
513
514 if ( !isset( $options['namespace'] ) ) {
515 $options['namespace'] = '';
516 }
517
518 if ( !isset( $options['contribs'] ) ) {
519 $options['contribs'] = 'user';
520 }
521
522 if ( $options['contribs'] == 'newbie' ) {
523 $options['target'] = '';
524 }
525
526 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) );
527
528 foreach ( $options as $name => $value ) {
529 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
530 continue;
531 }
532 $f .= "\t" . Html::hidden( $name, $value ) . "\n";
533 }
534
535 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
536
537 $f .= Xml::openElement( 'fieldset' );
538 $f .= Xml::element( 'legend', array(), $this->msg( 'sp-contributions-search' )->text() );
539 $f .= Xml::tags(
540 'label',
541 array( 'for' => 'target' ),
542 $this->msg( 'sp-contributions-username' )->parse()
543 ) . ' ';
544 $f .= Html::input(
545 'target',
546 $options['target'],
547 'text',
548 array(
549 'size' => '20',
550 'required' => '',
551 'class' => array(
552 'mw-autocomplete-user', // used by mediawiki.userSuggest
553 ),
554 ) + ( $options['target'] ? array() : array( 'autofocus' ) )
555 ) . ' ';
556 $f .= Html::namespaceSelector(
557 array(
558 'selected' => $options['namespace'],
559 'all' => '',
560 'label' => $this->msg( 'namespace' )->text()
561 ),
562 array(
563 'name' => 'namespace',
564 'id' => 'namespace',
565 'class' => 'namespaceselector',
566 )
567 ) . ' ';
568 $f .= Xml::submitButton( $this->msg( 'sp-contributions-submit' )->text() );
569 $f .= Xml::closeElement( 'fieldset' );
570 $f .= Xml::closeElement( 'form' );
571
572 return $f;
573 }
574
575 protected function getGroupName() {
576 return 'users';
577 }
578 }