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