DB error log
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index f51a5ee..0ae7fdc 100644 (file)
@@ -40,8 +40,8 @@ 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 ) );
+                       (strcasecmp( get_class( $object ), $class_name ) == 0) ||
+                        is_subclass_of( $object, $class_name );
        }
 }
 
@@ -150,20 +150,21 @@ function wfUrlencode ( $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);
@@ -226,6 +227,15 @@ function wfDebug( $text, $logonly = false )
        }
 }
 
+# Log for database errors
+function wfLogDBError( $text ) {
+       global $wgDBerrorLog;
+       if ( $wgDBerrorLog ) {
+               $text = date("D M j G:i:s T Y") . "\t$text";
+               error_log( $text, 3, $wgDBerrorLog );
+       }
+}
+
 function logProfilingData()
 {
        global $wgRequestTime, $wgDebugLogFile;
@@ -369,16 +379,34 @@ function wfAbruptExit(){
 }
 
 function wfDebugDieBacktrace( $msg = '' ) {
-       $msg .= "\n<p>Backtrace:</p>\n<ul>\n";
-       $backtrace = debug_backtrace();
-       foreach( $backtrace as $call ) {
-               $f = explode( DIRECTORY_SEPARATOR, $call['file'] );
-               $file = $f[count($f)-1];
-               $msg .= '<li>' . $file . " line " . $call['line'] . ', in ';
-               if( !empty( $call['class'] ) ) $msg .= $call['class'] . '::';
-               $msg .= $call['function'] . "()</li>\n";
-       }
-       die( $msg );
+       global $wgCommandLineMode;
+
+       if ( function_exists( 'debug_backtrace' ) ) {
+               if ( $wgCommandLineMode ) {
+                       $msg .= "\nBacktrace:\n";
+               } else {
+                       $msg .= "\n<p>Backtrace:</p>\n<ul>\n";
+               }
+               $backtrace = debug_backtrace();
+               foreach( $backtrace as $call ) {
+                       $f = explode( DIRECTORY_SEPARATOR, $call['file'] );
+                       $file = $f[count($f)-1];
+                       if ( $wgCommandLineMode ) {
+                               $msg .= "$file line {$call['line']}, in ";
+                       } else {
+                               $msg .= '<li>' . $file . " line " . $call['line'] . ', in ';
+                       }
+                       if( !empty( $call['class'] ) ) $msg .= $call['class'] . '::';
+                       $msg .= $call['function'] . "()";
+                       
+                       if ( $wgCommandLineMode ) {
+                               $msg .= "\n";
+                       } else {
+                               $msg .= "</li>\n";
+                       }
+               }
+        }
+        die( $msg );
 }
 
 function wfNumberOfArticles()
@@ -488,9 +516,9 @@ function wfRecordUpload( $name, $oldver, $size, $desc, $copyStatus = "", $source
        
        if ( $wgUseCopyrightUpload )
          {
-           $textdesc = '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $desc . "\n" .
-             '== ' . wfMsg ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
-             '== ' . wfMsg ( 'filesource' ) . " ==\n" . $source ;
+               $textdesc = '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $desc . "\n" .
+                 '== ' . wfMsg ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
+                 '== ' . wfMsg ( 'filesource' ) . " ==\n" . $source ;
          }
        else $textdesc = $desc ;
 
@@ -510,7 +538,7 @@ function wfRecordUpload( $name, $oldver, $size, $desc, $copyStatus = "", $source
                  wfStrencode( $name ) . "'";
                $res = wfQuery( $sql, DB_READ, $fname );
                if ( 0 == wfNumRows( $res ) ) {
-            $common =
+                       $common =
                          Namespace::getImage() . ",'" .
                          wfStrencode( $name ) . "','" .
                          wfStrencode( $desc ) . "','" . $wgUser->getID() . "','" .
@@ -670,7 +698,7 @@ function wfCheckLimits( $deflimit = 50, $optionname = 'rclimit' ) {
 function wfEscapeWikiText( $text )
 {
        $text = str_replace( 
-               array( '[',     '|',      "'",     'ISBN '    , '://'     , "\n=" ),
+               array( '[',             '|',      "'",     'ISBN '        , '://'         , "\n=" ),
                array( '&#91;', '&#124;', '&#39;', 'ISBN&#32;', '&#58;//' , "\n&#61;" ),
                htmlspecialchars($text) );
        return $text;
@@ -716,11 +744,10 @@ function wfSetVar( &$dest, $source )
 }
 
 # Sets dest to a reference to source and returns the original dest
+# Pity that doesn't work in PHP
 function &wfSetRef( &$dest, &$source )
 {
-       $temp =& $dest;
-       $dest =& $source;
-       return $temp;
+       die( "You can't rebind a variable in the caller's scope" );
 }
 
 # This function takes two arrays as input, and returns a CGI-style string, e.g.
@@ -762,7 +789,7 @@ function wfEscapeShellArg( )
                        $first = false;
                }
 
-               if (substr(php_uname(), 0, 7) == 'Windows') {
+               if ( wfIsWindows() ) {
                        $retVal .= '"' . str_replace( '"','\"', $arg ) . '"';
                } else {
                        $retVal .= escapeshellarg( $arg );
@@ -794,11 +821,11 @@ function wfMerge( $old, $mine, $yours, &$result ){
        fwrite( $yourtextFile, $yours ); fclose( $yourtextFile );
 
        # Check for a conflict
-    $cmd = wfEscapeShellArg( $wgDiff3 ) . ' -a --overlap-only ' .
-      wfEscapeShellArg( $mytextName ) . ' ' .
-      wfEscapeShellArg( $oldtextName ) . ' ' .
-      wfEscapeShellArg( $yourtextName );
-    $handle = popen( $cmd, 'r' );
+       $cmd = wfEscapeShellArg( $wgDiff3 ) . ' -a --overlap-only ' .
+         wfEscapeShellArg( $mytextName ) . ' ' .
+         wfEscapeShellArg( $oldtextName ) . ' ' .
+         wfEscapeShellArg( $yourtextName );
+       $handle = popen( $cmd, 'r' );
 
        if( fgets( $handle ) ){
                $conflict = true;
@@ -935,4 +962,14 @@ function wfArrayLookup( $a, $b )
        return array_flip( array_intersect( array_flip( $a ), array_keys( $b ) ) ); 
 }
 
+# Since Windows is so different to any of the other popular OSes, it seems appropriate
+# to have a simple way to test for its presence
+function wfIsWindows() {
+       if (substr(php_uname(), 0, 7) == 'Windows') {
+               return true;
+       } else {
+               return false;
+       }
+}
+
 ?>