* Add exception hooks to output pretty messages
[lhc/web/wiklou.git] / includes / SpecialIpblocklist.php
index 97ace8d..997bc5e 100644 (file)
@@ -1,8 +1,7 @@
 <?php
 /**
  *
- * @package MediaWiki
- * @subpackage SpecialPage
+ * @addtogroup SpecialPage
  */
 
 /**
  */
 function wfSpecialIpblocklist() {
        global $wgUser, $wgOut, $wgRequest;
-
+       
        $ip = $wgRequest->getVal( 'wpUnblockAddress', $wgRequest->getVal( 'ip' ) );
+       $id = $wgRequest->getVal( 'id' );
        $reason = $wgRequest->getText( 'wpUnblockReason' );
        $action = $wgRequest->getText( 'action' );
+       $successip = $wgRequest->getVal( 'successip' );
 
-       $ipu = new IPUnblockForm( $ip, $reason );
+       $ipu = new IPUnblockForm( $ip, $id, $reason );
 
-       if ( "success" == $action ) {
-               $ipu->showList( wfMsgWikiHtml( 'unblocked', $ip ) );
-       } 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;
+       var $ip, $reason, $id;
 
-       function IPUnblockForm( $ip, $reason ) {
-               $this->ip = $ip;
+       function IPUnblockForm( $ip, $id, $reason ) {
+               $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' ) );
@@ -55,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" ) );
@@ -64,140 +95,203 @@ class IPUnblockForm {
                }
                $token = htmlspecialchars( $wgUser->editToken() );
 
-               $wgOut->addHTML( "
-<form id=\"unblockip\" method=\"post\" action=\"{$action}\">
-       <table border='0'>
-               <tr>
-                       <td align='right'>{$ipa}:</td>
-                       <td align='left'>
-                               <input tabindex='1' type='text' size='20' name=\"wpUnblockAddress\" value=\"" . htmlspecialchars( $this->ip ) . "\" />
-                       </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" );
+               $addressPart = false;
+               if ( $this->id ) {
+                       $block = Block::newFromID( $this->id );
+                       if ( $block ) {
+                               $encName = htmlspecialchars( $block->getRedactedName() );
+                               $encId = $this->id;
+                               $addressPart = $encName . Xml::hidden( 'id', $encId );
+                       }
+               }
+               if ( !$addressPart ) {
+                       $addressPart = Xml::input( 'wpUnblockAddress', 20, $this->ip, array( 'type' => 'text', 'tabindex' => '1' ) );
+               }
+
+               $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
 
-               $block = new Block();
-               $this->ip = trim( $this->ip );
+       /**
+        * 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_*
+        */
 
-               if ( $this->ip{0} == "#" ) {
-                       $block->mId = substr( $this->ip, 1 );
+       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->mAddress = $this->ip;
+                       $block = new Block();
+                       $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( $ip );
+                               if ( !$block ) { 
+                                       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;
+                               }
+                       }
                }
+               // Yes, this is really necessary
+               $id = $block->mId;
 
-               # Delete block (if it exists)
-               # We should probably check for errors rather than just declaring success
-               $block->delete();
+               # Delete block
+               if ( !$block->delete() ) {
+                       return self::UNBLOCK_UNKNOWNERR;
+               }
 
                # Make log entry
                $log = new LogPage( 'block' );
-               $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $this->ip ), $this->reason );
+               $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason );
+               return self::UNBLOCK_SUCCESS;
+       }
 
