/**
* Just like exit() but makes a note of it.
* Commits open transactions except if the error parameter is set
+ *
+ * @obsolete Please return control to the caller or throw an exception
*/
function wfAbruptExit( $error = false ){
global $wgLoadBalancer;
}
/**
- * @todo document
+ * @obsolete Please return control the caller or throw an exception
*/
function wfErrorExit() {
wfAbruptExit( true );
*/
function wfDie( $msg='' ) {
echo $msg;
- die( -1 );
+ die( 1 );
}
/**
- * Die with a backtrace
- * This is meant as a debugging aid to track down where bad data comes from.
- * Shouldn't be used in production code except maybe in "shouldn't happen" areas.
+ * Throw a debugging exception. This function previously once exited the process,
+ * but now throws an exception instead, with similar results.
*
* @param string $msg Message shown when dieing.
*/
function wfDebugDieBacktrace( $msg = '' ) {
- global $wgCommandLineMode;
-
- $backtrace = wfBacktrace();
- if ( $backtrace !== false ) {
- if ( $wgCommandLineMode ) {
- $msg .= "\nBacktrace:\n$backtrace";
- } else {
- $msg .= "\n<p>Backtrace:</p>\n$backtrace";
- }
- }
- echo $msg;
- echo wfReportTime()."\n";
- die( -1 );
+ throw new MWException( $msg );
}
/**
case TS_ORACLE:
return gmdate( 'd-M-y h.i.s A', $uts) . ' +00:00';
default:
- wfDebugDieBacktrace( 'wfTimestamp() called with illegal output type.');
+ throw new MWException( 'wfTimestamp() called with illegal output type.');
}
}
*/
function wfAppendToArrayIfNotDefault( $key, $value, $default, &$changed ) {
if ( is_null( $changed ) ) {
- wfDebugDieBacktrace('GlobalFunctions::wfAppendToArrayIfNotDefault got null');
+ throw new MWException('GlobalFunctions::wfAppendToArrayIfNotDefault got null');
}
if ( $default[$key] !== $value ) {
$changed[$key] = $value;
* @return collected stdout as a string (trailing newlines stripped)
*/
function wfShellExec( $cmd, &$retval=null ) {
- global $IP;
+ global $IP, $wgMaxShellMemory;
if( ini_get( 'safe_mode' ) ) {
wfDebug( "wfShellExec can't run in safe_mode, PHP's exec functions are too broken.\n" );
if ( php_uname( 's' ) == 'Linux' ) {
$time = ini_get( 'max_execution_time' );
- $mem = ini_get( 'memory_limit' );
- if( preg_match( '/^([0-9]+)[Mm]$/', trim( $mem ), $m ) ) {
- $mem = intval( $m[1] * (1024*1024) );
- }
+ $mem = intval( $wgMaxShellMemory );
+
if ( $time > 0 && $mem > 0 ) {
$script = "$IP/bin/ulimit.sh";
if ( is_executable( $script ) ) {
- $memKB = intval( $mem / 1024 );
- $cmd = escapeshellarg( $script ) . " $time $memKB $cmd";
+ $cmd = escapeshellarg( $script ) . " $time $mem $cmd";
}
}
} elseif ( php_uname( 's' ) == 'Windows NT' ) {
$php_ver = PHP_VERSION;
if ( version_compare( $php_ver, (string)$req_ver, '<' ) )
- wfDebugDieBacktrace( "PHP $req_ver required--this is only $php_ver" );
+ throw new MWException( "PHP $req_ver required--this is only $php_ver" );
}
/**
global $wgVersion;
if ( version_compare( $wgVersion, (string)$req_ver, '<' ) )
- wfDebugDieBacktrace( "MediaWiki $req_ver required--this is only $wgVersion" );
+ throw new MWException( "MediaWiki $req_ver required--this is only $wgVersion" );
}
/**
}
}
+
+/**
+ * Convert an arbitrarily-long digit string from one numeric base
+ * to another, optionally zero-padding to a minimum column width.
+ *
+ * Supports base 2 through 36; digit values 10-36 are represented
+ * as lowercase letters a-z. Input is case-insensitive.
+ *
+ * @param $input string of digits
+ * @param $sourceBase int 2-36
+ * @param $destBase int 2-36
+ * @param $pad int 1 or greater
+ * @return string or false on invalid input
+ */
+function wfBaseConvert( $input, $sourceBase, $destBase, $pad=1 ) {
+ if( $sourceBase < 2 ||
+ $sourceBase > 36 ||
+ $destBase < 2 ||
+ $destBase > 36 ||
+ $pad < 1 ||
+ $sourceBase != intval( $sourceBase ) ||
+ $destBase != intval( $destBase ) ||
+ $pad != intval( $pad ) ||
+ !is_string( $input ) ||
+ $input == '' ) {
+ return false;
+ }
+
+ $digitChars = '0123456789abcdefghijklmnopqrstuvwxyz';
+ $inDigits = array();
+ $outChars = '';
+
+ // Decode and validate input string
+ $input = strtolower( $input );
+ for( $i = 0; $i < strlen( $input ); $i++ ) {
+ $n = strpos( $digitChars, $input{$i} );
+ if( $n === false || $n > $sourceBase ) {
+ return false;
+ }
+ $inDigits[] = $n;
+ }
+
+ // Iterate over the input, modulo-ing out an output digit
+ // at a time until input is gone.
+ while( count( $inDigits ) ) {
+ $work = 0;
+ $workDigits = array();
+
+ // Long division...
+ foreach( $inDigits as $digit ) {
+ $work *= $sourceBase;
+ $work += $digit;
+
+ if( $work < $destBase ) {
+ // Gonna need to pull another digit.
+ if( count( $workDigits ) ) {
+ // Avoid zero-padding; this lets us find
+ // the end of the input very easily when
+ // length drops to zero.
+ $workDigits[] = 0;
+ }
+ } else {
+ // Finally! Actual division!
+ $workDigits[] = intval( $work / $destBase );
+
+ // Isn't it annoying that most programming languages
+ // don't have a single divide-and-remainder operator,
+ // even though the CPU implements it that way?
+ $work = $work % $destBase;
+ }
+ }
+
+ // All that division leaves us with a remainder,
+ // which is conveniently our next output digit.
+ $outChars .= $digitChars[$work];
+
+ // And we continue!
+ $inDigits = $workDigits;
+ }
+
+ while( strlen( $outChars ) < $pad ) {
+ $outChars .= '0';
+ }
+
+ return strrev( $outChars );
+}
+
?>