Fix phpcs errors in includes/
[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 return $ret;
261 }
262
263 /**
264 * Get the Database object in use
265 *
266 * @return IDatabase
267 */
268 public function getDatabase() {
269 return $this->mDb;
270 }
271 }
272
273 class DeletedContributionsPage extends SpecialPage {
274 function __construct() {
275 parent::__construct( 'DeletedContributions', 'deletedhistory',
276 /*listed*/true, /*function*/false, /*file*/false );
277 }
278
279 /**
280 * Special page "deleted user contributions".
281 * Shows a list of the deleted contributions of a user.
282 *
283 * @param string $par (optional) user name of the user for which to show the contributions
284 */
285 function execute( $par ) {
286 $this->setHeaders();
287 $this->outputHeader();
288
289 $user = $this->getUser();
290
291 if ( !$this->userCanExecute( $user ) ) {
292 $this->displayRestrictionError();
293
294 return;
295 }
296
297 $request = $this->getRequest();
298 $out = $this->getOutput();
299 $out->setPageTitle( $this->msg( 'deletedcontributions-title' ) );
300
301 $options = array();
302
303 if ( $par !== null ) {
304 $target = $par;
305 } else {
306 $target = $request->getVal( 'target' );
307 }
308
309 if ( !strlen( $target ) ) {
310 $out->addHTML( $this->getForm( '' ) );
311
312 return;
313 }
314
315 $options['limit'] = $request->getInt( 'limit',
316 $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
317 $options['target'] = $target;
318
319 $userObj = User::newFromName( $target, false );
320 if ( !$userObj ) {
321 $out->addHTML( $this->getForm( '' ) );
322
323 return;
324 }
325 $this->getSkin()->setRelevantUser( $userObj );
326
327 $target = $userObj->getName();
328 $out->addSubtitle( $this->getSubTitle( $userObj ) );
329
330 if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
331 $options['namespace'] = intval( $ns );
332 } else {
333 $options['namespace'] = '';
334 }
335
336 $out->addHTML( $this->getForm( $options ) );
337
338 $pager = new DeletedContribsPager( $this->getContext(), $target, $options['namespace'] );
339 if ( !$pager->getNumRows() ) {
340 $out->addWikiMsg( 'nocontribs' );
341
342 return;
343 }
344
345 # Show a message about slave lag, if applicable
346 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
347 if ( $lag > 0 ) {
348 $out->showLagWarning( $lag );
349 }
350
351 $out->addHTML(
352 '<p>' . $pager->getNavigationBar() . '</p>' .
353 $pager->getBody() .
354 '<p>' . $pager->getNavigationBar() . '</p>' );
355
356 # If there were contributions, and it was a valid user or IP, show
357 # the appropriate "footer" message - WHOIS tools, etc.
358 if ( $target != 'newbies' ) {
359 $message = IP::isIPAddress( $target ) ?
360 'sp-contributions-footer-anon' :
361 'sp-contributions-footer';
362
363 if ( !$this->msg( $message )->isDisabled() ) {
364 $out->wrapWikiMsg(
365 "<div class='mw-contributions-footer'>\n$1\n</div>",
366 array( $message, $target )
367 );
368 }
369 }
370 }
371
372 /**
373 * Generates the subheading with links
374 * @param User $userObj User object for the target
375 * @return string Appropriately-escaped HTML to be output literally
376 * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
377 */
378 function getSubTitle( $userObj ) {
379 if ( $userObj->isAnon() ) {
380 $user = htmlspecialchars( $userObj->getName() );
381 } else {
382 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
383 }
384 $links = '';
385 $nt = $userObj->getUserPage();
386 $id = $userObj->getID();
387 $talk = $nt->getTalkPage();
388 if ( $talk ) {
389 # Talk page link
390 $tools[] = Linker::link( $talk, $this->msg( 'sp-contributions-talk' )->escaped() );
391 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
392 # Block / Change block / Unblock links
393 if ( $this->getUser()->isAllowed( 'block' ) ) {
394 if ( $userObj->isBlocked() ) {
395 $tools[] = Linker::linkKnown( # Change block link
396 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
397 $this->msg( 'change-blocklink' )->escaped()
398 );
399 $tools[] = Linker::linkKnown( # Unblock link
400 SpecialPage::getTitleFor( 'BlockList' ),
401 $this->msg( 'unblocklink' )->escaped(),
402 array(),
403 array(
404 'action' => 'unblock',
405 'ip' => $nt->getDBkey()
406 )
407 );
408 } else {
409 # User is not blocked
410 $tools[] = Linker::linkKnown( # Block link
411 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
412 $this->msg( 'blocklink' )->escaped()
413 );
414 }
415 }
416 # Block log link
417 $tools[] = Linker::linkKnown(
418 SpecialPage::getTitleFor( 'Log' ),
419 $this->msg( 'sp-contributions-blocklog' )->escaped(),
420 array(),
421 array(
422 'type' => 'block',
423 'page' => $nt->getPrefixedText()
424 )
425 );
426 # Suppression log link (bug 59120)
427 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
428 $tools[] = Linker::linkKnown(
429 SpecialPage::getTitleFor( 'Log', 'suppress' ),
430 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
431 array(),
432 array( 'offender' => $userObj->getName() )
433 );
434 }
435 }
436
437 # Uploads
438 $tools[] = Linker::linkKnown(
439 SpecialPage::getTitleFor( 'Listfiles', $userObj->getName() ),
440 $this->msg( 'sp-contributions-uploads' )->escaped()
441 );
442
443 # Other logs link
444 $tools[] = Linker::linkKnown(
445 SpecialPage::getTitleFor( 'Log' ),
446 $this->msg( 'sp-contributions-logs' )->escaped(),
447 array(),
448 array( 'user' => $nt->getText() )
449 );
450 # Link to contributions
451 $tools[] = Linker::linkKnown(
452 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
453 $this->msg( 'sp-deletedcontributions-contribs' )->escaped()
454 );
455
456 # Add a link to change user rights for privileged users
457 $userrightsPage = new UserrightsPage();
458 $userrightsPage->setContext( $this->getContext() );
459 if ( $userrightsPage->userCanChangeRights( $userObj ) ) {
460 $tools[] = Linker::linkKnown(
461 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
462 $this->msg( 'sp-contributions-userrights' )->escaped()
463 );
464 }
465
466 Hooks::run( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
467
468 $links = $this->getLanguage()->pipeList( $tools );
469
470 // Show a note if the user is blocked and display the last block log entry.
471 $block = Block::newFromTarget( $userObj, $userObj );
472 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
473 if ( $block->getType() == Block::TYPE_RANGE ) {
474 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
475 }
476
477 // LogEventsList::showLogExtract() wants the first parameter by ref
478 $out = $this->getOutput();
479 LogEventsList::showLogExtract(
480 $out,
481 'block',
482 $nt,
483 '',
484 array(
485 'lim' => 1,
486 'showIfEmpty' => false,
487 'msgKey' => array(
488 'sp-contributions-blocked-notice',
489 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
490 ),
491 'offset' => '' # don't use $this->getRequest() parameter offset
492 )
493 );
494 }
495 }
496
497 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
498 }
499
500 /**
501 * Generates the namespace selector form with hidden attributes.
502 * @param array $options The options to be included.
503 * @return string
504 */
505 function getForm( $options ) {
506 $options['title'] = $this->getPageTitle()->getPrefixedText();
507 if ( !isset( $options['target'] ) ) {
508 $options['target'] = '';
509 } else {
510 $options['target'] = str_replace( '_', ' ', $options['target'] );
511 }
512
513 if ( !isset( $options['namespace'] ) ) {
514 $options['namespace'] = '';
515 }
516
517 if ( !isset( $options['contribs'] ) ) {
518 $options['contribs'] = 'user';
519 }
520
521 if ( $options['contribs'] == 'newbie' ) {
522 $options['target'] = '';
523 }
524
525 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) );
526
527 foreach ( $options as $name => $value ) {
528 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
529 continue;
530 }
531 $f .= "\t" . Html::hidden( $name, $value ) . "\n";
532 }
533
534 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
535
536 $f .= Xml::openElement( 'fieldset' );
537 $f .= Xml::element( 'legend', array(), $this->msg( 'sp-contributions-search' )->text() );
538 $f .= Xml::tags(
539 'label',
540 array( 'for' => 'target' ),
541 $this->msg( 'sp-contributions-username' )->parse()
542 ) . ' ';
543 $f .= Html::input(
544 'target',
545 $options['target'],
546 'text',
547 array(
548 'size' => '20',
549 'required' => '',
550 'class' => array(
551 'mw-autocomplete-user', // used by mediawiki.userSuggest
552 ),
553 ) + ( $options['target'] ? array() : array( 'autofocus' ) )
554 ) . ' ';
555 $f .= Html::namespaceSelector(
556 array(
557 'selected' => $options['namespace'],
558 'all' => '',
559 'label' => $this->msg( 'namespace' )->text()
560 ),
561 array(
562 'name' => 'namespace',
563 'id' => 'namespace',
564 'class' => 'namespaceselector',
565 )
566 ) . ' ';
567 $f .= Xml::submitButton( $this->msg( 'sp-contributions-submit' )->text() );
568 $f .= Xml::closeElement( 'fieldset' );
569 $f .= Xml::closeElement( 'form' );
570
571 return $f;
572 }
573
574 protected function getGroupName() {
575 return 'users';
576 }
577 }