Merge "Simplify HTMLTitleTextField::validate"
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 3ea020f..336cb89 100644 (file)
@@ -30,7 +30,6 @@ use MediaWiki\Session\SessionManager;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Shell\Shell;
 use Wikimedia\ScopedCallback;
-use Wikimedia\Rdbms\DBReplicationWaitError;
 use Wikimedia\WrappedString;
 
 /**
@@ -141,7 +140,7 @@ function wfArrayDiff2_cmp( $a, $b ) {
 }
 
 /**
- * Like array_filter with ARRAY_FILTER_USE_BOTH, but works pre-5.6.
+ * @deprecated since 1.32, use array_filter() with ARRAY_FILTER_USE_BOTH directly
  *
  * @param array $arr
  * @param callable $callback Will be called with the array value and key (in that order) and
@@ -149,17 +148,11 @@ function wfArrayDiff2_cmp( $a, $b ) {
  * @return array
  */
 function wfArrayFilter( array $arr, callable $callback ) {
-       if ( defined( 'ARRAY_FILTER_USE_BOTH' ) ) {
-               return array_filter( $arr, $callback, ARRAY_FILTER_USE_BOTH );
-       }
-       $filteredKeys = array_filter( array_keys( $arr ), function ( $key ) use ( $arr, $callback ) {
-               return call_user_func( $callback, $arr[$key], $key );
-       } );
-       return array_intersect_key( $arr, array_fill_keys( $filteredKeys, true ) );
+       return array_filter( $arr, $callback, ARRAY_FILTER_USE_BOTH );
 }
 
 /**
- * Like array_filter with ARRAY_FILTER_USE_KEY, but works pre-5.6.
+ * @deprecated since 1.32, use array_filter() with ARRAY_FILTER_USE_KEY directly
  *
  * @param array $arr
  * @param callable $callback Will be called with the array key and should return a bool which
@@ -167,9 +160,7 @@ function wfArrayFilter( array $arr, callable $callback ) {
  * @return array
  */
 function wfArrayFilterByKey( array $arr, callable $callback ) {
-       return wfArrayFilter( $arr, function ( $val, $key ) use ( $callback ) {
-               return call_user_func( $callback, $key );
-       } );
+       return array_filter( $arr, $callback, ARRAY_FILTER_USE_KEY );
 }
 
 /**
@@ -556,17 +547,24 @@ function wfExpandUrl( $url, $defaultProto = PROTO_CURRENT ) {
        } elseif ( substr( $url, 0, 1 ) == '/' ) {
                // If $serverUrl is protocol-relative, prepend $defaultProtoWithoutSlashes,
                // otherwise leave it alone.
-               $url = ( $serverHasProto ? '' : $defaultProtoWithoutSlashes ) . $serverUrl . $url;
+               if ( $serverHasProto ) {
+                       $url = $serverUrl . $url;
+               } else {
+                       // If an HTTPS URL is synthesized from a protocol-relative $wgServer, allow the
+                       // user to override the port number (T67184)
+                       if ( $defaultProto === PROTO_HTTPS && $wgHttpsPort != 443 ) {
+                               if ( isset( $bits['port'] ) ) {
+                                       throw new Exception( 'A protocol-relative $wgServer may not contain a port number' );
+                               }
+                               $url = $defaultProtoWithoutSlashes . $serverUrl . ':' . $wgHttpsPort . $url;
+                       } else {
+                               $url = $defaultProtoWithoutSlashes . $serverUrl . $url;
+                       }
+               }
        }
 
        $bits = wfParseUrl( $url );
 
-       // ensure proper port for HTTPS arrives in URL
-       // https://phabricator.wikimedia.org/T67184
-       if ( $defaultProto === PROTO_HTTPS && $wgHttpsPort != 443 ) {
-               $bits['port'] = $wgHttpsPort;
-       }
-
        if ( $bits && isset( $bits['path'] ) ) {
                $bits['path'] = wfRemoveDotSegments( $bits['path'] );
                return wfAssembleUrl( $bits );
@@ -582,6 +580,19 @@ function wfExpandUrl( $url, $defaultProto = PROTO_CURRENT ) {
        return false;
 }
 
+/**
+ * Get the wiki's "server", i.e. the protocol and host part of the URL, with a
+ * protocol specified using a PROTO_* constant as in wfExpandUrl()
+ *
+ * @since 1.32
+ * @param string|int|null $proto One of the PROTO_* constants.
+ * @return string The URL
+ */
+function wfGetServerUrl( $proto ) {
+       $url = wfExpandUrl( '/', $proto );
+       return substr( $url, 0, -1 );
+}
+
 /**
  * This function will reassemble a URL parsed with wfParseURL.  This is useful
  * if you need to edit part of a URL and put it back together.
@@ -874,20 +885,13 @@ function wfParseUrl( $url ) {
 function wfExpandIRI( $url ) {
        return preg_replace_callback(
                '/((?:%[89A-F][0-9A-F])+)/i',
-               'wfExpandIRI_callback',
+               function ( array $matches ) {
+                       return urldecode( $matches[1] );
+               },
                wfExpandUrl( $url )
        );
 }
 
-/**
- * Private callback for wfExpandIRI
- * @param array $matches
- * @return string
- */
-function wfExpandIRI_callback( $matches ) {
-       return urldecode( $matches[1] );
-}
-
 /**
  * Make URL indexes, appropriate for the el_index field of externallinks.
  *
@@ -1303,11 +1307,11 @@ function wfGetLangObj( $langcode = false ) {
                return $langcode;
        }
 
-       global $wgContLang, $wgLanguageCode;
+       global $wgLanguageCode;
        if ( $langcode === true || $langcode === $wgLanguageCode ) {
                # $langcode is the language code of the wikis content language object.
                # or it is a boolean and value is true
-               return $wgContLang;
+               return MediaWikiServices::getInstance()->getContentLanguage();
        }
 
        global $wgLang;
@@ -1325,7 +1329,7 @@ function wfGetLangObj( $langcode = false ) {
 
        # $langcode is a string, but not a valid language code; use content language.
        wfDebug( "Invalid language code passed to wfGetLangObj, falling back to content language.\n" );
-       return $wgContLang;
+       return MediaWikiServices::getInstance()->getContentLanguage();
 }
 
 /**
@@ -2499,36 +2503,6 @@ function wfUsePHP( $req_ver ) {
        }
 }
 
-/**
- * This function works like "use VERSION" in Perl except it checks the version
- * of MediaWiki, the program will die with a backtrace if the current version
- * of MediaWiki is less than the version provided.
- *
- * This is useful for extensions which due to their nature are not kept in sync
- * with releases
- *
- * Note: Due to the behavior of PHP's version_compare() which is used in this
- * function, if you want to allow the 'wmf' development versions add a 'c' (or
- * any single letter other than 'a', 'b' or 'p') as a post-fix to your
- * targeted version number. For example if you wanted to allow any variation
- * of 1.22 use `wfUseMW( '1.22c' )`. Using an 'a' or 'b' instead of 'c' will
- * not result in the same comparison due to the internal logic of
- * version_compare().
- *
- * @see perldoc -f use
- *
- * @deprecated since 1.26, use the "requires" property of extension.json
- * @param string|int|float $req_ver The version to check, can be a string, an integer, or a float
- * @throws MWException
- */
-function wfUseMW( $req_ver ) {
-       global $wgVersion;
-
-       if ( version_compare( $wgVersion, (string)$req_ver, '<' ) ) {
-               throw new MWException( "MediaWiki $req_ver required--this is only $wgVersion" );
-       }
-}
-
 /**
  * Return the final portion of a pathname.
  * Reimplemented because PHP5's "basename()" is buggy with multibyte text.
@@ -2667,28 +2641,6 @@ function wfGetPrecompiledData( $name ) {
        return false;
 }
 
-/**
- * @since 1.32
- * @param string[] $data Array with string keys/values to export
- * @param string $header
- * @return string PHP code
- */
-function wfMakeStaticArrayFile( array $data, $header = 'Automatically generated' ) {
-       $format = "\t%s => %s,\n";
-       $code = "<?php\n"
-               . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n"
-               . "return [\n";
-       foreach ( $data as $key => $value ) {
-               $code .= sprintf(
-                       $format,
-                       var_export( $key, true ),
-                       var_export( $value, true )
-               );
-       }
-       $code .= "];\n";
-       return $code;
-}
-
 /**
  * Make a cache key for the local wiki.
  *
@@ -2937,17 +2889,13 @@ function wfGetNull() {
  * @param float|null $ifWritesSince Only wait if writes were done since this UNIX timestamp
  * @param string|bool $wiki Wiki identifier accepted by wfGetLB
  * @param string|bool $cluster Cluster name accepted by LBFactory. Default: false.
- * @param int|null $timeout Max wait time. Default: 1 day (cli), ~10 seconds (web)
+ * @param int|null $timeout Max wait time. Default: 60 seconds (cli), 1 second (web)
  * @return bool Success (able to connect and no timeouts reached)
  * @deprecated since 1.27 Use LBFactory::waitForReplication
  */
 function wfWaitForSlaves(
        $ifWritesSince = null, $wiki = false, $cluster = false, $timeout = null
 ) {
-       if ( $timeout === null ) {
-               $timeout = wfIsCLI() ? 60 : 10;
-       }
-
        if ( $cluster === '*' ) {
                $cluster = false;
                $wiki = false;
@@ -2955,20 +2903,18 @@ function wfWaitForSlaves(
                $wiki = wfWikiID();
        }
 
-       try {
-               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
-               $lbFactory->waitForReplication( [
-                       'wiki' => $wiki,
-                       'cluster' => $cluster,
-                       'timeout' => $timeout,
-                       // B/C: first argument used to be "max seconds of lag"; ignore such values
-                       'ifWritesSince' => ( $ifWritesSince > 1e9 ) ? $ifWritesSince : null
-               ] );
-       } catch ( DBReplicationWaitError $e ) {
-               return false;
+       $opts = [
+               'wiki' => $wiki,
+               'cluster' => $cluster,
+               // B/C: first argument used to be "max seconds of lag"; ignore such values
+               'ifWritesSince' => ( $ifWritesSince > 1e9 ) ? $ifWritesSince : null
+       ];
+       if ( $timeout !== null ) {
+               $opts['timeout'] = $timeout;
        }
 
-       return true;
+       $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+       return $lbFactory->waitForReplication( $opts );
 }
 
 /**
@@ -3112,6 +3058,7 @@ function wfBCP47( $code ) {
 /**
  * Get a specific cache object.
  *
+ * @deprecated since 1.32, use ObjectCache::getInstance() instead
  * @param int|string $cacheType A CACHE_* constants, or other key in $wgObjectCaches
  * @return BagOStuff
  */
@@ -3122,11 +3069,11 @@ function wfGetCache( $cacheType ) {
 /**
  * Get the main cache object
  *
+ * @deprecated since 1.32, use ObjectCache::getLocalClusterInstance() instead
  * @return BagOStuff
  */
 function wfGetMainCache() {
-       global $wgMainCacheType;
-       return ObjectCache::getInstance( $wgMainCacheType );
+       return ObjectCache::getLocalClusterInstance();
 }
 
 /**