Deferred loading of DateFormatter.php and IP.php. Standardised IP.php function naming style.

This commit is contained in:
Tim Starling 2006-07-14 17:02:49 +00:00
parent 8831c8e13d
commit 0ade783ae2
5 changed files with 73 additions and 88 deletions

View file

@ -214,7 +214,7 @@ class Block
*/
function loadRange( $address, $killExpired = true )
{
$iaddr = IP::ToHex( $address );
$iaddr = IP::toHex( $address );
if ( $iaddr === false ) {
# Invalid address
return false;
@ -507,7 +507,7 @@ class Block
$parts = explode( '/', $range );
if ( count( $parts ) == 2 ) {
$shift = 32 - $parts[1];
$ipint = IP::ToUnsigned( $parts[0] );
$ipint = IP::toUnsigned( $parts[0] );
$ipint = $ipint >> $shift << $shift;
$newip = long2ip( $ipint );
$range = "$newip/{$parts[1]}";

View file

@ -1,25 +1,11 @@
<?php
/**
* Contain things
* @todo document
* Date formatter, recognises dates in plain text and formats them accoding to user preferences.
*
* @package MediaWiki
* @subpackage Parser
*/
/** */
define('DF_ALL', -1);
define('DF_NONE', 0);
define('DF_MDY', 1);
define('DF_DMY', 2);
define('DF_YMD', 3);
define('DF_ISO1', 4);
define('DF_LASTPREF', 4);
define('DF_ISO2', 5);
define('DF_YDM', 6);
define('DF_DM', 7);
define('DF_MD', 8);
define('DF_LAST', 8);
/**
* @todo preferences, OutputPage
* @package MediaWiki
@ -32,6 +18,19 @@ class DateFormatter
var $regexes, $pDays, $pMonths, $pYears;
var $rules, $xMonths;
const ALL = -1;
const NONE = 0;
const MDY = 1;
const DMY = 2;
const YMD = 3;
const ISO1 = 4;
const LASTPREF = 4;
const ISO2 = 5;
const YDM = 6;
const DM = 7;
const MD = 8;
const LAST = 8;
/**
* @todo document
@ -55,43 +54,43 @@ class DateFormatter
$this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
# Real regular expressions
$this->regexes[DF_DMY] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
$this->regexes[DF_YDM] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
$this->regexes[DF_MDY] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
$this->regexes[DF_YMD] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
$this->regexes[DF_DM] = "/{$this->prxDM}{$this->regexTrail}";
$this->regexes[DF_MD] = "/{$this->prxMD}{$this->regexTrail}";
$this->regexes[DF_ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
$this->regexes[DF_ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
$this->regexes[self::DMY] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
$this->regexes[self::YDM] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
$this->regexes[self::MDY] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
$this->regexes[self::YMD] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
$this->regexes[self::DM] = "/{$this->prxDM}{$this->regexTrail}";
$this->regexes[self::MD] = "/{$this->prxMD}{$this->regexTrail}";
$this->regexes[self::ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
$this->regexes[self::ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
# Extraction keys
# See the comments in replace() for the meaning of the letters
$this->keys[DF_DMY] = 'jFY';
$this->keys[DF_YDM] = 'Y jF';
$this->keys[DF_MDY] = 'FjY';
$this->keys[DF_YMD] = 'Y Fj';
$this->keys[DF_DM] = 'jF';
$this->keys[DF_MD] = 'Fj';
$this->keys[DF_ISO1] = 'ymd'; # y means ISO year
$this->keys[DF_ISO2] = 'ymd';
$this->keys[self::DMY] = 'jFY';
$this->keys[self::YDM] = 'Y jF';
$this->keys[self::MDY] = 'FjY';
$this->keys[self::YMD] = 'Y Fj';
$this->keys[self::DM] = 'jF';
$this->keys[self::MD] = 'Fj';
$this->keys[self::ISO1] = 'ymd'; # y means ISO year
$this->keys[self::ISO2] = 'ymd';
# Target date formats
$this->targets[DF_DMY] = '[[F j|j F]] [[Y]]';
$this->targets[DF_YDM] = '[[Y]], [[F j|j F]]';
$this->targets[DF_MDY] = '[[F j]], [[Y]]';
$this->targets[DF_YMD] = '[[Y]] [[F j]]';
$this->targets[DF_DM] = '[[F j|j F]]';
$this->targets[DF_MD] = '[[F j]]';
$this->targets[DF_ISO1] = '[[Y|y]]-[[F j|m-d]]';
$this->targets[DF_ISO2] = '[[y-m-d]]';
$this->targets[self::DMY] = '[[F j|j F]] [[Y]]';
$this->targets[self::YDM] = '[[Y]], [[F j|j F]]';
$this->targets[self::MDY] = '[[F j]], [[Y]]';
$this->targets[self::YMD] = '[[Y]] [[F j]]';
$this->targets[self::DM] = '[[F j|j F]]';
$this->targets[self::MD] = '[[F j]]';
$this->targets[self::ISO1] = '[[Y|y]]-[[F j|m-d]]';
$this->targets[self::ISO2] = '[[y-m-d]]';
# Rules
# pref source target
$this->rules[DF_DMY][DF_MD] = DF_DM;
$this->rules[DF_ALL][DF_MD] = DF_MD;
$this->rules[DF_MDY][DF_DM] = DF_MD;
$this->rules[DF_ALL][DF_DM] = DF_DM;
$this->rules[DF_NONE][DF_ISO2] = DF_ISO1;
$this->rules[self::DMY][self::MD] = self::DM;
$this->rules[self::ALL][self::MD] = self::MD;
$this->rules[self::MDY][self::DM] = self::MD;
$this->rules[self::ALL][self::DM] = self::DM;
$this->rules[self::NONE][self::ISO2] = self::ISO1;
}
/**
@ -116,14 +115,14 @@ class DateFormatter
*/
function reformat( $preference, $text ) {
if ($preference == 'ISO 8601') $preference = 4; # The ISO 8601 option used to be 4
for ( $i=1; $i<=DF_LAST; $i++ ) {
for ( $i=1; $i<=self::LAST; $i++ ) {
$this->mSource = $i;
if ( @$this->rules[$preference][$i] ) {
# Specific rules
$this->mTarget = $this->rules[$preference][$i];
} elseif ( @$this->rules[DF_ALL][$i] ) {
} elseif ( @$this->rules[self::ALL][$i] ) {
# General rules
$this->mTarget = $this->rules[DF_ALL][$i];
$this->mTarget = $this->rules[self::ALL][$i];
} elseif ( $preference ) {
# User preference
$this->mTarget = $preference;
@ -131,7 +130,7 @@ class DateFormatter
# Default
$this->mTarget = $i;
}
$text = preg_replace_callback( $this->regexes[$i], 'wfMainDateReplace', $text );
$text = preg_replace_callback( $this->regexes[$i], array( &$this, 'replace' ), $text );
}
return $text;
}
@ -277,12 +276,4 @@ class DateFormatter
}
}
/**
* @todo document
*/
function wfMainDateReplace( $matches ) {
$df =& DateFormatter::getInstance();
return $df->replace( $matches );
}
?>

View file

@ -7,31 +7,30 @@
* @License GPL v2 or later
*/
// Some regex definition to "play" with IP address and IP address blocks
// An IP is made of 4 bytes from x00 to xFF which is d0 to d255
define( 'RE_IP_BYTE', '(25[0-5]|2[0-4]\d|1?\d{1,2})');
define( 'RE_IP_ADD' , RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE );
// An IP block is an IP address and a prefix (d1 to d32)
define( 'RE_IP_PREFIX' , '(3[0-2]|[12]?\d)');
define( 'RE_IP_BLOCK', RE_IP_ADD . '\/' . RE_IP_PREFIX);
class IP {
// Some regex definition to "play" with IP address and IP address blocks
// An IP is made of 4 bytes from x00 to xFF which is d0 to d255
const RE_BYTE = '(25[0-5]|2[0-4]\d|1?\d{1,2})';
const RE_ADD = self::RE_BYTE . '\.' . self::RE_BYTE . '\.' . self::RE_BYTE . '\.' . self::RE_BYTE;
// An IP block is an IP address and a prefix (d1 to d32)
const RE_PREFIX = '(3[0-2]|[12]?\d)';
const RE_BLOCK = self::RE_ADD . '\/' . self::RE_PREFIX;
/**
* Validate an IP address.
* @return boolean True if it is valid.
*/
public static function IsValid( $ip ) {
return preg_match( '/^' . RE_IP_ADD . '$/', $ip, $matches) ;
public static function isValid( $ip ) {
return preg_match( '/^' . self::RE_ADD . '$/', $ip, $matches) ;
}
/**
* Validate an IP Block.
* @return boolean True if it is valid.
*/
public static function IsValidBlock( $ipblock ) {
return ( count(self::ToArray($ipblock)) == 1 + 5 );
public static function isValidBlock( $ipblock ) {
return ( count(self::toArray($ipblock)) == 1 + 5 );
}
/**
@ -39,8 +38,8 @@ class IP {
* i.e. not RFC 1918 or similar
* Comes from ProxyTools.php
*/
function IsPublic( $ip ) {
$n = IP::ToUnsigned( $ip );
function isPublic( $ip ) {
$n = IP::toUnsigned( $ip );
if ( !$n ) {
return false;
}
@ -63,8 +62,8 @@ class IP {
}
foreach ( $privateRanges as $r ) {
$start = IP::ToUnsigned( $r[0] );
$end = IP::ToUnsigned( $r[1] );
$start = IP::toUnsigned( $r[0] );
$end = IP::toUnsigned( $r[1] );
if ( $n >= $start && $n <= $end ) {
return false;
}
@ -79,8 +78,8 @@ class IP {
* @parameter $ip string A quad dotted IP address
* @return array
*/
public static function ToArray( $ipblock ) {
if(! preg_match( '/^' . RE_IP_ADD . '(?:\/(?:'.RE_IP_PREFIX.'))?' . '$/', $ipblock, $matches ) ) {
public static function toArray( $ipblock ) {
if(! preg_match( '/^' . self::RE_ADD . '(?:\/(?:'.self::RE_PREFIX.'))?' . '$/', $ipblock, $matches ) ) {
return false;
} else {
return $matches;
@ -92,8 +91,8 @@ class IP {
* Comes from ProxyTools.php
* @param $ip Quad dotted IP address.
*/
public static function ToHex( $ip ) {
$n = self::ToUnsigned( $ip );
public static function toHex( $ip ) {
$n = self::toUnsigned( $ip );
if ( $n !== false ) {
$n = sprintf( '%08X', $n );
}
@ -106,7 +105,7 @@ class IP {
* Comes from ProxyTools.php
* @param $ip Quad dotted IP address.
*/
public static function ToUnsigned( $ip ) {
public static function toUnsigned( $ip ) {
$n = ip2long( $ip );
if ( $n == -1 || $n === false ) { # Return value on error depends on PHP version
$n = false;

View file

@ -55,7 +55,7 @@ function wfGetIP() {
# Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
foreach ( $ipchain as $i => $curIP ) {
if ( array_key_exists( $curIP, $trustedProxies ) ) {
if ( isset( $ipchain[$i + 1] ) && IP::IsPublic( $ipchain[$i + 1] ) ) {
if ( isset( $ipchain[$i + 1] ) && IP::isPublic( $ipchain[$i + 1] ) ) {
$ip = $ipchain[$i + 1];
}
} else {
@ -124,7 +124,7 @@ function wfParseCIDR( $range ) {
if ( count( $parts ) != 2 ) {
return array( false, false );
}
$network = IP::ToUnsigned( $parts[0] );
$network = IP::toUnsigned( $parts[0] );
if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) {
$bits = $parts[1];
} else {

View file

@ -45,15 +45,10 @@ require_once( "$IP/includes/OutputPage.php" );
require_once( "$IP/includes/MessageCache.php" );
require_once( "$IP/includes/Parser.php" );
require_once( "$IP/includes/LoadBalancer.php" );
require_once( "$IP/includes/IP.php" );
require_once( "$IP/includes/ProxyTools.php" );
require_once( "$IP/includes/ObjectCache.php" );
require_once( "$IP/includes/ImageFunctions.php" );
if ( $wgUseDynamicDates ) {
require_once( "$IP/includes/DateFormatter.php" );
}
wfProfileOut( $fname.'-includes' );
wfProfileIn( $fname.'-misc1' );