-               # Report to the user
-               $titleObj = Title::makeTitle( NS_SPECIAL, "Ipblocklist" );
-               $success = $titleObj->getFullURL( "action=success&ip=" . urlencode( $this->ip ) );
-               $wgOut->redirect( $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 = SpecialPage::getTitleFor( "Ipblocklist" );
+                       $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
+                       $wgOut->redirect( $success );
+               } 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;
+                       }
+                       $this->showForm( wfMsg( 'ipb_cant_unblock', htmlspecialchars( $this->id ) ) );
+               }
        }
 
        function showList( $msg ) {
-               global $wgOut;
+               global $wgOut, $wgUser;
 
                $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
                if ( "" != $msg ) {
                        $wgOut->setSubtitle( $msg );
                }
-               global $wgRequest;
-               list( $this->limit, $this->offset ) = $wgRequest->getLimitOffset();
-               $this->counter = 0;
 
-               $paging = '<p>' . wfViewPrevNext( $this->offset, $this->limit,
-                       Title::makeTitle( NS_SPECIAL, 'Ipblocklist' ),
-                       'ip=' . urlencode( $this->ip ) ) . "</p>\n";
-               $wgOut->addHTML( $paging );
+               // Purge expired entries on one in every 10 queries
+               if ( !mt_rand( 0, 10 ) ) {
+                       Block::purgeExpired();
+               }
 
-               $search = $this->searchForm();
-               $wgOut->addHTML( $search );
-               
-               $wgOut->addHTML( "<ul>" );
-               if( !Block::enumBlocks( array( &$this, "addRow" ), 0 ) ) {
-                       // FIXME hack to solve #bug 1487
-                       $wgOut->addHTML( '<li>'.wfMsgHtml( 'ipblocklistempty' ).'</li>' );
+               $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 ) == '#' ) {
+                       $conds['ipb_id'] = substr( $this->ip, 1 );
+               } 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 ) ) {
+                       $conds['ipb_address'] = Block::normaliseRange( $this->ip );
+                       $conds['ipb_auto'] = 0;
+               } else {
+                       $user = User::newFromName( $this->ip );
+                       if ( $user && ( $id = $user->getID() ) != 0 ) {
+                               $conds['ipb_user'] = $id;
+                       } else {
+                               // Uh...?
+                               $conds['ipb_address'] = $this->ip;
+                               $conds['ipb_auto'] = 0;
+                       }
+               }
+
+               $pager = new IPBlocklistPager( $this, $conds );
+               if ( $pager->getNumRows() ) {
+                       $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 {
+                       $wgOut->addWikiText( wfMsg( 'ipblocklist-empty' ) );
                }
-               $wgOut->addHTML( "</ul>\n" );
-               $wgOut->addHTML( $paging );
        }
 
        function searchForm() {
-               global $wgTitle;
+               global $wgTitle, $wgScript, $wgRequest;
                return
-                       wfElement( 'form', array(
-                               'action' => $wgTitle->getLocalUrl() ),
-                               null ) .
-                       wfElement( 'input', array(
-                               'type' => 'hidden',
-                               'name' => 'action',
-                               'value' => 'search' ) ).
-                       wfElement( 'input', array(
-                               'type' => 'hidden',
-                               'name' => 'limit',
-                               'value' => $this->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' )
+                       );
        }
 
        /**
         * Callback function to output a block
         */
-       function addRow( $block, $tag ) {
-               global $wgOut, $wgUser, $wgLang;
+       function formatRow( $block ) {
+               global $wgUser, $wgLang;
 
-               if( $this->ip != '' ) {
-                       if( $block->mAuto ) {
-                               if( stristr( $block->mId, $this->ip ) == false ) {
-                                       return;
-                               }
-                       } else {
-                               if( stristr( $block->mAddress, $this->ip ) == false ) {
-                                       return;
-                               }
-                       }
-               }
-
-               // Loading blocks is fast; displaying them is slow.
-               // Quick hack for paging.
-               $this->counter++;
-               if( $this->counter <= $this->offset ) {
-                       return;
-               }
-               if( $this->counter - $this->offset > $this->limit ) {
-                       return;
-               }
-
-               $fname = 'IPUnblockForm-addRow';
-               wfProfileIn( $fname );
+               wfProfileIn( __METHOD__ );
 
                static $sk=null, $msg=null;
 
@@ -205,51 +299,134 @@ class IPUnblockForm {
                        $sk = $wgUser->getSkin();
                if( is_null( $msg ) ) {
                        $msg = array();
-                       foreach( array( 'infiniteblock', 'expiringblock', 'contribslink', 'unblocklink' ) as $key ) {
+                       $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->mId; # Hide the IP addresses of auto-blocks; privacy
+                       $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 );
                }
                
-               # Prep the address for the unblock link, masking autoblocks as before
-               $addr = $block->mAuto ? '#' . $block->mId : $block->mAddress;
-               
                $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
 
-               if ( $block->mExpiry === "" ) {
-                       $formattedExpiry = $msg['infiniteblock'];
+               $properties = array();
+               if ( $block->mExpiry === "" || $block->mExpiry === Block::infinity() ) {
+                       $properties[] = $msg['infiniteblock'];
                } else {
-                       $formattedExpiry = wfMsgReplaceArgs( $msg['expiringblock'],
+                       $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
                                array( $wgLang->timeanddate( $block->mExpiry, true ) ) );
                }
+               if ( $block->mAnonOnly ) {
+                       $properties[] = $msg['anononlyblock'];
+               }
+               if ( $block->mCreateAccount ) {
+                       $properties[] = $msg['createaccountblock'];
+               }
+               if (!$block->mEnableAutoblock && $block->mUser ) {
+                       $properties[] = $msg['noautoblockblock'];
+               }
+
+               if ( $block->mBlockEmail && $block->mUser ) {
+                       $properties[] = $msg['emailblock'];
+               }
 
-               $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $formattedExpiry ) );
+               $properties = implode( ', ', $properties );
 
-               $wgOut->addHTML( "<li>{$line}" );
+               $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
 
+               $unblocklink = '';
                if ( $wgUser->isAllowed('block') ) {
-                       $titleObj = Title::makeTitle( NS_SPECIAL, "Ipblocklist" );
-                       $wgOut->addHTML( ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&ip=' . urlencode( $addr ) ) . ')' );
+                       $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
+                       $unblocklink = ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId ) ) . ')';
                }
-               $wgOut->addHTML( $sk->commentBlock( $block->mReason ) );
-               $wgOut->addHTML( "</li>\n" );
-               wfProfileOut( $fname );
+               
+               $comment = $sk->commentBlock( $block->mReason );
+               
+               $s = "{$line} $comment";        
+               if ( $block->mHideName )
+                       $s = '<span class="history-deleted">' . $s . '</span>';
+                               
+               wfProfileOut( __METHOD__ );
+               return "<li>$s $unblocklink</li>\n";
        }
 }
 
