- * Safe equivalent of eval() that parses a statement of the form
- * $varname = array( 'constant' => 'constant', ... ); or
- * $varname = 'constant'; or
- * $varname = 123;
- * and refuses to execute anything.
- *
- * @return True on success ($varname set globally), false on failue
- */
-function wfParsePHP( $php ) {
- $tokens = token_get_all( "<?php $php" );
- array_shift( $tokens ); // Discard <?php token
- $token = array_shift( $tokens ); // $varname token
- if ( @$token[0] == T_WHITESPACE )
- $varToken = array_shift( $tokens ); // discard whitespace
- if ( @$token[0] != T_VARIABLE )
- return false;
- $varname = substr( $token[1], 1 ); // discard leading $
-
- $token = array_shift( $tokens );
- if ( @$token[0] == T_WHITESPACE )
- $token = array_shift( $tokens ); // discard whitespace
- if ( $token[0] != '=' )
- return false;
-
- $isGood = true;
- $varvalue = wfParsePHP_recursive( $tokens, $isGood );
- if ( $isGood )
- $GLOBALS[$varname] = $varvalue;
- return $isGood;
-}
-
-function wfParsePHP_recursive( &$tokens, &$isGood ) {
- $isGood = true;
- $varvalue = null;
- $token = array_shift( $tokens );
- if ( @$token[0] == T_WHITESPACE )
- $token = array_shift( $tokens ); // discard whitespace
- if ( @$token[0] == T_ARRAY )
- $vartype = 'array';
- else if ( @$token[0] == T_CONSTANT_ENCAPSED_STRING )
- $vartype = 'string';
- else if ( @$token[0] == T_LNUMBER )
- $vartype = 'integer';
- else if ( @$token[0] == T_DNUMBER )
- $vartype = 'float';
- else if ( @$token[0] == T_STRING )
- $vartype = 'other';
- else
- return $isGood = false;
-
- switch ( $vartype ) {
- case 'string':
- $varvalue = stripslashes( substr( $token[1], 1, -1 ) );
- break;
- case 'integer':
- $base = 10;
- if ( $token[1][0] == '0' ) {
- if ( $token[1][1] == 'x' )
- $base = 16;
- else
- $base = 8;
- }
- $varvalue = intval( $token[1], $base );
- break;
- case 'float':
- $varvalue = (float)$token[1];
- break;
- case 'other':
- if ( strcasecmp( $token[1], 'true' ) == 0 )
- $varvalue = true;
- else if ( strcasecmp( $token[1], 'false' ) == 0 )
- $varvalue = false;
- else if ( strcasecmp( $token[1], 'null' ) == 0 )
- $varvalue = null;
+ * Set PHP's memory limit to the larger of php.ini or $wgMemoryLimit;
+ * @return Integer value memory was set to.
+ */
+
+function wfMemoryLimit () {
+ global $wgMemoryLimit;
+ $memlimit = wfShorthandToInteger( ini_get( "memory_limit" ) );
+ $conflimit = wfShorthandToInteger( $wgMemoryLimit );
+ if( $memlimit != -1 ) {
+ if( $conflimit == -1 ) {
+ wfDebug( "Removing PHP's memory limit\n" );
+ wfSuppressWarnings();
+ ini_set( "memory_limit", $conflimit );
+ wfRestoreWarnings();
+ return $conflimit;
+ } elseif ( $conflimit > $memlimit ) {
+ wfDebug( "Raising PHP's memory limit to $conflimit bytes\n" );
+ wfSuppressWarnings();
+ ini_set( "memory_limit", $conflimit );
+ wfRestoreWarnings();
+ return $conflimit;
+ }
+ }
+ return $memlimit;
+}
+
+/**
+ * Converts shorthand byte notation to integer form
+ * @param $string String
+ * @return Integer
+ */
+function wfShorthandToInteger ( $string = '' ) {
+ $string = trim($string);
+ if( empty($string) ) { return -1; }
+ $last = strtolower($string[strlen($string)-1]);
+ $val = intval($string);
+ switch($last) {
+ case 'g':
+ $val *= 1024;
+ case 'm':
+ $val *= 1024;
+ case 'k':
+ $val *= 1024;
+ }
+
+ return $val;
+}
+
+/* Get the normalised IETF language tag
+ * @param $code String: The language code.
+ * @return $langCode String: The language code which complying with BCP 47 standards.
+ */
+function wfBCP47( $code ) {
+ $codeSegment = explode( '-', $code );
+ foreach ( $codeSegment as $segNo => $seg ) {
+ if ( count( $codeSegment ) > 0 ) {
+ // ISO 3166 country code
+ if ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) )
+ $codeBCP[$segNo] = strtoupper( $seg );
+ // ISO 15924 script code
+ else if ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) )
+ $codeBCP[$segNo] = ucfirst( $seg );
+ // Use lowercase for other cases