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