Introduce 'FetchChangesList' hook; see docs/hooks.txt for more information
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index d729f0a..e8f721f 100644 (file)
@@ -27,7 +27,6 @@ $wgTotalEdits = -1;
 
 
 require_once( 'DatabaseFunctions.php' );
-require_once( 'UpdateClasses.php' );
 require_once( 'LogPage.php' );
 require_once( 'normal/UtfNormalUtil.php' );
 require_once( 'XmlFunctions.php' );
@@ -404,7 +403,7 @@ function wfMsgNoDBForContent( $key ) {
  * @return $useDB Boolean
  * @return String: the requested message.
  */
-function wfMsgReal( $key, $args, $useDB, $forContent=false, $transform = true ) {
+function wfMsgReal( $key, $args, $useDB = true, $forContent=false, $transform = true ) {
        $fname = 'wfMsgReal';
 
        $message = wfMsgGetKey( $key, $useDB, $forContent, $transform );
@@ -600,6 +599,8 @@ function wfMsgExt( $key, $options ) {
 /**
  * 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;
@@ -630,7 +631,7 @@ function wfAbruptExit( $error = false ){
 }
 
 /**
- * @todo document
+ * @obsolete Please return control the caller or throw an exception
  */
 function wfErrorExit() {
        wfAbruptExit( true );
@@ -643,30 +644,17 @@ function wfErrorExit() {
  */
 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 );
 }
 
 /**
@@ -1365,7 +1353,7 @@ function wfTimestamp($outputtype=TS_UNIX,$ts=0) {
                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.');
        }
 }
 
@@ -1606,7 +1594,7 @@ function wfEncryptPassword( $userid, $password ) {
  */
 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;
@@ -1673,7 +1661,7 @@ function wfUrlProtocols() {
  * @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" );
@@ -1683,15 +1671,12 @@ function wfShellExec( $cmd, &$retval=null ) {
 
        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' ) {
@@ -1727,7 +1712,7 @@ function wfUsePHP( $req_ver ) {
        $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" );
 }
 
 /**
@@ -1747,7 +1732,7 @@ function wfUseMW( $req_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" );
 }
 
 /**
@@ -1865,4 +1850,91 @@ class ReplacerCallback {
        }
 }
 
+
+/**
+ * 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 );
+}
+
 ?>