Switch some HTMLForms in special pages to OOUI
[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 /**
82 * This method basically executes the exact same code as the parent class, though with
83 * a hook added, to allow extensions to add additional queries.
84 *
85 * @param string $offset Index offset, inclusive
86 * @param int $limit Exact query limit
87 * @param bool $descending Query direction, false for ascending, true for descending
88 * @return ResultWrapper
89 */
90 function reallyDoQuery( $offset, $limit, $descending ) {
91 $data = array( parent::reallyDoQuery( $offset, $limit, $descending ) );
92
93 // This hook will allow extensions to add in additional queries, nearly
94 // identical to ContribsPager::reallyDoQuery.
95 Hooks::run(
96 'DeletedContribsPager::reallyDoQuery',
97 array( &$data, $this, $offset, $limit, $descending )
98 );
99
100 $result = array();
101
102 // loop all results and collect them in an array
103 foreach ( $data as $query ) {
104 foreach ( $query as $i => $row ) {
105 // use index column as key, allowing us to easily sort in PHP
106 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
107 }
108 }
109
110 // sort results
111 if ( $descending ) {
112 ksort( $result );
113 } else {
114 krsort( $result );
115 }
116
117 // enforce limit
118 $result = array_slice( $result, 0, $limit );
119
120 // get rid of array keys
121 $result = array_values( $result );
122
123 return new FakeResultWrapper( $result );
124 }
125
126 function getUserCond() {
127 $condition = array();
128
129 $condition['ar_user_text'] = $this->target;
130 $index = 'usertext_timestamp';
131
132 return array( $index, $condition );
133 }
134
135 function getIndexField() {
136 return 'ar_timestamp';
137 }
138
139 function getStartBody() {
140 return "<ul>\n";
141 }
142
143 function getEndBody() {
144 return "</ul>\n";
145 }
146
147 function getNavigationBar() {
148 if ( isset( $this->mNavigationBar ) ) {
149 return $this->mNavigationBar;
150 }
151
152 $linkTexts = array(
153 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
154 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
155 'first' => $this->msg( 'histlast' )->escaped(),
156 'last' => $this->msg( 'histfirst' )->escaped()
157 );
158
159 $pagingLinks = $this->getPagingLinks( $linkTexts );
160 $limitLinks = $this->getLimitLinks();
161 $lang = $this->getLanguage();
162 $limits = $lang->pipeList( $limitLinks );
163
164 $firstLast = $lang->pipeList( array( $pagingLinks['first'], $pagingLinks['last'] ) );
165 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
166 $prevNext = $this->msg( 'viewprevnext' )
167 ->rawParams(
168 $pagingLinks['prev'],
169 $pagingLinks['next'],
170 $limits
171 )->escaped();
172 $separator = $this->msg( 'word-separator' )->escaped();
173 $this->mNavigationBar = $firstLast . $separator . $prevNext;
174
175 return $this->mNavigationBar;
176 }
177
178 function getNamespaceCond() {
179 if ( $this->namespace !== '' ) {
180 return array( 'ar_namespace' => (int)$this->namespace );
181 } else {
182 return array();
183 }
184 }
185
186 /**
187 * Generates each row in the contributions list.
188 *
189 * @todo This would probably look a lot nicer in a table.
190 * @param stdClass $row
191 * @return string
192 */
193 function formatRow( $row ) {
194 $ret = '';
195 $classes = array();
196
197 /*
198 * There may be more than just revision rows. To make sure that we'll only be processing
199 * revisions here, let's _try_ to build a revision out of our row (without displaying
200 * notices though) and then trying to grab data from the built object. If we succeed,
201 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
202 * to extensions to subscribe to the hook to parse the row.
203 */
204 MediaWiki\suppressWarnings();
205 try {
206 $rev = Revision::newFromArchiveRow( $row );
207 $validRevision = (bool)$rev->getId();
208 } catch ( Exception $e ) {
209 $validRevision = false;
210 }
211 MediaWiki\restoreWarnings();
212
213 if ( $validRevision ) {
214 $ret = $this->formatRevisionRow( $row );
215 }
216
217 // Let extensions add data
218 Hooks::run( 'DeletedContributionsLineEnding', array( $this, &$ret, $row, &$classes ) );
219
220 if ( $classes === array() && $ret === '' ) {
221 wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
222 $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
223 } else {
224 $ret = Html::rawElement( 'li', array( 'class' => $classes ), $ret ) . "\n";
225 }
226
227 return $ret;
228 }
229
230 /**
231 * Generates each row in the contributions list for archive entries.
232 *
233 * Contributions which are marked "top" are currently on top of the history.
234 * For these contributions, a [rollback] link is shown for users with sysop
235 * privileges. The rollback link restores the most recent version that was not
236 * written by the target user.
237 *
238 * @todo This would probably look a lot nicer in a table.
239 * @param stdClass $row
240 * @return string
241 */
242 function formatRevisionRow( $row ) {
243 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
244
245 $rev = new Revision( array(
246 'title' => $page,
247 'id' => $row->ar_rev_id,
248 'comment' => $row->ar_comment,
249 'user' => $row->ar_user,
250 'user_text' => $row->ar_user_text,
251 'timestamp' => $row->ar_timestamp,
252 'minor_edit' => $row->ar_minor_edit,
253 'deleted' => $row->ar_deleted,
254 ) );
255
256 $undelete = SpecialPage::getTitleFor( 'Undelete' );
257
258 $logs = SpecialPage::getTitleFor( 'Log' );
259 $dellog = Linker::linkKnown(
260 $logs,
261 $this->messages['deletionlog'],
262 array(),
263 array(
264 'type' => 'delete',
265 'page' => $page->getPrefixedText()
266 )
267 );
268
269 $reviewlink = Linker::linkKnown(
270 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
271 $this->messages['undeleteviewlink']
272 );
273
274 $user = $this->getUser();
275
276 if ( $user->isAllowed( 'deletedtext' ) ) {
277 $last = Linker::linkKnown(
278 $undelete,
279 $this->messages['diff'],
280 array(),
281 array(
282 'target' => $page->getPrefixedText(),
283 'timestamp' => $rev->getTimestamp(),
284 'diff' => 'prev'
285 )
286 );
287 } else {
288 $last = $this->messages['diff'];
289 }
290
291 $comment = Linker::revComment( $rev );
292 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
293 $date = htmlspecialchars( $date );
294
295 if ( !$user->isAllowed( 'undelete' ) || !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
296 $link = $date; // unusable link
297 } else {
298 $link = Linker::linkKnown(
299 $undelete,
300 $date,
301 array( 'class' => 'mw-changeslist-date' ),
302 array(
303 'target' => $page->getPrefixedText(),
304 'timestamp' => $rev->getTimestamp()
305 )
306 );
307 }
308 // Style deleted items
309 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
310 $link = '<span class="history-deleted">' . $link . '</span>';
311 }
312
313 $pagelink = Linker::link(
314 $page,
315 null,
316 array( 'class' => 'mw-changeslist-title' )
317 );
318
319 if ( $rev->isMinor() ) {
320 $mflag = ChangesList::flag( 'minor' );
321 } else {
322 $mflag = '';
323 }
324
325 // Revision delete link
326 $del = Linker::getRevDeleteLink( $user, $rev, $page );
327 if ( $del ) {
328 $del .= ' ';
329 }
330
331 $tools = Html::rawElement(
332 'span',
333 array( 'class' => 'mw-deletedcontribs-tools' ),
334 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
335 array( $last, $dellog, $reviewlink ) ) )->escaped()
336 );
337
338 $separator = '<span class="mw-changeslist-separator">. .</span>';
339 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
340
341 # Denote if username is redacted for this edit
342 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
343 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
344 }
345
346 return $ret;
347 }
348
349 /**
350 * Get the Database object in use
351 *
352 * @return IDatabase
353 */
354 public function getDatabase() {
355 return $this->mDb;
356 }
357 }
358
359 class DeletedContributionsPage extends SpecialPage {
360 function __construct() {
361 parent::__construct( 'DeletedContributions', 'deletedhistory',
362 /*listed*/true, /*function*/false, /*file*/false );
363 }
364
365 /**
366 * Special page "deleted user contributions".
367 * Shows a list of the deleted contributions of a user.
368 *
369 * @param string $par (optional) user name of the user for which to show the contributions
370 */
371 function execute( $par ) {
372 $this->setHeaders();
373 $this->outputHeader();
374
375 $user = $this->getUser();
376
377 if ( !$this->userCanExecute( $user ) ) {
378 $this->displayRestrictionError();
379
380 return;
381 }
382
383 $request = $this->getRequest();
384 $out = $this->getOutput();
385 $out->setPageTitle( $this->msg( 'deletedcontributions-title' ) );
386
387 $options = array();
388
389 if ( $par !== null ) {
390 $target = $par;
391 } else {
392 $target = $request->getVal( 'target' );
393 }
394
395 if ( !strlen( $target ) ) {
396 $out->addHTML( $this->getForm( '' ) );
397
398 return;
399 }
400
401 $options['limit'] = $request->getInt( 'limit',
402 $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
403 $options['target'] = $target;
404
405 $userObj = User::newFromName( $target, false );
406 if ( !$userObj ) {
407 $out->addHTML( $this->getForm( '' ) );
408
409 return;
410 }
411 $this->getSkin()->setRelevantUser( $userObj );
412
413 $target = $userObj->getName();
414 $out->addSubtitle( $this->getSubTitle( $userObj ) );
415
416 if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
417 $options['namespace'] = intval( $ns );
418 } else {
419 $options['namespace'] = '';
420 }
421
422 $out->addHTML( $this->getForm( $options ) );
423
424 $pager = new DeletedContribsPager( $this->getContext(), $target, $options['namespace'] );
425 if ( !$pager->getNumRows() ) {
426 $out->addWikiMsg( 'nocontribs' );
427
428 return;
429 }
430
431 # Show a message about slave lag, if applicable
432 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
433 if ( $lag > 0 ) {
434 $out->showLagWarning( $lag );
435 }
436
437 $out->addHTML(
438 '<p>' . $pager->getNavigationBar() . '</p>' .
439 $pager->getBody() .
440 '<p>' . $pager->getNavigationBar() . '</p>' );
441
442 # If there were contributions, and it was a valid user or IP, show
443 # the appropriate "footer" message - WHOIS tools, etc.
444 if ( $target != 'newbies' ) {
445 $message = IP::isIPAddress( $target ) ?
446 'sp-contributions-footer-anon' :
447 'sp-contributions-footer';
448
449 if ( !$this->msg( $message )->isDisabled() ) {
450 $out->wrapWikiMsg(
451 "<div class='mw-contributions-footer'>\n$1\n</div>",
452 array( $message, $target )
453 );
454 }
455 }
456 }
457
458 /**
459 * Generates the subheading with links
460 * @param User $userObj User object for the target
461 * @return string Appropriately-escaped HTML to be output literally
462 * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
463 */
464 function getSubTitle( $userObj ) {
465 if ( $userObj->isAnon() ) {
466 $user = htmlspecialchars( $userObj->getName() );
467 } else {
468 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
469 }
470 $links = '';
471 $nt = $userObj->getUserPage();
472 $id = $userObj->getID();
473 $talk = $nt->getTalkPage();
474 if ( $talk ) {
475 # Talk page link
476 $tools[] = Linker::link( $talk, $this->msg( 'sp-contributions-talk' )->escaped() );
477 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
478 # Block / Change block / Unblock links
479 if ( $this->getUser()->isAllowed( 'block' ) ) {
480 if ( $userObj->isBlocked() ) {
481 $tools[] = Linker::linkKnown( # Change block link
482 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
483 $this->msg( 'change-blocklink' )->escaped()
484 );
485 $tools[] = Linker::linkKnown( # Unblock link
486 SpecialPage::getTitleFor( 'BlockList' ),
487 $this->msg( 'unblocklink' )->escaped(),
488 array(),
489 array(
490 'action' => 'unblock',
491 'ip' => $nt->getDBkey()
492 )
493 );
494 } else {
495 # User is not blocked
496 $tools[] = Linker::linkKnown( # Block link
497 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
498 $this->msg( 'blocklink' )->escaped()
499 );
500 }
501 }
502 # Block log link
503 $tools[] = Linker::linkKnown(
504 SpecialPage::getTitleFor( 'Log' ),
505 $this->msg( 'sp-contributions-blocklog' )->escaped(),
506 array(),
507 array(
508 'type' => 'block',
509 'page' => $nt->getPrefixedText()
510 )
511 );
512 # Suppression log link (bug 59120)
513 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
514 $tools[] = Linker::linkKnown(
515 SpecialPage::getTitleFor( 'Log', 'suppress' ),
516 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
517 array(),
518 array( 'offender' => $userObj->getName() )
519 );
520 }
521 }
522
523 # Uploads
524 $tools[] = Linker::linkKnown(
525 SpecialPage::getTitleFor( 'Listfiles', $userObj->getName() ),
526 $this->msg( 'sp-contributions-uploads' )->escaped()
527 );
528
529 # Other logs link
530 $tools[] = Linker::linkKnown(
531 SpecialPage::getTitleFor( 'Log' ),
532 $this->msg( 'sp-contributions-logs' )->escaped(),
533 array(),
534 array( 'user' => $nt->getText() )
535 );
536 # Link to contributions
537 $tools[] = Linker::linkKnown(
538 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
539 $this->msg( 'sp-deletedcontributions-contribs' )->escaped()
540 );
541
542 # Add a link to change user rights for privileged users
543 $userrightsPage = new UserrightsPage();
544 $userrightsPage->setContext( $this->getContext() );
545 if ( $userrightsPage->userCanChangeRights( $userObj ) ) {
546 $tools[] = Linker::linkKnown(
547 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
548 $this->msg( 'sp-contributions-userrights' )->escaped()
549 );
550 }
551
552 Hooks::run( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
553
554 $links = $this->getLanguage()->pipeList( $tools );
555
556 // Show a note if the user is blocked and display the last block log entry.
557 $block = Block::newFromTarget( $userObj, $userObj );
558 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
559 if ( $block->getType() == Block::TYPE_RANGE ) {
560 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
561 }
562
563 // LogEventsList::showLogExtract() wants the first parameter by ref
564 $out = $this->getOutput();
565 LogEventsList::showLogExtract(
566 $out,
567 'block',
568 $nt,
569 '',
570 array(
571 'lim' => 1,
572 'showIfEmpty' => false,
573 'msgKey' => array(
574 'sp-contributions-blocked-notice',
575 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
576 ),
577 'offset' => '' # don't use $this->getRequest() parameter offset
578 )
579 );
580 }
581 }
582
583 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
584 }
585
586 /**
587 * Generates the namespace selector form with hidden attributes.
588 * @param array $options The options to be included.
589 * @return string
590 */
591 function getForm( $options ) {
592 $options['title'] = $this->getPageTitle()->getPrefixedText();
593 if ( !isset( $options['target'] ) ) {
594 $options['target'] = '';
595 } else {
596 $options['target'] = str_replace( '_', ' ', $options['target'] );
597 }
598
599 if ( !isset( $options['namespace'] ) ) {
600 $options['namespace'] = '';
601 }
602
603 if ( !isset( $options['contribs'] ) ) {
604 $options['contribs'] = 'user';
605 }
606
607 if ( $options['contribs'] == 'newbie' ) {
608 $options['target'] = '';
609 }
610
611 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) );
612
613 foreach ( $options as $name => $value ) {
614 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
615 continue;
616 }
617 $f .= "\t" . Html::hidden( $name, $value ) . "\n";
618 }
619
620 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
621
622 $f .= Xml::openElement( 'fieldset' );
623 $f .= Xml::element( 'legend', array(), $this->msg( 'sp-contributions-search' )->text() );
624 $f .= Xml::tags(
625 'label',
626 array( 'for' => 'target' ),
627 $this->msg( 'sp-contributions-username' )->parse()
628 ) . ' ';
629 $f .= Html::input(
630 'target',
631 $options['target'],
632 'text',
633 array(
634 'size' => '20',
635 'required' => '',
636 'class' => array(
637 'mw-autocomplete-user', // used by mediawiki.userSuggest
638 ),
639 ) + ( $options['target'] ? array() : array( 'autofocus' ) )
640 ) . ' ';
641 $f .= Html::namespaceSelector(
642 array(
643 'selected' => $options['namespace'],
644 'all' => '',
645 'label' => $this->msg( 'namespace' )->text()
646 ),
647 array(
648 'name' => 'namespace',
649 'id' => 'namespace',
650 'class' => 'namespaceselector',
651 )
652 ) . ' ';
653 $f .= Xml::submitButton( $this->msg( 'sp-contributions-submit' )->text() );
654 $f .= Xml::closeElement( 'fieldset' );
655 $f .= Xml::closeElement( 'form' );
656
657 return $f;
658 }
659
660 protected function getGroupName() {
661 return 'users';
662 }
663 }