PHPDocumentor [http://en.wikipedia.org/wiki/PhpDocumentor] documentation tweaking...
[lhc/web/wiklou.git] / includes / SpecialIpblocklist.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * @todo document
9 */
10 function wfSpecialIpblocklist() {
11 global $wgUser, $wgOut, $wgRequest;
12
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' );
18
19 $ipu = new IPUnblockForm( $ip, $id, $reason );
20
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;
28 }
29 # Can't unblock when the database is locked
30 if( wfReadOnly() ) {
31 $wgOut->readOnlyPage();
32 return;
33 }
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;
40 }
41 $ipu->showForm( "" );
42 } else {
43 $ipu->showList( "" );
44 }
45 }
46
47 /**
48 * @todo document
49 * @addtogroup SpecialPage
50 */
51 class IPUnblockForm {
52 var $ip, $reason, $id;
53
54 function IPUnblockForm( $ip, $id, $reason ) {
55 $this->ip = strtr( $ip, '_', ' ' );
56 $this->id = $id;
57 $this->reason = $reason;
58 }
59
60 function showForm( $err ) {
61 global $wgOut, $wgUser, $wgSysopUserBans;
62
63 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
64 $wgOut->addWikiText( wfMsg( 'unblockiptext' ) );
65
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" );
71
72 if ( "" != $err ) {
73 $wgOut->setSubtitle( wfMsg( "formerror" ) );
74 $wgOut->addWikitext( "<span class='error'>{$err}</span>\n" );
75 }
76 $token = htmlspecialchars( $wgUser->editToken() );
77
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\" />";
85 }
86 }
87 if ( !$addressPart ) {
88 $addressPart = "<input tabindex='1' type='text' size='20' " .
89 "name=\"wpUnblockAddress\" value=\"" . htmlspecialchars( $this->ip ) . "\" />";
90 }
91
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" );
116
117 }
118
119 function doSubmit() {
120 global $wgOut;
121
122 if ( $this->id ) {
123 $block = Block::newFromID( $this->id );
124 if ( $block ) {
125 $this->ip = $block->getRedactedName();
126 }
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;
137 }
138 }
139 }
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;
148 }
149 }
150
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;
159 }
160 $this->showForm( wfMsg( 'ipb_cant_unblock', htmlspecialchars( $this->id ) ) );
161 }
162 }
163
164 function showList( $msg ) {
165 global $wgOut, $wgUser;
166
167 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
168 if ( "" != $msg ) {
169 $wgOut->setSubtitle( $msg );
170 }
171
172 // Purge expired entries on one in every 10 queries
173 if ( !mt_rand( 0, 10 ) ) {
174 Block::purgeExpired();
175 }
176
177 $conds = array();
178 $matches = array();
179 // Is user allowed to see all the blocks?
180 if ( !$wgUser->isAllowed( 'oversight' ) )
181 $conds['ipb_deleted'] = 0;
182 if ( $this->ip == '' ) {
183 // No extra conditions
184 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
185 $conds['ipb_id'] = substr( $this->ip, 1 );
186 } elseif ( IP::toUnsigned( $this->ip ) !== false ) {
187 $conds['ipb_address'] = $this->ip;
188 $conds['ipb_auto'] = 0;
189 } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip, $matches ) ) {
190 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
191 $conds['ipb_auto'] = 0;
192 } else {
193 $user = User::newFromName( $this->ip );
194 if ( $user && ( $id = $user->getID() ) != 0 ) {
195 $conds['ipb_user'] = $id;
196 } else {
197 // Uh...?
198 $conds['ipb_address'] = $this->ip;
199 $conds['ipb_auto'] = 0;
200 }
201 }
202
203 # TODO: difference message between
204 # a) an real empty list and
205 # b) requested ip/username not on list
206 $pager = new IPBlocklistPager( $this, $conds );
207 if ( $pager->getNumRows() ) {
208 $s = $this->searchForm() .
209 $pager->getNavigationBar();
210 $s .= "<ul>" .
211 $pager->getBody() .
212 "</ul>";
213 $s .= $pager->getNavigationBar();
214 } else {
215 $s = $this->searchForm() .
216 '<p>' . wfMsgHTML( 'ipblocklistempty' ) . '</p>';
217 }
218 $wgOut->addHTML( $s );
219 }
220
221 function searchForm() {
222 global $wgTitle, $wgScript, $wgRequest;
223 return
224 wfElement( 'form', array(
225 'action' => $wgScript ),
226 null ) .
227 wfHidden( 'title', $wgTitle->getPrefixedDbKey() ) .
228 wfElement( 'input', array(
229 'type' => 'hidden',
230 'name' => 'action',
231 'value' => 'search' ) ).
232 wfElement( 'input', array(
233 'type' => 'hidden',
234 'name' => 'limit',
235 'value' => $wgRequest->getText( 'limit' ) ) ) .
236 wfElement( 'input', array(
237 'name' => 'ip',
238 'value' => $this->ip ) ) .
239 wfElement( 'input', array(
240 'type' => 'submit',
241 'value' => wfMsg( 'ipblocklist-submit' ) ) ) .
242 '</form>';
243 }
244
245 /**
246 * Callback function to output a block
247 */
248 function formatRow( $block ) {
249 global $wgUser, $wgLang;
250
251 wfProfileIn( __METHOD__ );
252
253 static $sk=null, $msg=null;
254
255 if( is_null( $sk ) )
256 $sk = $wgUser->getSkin();
257 if( is_null( $msg ) ) {
258 $msg = array();
259 $keys = array( 'infiniteblock', 'expiringblock', 'contribslink', 'unblocklink',
260 'anononlyblock', 'createaccountblock', 'noautoblockblock' );
261 foreach( $keys as $key ) {
262 $msg[$key] = wfMsgHtml( $key );
263 }
264 $msg['blocklistline'] = wfMsg( 'blocklistline' );
265 $msg['contribslink'] = wfMsg( 'contribslink' );
266 }
267
268 # Prepare links to the blocker's user and talk pages
269 $blocker_id = $block->getBy();
270 $blocker_name = $block->getByName();
271 $blocker = $sk->userLink( $blocker_id, $blocker_name );
272 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
273
274 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
275 if( $block->mAuto ) {
276 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
277 } else {
278 $target = $sk->makeLinkObj( Title::makeTitle( NS_USER, $block->mAddress ), $block->mAddress );
279 $target .= ' (' . $sk->makeKnownLinkObj( SpecialPage::getSafeTitleFor( 'Contributions', $block->mAddress ), $msg['contribslink'] ) . ')';
280 }
281
282 $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
283
284 $properties = array();
285 if ( $block->mExpiry === "" || $block->mExpiry === Block::infinity() ) {
286 $properties[] = $msg['infiniteblock'];
287 } else {
288 $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
289 array( $wgLang->timeanddate( $block->mExpiry, true ) ) );
290 }
291 if ( $block->mAnonOnly ) {
292 $properties[] = $msg['anononlyblock'];
293 }
294 if ( $block->mCreateAccount ) {
295 $properties[] = $msg['createaccountblock'];
296 }
297 if (!$block->mEnableAutoblock && $block->mUser ) {
298 $properties[] = $msg['noautoblockblock'];
299 }
300
301 $properties = implode( ', ', $properties );
302
303 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
304
305 $unblocklink = '';
306 if ( $wgUser->isAllowed('block') ) {
307 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
308 $unblocklink = ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
309 }
310
311 $comment = $sk->commentBlock( $block->mReason );
312
313 $s = "{$line} $comment";
314 if ( $block->mHideName )
315 $s = '<span class="history-deleted">' . $s . '</span>';
316
317 wfProfileOut( __METHOD__ );
318 return "<li>$s $unblocklink</li>\n";
319 }
320 }
321
322 /**
323 * @todo document
324 */
325 class IPBlocklistPager extends ReverseChronologicalPager {
326 public $mForm, $mConds;
327
328 function __construct( $form, $conds = array() ) {
329 $this->mForm = $form;
330 $this->mConds = $conds;
331 parent::__construct();
332 }
333
334 function getStartBody() {
335 wfProfileIn( __METHOD__ );
336 # Do a link batch query
337 $this->mResult->seek( 0 );
338 $lb = new LinkBatch;
339
340 /*
341 while ( $row = $this->mResult->fetchObject() ) {
342 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
343 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
344 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
345 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
346 }*/
347 # Faster way
348 # Usernames and titles are in fact related by a simple substitution of space -> underscore
349 # The last few lines of Title::secureAndSplit() tell the story.
350 while ( $row = $this->mResult->fetchObject() ) {
351 $name = str_replace( ' ', '_', $row->user_name );
352 $lb->add( NS_USER, $name );
353 $lb->add( NS_USER_TALK, $name );
354 $name = str_replace( ' ', '_', $row->ipb_address );
355 $lb->add( NS_USER, $name );
356 $lb->add( NS_USER_TALK, $name );
357 }
358 $lb->execute();
359 wfProfileOut( __METHOD__ );
360 return '';
361 }
362
363 function formatRow( $row ) {
364 $block = new Block;
365 $block->initFromRow( $row );
366 return $this->mForm->formatRow( $block );
367 }
368
369 function getQueryInfo() {
370 $conds = $this->mConds;
371 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
372 $conds[] = 'ipb_by=user_id';
373 return array(
374 'tables' => array( 'ipblocks', 'user' ),
375 'fields' => $this->mDb->tableName( 'ipblocks' ) . '.*,user_name',
376 'conds' => $conds,
377 );
378 }
379
380 function getIndexField() {
381 return 'ipb_timestamp';
382 }
383 }
384
385 ?>