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