Special:IPBlockList's internal name is 'Ipblocklist', not 'IPBlockList' (throwing...
[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( 'deleterevision' ) ) {
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('deletedcontent') ) {
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 if( $wgUser->isAllowed( 'deleterevision' ) ) {
194 // If revision was hidden from sysops
195 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
196 $del = Xml::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
197 '(' . $this->message['rev-delundel'] . ')' ) . ' ';
198 // Otherwise, show the link...
199 } else {
200 $query = array(
201 'type' => 'archive',
202 'target' => $page->getPrefixedDbkey(),
203 'ids' => $rev->getTimestamp() );
204 $del = $this->mSkin->revDeleteLink( $query,
205 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ) . ' ';
206 }
207 } else {
208 $del = '';
209 }
210
211 $tools = Html::rawElement(
212 'span',
213 array( 'class' => 'mw-deletedcontribs-tools' ),
214 wfMsg( 'parentheses', $wgLang->pipeList( array( $last, $dellog, $reviewlink ) ) )
215 );
216
217 $ret = Html::rawElement(
218 'li',
219 array(),
220 "{$del}{$link} {$tools} . . {$mflag} {$pagelink} {$comment}"
221 ) . "\n";
222
223 wfProfileOut( __METHOD__ );
224 return $ret;
225 }
226
227 /**
228 * Get the Database object in use
229 *
230 * @return Database
231 */
232 public function getDatabase() {
233 return $this->mDb;
234 }
235 }
236
237 class DeletedContributionsPage extends SpecialPage {
238 function __construct() {
239 parent::__construct( 'DeletedContributions', 'deletedhistory',
240 /*listed*/ true, /*function*/ false, /*file*/ false );
241 }
242
243 /**
244 * Special page "deleted user contributions".
245 * Shows a list of the deleted contributions of a user.
246 *
247 * @return none
248 * @param $par String: (optional) user name of the user for which to show the contributions
249 */
250 function execute( $par ) {
251 global $wgUser;
252 $this->setHeaders();
253
254 if ( !$this->userCanExecute( $wgUser ) ) {
255 $this->displayRestrictionError();
256 return;
257 }
258
259 global $wgUser, $wgOut, $wgLang, $wgRequest;
260
261 $wgOut->setPageTitle( wfMsgExt( 'deletedcontributions-title', array( 'parsemag' ) ) );
262
263 $options = array();
264
265 if ( isset( $par ) ) {
266 $target = $par;
267 } else {
268 $target = $wgRequest->getVal( 'target' );
269 }
270
271 if ( !strlen( $target ) ) {
272 $wgOut->addHTML( $this->getForm( '' ) );
273 return;
274 }
275
276 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
277 $options['target'] = $target;
278
279 $nt = Title::makeTitleSafe( NS_USER, $target );
280 if ( !$nt ) {
281 $wgOut->addHTML( $this->getForm( '' ) );
282 return;
283 }
284 $id = User::idFromName( $nt->getText() );
285
286 $target = $nt->getText();
287 $wgOut->setSubtitle( $this->getSubTitle( $nt, $id ) );
288
289 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
290 $options['namespace'] = intval( $ns );
291 } else {
292 $options['namespace'] = '';
293 }
294
295 $wgOut->addHTML( $this->getForm( $options ) );
296
297 $pager = new DeletedContribsPager( $target, $options['namespace'] );
298 if ( !$pager->getNumRows() ) {
299 $wgOut->addWikiMsg( 'nocontribs' );
300 return;
301 }
302
303 # Show a message about slave lag, if applicable
304 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
305 $wgOut->showLagWarning( $lag );
306
307 $wgOut->addHTML(
308 '<p>' . $pager->getNavigationBar() . '</p>' .
309 $pager->getBody() .
310 '<p>' . $pager->getNavigationBar() . '</p>' );
311
312 # If there were contributions, and it was a valid user or IP, show
313 # the appropriate "footer" message - WHOIS tools, etc.
314 if( $target != 'newbies' ) {
315 $message = IP::isIPAddress( $target )
316 ? 'sp-contributions-footer-anon'
317 : 'sp-contributions-footer';
318
319
320 $text = wfMsgNoTrans( $message, $target );
321 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
322 $wgOut->wrapWikiMsg( "<div class='mw-contributions-footer'>\n$1\n</div>", array( $message, $target ) );
323 }
324 }
325 }
326
327 /**
328 * Generates the subheading with links
329 * @param $nt @see Title object for the target
330 */
331 function getSubTitle( $nt, $id ) {
332 global $wgSysopUserBans, $wgLang, $wgUser;
333
334 $sk = $wgUser->getSkin();
335
336 if ( 0 == $id ) {
337 $user = htmlspecialchars( $nt->getText() );
338 } else {
339 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
340 }
341 $talk = $nt->getTalkPage();
342 if( $talk ) {
343 # Talk page link
344 $tools[] = $sk->link( $talk, wfMsgHtml( 'talkpagelinktext' ) );
345 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
346 # Block link
347 if( $wgUser->isAllowed( 'block' ) )
348 $tools[] = $sk->linkKnown(
349 SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
350 wfMsgHtml( 'blocklink' )
351 );
352 # Block log link
353 $tools[] = $sk->linkKnown(
354 SpecialPage::getTitleFor( 'Log' ),
355 wfMsgHtml( 'sp-contributions-blocklog' ),
356 array(),
357 array(
358 'type' => 'block',
359 'page' => $nt->getPrefixedText()
360 )
361 );
362 }
363 # Other logs link
364 $tools[] = $sk->linkKnown(
365 SpecialPage::getTitleFor( 'Log' ),
366 wfMsgHtml( 'sp-contributions-logs' ),
367 array(),
368 array( 'user' => $nt->getText() )
369 );
370 # Link to undeleted contributions
371 $tools[] = $sk->linkKnown(
372 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
373 wfMsgHtml( 'sp-deletedcontributions-contribs' )
374 );
375
376 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
377
378 $links = $wgLang->pipeList( $tools );
379 }
380
381 // Old message 'contribsub' had one parameter, but that doesn't work for
382 // languages that want to put the "for" bit right after $user but before
383 // $links. If 'contribsub' is around, use it for reverse compatibility,
384 // otherwise use 'contribsub2'.
385 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
386 return wfMsgHtml( 'contribsub2', $user, $links );
387 } else {
388 return wfMsgHtml( 'contribsub', "$user ($links)" );
389 }
390 }
391
392 /**
393 * Generates the namespace selector form with hidden attributes.
394 * @param $options Array: the options to be included.
395 */
396 function getForm( $options ) {
397 global $wgScript, $wgRequest;
398
399 $options['title'] = SpecialPage::getTitleFor( 'DeletedContributions' )->getPrefixedText();
400 if ( !isset( $options['target'] ) ) {
401 $options['target'] = '';
402 } else {
403 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
404 }
405
406 if ( !isset( $options['namespace'] ) ) {
407 $options['namespace'] = '';
408 }
409
410 if ( !isset( $options['contribs'] ) ) {
411 $options['contribs'] = 'user';
412 }
413
414 if ( $options['contribs'] == 'newbie' ) {
415 $options['target'] = '';
416 }
417
418 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
419
420 foreach ( $options as $name => $value ) {
421 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
422 continue;
423 }
424 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
425 }
426
427 $f .= Xml::openElement( 'fieldset' ) .
428 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
429 Xml::tags( 'label', array( 'for' => 'target' ), wfMsgExt( 'sp-contributions-username', 'parseinline' ) ) . ' ' .
430 Html::input( 'target', $options['target'], 'text', array(
431 'size' => '20',
432 'required' => ''
433 ) + ( $options['target'] ? array() : array( 'autofocus' ) ) ) . ' '.
434 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
435 Xml::namespaceSelector( $options['namespace'], '' ) . ' ' .
436 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
437 Xml::closeElement( 'fieldset' ) .
438 Xml::closeElement( 'form' );
439 return $f;
440 }
441 }