-?>
+/**
+ * @todo document
+ * @addtogroup Pager
+ */
+class IPBlocklistPager extends ReverseChronologicalPager {
+       public $mForm, $mConds;
+
+       function __construct( $form, $conds = array() ) {
+               $this->mForm = $form;
+               $this->mConds = $conds;
+               parent::__construct();
+       }
+
+       function getStartBody() {
+               wfProfileIn( __METHOD__ );
+               # Do a link batch query
+               $this->mResult->seek( 0 );
+               $lb = new LinkBatch;
+
+               /*
+               while ( $row = $this->mResult->fetchObject() ) {
+                       $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
+                       $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
+                       $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
+                       $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
+               }*/
+               # Faster way
+               # Usernames and titles are in fact related by a simple substitution of space -> underscore
+               # The last few lines of Title::secureAndSplit() tell the story.
+               while ( $row = $this->mResult->fetchObject() ) {
+                       $name = str_replace( ' ', '_', $row->user_name );
+                       $lb->add( NS_USER, $name );
+                       $lb->add( NS_USER_TALK, $name );
+                       $name = str_replace( ' ', '_', $row->ipb_address );
+                       $lb->add( NS_USER, $name );
+                       $lb->add( NS_USER_TALK, $name );
+               }
+               $lb->execute();
+               wfProfileOut( __METHOD__ );
+               return '';
+       }
+       
+       function formatRow( $row ) {
+               $block = new Block;
+               $block->initFromRow( $row );
+               return $this->mForm->formatRow( $block );
+       }
+
+       function getQueryInfo() {
+               $conds = $this->mConds;
+               $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
+               $conds[] = 'ipb_by=user_id';
+               return array(
+                       'tables' => array( 'ipblocks', 'user' ),
+                       'fields' => $this->mDb->tableName( 'ipblocks' ) . '.*,user_name',
+                       'conds' => $conds,
+               );
+       }
+
+       function getIndexField() {
+               return 'ipb_timestamp';
+       }
+}
+
+