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