00001 <?php
00002
00003 if ( !defined( 'MEDIAWIKI' ) ) {
00004 die( "This file is part of MediaWiki, it is not a valid entry point" );
00005 }
00006
00011 require_once dirname(__FILE__) . '/normal/UtfNormalUtil.php';
00012 require_once dirname(__FILE__) . '/XmlFunctions.php';
00013
00014
00016
00024 if( !function_exists('iconv') ) {
00025 # iconv support is not in the default configuration and so may not be present.
00026 # Assume will only ever use utf-8 and iso-8859-1.
00027 # This will *not* work in all circumstances.
00028 function iconv( $from, $to, $string ) {
00029 if(strcasecmp( $from, $to ) == 0) return $string;
00030 if(strcasecmp( $from, 'utf-8' ) == 0) return utf8_decode( $string );
00031 if(strcasecmp( $to, 'utf-8' ) == 0) return utf8_encode( $string );
00032 return $string;
00033 }
00034 }
00035
00036 # UTF-8 substr function based on a PHP manual comment
00037 if ( !function_exists( 'mb_substr' ) ) {
00038 function mb_substr( $str, $start ) {
00039 $ar = array();
00040 preg_match_all( '/./us', $str, $ar );
00041
00042 if( func_num_args() >= 3 ) {
00043 $end = func_get_arg( 2 );
00044 return join( '', array_slice( $ar[0], $start, $end ) );
00045 } else {
00046 return join( '', array_slice( $ar[0], $start ) );
00047 }
00048 }
00049 }
00050
00051 if ( !function_exists( 'mb_strlen' ) ) {
00058 function mb_strlen( $str, $enc="" ) {
00059 $counts = count_chars( $str );
00060 $total = 0;
00061
00062
00063 for( $i = 0; $i < 0x80; $i++ ) {
00064 $total += $counts[$i];
00065 }
00066
00067
00068 for( $i = 0xc0; $i < 0xff; $i++ ) {
00069 $total += $counts[$i];
00070 }
00071 return $total;
00072 }
00073 }
00074
00075 if ( !function_exists( 'array_diff_key' ) ) {
00081 function array_diff_key( $left, $right ) {
00082 $result = $left;
00083 foreach ( $left as $key => $unused ) {
00084 if ( isset( $right[$key] ) ) {
00085 unset( $result[$key] );
00086 }
00087 }
00088 return $result;
00089 }
00090 }
00091
00092
00093 if ( !function_exists( 'istainted' ) ) {
00094 function istainted( $var ) {
00095 return 0;
00096 }
00097 function taint( $var, $level = 0 ) {}
00098 function untaint( $var, $level = 0 ) {}
00099 define( 'TC_HTML', 1 );
00100 define( 'TC_SHELL', 1 );
00101 define( 'TC_MYSQL', 1 );
00102 define( 'TC_PCRE', 1 );
00103 define( 'TC_SELF', 1 );
00104 }
00106
00107
00111 function wfArrayDiff2( $a, $b ) {
00112 return array_udiff( $a, $b, 'wfArrayDiff2_cmp' );
00113 }
00114 function wfArrayDiff2_cmp( $a, $b ) {
00115 if ( !is_array( $a ) ) {
00116 return strcmp( $a, $b );
00117 } elseif ( count( $a ) !== count( $b ) ) {
00118 return count( $a ) < count( $b ) ? -1 : 1;
00119 } else {
00120 reset( $a );
00121 reset( $b );
00122 while( ( list( $keyA, $valueA ) = each( $a ) ) && ( list( $keyB, $valueB ) = each( $b ) ) ) {
00123 $cmp = strcmp( $valueA, $valueB );
00124 if ( $cmp !== 0 ) {
00125 return $cmp;
00126 }
00127 }
00128 return 0;
00129 }
00130 }
00131
00137 function wfClone( $object ) {
00138 return clone( $object );
00139 }
00140
00145 function wfSeedRandom() {
00146
00147 }
00148
00156 function wfRandom() {
00157 # The maximum random value is "only" 2^31-1, so get two random
00158 # values to reduce the chance of dupes
00159 $max = mt_getrandmax() + 1;
00160 $rand = number_format( (mt_rand() * $max + mt_rand())
00161 / $max / $max, 12, '.', '' );
00162 return $rand;
00163 }
00164
00184 function wfUrlencode( $s ) {
00185 $s = urlencode( $s );
00186 $s = str_ireplace(
00187 array( '%3B','%3A','%40','%24','%21','%2A','%28','%29','%2C','%2F' ),
00188 array( ';', ':', '@', '$', '!', '*', '(', ')', ',', '/' ),
00189 $s
00190 );
00191
00192 return $s;
00193 }
00194
00208 function wfDebug( $text, $logonly = false ) {
00209 global $wgOut, $wgDebugLogFile, $wgDebugComments, $wgProfileOnly, $wgDebugRawPage;
00210 global $wgDebugLogPrefix;
00211 static $recursion = 0;
00212
00213 static $cache = array();
00214
00215 # Check for raw action using $_GET not $wgRequest, since the latter might not be initialised yet
00216 if ( isset( $_GET['action'] ) && $_GET['action'] == 'raw' && !$wgDebugRawPage ) {
00217 return;
00218 }
00219
00220 if ( $wgDebugComments && !$logonly ) {
00221 $cache[] = $text;
00222
00223 if ( !isset( $wgOut ) ) {
00224 return;
00225 }
00226 if ( !StubObject::isRealObject( $wgOut ) ) {
00227 if ( $recursion ) {
00228 return;
00229 }
00230 $recursion++;
00231 $wgOut->_unstub();
00232 $recursion--;
00233 }
00234
00235
00236 array_map( array( $wgOut, 'debug' ), $cache );
00237 $cache = array();
00238 }
00239 if ( '' != $wgDebugLogFile && !$wgProfileOnly ) {
00240 # Strip unprintables; they can switch terminal modes when binary data
00241 # gets dumped, which is pretty annoying.
00242 $text = preg_replace( '![\x00-\x08\x0b\x0c\x0e-\x1f]!', ' ', $text );
00243 $text = $wgDebugLogPrefix . $text;
00244 wfErrorLog( $text, $wgDebugLogFile );
00245 }
00246 }
00247
00252 function wfDebugMem( $exact = false ) {
00253 $mem = memory_get_usage();
00254 if( !$exact ) {
00255 $mem = floor( $mem / 1024 ) . ' kilobytes';
00256 } else {
00257 $mem .= ' bytes';
00258 }
00259 wfDebug( "Memory usage: $mem\n" );
00260 }
00261
00271 function wfDebugLog( $logGroup, $text, $public = true ) {
00272 global $wgDebugLogGroups, $wgShowHostnames;
00273 $text = trim($text)."\n";
00274 if( isset( $wgDebugLogGroups[$logGroup] ) ) {
00275 $time = wfTimestamp( TS_DB );
00276 $wiki = wfWikiID();
00277 if ( $wgShowHostnames ) {
00278 $host = wfHostname();
00279 } else {
00280 $host = '';
00281 }
00282 wfErrorLog( "$time $host $wiki: $text", $wgDebugLogGroups[$logGroup] );
00283 } else if ( $public === true ) {
00284 wfDebug( $text, true );
00285 }
00286 }
00287
00292 function wfLogDBError( $text ) {
00293 global $wgDBerrorLog, $wgDBname;
00294 if ( $wgDBerrorLog ) {
00295 $host = trim(`hostname`);
00296 $text = date('D M j G:i:s T Y') . "\t$host\t$wgDBname\t$text";
00297 wfErrorLog( $text, $wgDBerrorLog );
00298 }
00299 }
00300
00307 function wfErrorLog( $text, $file ) {
00308 if ( substr( $file, 0, 4 ) == 'udp:' ) {
00309 if ( preg_match( '!^(tcp|udp):(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $file, $m ) ) {
00310
00311 $protocol = $m[1];
00312 $host = $m[2];
00313 $port = $m[3];
00314 $prefix = isset( $m[4] ) ? $m[4] : false;
00315 } elseif ( preg_match( '!^(tcp|udp):(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $file, $m ) ) {
00316 $protocol = $m[1];
00317 $host = $m[2];
00318 $port = $m[3];
00319 $prefix = isset( $m[4] ) ? $m[4] : false;
00320 } else {
00321 throw new MWException( __METHOD__.": Invalid UDP specification" );
00322 }
00323
00324 if ( strval( $prefix ) !== '' ) {
00325 $text = preg_replace( '/^/m', $prefix . ' ', $text );
00326 if ( substr( $text, -1 ) != "\n" ) {
00327 $text .= "\n";
00328 }
00329 }
00330
00331 $sock = fsockopen( "$protocol://$host", $port );
00332 if ( !$sock ) {
00333 return;
00334 }
00335 fwrite( $sock, $text );
00336 fclose( $sock );
00337 } else {
00338 wfSuppressWarnings();
00339 $exists = file_exists( $file );
00340 $size = $exists ? filesize( $file ) : false;
00341 if ( !$exists || ( $size !== false && $size + strlen( $text ) < 0x7fffffff ) ) {
00342 error_log( $text, 3, $file );
00343 }
00344 wfRestoreWarnings();
00345 }
00346 }
00347
00351 function wfLogProfilingData() {
00352 global $wgRequestTime, $wgDebugLogFile, $wgDebugRawPage, $wgRequest;
00353 global $wgProfiler, $wgProfileLimit, $wgUser;
00354 # Profiling must actually be enabled...
00355 if( !isset( $wgProfiler ) ) return;
00356 # Get total page request time
00357 $now = wfTime();
00358 $elapsed = $now - $wgRequestTime;
00359 # Only show pages that longer than $wgProfileLimit time (default is 0)
00360 if( $elapsed <= $wgProfileLimit ) return;
00361 $prof = wfGetProfilingOutput( $wgRequestTime, $elapsed );
00362 $forward = '';
00363 if( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
00364 $forward = ' forwarded for ' . $_SERVER['HTTP_X_FORWARDED_FOR'];
00365 if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
00366 $forward .= ' client IP ' . $_SERVER['HTTP_CLIENT_IP'];
00367 if( !empty( $_SERVER['HTTP_FROM'] ) )
00368 $forward .= ' from ' . $_SERVER['HTTP_FROM'];
00369 if( $forward )
00370 $forward = "\t(proxied via {$_SERVER['REMOTE_ADDR']}{$forward})";
00371
00372 if( StubObject::isRealObject($wgUser) && $wgUser->isAnon() )
00373 $forward .= ' anon';
00374 $log = sprintf( "%s\t%04.3f\t%s\n",
00375 gmdate( 'YmdHis' ), $elapsed,
00376 urldecode( $wgRequest->getRequestURL() . $forward ) );
00377 if ( '' != $wgDebugLogFile && ( $wgRequest->getVal('action') != 'raw' || $wgDebugRawPage ) ) {
00378 wfErrorLog( $log . $prof, $wgDebugLogFile );
00379 }
00380 }
00381
00388 function wfReadOnly() {
00389 global $wgReadOnlyFile, $wgReadOnly;
00390
00391 if ( !is_null( $wgReadOnly ) ) {
00392 return (bool)$wgReadOnly;
00393 }
00394 if ( '' == $wgReadOnlyFile ) {
00395 return false;
00396 }
00397
00398 if ( is_file( $wgReadOnlyFile ) ) {
00399 $wgReadOnly = file_get_contents( $wgReadOnlyFile );
00400 } else {
00401 $wgReadOnly = false;
00402 }
00403 return (bool)$wgReadOnly;
00404 }
00405
00406 function wfReadOnlyReason() {
00407 global $wgReadOnly;
00408 wfReadOnly();
00409 return $wgReadOnly;
00410 }
00411
00425 function wfGetLangObj( $langcode = false ){
00426 # Identify which language to get or create a language object for.
00427 if( $langcode instanceof Language )
00428 # Great, we already have the object!
00429 return $langcode;
00430
00431 global $wgContLang;
00432 if( $langcode === $wgContLang->getCode() || $langcode === true )
00433 # $langcode is the language code of the wikis content language object.
00434 # or it is a boolean and value is true
00435 return $wgContLang;
00436
00437 global $wgLang;
00438 if( $langcode === $wgLang->getCode() || $langcode === false )
00439 # $langcode is the language code of user language object.
00440 # or it was a boolean and value is false
00441 return $wgLang;
00442
00443 $validCodes = array_keys( Language::getLanguageNames() );
00444 if( in_array( $langcode, $validCodes ) )
00445 # $langcode corresponds to a valid language.
00446 return Language::factory( $langcode );
00447
00448 # $langcode is a string, but not a valid language code; use content language.
00449 wfDebug( "Invalid language code passed to wfGetLangObj, falling back to content language.\n" );
00450 return $wgContLang;
00451 }
00452
00466 function wfMsg( $key ) {
00467 $args = func_get_args();
00468 array_shift( $args );
00469 return wfMsgReal( $key, $args, true );
00470 }
00471
00475 function wfMsgNoTrans( $key ) {
00476 $args = func_get_args();
00477 array_shift( $args );
00478 return wfMsgReal( $key, $args, true, false, false );
00479 }
00480
00503 function wfMsgForContent( $key ) {
00504 global $wgForceUIMsgAsContentMsg;
00505 $args = func_get_args();
00506 array_shift( $args );
00507 $forcontent = true;
00508 if( is_array( $wgForceUIMsgAsContentMsg ) &&
00509 in_array( $key, $wgForceUIMsgAsContentMsg ) )
00510 $forcontent = false;
00511 return wfMsgReal( $key, $args, true, $forcontent );
00512 }
00513
00517 function wfMsgForContentNoTrans( $key ) {
00518 global $wgForceUIMsgAsContentMsg;
00519 $args = func_get_args();
00520 array_shift( $args );
00521 $forcontent = true;
00522 if( is_array( $wgForceUIMsgAsContentMsg ) &&
00523 in_array( $key, $wgForceUIMsgAsContentMsg ) )
00524 $forcontent = false;
00525 return wfMsgReal( $key, $args, true, $forcontent, false );
00526 }
00527
00531 function wfMsgNoDB( $key ) {
00532 $args = func_get_args();
00533 array_shift( $args );
00534 return wfMsgReal( $key, $args, false );
00535 }
00536
00540 function wfMsgNoDBForContent( $key ) {
00541 global $wgForceUIMsgAsContentMsg;
00542 $args = func_get_args();
00543 array_shift( $args );
00544 $forcontent = true;
00545 if( is_array( $wgForceUIMsgAsContentMsg ) &&
00546 in_array( $key, $wgForceUIMsgAsContentMsg ) )
00547 $forcontent = false;
00548 return wfMsgReal( $key, $args, false, $forcontent );
00549 }
00550
00551
00561 function wfMsgReal( $key, $args, $useDB = true, $forContent=false, $transform = true ) {
00562 wfProfileIn( __METHOD__ );
00563 $message = wfMsgGetKey( $key, $useDB, $forContent, $transform );
00564 $message = wfMsgReplaceArgs( $message, $args );
00565 wfProfileOut( __METHOD__ );
00566 return $message;
00567 }
00568
00573 function wfMsgWeirdKey ( $key ) {
00574 $source = wfMsgGetKey( $key, false, true, false );
00575 if ( wfEmptyMsg( $key, $source ) )
00576 return "";
00577 else
00578 return $source;
00579 }
00580
00591 function wfMsgGetKey( $key, $useDB, $langCode = false, $transform = true ) {
00592 global $wgContLang, $wgMessageCache;
00593
00594 wfRunHooks('NormalizeMessageKey', array(&$key, &$useDB, &$langCode, &$transform));
00595
00596 # If $wgMessageCache isn't initialised yet, try to return something sensible.
00597 if( is_object( $wgMessageCache ) ) {
00598 $message = $wgMessageCache->get( $key, $useDB, $langCode );
00599 if ( $transform ) {
00600 $message = $wgMessageCache->transform( $message );
00601 }
00602 } else {
00603 $lang = wfGetLangObj( $langCode );
00604
00605 # MessageCache::get() does this already, Language::getMessage() doesn't
00606 # ISSUE: Should we try to handle "message/lang" here too?
00607 $key = str_replace( ' ' , '_' , $wgContLang->lcfirst( $key ) );
00608
00609 if( is_object( $lang ) ) {
00610 $message = $lang->getMessage( $key );
00611 } else {
00612 $message = false;
00613 }
00614 }
00615
00616 return $message;
00617 }
00618
00627 function wfMsgReplaceArgs( $message, $args ) {
00628 # Fix windows line-endings
00629 # Some messages are split with explode("\n", $msg)
00630 $message = str_replace( "\r", '', $message );
00631
00632
00633 if ( count( $args ) ) {
00634 if ( is_array( $args[0] ) ) {
00635 $args = array_values( $args[0] );
00636 }
00637 $replacementKeys = array();
00638 foreach( $args as $n => $param ) {
00639 $replacementKeys['$' . ($n + 1)] = $param;
00640 }
00641 $message = strtr( $message, $replacementKeys );
00642 }
00643
00644 return $message;
00645 }
00646
00658 function wfMsgHtml( $key ) {
00659 $args = func_get_args();
00660 array_shift( $args );
00661 return wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $key, true ) ), $args );
00662 }
00663
00675 function wfMsgWikiHtml( $key ) {
00676 global $wgOut;
00677 $args = func_get_args();
00678 array_shift( $args );
00679 return wfMsgReplaceArgs( $wgOut->parse( wfMsgGetKey( $key, true ), true ), $args );
00680 }
00681
00700 function wfMsgExt( $key, $options ) {
00701 global $wgOut;
00702
00703 $args = func_get_args();
00704 array_shift( $args );
00705 array_shift( $args );
00706 $options = (array)$options;
00707
00708 foreach( $options as $arrayKey => $option ) {
00709 if( !preg_match( '/^[0-9]+|language$/', $arrayKey ) ) {
00710 # An unknown index, neither numeric nor "language"
00711 trigger_error( "wfMsgExt called with incorrect parameter key $arrayKey", E_USER_WARNING );
00712 } elseif( preg_match( '/^[0-9]+$/', $arrayKey ) && !in_array( $option,
00713 array( 'parse', 'parseinline', 'escape', 'escapenoentities',
00714 'replaceafter', 'parsemag', 'content' ) ) ) {
00715 # A numeric index with unknown value
00716 trigger_error( "wfMsgExt called with incorrect parameter $option", E_USER_WARNING );
00717 }
00718 }
00719
00720 if( in_array('content', $options, true ) ) {
00721 $forContent = true;
00722 $langCode = true;
00723 } elseif( array_key_exists('language', $options) ) {
00724 $forContent = false;
00725 $langCode = wfGetLangObj( $options['language'] );
00726 } else {
00727 $forContent = false;
00728 $langCode = false;
00729 }
00730
00731 $string = wfMsgGetKey( $key, true, $langCode, false );
00732
00733 if( !in_array('replaceafter', $options, true ) ) {
00734 $string = wfMsgReplaceArgs( $string, $args );
00735 }
00736
00737 if( in_array('parse', $options, true ) ) {
00738 $string = $wgOut->parse( $string, true, !$forContent );
00739 } elseif ( in_array('parseinline', $options, true ) ) {
00740 $string = $wgOut->parse( $string, true, !$forContent );
00741 $m = array();
00742 if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
00743 $string = $m[1];
00744 }
00745 } elseif ( in_array('parsemag', $options, true ) ) {
00746 global $wgMessageCache;
00747 if ( isset( $wgMessageCache ) ) {
00748 $string = $wgMessageCache->transform( $string,
00749 !$forContent,
00750 is_object( $langCode ) ? $langCode : null );
00751 }
00752 }
00753
00754 if ( in_array('escape', $options, true ) ) {
00755 $string = htmlspecialchars ( $string );
00756 } elseif ( in_array( 'escapenoentities', $options, true ) ) {
00757 $string = Sanitizer::escapeHtmlAllowEntities( $string );
00758 }
00759
00760 if( in_array('replaceafter', $options, true ) ) {
00761 $string = wfMsgReplaceArgs( $string, $args );
00762 }
00763
00764 return $string;
00765 }
00766
00767
00774 function wfAbruptExit( $error = false ){
00775 static $called = false;
00776 if ( $called ){
00777 exit( -1 );
00778 }
00779 $called = true;
00780
00781 $bt = wfDebugBacktrace();
00782 if( $bt ) {
00783 for($i = 0; $i < count($bt) ; $i++){
00784 $file = isset($bt[$i]['file']) ? $bt[$i]['file'] : "unknown";
00785 $line = isset($bt[$i]['line']) ? $bt[$i]['line'] : "unknown";
00786 wfDebug("WARNING: Abrupt exit in $file at line $line\n");
00787 }
00788 } else {
00789 wfDebug("WARNING: Abrupt exit\n");
00790 }
00791
00792 wfLogProfilingData();
00793
00794 if ( !$error ) {
00795 wfGetLB()->closeAll();
00796 }
00797 exit( -1 );
00798 }
00799
00803 function wfErrorExit() {
00804 wfAbruptExit( true );
00805 }
00806
00812 function wfDie( $msg='' ) {
00813 echo $msg;
00814 die( 1 );
00815 }
00816
00823 function wfDebugDieBacktrace( $msg = '' ) {
00824 throw new MWException( $msg );
00825 }
00826
00833 function wfHostname() {
00834 static $host;
00835 if ( is_null( $host ) ) {
00836 if ( function_exists( 'posix_uname' ) ) {
00837
00838 $uname = @posix_uname();
00839 } else {
00840 $uname = false;
00841 }
00842 if( is_array( $uname ) && isset( $uname['nodename'] ) ) {
00843 $host = $uname['nodename'];
00844 } elseif ( getenv( 'COMPUTERNAME' ) ) {
00845 # Windows computer name
00846 $host = getenv( 'COMPUTERNAME' );
00847 } else {
00848 # This may be a virtual server.
00849 $host = $_SERVER['SERVER_NAME'];
00850 }
00851 }
00852 return $host;
00853 }
00854
00860 function wfReportTime() {
00861 global $wgRequestTime, $wgShowHostnames;
00862
00863 $now = wfTime();
00864 $elapsed = $now - $wgRequestTime;
00865
00866 return $wgShowHostnames
00867 ? sprintf( "<!-- Served by %s in %01.3f secs. -->", wfHostname(), $elapsed )
00868 : sprintf( "<!-- Served in %01.3f secs. -->", $elapsed );
00869 }
00870
00884 function wfDebugBacktrace() {
00885 static $disabled = null;
00886
00887 if( extension_loaded( 'Zend Optimizer' ) ) {
00888 wfDebug( "Zend Optimizer detected; skipping debug_backtrace for safety.\n" );
00889 return array();
00890 }
00891
00892 if ( is_null( $disabled ) ) {
00893 $disabled = false;
00894 $functions = explode( ',', ini_get( 'disable_functions' ) );
00895 $functions = array_map( 'trim', $functions );
00896 $functions = array_map( 'strtolower', $functions );
00897 if ( in_array( 'debug_backtrace', $functions ) ) {
00898 wfDebug( "debug_backtrace is in disabled_functions\n" );
00899 $disabled = true;
00900 }
00901 }
00902 if ( $disabled ) {
00903 return array();
00904 }
00905
00906 return array_slice( debug_backtrace(), 1 );
00907 }
00908
00909 function wfBacktrace() {
00910 global $wgCommandLineMode;
00911
00912 if ( $wgCommandLineMode ) {
00913 $msg = '';
00914 } else {
00915 $msg = "<ul>\n";
00916 }
00917 $backtrace = wfDebugBacktrace();
00918 foreach( $backtrace as $call ) {
00919 if( isset( $call['file'] ) ) {
00920 $f = explode( DIRECTORY_SEPARATOR, $call['file'] );
00921 $file = $f[count($f)-1];
00922 } else {
00923 $file = '-';
00924 }
00925 if( isset( $call['line'] ) ) {
00926 $line = $call['line'];
00927 } else {
00928 $line = '-';
00929 }
00930 if ( $wgCommandLineMode ) {
00931 $msg .= "$file line $line calls ";
00932 } else {
00933 $msg .= '<li>' . $file . ' line ' . $line . ' calls ';
00934 }
00935 if( !empty( $call['class'] ) ) $msg .= $call['class'] . '::';
00936 $msg .= $call['function'] . '()';
00937
00938 if ( $wgCommandLineMode ) {
00939 $msg .= "\n";
00940 } else {
00941 $msg .= "</li>\n";
00942 }
00943 }
00944 if ( $wgCommandLineMode ) {
00945 $msg .= "\n";
00946 } else {
00947 $msg .= "</ul>\n";
00948 }
00949
00950 return $msg;
00951 }
00952
00953
00954
00955
00956
00960 function wfShowingResults( $offset, $limit ) {
00961 global $wgLang;
00962 return wfMsgExt( 'showingresults', array( 'parseinline' ), $wgLang->formatNum( $limit ),
00963 $wgLang->formatNum( $offset+1 ) );
00964 }
00965
00969 function wfShowingResultsNum( $offset, $limit, $num ) {
00970 global $wgLang;
00971 return wfMsgExt( 'showingresultsnum', array( 'parseinline' ), $wgLang->formatNum( $limit ),
00972 $wgLang->formatNum( $offset+1 ), $wgLang->formatNum( $num ) );
00973 }
00974
00983 function wfViewPrevNext( $offset, $limit, $link, $query = '', $atend = false ) {
00984 global $wgLang;
00985 $fmtLimit = $wgLang->formatNum( $limit );
00986 # Get prev/next link display text
00987 $prev = wfMsgHtml( 'prevn', $fmtLimit );
00988 $next = wfMsgHtml( 'nextn', $fmtLimit );
00989 # Get prev/next link title text
00990 $pTitle = wfMsgExt( 'prevn-title', array('parsemag','escape'), $fmtLimit );
00991 $nTitle = wfMsgExt( 'nextn-title', array('parsemag','escape'), $fmtLimit );
00992 # Fetch the title object
00993 if( is_object( $link ) ) {
00994 $title =& $link;
00995 } else {
00996 $title = Title::newFromText( $link );
00997 if( is_null( $title ) ) {
00998 return false;
00999 }
01000 }
01001 # Make 'previous' link
01002 if( 0 != $offset ) {
01003 $po = $offset - $limit;
01004 $po = max($po,0);
01005 $q = "limit={$limit}&offset={$po}";
01006 if( $query != '' ) {
01007 $q .= '&'.$query;
01008 }
01009 $plink = '<a href="' . $title->escapeLocalUrl( $q ) . "\" title=\"{$pTitle}\" class=\"mw-prevlink\">{$prev}</a>";
01010 } else {
01011 $plink = $prev;
01012 }
01013 # Make 'next' link
01014 $no = $offset + $limit;
01015 $q = "limit={$limit}&offset={$no}";
01016 if( $query != '' ) {
01017 $q .= '&'.$query;
01018 }
01019 if( $atend ) {
01020 $nlink = $next;
01021 } else {
01022 $nlink = '<a href="' . $title->escapeLocalUrl( $q ) . "\" title=\"{$nTitle}\" class=\"mw-nextlink\">{$next}</a>";
01023 }
01024 # Make links to set number of items per page
01025 $nums = $wgLang->pipeList( array(
01026 wfNumLink( $offset, 20, $title, $query ),
01027 wfNumLink( $offset, 50, $title, $query ),
01028 wfNumLink( $offset, 100, $title, $query ),
01029 wfNumLink( $offset, 250, $title, $query ),
01030 wfNumLink( $offset, 500, $title, $query )
01031 ) );
01032 return wfMsg( 'viewprevnext', $plink, $nlink, $nums );
01033 }
01034
01042 function wfNumLink( $offset, $limit, $title, $query = '' ) {
01043 global $wgLang;
01044 if( $query == '' ) {
01045 $q = '';
01046 } else {
01047 $q = $query.'&';
01048 }
01049 $q .= "limit={$limit}&offset={$offset}";
01050 $fmtLimit = $wgLang->formatNum( $limit );
01051 $lTitle = wfMsgExt('shown-title',array('parsemag','escape'),$limit);
01052 $s = '<a href="' . $title->escapeLocalUrl( $q ) . "\" title=\"{$lTitle}\" class=\"mw-numlink\">{$fmtLimit}</a>";
01053 return $s;
01054 }
01055
01062 function wfClientAcceptsGzip() {
01063 global $wgUseGzip;
01064 if( $wgUseGzip ) {
01065 # FIXME: we may want to blacklist some broken browsers
01066 $m = array();
01067 if( preg_match(
01068 '/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/',
01069 $_SERVER['HTTP_ACCEPT_ENCODING'],
01070 $m ) ) {
01071 if( isset( $m[2] ) && ( $m[1] == 'q' ) && ( $m[2] == 0 ) ) return false;
01072 wfDebug( " accepts gzip\n" );
01073 return true;
01074 }
01075 }
01076 return false;
01077 }
01078
01088 function wfCheckLimits( $deflimit = 50, $optionname = 'rclimit' ) {
01089 global $wgRequest;
01090 return $wgRequest->getLimitOffset( $deflimit, $optionname );
01091 }
01092
01103 function wfEscapeWikiText( $text ) {
01104 $text = str_replace(
01105 array( '[', '|', ']', '\'', 'ISBN ', 'RFC ', '://', "\n=", '{{' ), # }}
01106 array( '[', '|', ']', ''', 'ISBN ', 'RFC ', '://', "\n=", '{{' ),
01107 htmlspecialchars($text) );
01108 return $text;
01109 }
01110
01114 function wfQuotedPrintable( $string, $charset = '' ) {
01115 # Probably incomplete; see RFC 2045
01116 if( empty( $charset ) ) {
01117 global $wgInputEncoding;
01118 $charset = $wgInputEncoding;
01119 }
01120 $charset = strtoupper( $charset );
01121 $charset = str_replace( 'ISO-8859', 'ISO8859', $charset );
01122
01123 $illegal = '\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff=';
01124 $replace = $illegal . '\t ?_';
01125 if( !preg_match( "/[$illegal]/", $string ) ) return $string;
01126 $out = "=?$charset?Q?";
01127 $out .= preg_replace( "/([$replace])/e", 'sprintf("=%02X",ord("$1"))', $string );
01128 $out .= '?=';
01129 return $out;
01130 }
01131
01132
01137 function wfTime() {
01138 return microtime(true);
01139 }
01140
01145 function wfSetVar( &$dest, $source ) {
01146 $temp = $dest;
01147 if ( !is_null( $source ) ) {
01148 $dest = $source;
01149 }
01150 return $temp;
01151 }
01152
01156 function wfSetBit( &$dest, $bit, $state = true ) {
01157 $temp = (bool)($dest & $bit );
01158 if ( !is_null( $state ) ) {
01159 if ( $state ) {
01160 $dest |= $bit;
01161 } else {
01162 $dest &= ~$bit;
01163 }
01164 }
01165 return $temp;
01166 }
01167
01173 function wfArrayToCGI( $array1, $array2 = NULL )
01174 {
01175 if ( !is_null( $array2 ) ) {
01176 $array1 = $array1 + $array2;
01177 }
01178
01179 $cgi = '';
01180 foreach ( $array1 as $key => $value ) {
01181 if ( '' !== $value ) {
01182 if ( '' != $cgi ) {
01183 $cgi .= '&';
01184 }
01185 if(is_array($value))
01186 {
01187 $firstTime = true;
01188 foreach($value as $v)
01189 {
01190 $cgi .= ($firstTime ? '' : '&') .
01191 urlencode( $key . '[]' ) . '=' .
01192 urlencode( $v );
01193 $firstTime = false;
01194 }
01195 }
01196 else
01197 $cgi .= urlencode( $key ) . '=' .
01198 urlencode( $value );
01199 }
01200 }
01201 return $cgi;
01202 }
01203
01214 function wfCgiToArray( $query ) {
01215 if( isset( $query[0] ) and $query[0] == '?' ) {
01216 $query = substr( $query, 1 );
01217 }
01218 $bits = explode( '&', $query );
01219 $ret = array();
01220 foreach( $bits as $bit ) {
01221 if( $bit === '' ) {
01222 continue;
01223 }
01224 list( $key, $value ) = explode( '=', $bit );
01225 $key = urldecode( $key );
01226 $value = urldecode( $value );
01227 $ret[$key] = $value;
01228 }
01229 return $ret;
01230 }
01231
01240 function wfAppendQuery( $url, $query ) {
01241 if( $query != '' ) {
01242 if( false === strpos( $url, '?' ) ) {
01243 $url .= '?';
01244 } else {
01245 $url .= '&';
01246 }
01247 $url .= $query;
01248 }
01249 return $url;
01250 }
01251
01258 function wfExpandUrl( $url ) {
01259 if( substr( $url, 0, 1 ) == '/' ) {
01260 global $wgServer;
01261 return $wgServer . $url;
01262 } else {
01263 return $url;
01264 }
01265 }
01266
01271 function wfPurgeSquidServers ($urlArr) {
01272 SquidUpdate::purge( $urlArr );
01273 }
01274
01283 function wfEscapeShellArg( ) {
01284 wfInitShellLocale();
01285
01286 $args = func_get_args();
01287 $first = true;
01288 $retVal = '';
01289 foreach ( $args as $arg ) {
01290 if ( !$first ) {
01291 $retVal .= ' ';
01292 } else {
01293 $first = false;
01294 }
01295
01296 if ( wfIsWindows() ) {
01297
01298
01299
01300 $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE );
01301 $arg = '';
01302 $delim = false;
01303 foreach ( $tokens as $token ) {
01304 if ( $delim ) {
01305 $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
01306 } else {
01307 $arg .= $token;
01308 }
01309 $delim = !$delim;
01310 }
01311
01312
01313 $m = array();
01314 if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
01315 $arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
01316 }
01317
01318
01319 $retVal .= '"' . $arg . '"';
01320 } else {
01321 $retVal .= escapeshellarg( $arg );
01322 }
01323 }
01324 return $retVal;
01325 }
01326
01331 function wfMerge( $old, $mine, $yours, &$result ){
01332 global $wgDiff3;
01333
01334 # This check may also protect against code injection in
01335 # case of broken installations.
01336 if( !$wgDiff3 || !file_exists( $wgDiff3 ) ) {
01337 wfDebug( "diff3 not found\n" );
01338 return false;
01339 }
01340
01341 # Make temporary files
01342 $td = wfTempDir();
01343 $oldtextFile = fopen( $oldtextName = tempnam( $td, 'merge-old-' ), 'w' );
01344 $mytextFile = fopen( $mytextName = tempnam( $td, 'merge-mine-' ), 'w' );
01345 $yourtextFile = fopen( $yourtextName = tempnam( $td, 'merge-your-' ), 'w' );
01346
01347 fwrite( $oldtextFile, $old ); fclose( $oldtextFile );
01348 fwrite( $mytextFile, $mine ); fclose( $mytextFile );
01349 fwrite( $yourtextFile, $yours ); fclose( $yourtextFile );
01350
01351 # Check for a conflict
01352 $cmd = $wgDiff3 . ' -a --overlap-only ' .
01353 wfEscapeShellArg( $mytextName ) . ' ' .
01354 wfEscapeShellArg( $oldtextName ) . ' ' .
01355 wfEscapeShellArg( $yourtextName );
01356 $handle = popen( $cmd, 'r' );
01357
01358 if( fgets( $handle, 1024 ) ){
01359 $conflict = true;
01360 } else {
01361 $conflict = false;
01362 }
01363 pclose( $handle );
01364
01365 # Merge differences
01366 $cmd = $wgDiff3 . ' -a -e --merge ' .
01367 wfEscapeShellArg( $mytextName, $oldtextName, $yourtextName );
01368 $handle = popen( $cmd, 'r' );
01369 $result = '';
01370 do {
01371 $data = fread( $handle, 8192 );
01372 if ( strlen( $data ) == 0 ) {
01373 break;
01374 }
01375 $result .= $data;
01376 } while ( true );
01377 pclose( $handle );
01378 unlink( $mytextName ); unlink( $oldtextName ); unlink( $yourtextName );
01379
01380 if ( $result === '' && $old !== '' && $conflict == false ) {
01381 wfDebug( "Unexpected null result from diff3. Command: $cmd\n" );
01382 $conflict = true;
01383 }
01384 return ! $conflict;
01385 }
01386
01395 function wfDiff( $before, $after, $params = '-u' ) {
01396 global $wgDiff;
01397
01398 # This check may also protect against code injection in
01399 # case of broken installations.
01400 if( !file_exists( $wgDiff ) ){
01401 wfDebug( "diff executable not found\n" );
01402 $diffs = new Diff( explode( "\n", $before ), explode( "\n", $after ) );
01403 $format = new UnifiedDiffFormatter();
01404 return $format->format( $diffs );
01405 }
01406
01407 # Make temporary files
01408 $td = wfTempDir();
01409 $oldtextFile = fopen( $oldtextName = tempnam( $td, 'merge-old-' ), 'w' );
01410 $newtextFile = fopen( $newtextName = tempnam( $td, 'merge-your-' ), 'w' );
01411
01412 fwrite( $oldtextFile, $before ); fclose( $oldtextFile );
01413 fwrite( $newtextFile, $after ); fclose( $newtextFile );
01414
01415
01416 $cmd = "$wgDiff " . $params . ' ' .wfEscapeShellArg( $oldtextName, $newtextName );
01417
01418 $h = popen( $cmd, 'r' );
01419
01420 $diff = '';
01421
01422 do {
01423 $data = fread( $h, 8192 );
01424 if ( strlen( $data ) == 0 ) {
01425 break;
01426 }
01427 $diff .= $data;
01428 } while ( true );
01429
01430
01431 pclose( $h );
01432 unlink( $oldtextName );
01433 unlink( $newtextName );
01434
01435
01436 $diff_lines = explode( "\n", $diff );
01437 if (strpos( $diff_lines[0], '---' ) === 0) {
01438 unset($diff_lines[0]);
01439 }
01440 if (strpos( $diff_lines[1], '+++' ) === 0) {
01441 unset($diff_lines[1]);
01442 }
01443
01444 $diff = implode( "\n", $diff_lines );
01445
01446 return $diff;
01447 }
01448
01455 function wfVarDump( $var ) {
01456 global $wgOut;
01457 $s = str_replace("\n","<br />\n", var_export( $var, true ) . "\n");
01458 if ( headers_sent() || !@is_object( $wgOut ) ) {
01459 print $s;
01460 } else {
01461 $wgOut->addHTML( $s );
01462 }
01463 }
01464
01468 function wfHttpError( $code, $label, $desc ) {
01469 global $wgOut;
01470 $wgOut->disable();
01471 header( "HTTP/1.0 $code $label" );
01472 header( "Status: $code $label" );
01473 $wgOut->sendCacheControl();
01474
01475 header( 'Content-type: text/html; charset=utf-8' );
01476 print "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">".
01477 "<html><head><title>" .
01478 htmlspecialchars( $label ) .
01479 "</title></head><body><h1>" .
01480 htmlspecialchars( $label ) .
01481 "</h1><p>" .
01482 nl2br( htmlspecialchars( $desc ) ) .
01483 "</p></body></html>\n";
01484 }
01485
01503 function wfResetOutputBuffers( $resetGzipEncoding=true ) {
01504 if( $resetGzipEncoding ) {
01505
01506
01507 global $wgDisableOutputCompression;
01508 $wgDisableOutputCompression = true;
01509 }
01510 while( $status = ob_get_status() ) {
01511 if( $status['type'] == 0 ) {
01512
01513
01514
01515
01516
01517 break;
01518 }
01519 if( !ob_end_clean() ) {
01520
01521
01522 break;
01523 }
01524 if( $resetGzipEncoding ) {
01525 if( $status['name'] == 'ob_gzhandler' ) {
01526
01527
01528 header( 'Content-Encoding:' );
01529 break;
01530 }
01531 }
01532 }
01533 }
01534
01547 function wfClearOutputBuffers() {
01548 wfResetOutputBuffers( false );
01549 }
01550
01555 function wfAcceptToPrefs( $accept, $def = '*/*' ) {
01556 # No arg means accept anything (per HTTP spec)
01557 if( !$accept ) {
01558 return array( $def => 1.0 );
01559 }
01560
01561 $prefs = array();
01562
01563 $parts = explode( ',', $accept );
01564
01565 foreach( $parts as $part ) {
01566 # FIXME: doesn't deal with params like 'text/html; level=1'
01567 @list( $value, $qpart ) = explode( ';', trim( $part ) );
01568 $match = array();
01569 if( !isset( $qpart ) ) {
01570 $prefs[$value] = 1.0;
01571 } elseif( preg_match( '/q\s*=\s*(\d*\.\d+)/', $qpart, $match ) ) {
01572 $prefs[$value] = floatval($match[1]);
01573 }
01574 }
01575
01576 return $prefs;
01577 }
01578
01591 function mimeTypeMatch( $type, $avail ) {
01592 if( array_key_exists($type, $avail) ) {
01593 return $type;
01594 } else {
01595 $parts = explode( '/', $type );
01596 if( array_key_exists( $parts[0] . '/*', $avail ) ) {
01597 return $parts[0] . '/*';
01598 } elseif( array_key_exists( '*/*', $avail ) ) {
01599 return '*/*';
01600 } else {
01601 return NULL;
01602 }
01603 }
01604 }
01605
01619 function wfNegotiateType( $cprefs, $sprefs ) {
01620 $combine = array();
01621
01622 foreach( array_keys($sprefs) as $type ) {
01623 $parts = explode( '/', $type );
01624 if( $parts[1] != '*' ) {
01625 $ckey = mimeTypeMatch( $type, $cprefs );
01626 if( $ckey ) {
01627 $combine[$type] = $sprefs[$type] * $cprefs[$ckey];
01628 }
01629 }
01630 }
01631
01632 foreach( array_keys( $cprefs ) as $type ) {
01633 $parts = explode( '/', $type );
01634 if( $parts[1] != '*' && !array_key_exists( $type, $sprefs ) ) {
01635 $skey = mimeTypeMatch( $type, $sprefs );
01636 if( $skey ) {
01637 $combine[$type] = $sprefs[$skey] * $cprefs[$type];
01638 }
01639 }
01640 }
01641
01642 $bestq = 0;
01643 $besttype = NULL;
01644
01645 foreach( array_keys( $combine ) as $type ) {
01646 if( $combine[$type] > $bestq ) {
01647 $besttype = $type;
01648 $bestq = $combine[$type];
01649 }
01650 }
01651
01652 return $besttype;
01653 }
01654
01662 function wfArrayLookup( $a, $b ) {
01663 return array_flip( array_intersect( array_flip( $a ), array_keys( $b ) ) );
01664 }
01665
01670 function wfTimestampNow() {
01671 # return NOW
01672 return wfTimestamp( TS_MW, time() );
01673 }
01674
01678 function wfSuppressWarnings( $end = false ) {
01679 static $suppressCount = 0;
01680 static $originalLevel = false;
01681
01682 if ( $end ) {
01683 if ( $suppressCount ) {
01684 --$suppressCount;
01685 if ( !$suppressCount ) {
01686 error_reporting( $originalLevel );
01687 }
01688 }
01689 } else {
01690 if ( !$suppressCount ) {
01691 $originalLevel = error_reporting( E_ALL & ~( E_WARNING | E_NOTICE ) );
01692 }
01693 ++$suppressCount;
01694 }
01695 }
01696
01700 function wfRestoreWarnings() {
01701 wfSuppressWarnings( true );
01702 }
01703
01704 # Autodetect, convert and provide timestamps of various types
01705
01709 define('TS_UNIX', 0);
01710
01714 define('TS_MW', 1);
01715
01719 define('TS_DB', 2);
01720
01724 define('TS_RFC2822', 3);
01725
01731 define('TS_ISO_8601', 4);
01732
01740 define('TS_EXIF', 5);
01741
01745 define('TS_ORACLE', 6);
01746
01750 define('TS_POSTGRES', 7);
01751
01755 define('TS_DB2', 8);
01756
01763 function wfTimestamp($outputtype=TS_UNIX,$ts=0) {
01764 $uts = 0;
01765 $da = array();
01766 if ($ts==0) {
01767 $uts=time();
01768 } elseif (preg_match('/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D',$ts,$da)) {
01769 # TS_DB
01770 } elseif (preg_match('/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D',$ts,$da)) {
01771 # TS_EXIF
01772 } elseif (preg_match('/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D',$ts,$da)) {
01773 # TS_MW
01774 } elseif (preg_match('/^\d{1,13}$/D',$ts)) {
01775 # TS_UNIX
01776 $uts = $ts;
01777 } elseif (preg_match('/^\d{1,2}-...-\d\d(?:\d\d)? \d\d\.\d\d\.\d\d/', $ts)) {
01778 # TS_ORACLE
01779 $uts = strtotime(preg_replace('/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
01780 str_replace("+00:00", "UTC", $ts)));
01781 } elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z$/', $ts, $da)) {
01782 # TS_ISO_8601
01783 } elseif (preg_match('/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/',$ts,$da)) {
01784 # TS_POSTGRES
01785 } elseif (preg_match('/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d* GMT$/',$ts,$da)) {
01786 # TS_POSTGRES
01787 } else {
01788 # Bogus value; fall back to the epoch...
01789 wfDebug("wfTimestamp() fed bogus time value: $outputtype; $ts\n");
01790 $uts = 0;
01791 }
01792
01793 if (count( $da ) ) {
01794
01795
01796 $uts=gmmktime((int)$da[4],(int)$da[5],(int)$da[6],
01797 (int)$da[2],(int)$da[3],(int)$da[1]);
01798 }
01799
01800 switch($outputtype) {
01801 case TS_UNIX:
01802 return $uts;
01803 case TS_MW:
01804 return gmdate( 'YmdHis', $uts );
01805 case TS_DB:
01806 return gmdate( 'Y-m-d H:i:s', $uts );
01807 case TS_ISO_8601:
01808 return gmdate( 'Y-m-d\TH:i:s\Z', $uts );
01809
01810 case TS_EXIF:
01811 return gmdate( 'Y:m:d H:i:s', $uts );
01812 case TS_RFC2822:
01813 return gmdate( 'D, d M Y H:i:s', $uts ) . ' GMT';
01814 case TS_ORACLE:
01815 return gmdate( 'd-M-y h.i.s A', $uts) . ' +00:00';
01816 case TS_POSTGRES:
01817 return gmdate( 'Y-m-d H:i:s', $uts) . ' GMT';
01818 case TS_DB2:
01819 return gmdate( 'Y-m-d H:i:s', $uts);
01820 default:
01821 throw new MWException( 'wfTimestamp() called with illegal output type.');
01822 }
01823 }
01824
01832 function wfTimestampOrNull( $outputtype = TS_UNIX, $ts = null ) {
01833 if( is_null( $ts ) ) {
01834 return null;
01835 } else {
01836 return wfTimestamp( $outputtype, $ts );
01837 }
01838 }
01839
01845 function wfIsWindows() {
01846 if (substr(php_uname(), 0, 7) == 'Windows') {
01847 return true;
01848 } else {
01849 return false;
01850 }
01851 }
01852
01856 function swap( &$x, &$y ) {
01857 $z = $x;
01858 $x = $y;
01859 $y = $z;
01860 }
01861
01862 function wfGetCachedNotice( $name ) {
01863 global $wgOut, $wgRenderHashAppend, $parserMemc;
01864 $fname = 'wfGetCachedNotice';
01865 wfProfileIn( $fname );
01866
01867 $needParse = false;
01868
01869 if( $name === 'default' ) {
01870
01871 global $wgSiteNotice;
01872 $notice = $wgSiteNotice;
01873 if( empty( $notice ) ) {
01874 wfProfileOut( $fname );
01875 return false;
01876 }
01877 } else {
01878 $notice = wfMsgForContentNoTrans( $name );
01879 if( wfEmptyMsg( $name, $notice ) || $notice == '-' ) {
01880 wfProfileOut( $fname );
01881 return( false );
01882 }
01883 }
01884
01885
01886 $key = wfMemcKey( $name . $wgRenderHashAppend );
01887 $cachedNotice = $parserMemc->get( $key );
01888 if( is_array( $cachedNotice ) ) {
01889 if( md5( $notice ) == $cachedNotice['hash'] ) {
01890 $notice = $cachedNotice['html'];
01891 } else {
01892 $needParse = true;
01893 }
01894 } else {
01895 $needParse = true;
01896 }
01897
01898 if( $needParse ) {
01899 if( is_object( $wgOut ) ) {
01900 $parsed = $wgOut->parse( $notice );
01901 $parserMemc->set( $key, array( 'html' => $parsed, 'hash' => md5( $notice ) ), 600 );
01902 $notice = $parsed;
01903 } else {
01904 wfDebug( 'wfGetCachedNotice called for ' . $name . ' with no $wgOut available'."\n" );
01905 $notice = '';
01906 }
01907 }
01908
01909 wfProfileOut( $fname );
01910 return $notice;
01911 }
01912
01913 function wfGetNamespaceNotice() {
01914 global $wgTitle;
01915
01916 # Paranoia
01917 if ( !isset( $wgTitle ) || !is_object( $wgTitle ) )
01918 return "";
01919
01920 $fname = 'wfGetNamespaceNotice';
01921 wfProfileIn( $fname );
01922
01923 $key = "namespacenotice-" . $wgTitle->getNsText();
01924 $namespaceNotice = wfGetCachedNotice( $key );
01925 if ( $namespaceNotice && substr ( $namespaceNotice , 0 ,7 ) != "<p><" ) {
01926 $namespaceNotice = '<div id="namespacebanner">' . $namespaceNotice . "</div>";
01927 } else {
01928 $namespaceNotice = "";
01929 }
01930
01931 wfProfileOut( $fname );
01932 return $namespaceNotice;
01933 }
01934
01935 function wfGetSiteNotice() {
01936 global $wgUser, $wgSiteNotice;
01937 $fname = 'wfGetSiteNotice';
01938 wfProfileIn( $fname );
01939 $siteNotice = '';
01940
01941 if( wfRunHooks( 'SiteNoticeBefore', array( &$siteNotice ) ) ) {
01942 if( is_object( $wgUser ) && $wgUser->isLoggedIn() ) {
01943 $siteNotice = wfGetCachedNotice( 'sitenotice' );
01944 } else {
01945 $anonNotice = wfGetCachedNotice( 'anonnotice' );
01946 if( !$anonNotice ) {
01947 $siteNotice = wfGetCachedNotice( 'sitenotice' );
01948 } else {
01949 $siteNotice = $anonNotice;
01950 }
01951 }
01952 if( !$siteNotice ) {
01953 $siteNotice = wfGetCachedNotice( 'default' );
01954 }
01955 }
01956
01957 wfRunHooks( 'SiteNoticeAfter', array( &$siteNotice ) );
01958 wfProfileOut( $fname );
01959 return $siteNotice;
01960 }
01961
01966 function &wfGetMimeMagic() {
01967 return MimeMagic::singleton();
01968 }
01969
01980 function wfTempDir() {
01981 foreach( array( 'TMPDIR', 'TMP', 'TEMP' ) as $var ) {
01982 $tmp = getenv( $var );
01983 if( $tmp && file_exists( $tmp ) && is_dir( $tmp ) && is_writable( $tmp ) ) {
01984 return $tmp;
01985 }
01986 }
01987 # Hope this is Unix of some kind!
01988 return '/tmp';
01989 }
01990
01999 function wfMkdirParents( $dir, $mode = null, $caller = null ) {
02000 global $wgDirectoryMode;
02001
02002 if ( !is_null( $caller ) ) {
02003 wfDebug( "$caller: called wfMkdirParents($dir)" );
02004 }
02005
02006 if( strval( $dir ) === '' || file_exists( $dir ) )
02007 return true;
02008
02009 if ( is_null( $mode ) )
02010 $mode = $wgDirectoryMode;
02011
02012 return mkdir( $dir, $mode, true );
02013 }
02014
02018 function wfIncrStats( $key ) {
02019 global $wgStatsMethod;
02020
02021 if( $wgStatsMethod == 'udp' ) {
02022 global $wgUDPProfilerHost, $wgUDPProfilerPort, $wgDBname;
02023 static $socket;
02024 if (!$socket) {
02025 $socket=socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
02026 $statline="stats/{$wgDBname} - 1 1 1 1 1 -total\n";
02027 socket_sendto($socket,$statline,strlen($statline),0,$wgUDPProfilerHost,$wgUDPProfilerPort);
02028 }
02029 $statline="stats/{$wgDBname} - 1 1 1 1 1 {$key}\n";
02030 @socket_sendto($socket,$statline,strlen($statline),0,$wgUDPProfilerHost,$wgUDPProfilerPort);
02031 } elseif( $wgStatsMethod == 'cache' ) {
02032 global $wgMemc;
02033 $key = wfMemcKey( 'stats', $key );
02034 if ( is_null( $wgMemc->incr( $key ) ) ) {
02035 $wgMemc->add( $key, 1 );
02036 }
02037 } else {
02038
02039 }
02040 }
02041
02048 function wfPercent( $nr, $acc = 2, $round = true ) {
02049 $ret = sprintf( "%.${acc}f", $nr );
02050 return $round ? round( $ret, $acc ) . '%' : "$ret%";
02051 }
02052
02061 function wfEncryptPassword( $userid, $password ) {
02062 wfDeprecated(__FUNCTION__);
02063 # Just wrap around User::oldCrypt()
02064 return User::oldCrypt($password, $userid);
02065 }
02066
02070 function wfAppendToArrayIfNotDefault( $key, $value, $default, &$changed ) {
02071 if ( is_null( $changed ) ) {
02072 throw new MWException('GlobalFunctions::wfAppendToArrayIfNotDefault got null');
02073 }
02074 if ( $default[$key] !== $value ) {
02075 $changed[$key] = $value;
02076 }
02077 }
02078
02088 function wfEmptyMsg( $msg, $wfMsgOut ) {
02089 return $wfMsgOut === htmlspecialchars( "<$msg>" );
02090 }
02091
02099 function in_string( $needle, $str ) {
02100 return strpos( $str, $needle ) !== false;
02101 }
02102
02103 function wfSpecialList( $page, $details ) {
02104 global $wgContLang;
02105 $details = $details ? ' ' . $wgContLang->getDirMark() . "($details)" : "";
02106 return $page . $details;
02107 }
02108
02114 function wfUrlProtocols() {
02115 global $wgUrlProtocols;
02116
02117
02118
02119 if ( is_array( $wgUrlProtocols ) ) {
02120 $protocols = array();
02121 foreach ($wgUrlProtocols as $protocol)
02122 $protocols[] = preg_quote( $protocol, '/' );
02123
02124 return implode( '|', $protocols );
02125 } else {
02126 return $wgUrlProtocols;
02127 }
02128 }
02129
02153 function wfIniGetBool( $setting ) {
02154 $val = ini_get( $setting );
02155
02156 return strtolower( $val ) == 'on'
02157 || strtolower( $val ) == 'true'
02158 || strtolower( $val ) == 'yes'
02159 || preg_match( "/^\s*[+-]?0*[1-9]/", $val );
02160 }
02161
02170 function wfShellExec( $cmd, &$retval=null ) {
02171 global $IP, $wgMaxShellMemory, $wgMaxShellFileSize, $wgMaxShellTime;
02172
02173 static $disabled;
02174 if ( is_null( $disabled ) ) {
02175 $disabled = false;
02176 if( wfIniGetBool( 'safe_mode' ) ) {
02177 wfDebug( "wfShellExec can't run in safe_mode, PHP's exec functions are too broken.\n" );
02178 $disabled = true;
02179 }
02180 $functions = explode( ',', ini_get( 'disable_functions' ) );
02181 $functions = array_map( 'trim', $functions );
02182 $functions = array_map( 'strtolower', $functions );
02183 if ( in_array( 'passthru', $functions ) ) {
02184 wfDebug( "passthru is in disabled_functions\n" );
02185 $disabled = true;
02186 }
02187 }
02188 if ( $disabled ) {
02189 $retval = 1;
02190 return "Unable to run external programs in safe mode.";
02191 }
02192
02193 wfInitShellLocale();
02194
02195 if ( php_uname( 's' ) == 'Linux' ) {
02196 $time = intval( $wgMaxShellTime );
02197 $mem = intval( $wgMaxShellMemory );
02198 $filesize = intval( $wgMaxShellFileSize );
02199
02200 if ( $time > 0 && $mem > 0 ) {
02201 $script = "$IP/bin/ulimit4.sh";
02202 if ( is_executable( $script ) ) {
02203 $cmd = escapeshellarg( $script ) . " $time $mem $filesize " . escapeshellarg( $cmd );
02204 }
02205 }
02206 } elseif ( php_uname( 's' ) == 'Windows NT' ) {
02207 # This is a hack to work around PHP's flawed invocation of cmd.exe
02208 # http://news.php.net/php.internals/21796
02209 $cmd = '"' . $cmd . '"';
02210 }
02211 wfDebug( "wfShellExec: $cmd\n" );
02212
02213 $retval = 1;
02214 ob_start();
02215 passthru( $cmd, $retval );
02216 $output = ob_get_contents();
02217 ob_end_clean();
02218
02219 if ( $retval == 127 ) {
02220 wfDebugLog( 'exec', "Possibly missing executable file: $cmd\n" );
02221 }
02222 return $output;
02223 }
02224
02229 function wfInitShellLocale() {
02230 static $done = false;
02231 if ( $done ) return;
02232 $done = true;
02233 global $wgShellLocale;
02234 if ( !wfIniGetBool( 'safe_mode' ) ) {
02235 putenv( "LC_CTYPE=$wgShellLocale" );
02236 setlocale( LC_CTYPE, $wgShellLocale );
02237 }
02238 }
02239
02255 function wfUsePHP( $req_ver ) {
02256 $php_ver = PHP_VERSION;
02257
02258 if ( version_compare( $php_ver, (string)$req_ver, '<' ) )
02259 throw new MWException( "PHP $req_ver required--this is only $php_ver" );
02260 }
02261
02275 function wfUseMW( $req_ver ) {
02276 global $wgVersion;
02277
02278 if ( version_compare( $wgVersion, (string)$req_ver, '<' ) )
02279 throw new MWException( "MediaWiki $req_ver required--this is only $wgVersion" );
02280 }
02281
02285 function wfRegexReplacement( $string ) {
02286 return StringUtils::escapeRegexReplacement( $string );
02287 }
02288
02301 function wfBaseName( $path, $suffix='' ) {
02302 $encSuffix = ($suffix == '')
02303 ? ''
02304 : ( '(?:' . preg_quote( $suffix, '#' ) . ')?' );
02305 $matches = array();
02306 if( preg_match( "#([^/\\\\]*?){$encSuffix}[/\\\\]*$#", $path, $matches ) ) {
02307 return $matches[1];
02308 } else {
02309 return '';
02310 }
02311 }
02312
02322 function wfRelativePath( $path, $from ) {
02323
02324 $path = str_replace( '/', DIRECTORY_SEPARATOR, $path );
02325 $from = str_replace( '/', DIRECTORY_SEPARATOR, $from );
02326
02327
02328 $path = rtrim( $path, DIRECTORY_SEPARATOR );
02329 $from = rtrim( $from, DIRECTORY_SEPARATOR );
02330
02331 $pieces = explode( DIRECTORY_SEPARATOR, dirname( $path ) );
02332 $against = explode( DIRECTORY_SEPARATOR, $from );
02333
02334 if( $pieces[0] !== $against[0] ) {
02335
02336
02337 return $path;
02338 }
02339
02340
02341 while( count( $pieces ) && count( $against )
02342 && $pieces[0] == $against[0] ) {
02343 array_shift( $pieces );
02344 array_shift( $against );
02345 }
02346
02347
02348 while( count( $against ) ) {
02349 array_unshift( $pieces, '..' );
02350 array_shift( $against );
02351 }
02352
02353 array_push( $pieces, wfBaseName( $path ) );
02354
02355 return implode( DIRECTORY_SEPARATOR, $pieces );
02356 }
02357
02365 function wfArrayMerge( $array1 ) {
02366 $args = func_get_args();
02367 $args = array_reverse( $args, true );
02368 $out = array();
02369 foreach ( $args as $arg ) {
02370 $out += $arg;
02371 }
02372 return $out;
02373 }
02374
02391 function wfMergeErrorArrays() {
02392 $args = func_get_args();
02393 $out = array();
02394 foreach ( $args as $errors ) {
02395 foreach ( $errors as $params ) {
02396 $spec = implode( "\t", $params );
02397 $out[$spec] = $params;
02398 }
02399 }
02400 return array_values( $out );
02401 }
02402
02413 function wfParseUrl( $url ) {
02414 global $wgUrlProtocols;
02415 wfSuppressWarnings();
02416 $bits = parse_url( $url );
02417 wfRestoreWarnings();
02418 if ( !$bits ) {
02419 return false;
02420 }
02421
02422
02423 if ( in_array( $bits['scheme'] . '://', $wgUrlProtocols ) ) {
02424 $bits['delimiter'] = '://';
02425 } elseif ( in_array( $bits['scheme'] . ':', $wgUrlProtocols ) ) {
02426 $bits['delimiter'] = ':';
02427
02428
02429 if ( isset ( $bits['path'] ) ) {
02430 $bits['host'] = $bits['path'];
02431 $bits['path'] = '';
02432 }
02433 } else {
02434 return false;
02435 }
02436
02437 return $bits;
02438 }
02439
02443 function wfMakeUrlIndex( $url ) {
02444 $bits = wfParseUrl( $url );
02445
02446
02447
02448 if ( $bits['scheme'] == 'mailto' ) {
02449 $mailparts = explode( '@', $bits['host'], 2 );
02450 if ( count($mailparts) === 2 ) {
02451 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
02452 } else {
02453
02454 $domainpart = '';
02455 }
02456 $reversedHost = $domainpart . '@' . $mailparts[0];
02457 } else {
02458 $reversedHost = strtolower( implode( '.', array_reverse( explode( '.', $bits['host'] ) ) ) );
02459 }
02460
02461
02462 if ( substr( $reversedHost, -1, 1 ) !== '.' ) {
02463 $reversedHost .= '.';
02464 }
02465
02466 $prot = $bits['scheme'];
02467 $index = $prot . $bits['delimiter'] . $reversedHost;
02468
02469 if ( isset( $bits['port'] ) ) $index .= ':' . $bits['port'];
02470 if ( isset( $bits['path'] ) ) {
02471 $index .= $bits['path'];
02472 } else {
02473 $index .= '/';
02474 }
02475 if ( isset( $bits['query'] ) ) $index .= '?' . $bits['query'];
02476 if ( isset( $bits['fragment'] ) ) $index .= '#' . $bits['fragment'];
02477 return $index;
02478 }
02479
02484 function wfDoUpdates()
02485 {
02486 global $wgPostCommitUpdateList, $wgDeferredUpdateList;
02487 foreach ( $wgDeferredUpdateList as $update ) {
02488 $update->doUpdate();
02489 }
02490 foreach ( $wgPostCommitUpdateList as $update ) {
02491 $update->doUpdate();
02492 }
02493 $wgDeferredUpdateList = array();
02494 $wgPostCommitUpdateList = array();
02495 }
02496
02500 function wfExplodeMarkup( $separator, $text ) {
02501 return StringUtils::explodeMarkup( $separator, $text );
02502 }
02503
02518 function wfBaseConvert( $input, $sourceBase, $destBase, $pad=1, $lowercase=true ) {
02519 $input = strval( $input );
02520 if( $sourceBase < 2 ||
02521 $sourceBase > 36 ||
02522 $destBase < 2 ||
02523 $destBase > 36 ||
02524 $pad < 1 ||
02525 $sourceBase != intval( $sourceBase ) ||
02526 $destBase != intval( $destBase ) ||
02527 $pad != intval( $pad ) ||
02528 !is_string( $input ) ||
02529 $input == '' ) {
02530 return false;
02531 }
02532 $digitChars = ( $lowercase ) ? '0123456789abcdefghijklmnopqrstuvwxyz' : '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
02533 $inDigits = array();
02534 $outChars = '';
02535
02536
02537 $input = strtolower( $input );
02538 for( $i = 0; $i < strlen( $input ); $i++ ) {
02539 $n = strpos( $digitChars, $input{$i} );
02540 if( $n === false || $n > $sourceBase ) {
02541 return false;
02542 }
02543 $inDigits[] = $n;
02544 }
02545
02546
02547
02548 while( count( $inDigits ) ) {
02549 $work = 0;
02550 $workDigits = array();
02551
02552
02553 foreach( $inDigits as $digit ) {
02554 $work *= $sourceBase;
02555 $work += $digit;
02556
02557 if( $work < $destBase ) {
02558
02559 if( count( $workDigits ) ) {
02560
02561
02562
02563 $workDigits[] = 0;
02564 }
02565 } else {
02566
02567 $workDigits[] = intval( $work / $destBase );
02568
02569
02570
02571
02572 $work = $work % $destBase;
02573 }
02574 }
02575
02576
02577
02578 $outChars .= $digitChars[$work];
02579
02580
02581 $inDigits = $workDigits;
02582 }
02583
02584 while( strlen( $outChars ) < $pad ) {
02585 $outChars .= '0';
02586 }
02587
02588 return strrev( $outChars );
02589 }
02590
02596 function wfCreateObject( $name, $p ){
02597 $p = array_values( $p );
02598 switch ( count( $p ) ) {
02599 case 0:
02600 return new $name;
02601 case 1:
02602 return new $name( $p[0] );
02603 case 2:
02604 return new $name( $p[0], $p[1] );
02605 case 3:
02606 return new $name( $p[0], $p[1], $p[2] );
02607 case 4:
02608 return new $name( $p[0], $p[1], $p[2], $p[3] );
02609 case 5:
02610 return new $name( $p[0], $p[1], $p[2], $p[3], $p[4] );
02611 case 6:
02612 return new $name( $p[0], $p[1], $p[2], $p[3], $p[4], $p[5] );
02613 default:
02614 throw new MWException( "Too many arguments to construtor in wfCreateObject" );
02615 }
02616 }
02617
02622 function wfGetHTTP( $url, $timeout = 'default' ) {
02623 wfDeprecated(__FUNCTION__);
02624 return Http::get( $url, $timeout );
02625 }
02626
02631 function wfIsLocalURL( $url ) {
02632 wfDeprecated(__FUNCTION__);
02633 return Http::isLocalURL( $url );
02634 }
02635
02636 function wfHttpOnlySafe() {
02637 global $wgHttpOnlyBlacklist;
02638 if( !version_compare("5.2", PHP_VERSION, "<") )
02639 return false;
02640
02641 if( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
02642 foreach( $wgHttpOnlyBlacklist as $regex ) {
02643 if( preg_match( $regex, $_SERVER['HTTP_USER_AGENT'] ) ) {
02644 return false;
02645 }
02646 }
02647 }
02648
02649 return true;
02650 }
02651
02655 function wfSetupSession() {
02656 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgCookieHttpOnly;
02657 if( $wgSessionsInMemcached ) {
02658 require_once( 'MemcachedSessions.php' );
02659 } elseif( 'files' != ini_get( 'session.save_handler' ) ) {
02660 # If it's left on 'user' or another setting from another
02661 # application, it will end up failing. Try to recover.
02662 ini_set ( 'session.save_handler', 'files' );
02663 }
02664 $httpOnlySafe = wfHttpOnlySafe();
02665 wfDebugLog( 'cookie',
02666 'session_set_cookie_params: "' . implode( '", "',
02667 array(
02668 0,
02669 $wgCookiePath,
02670 $wgCookieDomain,
02671 $wgCookieSecure,
02672 $httpOnlySafe && $wgCookieHttpOnly ) ) . '"' );
02673 if( $httpOnlySafe && $wgCookieHttpOnly ) {
02674 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgCookieHttpOnly );
02675 } else {
02676
02677 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
02678 }
02679 session_cache_limiter( 'private, must-revalidate' );
02680 wfSuppressWarnings();
02681 session_start();
02682 wfRestoreWarnings();
02683 }
02684
02690 function wfGetPrecompiledData( $name ) {
02691 global $IP;
02692
02693 $file = "$IP/serialized/$name";
02694 if ( file_exists( $file ) ) {
02695 $blob = file_get_contents( $file );
02696 if ( $blob ) {
02697 return unserialize( $blob );
02698 }
02699 }
02700 return false;
02701 }
02702
02703 function wfGetCaller( $level = 2 ) {
02704 $backtrace = wfDebugBacktrace();
02705 if ( isset( $backtrace[$level] ) ) {
02706 return wfFormatStackFrame($backtrace[$level]);
02707 } else {
02708 $caller = 'unknown';
02709 }
02710 return $caller;
02711 }
02712
02714 function wfGetAllCallers() {
02715 return implode('/', array_map('wfFormatStackFrame',array_reverse(wfDebugBacktrace())));
02716 }
02717
02719 function wfFormatStackFrame($frame) {
02720 return isset( $frame["class"] )?
02721 $frame["class"]."::".$frame["function"]:
02722 $frame["function"];
02723 }
02724
02728 function wfMemcKey( ) {
02729 $args = func_get_args();
02730 $key = wfWikiID() . ':' . implode( ':', $args );
02731 return $key;
02732 }
02733
02737 function wfForeignMemcKey( $db, $prefix ) {
02738 $args = array_slice( func_get_args(), 2 );
02739 if ( $prefix ) {
02740 $key = "$db-$prefix:" . implode( ':', $args );
02741 } else {
02742 $key = $db . ':' . implode( ':', $args );
02743 }
02744 return $key;
02745 }
02746
02751 function wfWikiID( $db = null ) {
02752 if( $db instanceof Database ) {
02753 return $db->getWikiID();
02754 } else {
02755 global $wgDBprefix, $wgDBname;
02756 if ( $wgDBprefix ) {
02757 return "$wgDBname-$wgDBprefix";
02758 } else {
02759 return $wgDBname;
02760 }
02761 }
02762 }
02763
02767 function wfSplitWikiID( $wiki ) {
02768 $bits = explode( '-', $wiki, 2 );
02769 if ( count( $bits ) < 2 ) {
02770 $bits[] = '';
02771 }
02772 return $bits;
02773 }
02774
02775
02776
02777
02778
02779
02780
02781
02782
02783
02784
02785
02786
02787
02788
02789
02790
02791 function &wfGetDB( $db, $groups = array(), $wiki = false ) {
02792 return wfGetLB( $wiki )->getConnection( $db, $groups, $wiki );
02793 }
02794
02802 function wfGetLB( $wiki = false ) {
02803 return wfGetLBFactory()->getMainLB( $wiki );
02804 }
02805
02809 function &wfGetLBFactory() {
02810 return LBFactory::singleton();
02811 }
02812
02824 function wfFindFile( $title, $time = false, $flags = 0, $bypass = false ) {
02825 if( !$time && !$flags && !$bypass ) {
02826 return FileCache::singleton()->findFile( $title );
02827 } else {
02828 return RepoGroup::singleton()->findFile( $title, $time, $flags );
02829 }
02830 }
02831
02836 function wfLocalFile( $title ) {
02837 return RepoGroup::singleton()->getLocalRepo()->newFile( $title );
02838 }
02839
02845 function wfQueriesMustScale() {
02846 global $wgMiserMode;
02847 return $wgMiserMode
02848 || ( SiteStats::pages() > 100000
02849 && SiteStats::edits() > 1000000
02850 && SiteStats::users() > 10000 );
02851 }
02852
02860 function wfScript( $script = 'index' ) {
02861 global $wgScriptPath, $wgScriptExtension;
02862 return "{$wgScriptPath}/{$script}{$wgScriptExtension}";
02863 }
02864
02872 function wfBoolToStr( $value ) {
02873 return $value ? 'true' : 'false';
02874 }
02875
02885 function wfLoadExtensionMessages( $extensionName, $langcode = false ) {
02886 global $wgExtensionMessagesFiles, $wgMessageCache, $wgLang, $wgContLang;
02887
02888 #For recording whether extension message files have been loaded in a given language.
02889 static $loaded = array();
02890
02891 if( !array_key_exists( $extensionName, $loaded ) ) {
02892 $loaded[$extensionName] = array();
02893 }
02894
02895 if ( !isset($wgExtensionMessagesFiles[$extensionName]) ) {
02896 throw new MWException( "Messages file for extensions $extensionName is not defined" );
02897 }
02898
02899 if( !$langcode && !array_key_exists( '*', $loaded[$extensionName] ) ) {
02900 # Just do en, content language and user language.
02901 $wgMessageCache->loadMessagesFile( $wgExtensionMessagesFiles[$extensionName], false );
02902 # Mark that they have been loaded.
02903 $loaded[$extensionName]['en'] = true;
02904 $loaded[$extensionName][$wgLang->getCode()] = true;
02905 $loaded[$extensionName][$wgContLang->getCode()] = true;
02906 # Mark that this part has been done to avoid weird if statements.
02907 $loaded[$extensionName]['*'] = true;
02908 } elseif( is_string( $langcode ) && !array_key_exists( $langcode, $loaded[$extensionName] ) ) {
02909 # Load messages for specified language.
02910 $wgMessageCache->loadMessagesFile( $wgExtensionMessagesFiles[$extensionName], $langcode );
02911 # Mark that they have been loaded.
02912 $loaded[$extensionName][$langcode] = true;
02913 }
02914 }
02915
02922 function wfGetNull() {
02923 return wfIsWindows()
02924 ? 'NUL'
02925 : '/dev/null';
02926 }
02927
02935 function wfMaxlagError( $host, $lag, $maxLag ) {
02936 global $wgShowHostnames;
02937 header( 'HTTP/1.1 503 Service Unavailable' );
02938 header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
02939 header( 'X-Database-Lag: ' . intval( $lag ) );
02940 header( 'Content-Type: text/plain' );
02941 if( $wgShowHostnames ) {
02942 echo "Waiting for $host: $lag seconds lagged\n";
02943 } else {
02944 echo "Waiting for a database server: $lag seconds lagged\n";
02945 }
02946 }
02947
02953 function wfDeprecated( $function ) {
02954 global $wgDebugLogFile;
02955 if ( !$wgDebugLogFile ) {
02956 return;
02957 }
02958 $callers = wfDebugBacktrace();
02959 if( isset( $callers[2] ) ){
02960 $callerfunc = $callers[2];
02961 $callerfile = $callers[1];
02962 if( isset( $callerfile['file'] ) && isset( $callerfile['line'] ) ){
02963 $file = $callerfile['file'] . ' at line ' . $callerfile['line'];
02964 } else {
02965 $file = '(internal function)';
02966 }
02967 $func = '';
02968 if( isset( $callerfunc['class'] ) )
02969 $func .= $callerfunc['class'] . '::';
02970 $func .= @$callerfunc['function'];
02971 $msg = "Use of $function is deprecated. Called from $func in $file";
02972 } else {
02973 $msg = "Use of $function is deprecated.";
02974 }
02975 wfDebug( "$msg\n" );
02976 }
02977
02991 function wfWaitForSlaves( $maxLag ) {
02992 if( $maxLag ) {
02993 $lb = wfGetLB();
02994 list( $host, $lag ) = $lb->getMaxLag();
02995 while( $lag > $maxLag ) {
02996 $name = @gethostbyaddr( $host );
02997 if( $name !== false ) {
02998 $host = $name;
02999 }
03000 print "Waiting for $host (lagged $lag seconds)...\n";
03001 sleep($maxLag);
03002 list( $host, $lag ) = $lb->getMaxLag();
03003 }
03004 }
03005 }
03006
03011 function wfOut( $s ) {
03012 static $lineStarted = false;
03013 global $wgCommandLineMode;
03014 if ( $wgCommandLineMode && !defined( 'MEDIAWIKI_INSTALL' ) ) {
03015 echo $s;
03016 } else {
03017 echo htmlspecialchars( $s );
03018 }
03019 flush();
03020 }
03021
03025 function wfGenerateToken( $salt = '' ) {
03026 $salt = serialize($salt);
03027
03028 return md5( mt_rand( 0, 0x7fffffff ) . $salt );
03029 }
03030
03035 function wfStripIllegalFilenameChars( $name ) {
03036 $name = wfBaseName( $name );
03037 $name = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $name );
03038 return $name;
03039 }