* Add exception hooks to output pretty messages
[lhc/web/wiklou.git] / includes / SpecialIpblocklist.php
index 6df7f03..997bc5e 100644 (file)
@@ -1,8 +1,7 @@
 <?php
 /**
  *
- * @package MediaWiki
- * @subpackage SpecialPage
+ * @addtogroup SpecialPage
  */
 
 /**
@@ -10,7 +9,7 @@
  */
 function wfSpecialIpblocklist() {
        global $wgUser, $wgOut, $wgRequest;
-
+       
        $ip = $wgRequest->getVal( 'wpUnblockAddress', $wgRequest->getVal( 'ip' ) );
        $id = $wgRequest->getVal( 'id' );
        $reason = $wgRequest->getText( 'wpUnblockReason' );
@@ -19,38 +18,66 @@ function wfSpecialIpblocklist() {
 
        $ipu = new IPUnblockForm( $ip, $id, $reason );
 
-       if ( "success" == $action ) {
-               $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
-       } else if ( "submit" == $action && $wgRequest->wasPosted() &&
-               $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
-               if ( ! $wgUser->isAllowed('block') ) {
-                       $wgOut->sysopRequired();
+       if( $action == 'unblock' ) {
+               # Check permissions
+               if( !$wgUser->isAllowed( 'block' ) ) {
+                       $wgOut->permissionRequired( 'block' );
+                       return;
+               }
+               # Check for database lock
+               if( wfReadOnly() ) {
+                       $wgOut->readOnlyPage();
+                       return;
+               }
+               # Show unblock form
+               $ipu->showForm( '' );
+       } elseif( $action == 'submit' && $wgRequest->wasPosted()
+               && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
+               # Check permissions
+               if( !$wgUser->isAllowed( 'block' ) ) {
+                       $wgOut->permissionRequired( 'block' );
                        return;
                }
+               # Check for database lock
+               if( wfReadOnly() ) {
+                       $wgOut->readOnlyPage();
+                       return;
+               }
+               # Remove blocks and redirect user to success page
                $ipu->doSubmit();
-       } else if ( "unblock" == $action ) {
-               $ipu->showForm( "" );
+       } elseif( $action == 'success' ) {
+               # Inform the user of a successful unblock
+               # (No need to check permissions or locks here,
+               # if something was done, then it's too late!)
+               if ( substr( $successip, 0, 1) == '#' ) {
+                       // A block ID was unblocked
+                       $ipu->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
+               } else {
+                       // A username/IP was unblocked
+                       $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
+               }
        } else {
-               $ipu->showList( "" );
+               # Just show the block list
+               $ipu->showList( '' );
        }
+
 }
 
 /**
- *
- * @package MediaWiki
- * @subpackage SpecialPage
+ * implements Special:ipblocklist GUI
+ * @addtogroup SpecialPage
  */
 class IPUnblockForm {
        var $ip, $reason, $id;
 
        function IPUnblockForm( $ip, $id, $reason ) {
-               $this->ip = $ip;
+               $this->ip = strtr( $ip, '_', ' ' );
                $this->id = $id;
                $this->reason = $reason;
        }
 
        function showForm( $err ) {
-               global $wgOut, $wgUser, $wgSysopUserBans;
+               global $wgOut, $wgUser, $wgSysopUserBans, $wgContLang;
 
                $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
                $wgOut->addWikiText( wfMsg( 'unblockiptext' ) );
@@ -58,8 +85,9 @@ class IPUnblockForm {
                $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
                $ipr = wfMsgHtml( 'ipbreason' );
                $ipus = wfMsgHtml( 'ipusubmit' );
-               $titleObj = Title::makeTitle( NS_SPECIAL, "Ipblocklist" );
-               $action = $titleObj->escapeLocalURL( "action=submit" );
+               $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
+               $action = $titleObj->getLocalURL( "action=submit" );
+               $alignRight = $wgContLang->isRtl() ? 'left' : 'right';
 
                if ( "" != $err ) {
                        $wgOut->setSubtitle( wfMsg( "formerror" ) );
@@ -72,80 +100,115 @@ class IPUnblockForm {
                        $block = Block::newFromID( $this->id );
                        if ( $block ) {
                                $encName = htmlspecialchars( $block->getRedactedName() );
-                               $encId = htmlspecialchars( $this->id );
-                               $addressPart = $encName . "<input type='hidden' name=\"id\" value=\"$encId\" />";
+                               $encId = $this->id;
+                               $addressPart = $encName . Xml::hidden( 'id', $encId );
                        }
                }
                if ( !$addressPart ) {
-                       $addressPart = "<input tabindex='1' type='text' size='20' " .
-                               "name=\"wpUnblockAddress\" value=\"" . htmlspecialchars( $this->ip ) . "\" />";
+                       $addressPart = Xml::input( 'wpUnblockAddress', 20, $this->ip, array( 'type' => 'text', 'tabindex' => '1' ) );
                }
 
-               $wgOut->addHTML( "
-<form id=\"unblockip\" method=\"post\" action=\"{$action}\">
-       <table border='0'>
-               <tr>
-                       <td align='right'>{$ipa}:</td>
-                       <td align='left'>
-                               {$addressPart}
-                       </td>
-               </tr>
-               <tr>
-                       <td align='right'>{$ipr}:</td>
-                       <td align='left'>
-                               <input tabindex='1' type='text' size='40' name=\"wpUnblockReason\" value=\"" . htmlspecialchars( $this->reason ) . "\" />
-                       </td>
-               </tr>
-               <tr>
-                       <td>&nbsp;</td>
-                       <td align='left'>
-                               <input tabindex='2' type='submit' name=\"wpBlock\" value=\"{$ipus}\" />
-                       </td>
-               </tr>
-       </table>
-       <input type='hidden' name='wpEditToken' value=\"{$token}\" />
-</form>\n" );
+               $wgOut->addHTML(
+                       Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
+                       Xml::openElement( 'table', array( 'border' => '0' ) ).
+                       "<tr>
+                               <td align='$alignRight'>
+                                       {$ipa}
+                               </td>
+                               <td>
+                                       {$addressPart}
+                               </td>
+                       </tr>
+                       <tr>
+                               <td align='$alignRight'>
+                                       {$ipr}
+                               </td>
+                               <td>" .
+                                       Xml::input( 'wpUnblockReason', 40, $this->reason, array( 'type' => 'text', 'tabindex' => '2' ) ) .
+                               "</td>
+                       </tr>
+                       <tr>
+                               <td>&nbsp;</td>
+                               <td>" .
+                                       Xml::submitButton( $ipus, array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
+                               "</td>
+                       </tr>" .
+                       Xml::closeElement( 'table' ) .
+                       Xml::hidden( 'wpEditToken', $token ) .
+                       Xml::closeElement( 'form' ) . "\n"
+               );
 
        }
 
-       function doSubmit() {
-               global $wgOut;
+       const UNBLOCK_SUCCESS = 0; // Success
+       const UNBLOCK_NO_SUCH_ID = 1; // No such block ID
+       const UNBLOCK_USER_NOT_BLOCKED = 2; // IP wasn't blocked
+       const UNBLOCK_BLOCKED_AS_RANGE = 3; // IP is part of a range block
+       const UNBLOCK_UNKNOWNERR = 4; // Unknown error
 
-               if ( $this->id ) {
-                       $block = Block::newFromID( $this->id );
-                       if ( $block ) {
-                               $this->ip = $block->getRedactedName();
+       /**
+        * Backend code for unblocking. doSubmit() wraps around this.
+        * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
+        * case it contains the range $ip is part of.
+        * Returns one of UNBLOCK_*
+        */
+
+       static function doUnblock(&$id, &$ip, &$reason, &$range = null)
+       {
+               if ( $id ) {
+                       $block = Block::newFromID( $id );
+                       if ( !$block ) {
+                               return self::UNBLOCK_NO_SUCH_ID;
                        }
+                       $ip = $block->getRedactedName();
                } else {
                        $block = new Block();
-                       $this->ip = trim( $this->ip );
-                       if ( substr( $this->ip, 0, 1 ) == "#" ) {
-                               $id = substr( $this->ip, 1 );
+                       $ip = trim( $ip );
+                       if ( substr( $ip, 0, 1 ) == "#" ) {
+                               $id = substr( $ip, 1 );
                                $block = Block::newFromID( $id );
+                               if( !$block ) {
+                                       return self::UNBLOCK_NO_SUCH_ID;
+                               }
                        } else {
-                               $block = Block::newFromDB( $this->ip );
+                               $block = Block::newFromDB( $ip );
                                if ( !$block ) { 
-                                       $block = null;
+                                       return self::UNBLOCK_USER_NOT_BLOCKED;
+                               }
+                               if( $block->mRangeStart != $block->mRangeEnd
+                                               && !strstr( $ip, "/" ) ) {
+                                       /* If the specified IP is a single address, and the block is
+                                        * a range block, don't unblock the range. */
+                                        $range = $block->mAddress;
+                                        return self::UNBLOCK_BLOCKED_AS_RANGE;
                                }
                        }
                }
-               $success = false;
-               if ( $block ) {
-                       # Delete block
-                       if ( $block->delete() ) {
-                               # Make log entry
-                               $log = new LogPage( 'block' );
-                               $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $this->ip ), $this->reason );
-                               $success = true;
-                       }
+               // Yes, this is really necessary
+               $id = $block->mId;
+
+               # Delete block
+               if ( !$block->delete() ) {
+                       return self::UNBLOCK_UNKNOWNERR;
                }
 
-               if ( $success ) {
+               # Make log entry
+               $log = new LogPage( 'block' );
+               $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason );
+               return self::UNBLOCK_SUCCESS;
+       }
+
+       function doSubmit() {
+               global $wgOut;
+               $retval = self::doUnblock(&$this->id, &$this->ip, &$this->reason, &$range);
+               if($retval == self::UNBLOCK_SUCCESS) {
                        # Report to the user
-                       $titleObj = Title::makeTitle( NS_SPECIAL, "Ipblocklist" );
+                       $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
                        $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
                        $wgOut->redirect( $success );
-               } else {
+               } else if($retval == self::UNBLOCK_BLOCKED_AS_RANGE) {
+                       $this->showForm( wfMsg( 'ipb_blocked_as_range', $this->ip, $range ) );
+               } else { // UI code doesn't distinguish between errors much. Maybe it should
                        if ( !$this->ip && $this->id ) {
                                $this->ip = '#' . $this->id;
                        }
@@ -154,7 +217,7 @@ class IPUnblockForm {
        }
 
        function showList( $msg ) {
-               global $wgOut;
+               global $wgOut, $wgUser;
 
                $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
                if ( "" != $msg ) {
@@ -167,6 +230,10 @@ class IPUnblockForm {
                }
 
                $conds = array();
+               $matches = array();
+               // Is user allowed to see all the blocks?
+               if ( !$wgUser->isAllowed( 'oversight' ) )
+                       $conds['ipb_deleted'] = 0;
                if ( $this->ip == '' ) {
                        // No extra conditions
                } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
@@ -174,7 +241,7 @@ class IPUnblockForm {
                } elseif ( IP::toUnsigned( $this->ip ) !== false ) {
                        $conds['ipb_address'] = $this->ip;
                        $conds['ipb_auto'] = 0;
-               } elseif( preg_match( "/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/", $this->ip, $matches ) ) {
+               } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip, $matches ) ) {
                        $conds['ipb_address'] = Block::normaliseRange( $this->ip );
                        $conds['ipb_auto'] = 0;
                } else {
@@ -189,41 +256,33 @@ class IPUnblockForm {
                }
 
                $pager = new IPBlocklistPager( $this, $conds );
-               $s = $pager->getNavigationBar() .
-                       $this->searchForm();
                if ( $pager->getNumRows() ) {
-                       $s .= "<ul>" . 
-                               $pager->getBody() .
-                               "</ul>";
+                       $wgOut->addHTML(
+                               $this->searchForm() .
+                               $pager->getNavigationBar() .
+                               Xml::tags( 'ul', null, $pager->getBody() ) .
+                               $pager->getNavigationBar()
+                       );
+               } elseif ( $this->ip != '') {
+                       $wgOut->addHTML( $this->searchForm() );
+                       $wgOut->addWikiText( wfMsg( 'ipblocklist-no-results' ) );
                } else {
-                       $s .= '<p>' . wfMsgHTML( 'ipblocklistempty' ) . '</p>';
+                       $wgOut->addWikiText( wfMsg( 'ipblocklist-empty' ) );
                }
-               $s .= $pager->getNavigationBar();
-               $wgOut->addHTML( $s );
        }
 
        function searchForm() {
                global $wgTitle, $wgScript, $wgRequest;
                return
-                       wfElement( 'form', array(
-                               'action' => $wgScript ),
-                               null ) .
-                       wfHidden( 'title', $wgTitle->getPrefixedDbKey() ) .
-                       wfElement( 'input', array(
-                               'type' => 'hidden',
-                               'name' => 'action',
-                               'value' => 'search' ) ).
-                       wfElement( 'input', array(
-                               'type' => 'hidden',
-                               'name' => 'limit',
-                               'value' => $wgRequest->getText( 'limit' ) ) ) .
-                       wfElement( 'input', array(
-                               'name' => 'ip',
-                               'value' => $this->ip ) ) .
-                       wfElement( 'input', array(
-                               'type' => 'submit',
-                               'value' => wfMsg( 'search' ) ) ) .
-                       '</form>';
+                       Xml::tags( 'form', array( 'action' => $wgScript ),
+                               Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() ) .
+                               Xml::openElement( 'fieldset' ) .
+                               Xml::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
+                               Xml::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip ) .
+                               '&nbsp;' .
+                               Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) .
+                               Xml::closeElement( 'fieldset' )
+                       );
        }
 
        /**
@@ -240,26 +299,26 @@ class IPUnblockForm {
                        $sk = $wgUser->getSkin();
                if( is_null( $msg ) ) {
                        $msg = array();
-                       $keys = array( 'infiniteblock', 'expiringblock', 'contribslink', 'unblocklink', 
-                               'anononlyblock', 'createaccountblock' );
+                       $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink',
+                               'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock' );
                        foreach( $keys as $key ) {
                                $msg[$key] = wfMsgHtml( $key );
                        }
                        $msg['blocklistline'] = wfMsg( 'blocklistline' );
-                       $msg['contribslink'] = wfMsg( 'contribslink' );
                }
 
                # Prepare links to the blocker's user and talk pages
+               $blocker_id = $block->getBy();
                $blocker_name = $block->getByName();
-               $blocker = $sk->MakeLinkObj( Title::makeTitle( NS_USER, $blocker_name ), $blocker_name );
-               $blocker .= ' (' . $sk->makeLinkObj( Title::makeTitle( NS_USER_TALK, $blocker_name ), $wgLang->getNsText( NS_TALK ) ) . ')';
+               $blocker = $sk->userLink( $blocker_id, $blocker_name );
+               $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
 
                # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
                if( $block->mAuto ) {
                        $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
                } else {
-                       $target = $sk->makeLinkObj( Title::makeTitle( NS_USER, $block->mAddress ), $block->mAddress );
-                       $target .= ' (' . $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions' ), $msg['contribslink'], 'target=' . urlencode( $block->mAddress ) ) . ')';
+                       $target = $sk->userLink( $block->mUser, $block->mAddress )
+                               . $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
                }
                
                $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
@@ -277,23 +336,39 @@ class IPUnblockForm {
                if ( $block->mCreateAccount ) {
                        $properties[] = $msg['createaccountblock'];
                }
+               if (!$block->mEnableAutoblock && $block->mUser ) {
+                       $properties[] = $msg['noautoblockblock'];
+               }
+
+               if ( $block->mBlockEmail && $block->mUser ) {
+                       $properties[] = $msg['emailblock'];
+               }
+
                $properties = implode( ', ', $properties );
 
                $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
 
-               $s = "<li>{$line}";
-
+               $unblocklink = '';
                if ( $wgUser->isAllowed('block') ) {
-                       $titleObj = Title::makeTitle( NS_SPECIAL, "Ipblocklist" );
-                       $s .= ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
+                       $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
+                       $unblocklink = ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
                }
-               $s .= $sk->commentBlock( $block->mReason );
-               $s .= "</li>\n";
+               
+               $comment = $sk->commentBlock( $block->mReason );
+               
+               $s = "{$line} $comment";        
+               if ( $block->mHideName )
+                       $s = '<span class="history-deleted">' . $s . '</span>';
+                               
                wfProfileOut( __METHOD__ );
-               return $s;
+               return "<li>$s $unblocklink</li>\n";
        }
 }
 
+/**
+ * @todo document
+ * @addtogroup Pager
+ */
 class IPBlocklistPager extends ReverseChronologicalPager {
        public $mForm, $mConds;
 
@@ -354,4 +429,4 @@ class IPBlocklistPager extends ReverseChronologicalPager {
        }
 }
 
-?>
+