* bug 21267 change "show/hide" to "show", if user cannot submit Special:Revisiondelete
[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 $nt @see Title object for the target
329 */
330 function getSubTitle( $nt, $id ) {
331 global $wgSysopUserBans, $wgLang, $wgUser;
332
333 $sk = $wgUser->getSkin();
334
335 if ( 0 == $id ) {
336 $user = htmlspecialchars( $nt->getText() );
337 } else {
338 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
339 }
340 $talk = $nt->getTalkPage();
341 if( $talk ) {
342 # Talk page link
343 $tools[] = $sk->link( $talk, wfMsgHtml( 'talkpagelinktext' ) );
344 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
345 # Block link
346 if( $wgUser->isAllowed( 'block' ) )
347 $tools[] = $sk->linkKnown(
348 SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
349 wfMsgHtml( 'blocklink' )
350 );
351 # Block log link
352 $tools[] = $sk->linkKnown(
353 SpecialPage::getTitleFor( 'Log' ),
354 wfMsgHtml( 'sp-contributions-blocklog' ),
355 array(),
356 array(
357 'type' => 'block',
358 'page' => $nt->getPrefixedText()
359 )
360 );
361 }
362 # Other logs link
363 $tools[] = $sk->linkKnown(
364 SpecialPage::getTitleFor( 'Log' ),
365 wfMsgHtml( 'sp-contributions-logs' ),
366 array(),
367 array( 'user' => $nt->getText() )
368 );
369 # Link to undeleted contributions
370 $tools[] = $sk->linkKnown(
371 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
372 wfMsgHtml( 'sp-deletedcontributions-contribs' )
373 );
374
375 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
376
377 $links = $wgLang->pipeList( $tools );
378 }
379
380 // Old message 'contribsub' had one parameter, but that doesn't work for
381 // languages that want to put the "for" bit right after $user but before
382 // $links. If 'contribsub' is around, use it for reverse compatibility,
383 // otherwise use 'contribsub2'.
384 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
385 return wfMsgHtml( 'contribsub2', $user, $links );
386 } else {
387 return wfMsgHtml( 'contribsub', "$user ($links)" );
388 }
389 }
390
391 /**
392 * Generates the namespace selector form with hidden attributes.
393 * @param $options Array: the options to be included.
394 */
395 function getForm( $options ) {
396 global $wgScript, $wgRequest;
397
398 $options['title'] = SpecialPage::getTitleFor( 'DeletedContributions' )->getPrefixedText();
399 if ( !isset( $options['target'] ) ) {
400 $options['target'] = '';
401 } else {
402 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
403 }
404
405 if ( !isset( $options['namespace'] ) ) {
406 $options['namespace'] = '';
407 }
408
409 if ( !isset( $options['contribs'] ) ) {
410 $options['contribs'] = 'user';
411 }
412
413 if ( $options['contribs'] == 'newbie' ) {
414 $options['target'] = '';
415 }
416
417 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
418
419 foreach ( $options as $name => $value ) {
420 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
421 continue;
422 }
423 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
424 }
425
426 $f .= Xml::openElement( 'fieldset' ) .
427 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
428 Xml::tags( 'label', array( 'for' => 'target' ), wfMsgExt( 'sp-contributions-username', 'parseinline' ) ) . ' ' .
429 Html::input( 'target', $options['target'], 'text', array(
430 'size' => '20',
431 'required' => ''
432 ) + ( $options['target'] ? array() : array( 'autofocus' ) ) ) . ' '.
433 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
434 Xml::namespaceSelector( $options['namespace'], '' ) . ' ' .
435 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
436 Xml::closeElement( 'fieldset' ) .
437 Xml::closeElement( 'form' );
438 return $f;
439 }
440 }