Doc: Document problem sorting inserted data
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 2c26fef..4ef731b 100644 (file)
@@ -30,7 +30,7 @@ if ( !defined( 'MEDIAWIKI' ) ) {
 /**
  * Compatibility functions
  *
- * We support PHP 5.3.2 and up.
+ * We support PHP 5.3.3 and up.
  * Re-implementations of newer functions or functions in non-standard
  * PHP extensions may be included here.
  */
@@ -959,7 +959,7 @@ function wfMatchesDomainList( $url, $domains ) {
  *     - false: same as 'log'
  */
 function wfDebug( $text, $dest = 'all' ) {
-       global $wgDebugLogFile, $wgDebugRawPage, $wgDebugLogPrefix;
+       global $wgDebugRawPage, $wgDebugLogPrefix;
 
        if ( !$wgDebugRawPage && wfIsDebugRawPage() ) {
                return;
@@ -974,6 +974,7 @@ function wfDebug( $text, $dest = 'all' ) {
 
        $timer = wfDebugTimer();
        if ( $timer !== '' ) {
+               // Prepend elapsed request time and real memory usage to each line
                $text = preg_replace( '/[^\n]/', $timer . '\0', $text, 1 );
        }
 
@@ -981,13 +982,13 @@ function wfDebug( $text, $dest = 'all' ) {
                MWDebug::debugMsg( $text );
        }
 
-       if ( $wgDebugLogFile != '' ) {
-               # Strip unprintables; they can switch terminal modes when binary data
-               # gets dumped, which is pretty annoying.
-               $text = preg_replace( '![\x00-\x08\x0b\x0c\x0e-\x1f]!', ' ', $text );
-               $text = $wgDebugLogPrefix . $text;
-               wfErrorLog( $text, $wgDebugLogFile );
+       $ctx = array();
+       if ( $wgDebugLogPrefix !== '' ) {
+               $ctx['prefix'] = $wgDebugLogPrefix;
        }
+
+       $logger = MWLogger::getInstance( 'wfDebug' );
+       $logger->debug( rtrim( $text, "\n" ), $ctx );
 }
 
 /**
@@ -1068,8 +1069,6 @@ function wfDebugMem( $exact = false ) {
 function wfDebugLog( $logGroup, $text, $dest = 'all' ) {
        global $wgDebugLogGroups;
 
-       $text = trim( $text ) . "\n";
-
        // Turn $dest into a string if it's a boolean (for b/c)
        if ( $dest === true ) {
                $dest = 'all';
@@ -1077,34 +1076,16 @@ function wfDebugLog( $logGroup, $text, $dest = 'all' ) {
                $dest = 'private';
        }
 
-       if ( !isset( $wgDebugLogGroups[$logGroup] ) ) {
-               if ( $dest !== 'private' ) {
-                       wfDebug( "[$logGroup] $text", $dest );
-               }
-               return;
-       }
+       $text = trim( $text );
 
        if ( $dest === 'all' ) {
-               MWDebug::debugMsg( "[$logGroup] $text" );
-       }
-
-       $logConfig = $wgDebugLogGroups[$logGroup];
-       if ( $logConfig === false ) {
-               return;
-       }
-       if ( is_array( $logConfig ) ) {
-               if ( isset( $logConfig['sample'] ) && mt_rand( 1, $logConfig['sample'] ) !== 1 ) {
-                       return;
-               }
-               $destination = $logConfig['destination'];
-       } else {
-               $destination = strval( $logConfig );
+               MWDebug::debugMsg( "[{$logGroup}] {$text}\n" );
        }
 
-       $time = wfTimestamp( TS_DB );
-       $wiki = wfWikiID();
-       $host = wfHostname();
-       wfErrorLog( "$time $host $wiki: $text", $destination );
+       $logger = MWLogger::getInstance( $logGroup );
+       $logger->debug( $text, array(
+               'private' => ( $dest === 'private' ),
+       ) );
 }
 
 /**
@@ -1113,30 +1094,8 @@ function wfDebugLog( $logGroup, $text, $dest = 'all' ) {
  * @param string $text Database error message.
  */
 function wfLogDBError( $text ) {
-       global $wgDBerrorLog, $wgDBerrorLogTZ;
-       static $logDBErrorTimeZoneObject = null;
-
-       if ( $wgDBerrorLog ) {
-               $host = wfHostname();
-               $wiki = wfWikiID();
-
-               if ( $wgDBerrorLogTZ && !$logDBErrorTimeZoneObject ) {
-                       $logDBErrorTimeZoneObject = new DateTimeZone( $wgDBerrorLogTZ );
-               }
-
-               // Workaround for https://bugs.php.net/bug.php?id=52063
-               // Can be removed when min PHP > 5.3.2
-               if ( $logDBErrorTimeZoneObject === null ) {
-                       $d = date_create( "now" );
-               } else {
-                       $d = date_create( "now", $logDBErrorTimeZoneObject );
-               }
-
-               $date = $d->format( 'D M j G:i:s T Y' );
-
-               $text = "$date\t$host\t$wiki\t" . trim( $text ) . "\n";
-               wfErrorLog( $text, $wgDBerrorLog );
-       }
+       $logger = MWLogger::getInstance( 'wfLogDBError' );
+       $logger->error( trim( $text ) );
 }
 
 /**
@@ -1194,58 +1153,10 @@ function wfLogWarning( $msg, $callerOffset = 1, $level = E_USER_WARNING ) {
  * @throws MWException
  */
 function wfErrorLog( $text, $file ) {
-       if ( substr( $file, 0, 4 ) == 'udp:' ) {
-               # Needs the sockets extension
-               if ( preg_match( '!^(tcp|udp):(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $file, $m ) ) {
-                       // IPv6 bracketed host
-                       $host = $m[2];
-                       $port = intval( $m[3] );
-                       $prefix = isset( $m[4] ) ? $m[4] : false;
-                       $domain = AF_INET6;
-               } elseif ( preg_match( '!^(tcp|udp):(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $file, $m ) ) {
-                       $host = $m[2];
-                       if ( !IP::isIPv4( $host ) ) {
-                               $host = gethostbyname( $host );
-                       }
-                       $port = intval( $m[3] );
-                       $prefix = isset( $m[4] ) ? $m[4] : false;
-                       $domain = AF_INET;
-               } else {
-                       throw new MWException( __METHOD__ . ': Invalid UDP specification' );
-               }
-
-               // Clean it up for the multiplexer
-               if ( strval( $prefix ) !== '' ) {
-                       $text = preg_replace( '/^/m', $prefix . ' ', $text );
-
-                       // Limit to 64KB
-                       if ( strlen( $text ) > 65506 ) {
-                               $text = substr( $text, 0, 65506 );
-                       }
-
-                       if ( substr( $text, -1 ) != "\n" ) {
-                               $text .= "\n";
-                       }
-               } elseif ( strlen( $text ) > 65507 ) {
-                       $text = substr( $text, 0, 65507 );
-               }
-
-               $sock = socket_create( $domain, SOCK_DGRAM, SOL_UDP );
-               if ( !$sock ) {
-                       return;
-               }
-
-               socket_sendto( $sock, $text, strlen( $text ), 0, $host, $port );
-               socket_close( $sock );
-       } else {
-               wfSuppressWarnings();
-               $exists = file_exists( $file );
-               $size = $exists ? filesize( $file ) : false;
-               if ( !$exists || ( $size !== false && $size + strlen( $text ) < 0x7fffffff ) ) {
-                       file_put_contents( $file, $text, FILE_APPEND );
-               }
-               wfRestoreWarnings();
-       }
+       $logger = MWLogger::getInstance( 'wfErrorLog' );
+       $logger->info( trim( $text ), array(
+               'destination' => $file,
+       ) );
 }
 
 /**
@@ -3765,11 +3676,19 @@ function wfGetNull() {
 }
 
 /**
- * Modern version of wfWaitForSlaves(). Instead of looking at replication lag
- * and waiting for it to go down, this waits for the slaves to catch up to the
- * master position. Use this when updating very large numbers of rows, as
- * in maintenance scripts, to avoid causing too much lag.  Of course, this is
- * a no-op if there are no slaves.
+ * Waits for the slaves to catch up to the master position
+ *
+ * Use this when updating very large numbers of rows, as in maintenance scripts,
+ * to avoid causing too much lag. Of course, this is a no-op if there are no slaves.
+ *
+ * By default this waits on the main DB cluster of the current wiki.
+ * If $cluster is set to "*" it will wait on all DB clusters, including
+ * external ones. If the lag being waiting on is caused by the code that
+ * does this check, it makes since to use $ifWritesSince, particularly if
+ * cluster is "*", to avoid excess overhead.
+ *
+ * Never call this function after a big DB write that is still in a transaction.
+ * This only makes sense after the possible lag inducing changes were committed.
  *
  * @param float|null $ifWritesSince Only wait if writes were done since this UNIX timestamp
  * @param string|bool $wiki Wiki identifier accepted by wfGetLB
@@ -3787,31 +3706,49 @@ function wfWaitForSlaves(
                $timeout = ( PHP_SAPI === 'cli' ) ? 86400 : 10;
        }
 
-       if ( $cluster !== false ) {
-               $lb = wfGetLBFactory()->getExternalLB( $cluster );
+       // Figure out which clusters need to be checked
+       $lbs = array();
+       if ( $cluster === '*' ) {
+               wfGetLBFactory()->forEachLB( function ( LoadBalancer $lb ) use ( &$lbs ) {
+                       $lbs[] = $lb;
+               } );
+       } elseif ( $cluster !== false ) {
+               $lbs[] = wfGetLBFactory()->getExternalLB( $cluster );
        } else {
-               $lb = wfGetLB( $wiki );
+               $lbs[] = wfGetLB( $wiki );
+       }
+
+       // Get all the master positions of applicable DBs right now.
+       // This can be faster since waiting on one cluster reduces the
+       // time needed to wait on the next clusters.
+       $masterPositions = array_fill( 0, count( $lbs ), false );
+       foreach ( $lbs as $i => $lb ) {
+               // bug 27975 - Don't try to wait for slaves if there are none
+               // Prevents permission error when getting master position
+               if ( $lb->getServerCount() > 1 ) {
+                       if ( $ifWritesSince && !$lb->hasMasterConnection() ) {
+                               continue; // assume no writes done
+                       }
+                       // Use the empty string to not trigger selectDB() since the connection
+                       // may have been to a server that does not have a DB for the current wiki.
+                       $dbw = $lb->getConnection( DB_MASTER, array(), '' );
+                       if ( $ifWritesSince && $dbw->lastDoneWrites() < $ifWritesSince ) {
+                               continue; // no writes since the last wait
+                       }
+                       $masterPositions[$i] = $dbw->getMasterPos();
+               }
        }
 
-       // bug 27975 - Don't try to wait for slaves if there are none
-       // Prevents permission error when getting master position
-       if ( $lb->getServerCount() > 1 ) {
-               if ( $ifWritesSince && !$lb->hasMasterConnection() ) {
-                       return true; // assume no writes done
-               }
-               $dbw = $lb->getConnection( DB_MASTER, array(), $wiki );
-               if ( $ifWritesSince && $dbw->lastDoneWrites() < $ifWritesSince ) {
-                       return true; // no writes since the last wait
-               }
-               $pos = $dbw->getMasterPos();
-               // The DBMS may not support getMasterPos() or the whole
-               // load balancer might be fake (e.g. $wgAllDBsAreLocalhost).
-               if ( $pos !== false ) {
-                       return $lb->waitForAll( $pos, $timeout );
+       $ok = true;
+       foreach ( $lbs as $i => $lb ) {
+               if ( $masterPositions[$i] ) {
+                       // The DBMS may not support getMasterPos() or the whole
+                       // load balancer might be fake (e.g. $wgAllDBsAreLocalhost).
+                       $ok = $lb->waitForAll( $masterPositions[$i], $timeout ) && $ok;
                }
        }
 
-       return true;
+       return $ok;
 }
 
 /**