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