Fix r20109, which was kind of insane. It was a wikitext list with only some output...
[mediawiki.git] / includes / SpecialIpblocklist.php
blob36db0f11d6ec93d52c55ee6ed9b37fa8abea02ca
1 <?php
2 /**
4 * @addtogroup SpecialPage
5 */
7 /**
8 * @todo document
9 */
10 function wfSpecialIpblocklist() {
11 global $wgUser, $wgOut, $wgRequest;
13 $ip = $wgRequest->getVal( 'wpUnblockAddress', $wgRequest->getVal( 'ip' ) );
14 $id = $wgRequest->getVal( 'id' );
15 $reason = $wgRequest->getText( 'wpUnblockReason' );
16 $action = $wgRequest->getText( 'action' );
17 $successip = $wgRequest->getVal( 'successip' );
19 $ipu = new IPUnblockForm( $ip, $id, $reason );
21 if ( "success" == $action ) {
22 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
23 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
24 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
25 if ( ! $wgUser->isAllowed('block') ) {
26 $wgOut->permissionRequired( 'block' );
27 return;
29 # Can't unblock when the database is locked
30 if( wfReadOnly() ) {
31 $wgOut->readOnlyPage();
32 return;
34 $ipu->doSubmit();
35 } else if ( "unblock" == $action ) {
36 # Can't unblock when the database is locked
37 if( wfReadOnly() ) {
38 $wgOut->readOnlyPage();
39 return;
41 $ipu->showForm( "" );
42 } else {
43 $ipu->showList( "" );
47 /**
49 * @addtogroup SpecialPage
51 class IPUnblockForm {
52 var $ip, $reason, $id;
54 function IPUnblockForm( $ip, $id, $reason ) {
55 $this->ip = strtr( $ip, '_', ' ' );
56 $this->id = $id;
57 $this->reason = $reason;
60 function showForm( $err ) {
61 global $wgOut, $wgUser, $wgSysopUserBans;
63 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
64 $wgOut->addWikiText( wfMsg( 'unblockiptext' ) );
66 $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
67 $ipr = wfMsgHtml( 'ipbreason' );
68 $ipus = wfMsgHtml( 'ipusubmit' );
69 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
70 $action = $titleObj->escapeLocalURL( "action=submit" );
72 if ( "" != $err ) {
73 $wgOut->setSubtitle( wfMsg( "formerror" ) );
74 $wgOut->addWikitext( "<span class='error'>{$err}</span>\n" );
76 $token = htmlspecialchars( $wgUser->editToken() );
78 $addressPart = false;
79 if ( $this->id ) {
80 $block = Block::newFromID( $this->id );
81 if ( $block ) {
82 $encName = htmlspecialchars( $block->getRedactedName() );
83 $encId = htmlspecialchars( $this->id );
84 $addressPart = $encName . "<input type='hidden' name=\"id\" value=\"$encId\" />";
87 if ( !$addressPart ) {
88 $addressPart = "<input tabindex='1' type='text' size='20' " .
89 "name=\"wpUnblockAddress\" value=\"" . htmlspecialchars( $this->ip ) . "\" />";
92 $wgOut->addHTML( "
93 <form id=\"unblockip\" method=\"post\" action=\"{$action}\">
94 <table border='0'>
95 <tr>
96 <td align='right'>{$ipa}:</td>
97 <td align='left'>
98 {$addressPart}
99 </td>
100 </tr>
101 <tr>
102 <td align='right'>{$ipr}:</td>
103 <td align='left'>
104 <input tabindex='1' type='text' size='40' name=\"wpUnblockReason\" value=\"" . htmlspecialchars( $this->reason ) . "\" />
105 </td>
106 </tr>
107 <tr>
108 <td>&nbsp;</td>
109 <td align='left'>
110 <input tabindex='2' type='submit' name=\"wpBlock\" value=\"{$ipus}\" />
111 </td>
112 </tr>
113 </table>
114 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
115 </form>\n" );
119 function doSubmit() {
120 global $wgOut;
122 if ( $this->id ) {
123 $block = Block::newFromID( $this->id );
124 if ( $block ) {
125 $this->ip = $block->getRedactedName();
127 } else {
128 $block = new Block();
129 $this->ip = trim( $this->ip );
130 if ( substr( $this->ip, 0, 1 ) == "#" ) {
131 $id = substr( $this->ip, 1 );
132 $block = Block::newFromID( $id );
133 } else {
134 $block = Block::newFromDB( $this->ip );
135 if ( !$block ) {
136 $block = null;
140 $success = false;
141 if ( $block ) {
142 # Delete block
143 if ( $block->delete() ) {
144 # Make log entry
145 $log = new LogPage( 'block' );
146 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $this->ip ), $this->reason );
147 $success = true;
151 if ( $success ) {
152 # Report to the user
153 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
154 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
155 $wgOut->redirect( $success );
156 } else {
157 if ( !$this->ip && $this->id ) {
158 $this->ip = '#' . $this->id;
160 $this->showForm( wfMsg( 'ipb_cant_unblock', htmlspecialchars( $this->id ) ) );
164 function showList( $msg ) {
165 global $wgOut;
167 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
168 if ( "" != $msg ) {
169 $wgOut->setSubtitle( $msg );
172 // Purge expired entries on one in every 10 queries
173 if ( !mt_rand( 0, 10 ) ) {
174 Block::purgeExpired();
177 $conds = array();
178 $matches = array();
179 if ( $this->ip == '' ) {
180 // No extra conditions
181 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
182 $conds['ipb_id'] = substr( $this->ip, 1 );
183 } elseif ( IP::toUnsigned( $this->ip ) !== false ) {
184 $conds['ipb_address'] = $this->ip;
185 $conds['ipb_auto'] = 0;
186 } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip, $matches ) ) {
187 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
188 $conds['ipb_auto'] = 0;
189 } else {
190 $user = User::newFromName( $this->ip );
191 if ( $user && ( $id = $user->getID() ) != 0 ) {
192 $conds['ipb_user'] = $id;
193 } else {
194 // Uh...?
195 $conds['ipb_address'] = $this->ip;
196 $conds['ipb_auto'] = 0;
200 # TODO: difference message between
201 # a) an real empty list and
202 # b) requested ip/username not on list
203 $pager = new IPBlocklistPager( $this, $conds );
204 if ( $pager->getNumRows() ) {
205 $s = $this->searchForm() .
206 $pager->getNavigationBar();
207 $s .= "<ul>" .
208 $pager->getBody() .
209 "</ul>";
210 $s .= $pager->getNavigationBar();
211 } else {
212 $s = $this->searchForm() .
213 '<p>' . wfMsgHTML( 'ipblocklistempty' ) . '</p>';
215 $wgOut->addHTML( $s );
218 function searchForm() {
219 global $wgTitle, $wgScript, $wgRequest;
220 return
221 wfElement( 'form', array(
222 'action' => $wgScript ),
223 null ) .
224 wfHidden( 'title', $wgTitle->getPrefixedDbKey() ) .
225 wfElement( 'input', array(
226 'type' => 'hidden',
227 'name' => 'action',
228 'value' => 'search' ) ).
229 wfElement( 'input', array(
230 'type' => 'hidden',
231 'name' => 'limit',
232 'value' => $wgRequest->getText( 'limit' ) ) ) .
233 wfElement( 'input', array(
234 'name' => 'ip',
235 'value' => $this->ip ) ) .
236 wfElement( 'input', array(
237 'type' => 'submit',
238 'value' => wfMsg( 'ipblocklist-submit' ) ) ) .
239 '</form>';
243 * Callback function to output a block
245 function formatRow( $block ) {
246 global $wgUser, $wgLang;
248 wfProfileIn( __METHOD__ );
250 static $sk=null, $msg=null;
252 if( is_null( $sk ) )
253 $sk = $wgUser->getSkin();
254 if( is_null( $msg ) ) {
255 $msg = array();
256 $keys = array( 'infiniteblock', 'expiringblock', 'contribslink', 'unblocklink',
257 'anononlyblock', 'createaccountblock', 'noautoblockblock' );
258 foreach( $keys as $key ) {
259 $msg[$key] = wfMsgHtml( $key );
261 $msg['blocklistline'] = wfMsg( 'blocklistline' );
262 $msg['contribslink'] = wfMsg( 'contribslink' );
265 # Prepare links to the blocker's user and talk pages
266 $blocker_id = $block->getBy();
267 $blocker_name = $block->getByName();
268 $blocker = $sk->userLink( $blocker_id, $blocker_name );
269 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
271 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
272 if( $block->mAuto ) {
273 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
274 } else {
275 $target = $sk->makeLinkObj( Title::makeTitle( NS_USER, $block->mAddress ), $block->mAddress );
276 $target .= ' (' . $sk->makeKnownLinkObj( SpecialPage::getSafeTitleFor( 'Contributions', $block->mAddress ), $msg['contribslink'] ) . ')';
279 $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
281 $properties = array();
282 if ( $block->mExpiry === "" || $block->mExpiry === Block::infinity() ) {
283 $properties[] = $msg['infiniteblock'];
284 } else {
285 $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
286 array( $wgLang->timeanddate( $block->mExpiry, true ) ) );
288 if ( $block->mAnonOnly ) {
289 $properties[] = $msg['anononlyblock'];
291 if ( $block->mCreateAccount ) {
292 $properties[] = $msg['createaccountblock'];
294 if (!$block->mEnableAutoblock && $block->mUser ) {
295 $properties[] = $msg['noautoblockblock'];
298 $properties = implode( ', ', $properties );
300 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
302 $s = "<li>{$line}";
304 if ( $wgUser->isAllowed('block') ) {
305 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
306 $s .= ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
308 $s .= $sk->commentBlock( $block->mReason );
309 $s .= "</li>\n";
310 wfProfileOut( __METHOD__ );
311 return $s;
315 class IPBlocklistPager extends ReverseChronologicalPager {
316 public $mForm, $mConds;
318 function __construct( $form, $conds = array() ) {
319 $this->mForm = $form;
320 $this->mConds = $conds;
321 parent::__construct();
324 function getStartBody() {
325 wfProfileIn( __METHOD__ );
326 # Do a link batch query
327 $this->mResult->seek( 0 );
328 $lb = new LinkBatch;
331 while ( $row = $this->mResult->fetchObject() ) {
332 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
333 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
334 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
335 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
337 # Faster way
338 # Usernames and titles are in fact related by a simple substitution of space -> underscore
339 # The last few lines of Title::secureAndSplit() tell the story.
340 while ( $row = $this->mResult->fetchObject() ) {
341 $name = str_replace( ' ', '_', $row->user_name );
342 $lb->add( NS_USER, $name );
343 $lb->add( NS_USER_TALK, $name );
344 $name = str_replace( ' ', '_', $row->ipb_address );
345 $lb->add( NS_USER, $name );
346 $lb->add( NS_USER_TALK, $name );
348 $lb->execute();
349 wfProfileOut( __METHOD__ );
350 return '';
353 function formatRow( $row ) {
354 $block = new Block;
355 $block->initFromRow( $row );
356 return $this->mForm->formatRow( $block );
359 function getQueryInfo() {
360 $conds = $this->mConds;
361 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
362 $conds[] = 'ipb_by=user_id';
363 return array(
364 'tables' => array( 'ipblocks', 'user' ),
365 'fields' => $this->mDb->tableName( 'ipblocks' ) . '.*,user_name',
366 'conds' => $conds,
370 function getIndexField() {
371 return 'ipb_timestamp';