X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2FGlobalFunctions.php;h=7e4d9117a012b63d621c73fe4800ba23e78286ff;hb=5cc5b123cd49440dc5a3c6ab1af356494febecf5;hp=d78d2637225d3a08f9d3ee67fb9747720a8ba869;hpb=8f22cb45b1ccd2d18bb4ef1247a18efbb9a87e2d;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index d78d263722..7e4d9117a0 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -5,9 +5,9 @@ $wgNumberOfArticles = -1; # Unset $wgTotalViews = -1; $wgTotalEdits = -1; -require_once( "DatabaseFunctions.php" ); -require_once( "UpdateClasses.php" ); -require_once( "LogPage.php" ); +require_once( 'DatabaseFunctions.php' ); +require_once( 'UpdateClasses.php' ); +require_once( 'LogPage.php' ); /* * Compatibility functions @@ -23,8 +23,8 @@ if( !function_exists('iconv') ) { # This will *not* work in all circumstances. function iconv( $from, $to, $string ) { if(strcasecmp( $from, $to ) == 0) return $string; - if(strcasecmp( $from, "utf-8" ) == 0) return utf8_decode( $string ); - if(strcasecmp( $to, "utf-8" ) == 0) return utf8_encode( $string ); + if(strcasecmp( $from, 'utf-8' ) == 0) return utf8_decode( $string ); + if(strcasecmp( $to, 'utf-8' ) == 0) return utf8_encode( $string ); return $string; } } @@ -32,10 +32,35 @@ if( !function_exists('iconv') ) { if( !function_exists('file_get_contents') ) { # Exists in PHP 4.3.0+ function file_get_contents( $filename ) { - return implode( "", file( $filename ) ); + return implode( '', file( $filename ) ); } } +if( !function_exists('is_a') ) { + # Exists in PHP 4.2.0+ + function is_a( $object, $class_name ) { + return + (strcasecmp( get_class( $object, $class_name ) == 0) || + is_subclass_of( $object, $class_name ) ); + } +} + +# html_entity_decode exists in PHP 4.3.0+ but is FATALLY BROKEN even then, +# with no UTF-8 support. +function do_html_entity_decode( $string, $quote_style=ENT_COMPAT, $charset='ISO-8859-1' ) { + static $trans; + if( !isset( $trans ) ) { + $trans = array_flip( get_html_translation_table( HTML_ENTITIES, $quote_style ) ); + # Assumes $charset will always be the same through a run, and only understands + # utf-8 or default. Note - mixing latin1 named entities and unicode numbered + # ones will result in a bad link. + if( strcasecmp( 'utf-8', $charset ) == 0 ) { + $trans = array_map( 'utf8_encode', $trans ); + } + } + return strtr( $string, $trans ); +} + $wgRandomSeeded = false; function wfSeedRandom() @@ -52,40 +77,40 @@ function wfSeedRandom() # Generates a URL from a URL-encoded title and a query string # Title::getLocalURL() is preferred in most cases # -function wfLocalUrl( $a, $q = "" ) +function wfLocalUrl( $a, $q = '' ) { global $wgServer, $wgScript, $wgArticlePath; - $a = str_replace( " ", "_", $a ); + $a = str_replace( ' ', '_', $a ); - if ( "" == $a ) { - if( "" == $q ) { + if ( '' == $a ) { + if( '' == $q ) { $a = $wgScript; } else { $a = "{$wgScript}?{$q}"; } - } else if ( "" == $q ) { + } else if ( '' == $q ) { $a = str_replace( "$1", $a, $wgArticlePath ); } else if ($wgScript != '' ) { $a = "{$wgScript}?title={$a}&{$q}"; - } else { //XXX ugly hack for toplevel wikis - $a = "/{$a}&{$q}"; + } else { //XXX hackish solution for toplevel wikis + $a = "/{$a}?{$q}"; } return $a; } -function wfLocalUrlE( $a, $q = "" ) +function wfLocalUrlE( $a, $q = '' ) { return wfEscapeHTML( wfLocalUrl( $a, $q ) ); # die( "Call to obsolete function wfLocalUrlE()" ); } -function wfFullUrl( $a, $q = "" ) { - wfDebugDieBacktrace( "Call to obsolete function wfFullUrl(); use Title::getFullURL" ); +function wfFullUrl( $a, $q = '' ) { + wfDebugDieBacktrace( 'Call to obsolete function wfFullUrl(); use Title::getFullURL' ); } -function wfFullUrlE( $a, $q = "" ) { - wfDebugDieBacktrace( "Call to obsolete function wfFullUrlE(); use Title::getFullUrlE" ); +function wfFullUrlE( $a, $q = '' ) { + wfDebugDieBacktrace( 'Call to obsolete function wfFullUrlE(); use Title::getFullUrlE' ); } @@ -118,27 +143,28 @@ function wfImageArchiveUrl( $name ) function wfUrlencode ( $s ) { $s = urlencode( $s ); - $s = preg_replace( "/%3[Aa]/", ":", $s ); - $s = preg_replace( "/%2[Ff]/", "/", $s ); + $s = preg_replace( '/%3[Aa]/', ':', $s ); + $s = preg_replace( '/%2[Ff]/', '/', $s ); return $s; } function wfUtf8Sequence($codepoint) { - if($codepoint < 0x80) return chr($codepoint); - if($codepoint < 0x800) return chr($codepoint >> 6 & 0x3f | 0xc0) . - chr($codepoint & 0x3f | 0x80); - if($codepoint < 0x10000) return chr($codepoint >> 12 & 0x0f | 0xe0) . - chr($codepoint >> 6 & 0x3f | 0x80) . - chr($codepoint & 0x3f | 0x80); + if($codepoint < 0x80) return chr($codepoint); + if($codepoint < 0x800) return chr($codepoint >> 6 & 0x3f | 0xc0) . + chr($codepoint & 0x3f | 0x80); + if($codepoint < 0x10000) return chr($codepoint >> 12 & 0x0f | 0xe0) . + chr($codepoint >> 6 & 0x3f | 0x80) . + chr($codepoint & 0x3f | 0x80); if($codepoint < 0x100000) return chr($codepoint >> 18 & 0x07 | 0xf0) . # Double-check this - chr($codepoint >> 12 & 0x3f | 0x80) . - chr($codepoint >> 6 & 0x3f | 0x80) . - chr($codepoint & 0x3f | 0x80); + chr($codepoint >> 12 & 0x3f | 0x80) . + chr($codepoint >> 6 & 0x3f | 0x80) . + chr($codepoint & 0x3f | 0x80); # Doesn't yet handle outside the BMP return "&#$codepoint;"; } +# Converts numeric character entities to UTF-8 function wfMungeToUtf8($string) { global $wgInputEncoding; # This is debatable #$string = iconv($wgInputEncoding, "UTF-8", $string); @@ -148,6 +174,47 @@ function wfMungeToUtf8($string) { return $string; } +# Converts a single UTF-8 character into the corresponding HTML character entity +function wfUtf8Entity( $matches ) { + $char = $matches[0]; + # Find the length + $z = ord( $char{0} ); + if ( $z & 0x80 ) { + $length = 0; + while ( $z & 0x80 ) { + $length++; + $z <<= 1; + } + } else { + $length = 1; + } + + if ( $length != strlen( $char ) ) { + return ''; + } + if ( $length == 1 ) { + return $char; + } + + # Mask off the length-determining bits and shift back to the original location + $z &= 0xff; + $z >>= $length; + + # Add in the free bits from subsequent bytes + for ( $i=1; $i<$length; $i++ ) { + $z <<= 6; + $z |= ord( $char{$i} ) & 0x3f; + } + + # Make entity + return "&#$z;"; +} + +# Converts all multi-byte characters in a UTF-8 string into the appropriate character entity +function wfUtf8ToHTML($string) { + return preg_replace_callback( '/[\\xc0-\\xfd][\\x80-\\xbf]*/', 'wfUtf8Entity', $string ); +} + function wfDebug( $text, $logonly = false ) { global $wgOut, $wgDebugLogFile, $wgDebugComments, $wgProfileOnly; @@ -169,23 +236,25 @@ function logProfilingData() list( $usec, $sec ) = explode( " ", $wgRequestTime ); $start = (float)$sec + (float)$usec; $elapsed = $now - $start; - if ( "" != $wgDebugLogFile ) { + if ( $wgProfiling ) { $prof = wfGetProfilingOutput( $start, $elapsed ); - $forward = ""; + $forward = ''; if( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) - $forward = " forwarded for " . $_SERVER['HTTP_X_FORWARDED_FOR']; + $forward = ' forwarded for ' . $_SERVER['HTTP_X_FORWARDED_FOR']; if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) - $forward .= " client IP " . $_SERVER['HTTP_CLIENT_IP']; + $forward .= ' client IP ' . $_SERVER['HTTP_CLIENT_IP']; if( !empty( $_SERVER['HTTP_FROM'] ) ) - $forward .= " from " . $_SERVER['HTTP_FROM']; + $forward .= ' from ' . $_SERVER['HTTP_FROM']; if( $forward ) $forward = "\t(proxied via {$_SERVER['REMOTE_ADDR']}{$forward})"; if($wgUser->getId() == 0) - $forward .= " anon"; + $forward .= ' anon'; $log = sprintf( "%s\t%04.3f\t%s\n", - gmdate( "YmdHis" ), $elapsed, + gmdate( 'YmdHis' ), $elapsed, urldecode( $_SERVER['REQUEST_URI'] . $forward ) ); - error_log( $log . $prof, 3, $wgDebugLogFile ); + if ( '' != $wgDebugLogFile ) { + error_log( $log . $prof, 3, $wgDebugLogFile ); + } } } @@ -222,7 +291,7 @@ function wfMsgNoDB( $key ) { function wfMsgReal( $key, $args, $useDB ) { global $wgReplacementKeys, $wgMessageCache, $wgLang; - $fname = "wfMsg"; + $fname = 'wfMsg'; wfProfileIn( $fname ); if ( $wgMessageCache ) { $message = $wgMessageCache->get( $key, $useDB ); @@ -243,69 +312,28 @@ function wfMsgReal( $key, $args, $useDB ) { function wfCleanFormFields( $fields ) { - wfDebugDieBacktrace( "Call to obsolete wfCleanFormFields(). Use wgRequest instead..." ); + wfDebugDieBacktrace( 'Call to obsolete wfCleanFormFields(). Use wgRequest instead...' ); } function wfMungeQuotes( $in ) { - $out = str_replace( "%", "%25", $in ); - $out = str_replace( "'", "%27", $out ); - $out = str_replace( "\"", "%22", $out ); + $out = str_replace( '%', '%25', $in ); + $out = str_replace( "'", '%27', $out ); + $out = str_replace( '"', '%22', $out ); return $out; } function wfDemungeQuotes( $in ) { - $out = str_replace( "%22", "\"", $in ); - $out = str_replace( "%27", "'", $out ); - $out = str_replace( "%25", "%", $out ); + $out = str_replace( '%22', '"', $in ); + $out = str_replace( '%27', "'", $out ); + $out = str_replace( '%25', '%', $out ); return $out; } function wfCleanQueryVar( $var ) { - wfDebugDieBacktrace( "Call to obsolete function wfCleanQueryVar(); use wgRequest instead" ); -} - -function wfSpecialPage() -{ - global $wgUser, $wgOut, $wgTitle, $wgLang; - - /* FIXME: this list probably shouldn't be language-specific, per se */ - $validSP = $wgLang->getValidSpecialPages(); - $sysopSP = $wgLang->getSysopSpecialPages(); - $devSP = $wgLang->getDeveloperSpecialPages(); - - $wgOut->setArticleRelated( false ); - $wgOut->setRobotpolicy( "noindex,follow" ); - - $bits = split( "/", $wgTitle->getDBkey(), 2 ); - $t = $bits[0]; - if( empty( $bits[1] ) ) { - $par = NULL; - } else { - $par = $bits[1]; - } - - if ( array_key_exists( $t, $validSP ) || - ( $wgUser->isSysop() && array_key_exists( $t, $sysopSP ) ) || - ( $wgUser->isDeveloper() && array_key_exists( $t, $devSP ) ) ) { - if($par !== NULL) - $wgTitle = Title::makeTitle( Namespace::getSpecial(), $t ); - - $wgOut->setPageTitle( wfMsg( strtolower( $wgTitle->getText() ) ) ); - - $inc = "Special" . $t . ".php"; - require_once( $inc ); - $call = "wfSpecial" . $t; - $call( $par ); - } else if ( array_key_exists( $t, $sysopSP ) ) { - $wgOut->sysopRequired(); - } else if ( array_key_exists( $t, $devSP ) ) { - $wgOut->developerRequired(); - } else { - $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" ); - } + wfDebugDieBacktrace( 'Call to obsolete function wfCleanQueryVar(); use wgRequest instead' ); } function wfSearch( $s ) @@ -328,30 +356,32 @@ function wfAbruptExit(){ } $called = true; - if( function_exists( "debug_backtrace" ) ){ // PHP >= 4.3 + if( function_exists( 'debug_backtrace' ) ){ // PHP >= 4.3 $bt = debug_backtrace(); for($i = 0; $i < count($bt) ; $i++){ - $file = $bt[$i]["file"]; - $line = $bt[$i]["line"]; + $file = $bt[$i]['file']; + $line = $bt[$i]['line']; wfDebug("WARNING: Abrupt exit in $file at line $line\n"); } } else { - wfDebug("WARNING: Abrupt exit\n"); + wfDebug('WARNING: Abrupt exit\n'); } exit(); } -function wfDebugDieBacktrace( $msg = "" ) { - $msg .= "\n

Backtrace:

\n