Merge "Add tests for HttpError"
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 9e81a3c..8b3b959 100644 (file)
@@ -170,13 +170,13 @@ if ( !function_exists( 'hash_equals' ) ) {
  * This queues an extension to be loaded through
  * the ExtensionRegistry system.
  *
- * @param string $name Name of the extension to load
+ * @param string $ext Name of the extension to load
  * @param string|null $path Absolute path of where to find the extension.json file
  */
-function wfLoadExtension( $name, $path = null ) {
+function wfLoadExtension( $ext, $path = null ) {
        if ( !$path ) {
-               global $IP;
-               $path = "$IP/extensions/$name/extension.json";
+               global $wgExtensionDirectory;
+               $path = "$wgExtensionDirectory/$ext/extension.json";
        }
        ExtensionRegistry::getInstance()->queue( $path );
 }
@@ -194,10 +194,10 @@ function wfLoadExtension( $name, $path = null ) {
  * @param string[] $exts Array of extension names to load
  */
 function wfLoadExtensions( array $exts ) {
-       global $IP;
+       global $wgExtensionDirectory;
        $registry = ExtensionRegistry::getInstance();
        foreach ( $exts as $ext ) {
-               $registry->queue( "$IP/extensions/$ext/extension.json" );
+               $registry->queue( "$wgExtensionDirectory/$ext/extension.json" );
        }
 }
 
@@ -205,13 +205,13 @@ function wfLoadExtensions( array $exts ) {
  * Load a skin
  *
  * @see wfLoadExtension
- * @param string $name Name of the extension to load
+ * @param string $skin Name of the extension to load
  * @param string|null $path Absolute path of where to find the skin.json file
  */
-function wfLoadSkin( $name, $path = null ) {
+function wfLoadSkin( $skin, $path = null ) {
        if ( !$path ) {
-               global $IP;
-               $path = "$IP/skins/$name/skin.json";
+               global $wgStyleDirectory;
+               $path = "$wgStyleDirectory/$skin/skin.json";
        }
        ExtensionRegistry::getInstance()->queue( $path );
 }
@@ -223,10 +223,10 @@ function wfLoadSkin( $name, $path = null ) {
  * @param string[] $skins Array of extension names to load
  */
 function wfLoadSkins( array $skins ) {
-       global $IP;
+       global $wgStyleDirectory;
        $registry = ExtensionRegistry::getInstance();
        foreach ( $skins as $skin ) {
-               $registry->queue( "$IP/skins/$skin/skin.json" );
+               $registry->queue( "$wgStyleDirectory/$skin/skin.json" );
        }
 }
 
@@ -1346,9 +1346,14 @@ function wfReadOnlyReason() {
                }
                // Callers use this method to be aware that data presented to a user
                // may be very stale and thus allowing submissions can be problematic.
-               if ( $wgReadOnly === false && wfGetLB()->getLaggedSlaveMode() ) {
+               try {
+                       if ( $wgReadOnly === false && wfGetLB()->getLaggedSlaveMode() ) {
+                               $wgReadOnly = 'The database has been automatically locked ' .
+                                       'while the slave database servers catch up to the master';
+                       }
+               } catch ( DBConnectionError $e ) {
                        $wgReadOnly = 'The database has been automatically locked ' .
-                               'while the slave database servers catch up to the master';
+                               'until the slave database servers become available';
                }
        }
 
@@ -2459,7 +2464,7 @@ function wfTimestampNow() {
 function wfIsWindows() {
        static $isWindows = null;
        if ( $isWindows === null ) {
-               $isWindows = substr( php_uname(), 0, 7 ) == 'Windows';
+               $isWindows = strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN';
        }
        return $isWindows;
 }
@@ -2521,7 +2526,7 @@ function wfMkdirParents( $dir, $mode = null, $caller = null ) {
                wfDebug( "$caller: called wfMkdirParents($dir)\n" );
        }
 
-       if ( strval( $dir ) === '' || ( file_exists( $dir ) && is_dir( $dir ) ) ) {
+       if ( strval( $dir ) === '' || is_dir( $dir ) ) {
                return true;
        }
 
@@ -3348,7 +3353,7 @@ function wfBaseConvert( $input, $sourceBase, $destBase, $pad = 1,
                // Removing leading zeros works around broken base detection code in
                // some PHP versions (see <https://bugs.php.net/bug.php?id=50175> and
                // <https://bugs.php.net/bug.php?id=55398>).
-               $result = gmp_strval( gmp_init( ltrim( $input, '0' ), $sourceBase ), $destBase );
+               $result = gmp_strval( gmp_init( ltrim( $input, '0' ) ?: '0', $sourceBase ), $destBase );
        } elseif ( extension_loaded( 'bcmath' ) && ( $engine == 'auto' || $engine == 'bcmath' ) ) {
                $decimal = '0';
                foreach ( str_split( strtolower( $input ) ) as $char ) {
@@ -3750,6 +3755,7 @@ function wfWaitForSlaves(
        }
 
        // Figure out which clusters need to be checked
+       /** @var LoadBalancer[] $lbs */
        $lbs = array();
        if ( $cluster === '*' ) {
                wfGetLBFactory()->forEachLB( function ( LoadBalancer $lb ) use ( &$lbs ) {
@@ -3766,20 +3772,14 @@ function wfWaitForSlaves(
        // 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();
+               if ( $lb->getServerCount() <= 1 ) {
+                       // Bug 27975 - Don't try to wait for slaves if there are none
+                       // Prevents permission error when getting master position
+                       continue;
+               } elseif ( $ifWritesSince && $lb->lastMasterChangeTimestamp() < $ifWritesSince ) {
+                       continue; // no writes since the last wait
                }
+               $masterPositions[$i] = $lb->getMasterPos();
        }
 
        $ok = true;