Remove duplicate param escaping code
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 2025e17..dfced1c 100644 (file)
@@ -2660,13 +2660,19 @@ function wfIniGetBool( $setting ) {
  * Also fixes the locale problems on Linux in PHP 5.2.6+ (bug backported to
  * earlier distro releases of PHP)
  *
- * @param string $args,...
+ * @param string ... strings to escape and glue together, or a single array of strings parameter
  * @return string
  */
 function wfEscapeShellArg( /*...*/ ) {
        wfInitShellLocale();
 
        $args = func_get_args();
+       if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
+               // If only one argument has been passed, and that argument is an array,
+               // treat it as a list of arguments
+               $args = reset( $args );
+       }
+
        $first = true;
        $retVal = '';
        foreach ( $args as $arg ) {
@@ -2757,6 +2763,8 @@ function wfShellExecDisabled() {
  * @param array $options Array of options:
  *   - duplicateStderr: Set this to true to duplicate stderr to stdout,
  *     including errors from limit.sh
+ *   - profileMethod: By default this function will profile based on the calling
+ *     method. Set this to a string for an alternative method to profile from
  *
  * @return string Collected stdout as a string
  */
@@ -2775,6 +2783,7 @@ function wfShellExec( $cmd, &$retval = null, $environ = array(),
        }
 
        $includeStderr = isset( $options['duplicateStderr'] ) && $options['duplicateStderr'];
+       $profileMethod = isset( $options['profileMethod'] ) ? $options['profileMethod'] : wfGetCaller();
 
        wfInitShellLocale();
 
@@ -2796,12 +2805,7 @@ function wfShellExec( $cmd, &$retval = null, $environ = array(),
                }
        }
        if ( is_array( $cmd ) ) {
-               // Command line may be given as an array, escape each value and glue them together with a space
-               $cmdVals = array();
-               foreach ( $cmd as $val ) {
-                       $cmdVals[] = wfEscapeShellArg( $val );
-               }
-               $cmd = implode( ' ', $cmdVals );
+               $cmd = wfEscapeShellArg( $cmd );
        }
 
        $cmd = $envcmd . $cmd;
@@ -2848,6 +2852,7 @@ function wfShellExec( $cmd, &$retval = null, $environ = array(),
                $desc[3] = array( 'pipe', 'w' );
        }
        $pipes = null;
+       $scoped = Profiler::instance()->scopedProfileIn( __FUNCTION__ . '-' . $profileMethod );
        $proc = proc_open( $cmd, $desc, $pipes );
        if ( !$proc ) {
                wfDebugLog( 'exec', "proc_open() failed: $cmd" );
@@ -2999,7 +3004,8 @@ function wfShellExec( $cmd, &$retval = null, $environ = array(),
  * @return string Collected stdout and stderr as a string
  */
 function wfShellExecWithStderr( $cmd, &$retval = null, $environ = array(), $limits = array() ) {
-       return wfShellExec( $cmd, $retval, $environ, $limits, array( 'duplicateStderr' => true ) );
+       return wfShellExec( $cmd, $retval, $environ, $limits,
+               array( 'duplicateStderr' => true, 'profileMethod' => wfGetCaller() ) );
 }
 
 /**
@@ -3019,15 +3025,6 @@ function wfInitShellLocale() {
        }
 }
 
-/**
- * Alias to wfShellWikiCmd()
- *
- * @see wfShellWikiCmd()
- */
-function wfShellMaintenanceCmd( $script, array $parameters = array(), array $options = array() ) {
-       return wfShellWikiCmd( $script, $parameters, $options );
-}
-
 /**
  * Generate a shell-escaped command line string to run a MediaWiki cli script.
  * Note that $parameters should be a flat array and an option with an argument
@@ -3051,7 +3048,7 @@ function wfShellWikiCmd( $script, array $parameters = array(), array $options =
        }
        $cmd[] = $script;
        // Escape each parameter for shell
-       return implode( " ", array_map( 'wfEscapeShellArg', array_merge( $cmd, $parameters ) ) );
+       return wfEscapeShellArg( array_merge( $cmd, $parameters ) );
 }
 
 /**
@@ -3096,10 +3093,7 @@ function wfMerge( $old, $mine, $yours, &$result ) {
        fclose( $yourtextFile );
 
        # Check for a conflict
-       $cmd = wfEscapeShellArg( $wgDiff3 ) . ' -a --overlap-only ' .
-               wfEscapeShellArg( $mytextName ) . ' ' .
-               wfEscapeShellArg( $oldtextName ) . ' ' .
-               wfEscapeShellArg( $yourtextName );
+       $cmd = wfEscapeShellArg( $wgDiff3, '-a', '--overlap-only', $mytextName, $oldtextName, $yourtextName );
        $handle = popen( $cmd, 'r' );
 
        if ( fgets( $handle, 1024 ) ) {
@@ -3110,8 +3104,7 @@ function wfMerge( $old, $mine, $yours, &$result ) {
        pclose( $handle );
 
        # Merge differences
-       $cmd = wfEscapeShellArg( $wgDiff3 ) . ' -a -e --merge ' .
-               wfEscapeShellArg( $mytextName, $oldtextName, $yourtextName );
+       $cmd = wfEscapeShellArg( $wgDiff3, '-a', '-e', '--merge', $mytextName, $oldtextName, $yourtextName );
        $handle = popen( $cmd, 'r' );
        $result = '';
        do {
@@ -3659,19 +3652,7 @@ function wfGetLBFactory() {
  * Shortcut for RepoGroup::singleton()->findFile()
  *
  * @param string $title String or Title object
- * @param array $options Associative array of options:
- *     time:           requested time for an archived image, or false for the
- *                     current version. An image object will be returned which was
- *                     created at the specified time.
- *
- *     ignoreRedirect: If true, do not follow file redirects
- *
- *     private:        If true, return restricted (deleted) files if the current
- *                     user is allowed to view them. Otherwise, such files will not
- *                     be found.
- *
- *     bypassCache:    If true, do not use the process-local cache of File objects
- *
+ * @param array $options Associative array of options (see RepoGroup::findFile)
  * @return File|bool File, or false if the file does not exist
  */
 function wfFindFile( $title, $options = array() ) {
@@ -4008,16 +3989,6 @@ function wfGetParserCacheStorage() {
        return ObjectCache::getInstance( $wgParserCacheType );
 }
 
-/**
- * Get the cache object used by the language converter
- *
- * @return BagOStuff
- */
-function wfGetLangConverterCacheStorage() {
-       global $wgLanguageConverterCacheType;
-       return ObjectCache::getInstance( $wgLanguageConverterCacheType );
-}
-
 /**
  * Call hook functions defined in $wgHooks
  *
@@ -4198,3 +4169,91 @@ function wfIsConfiguredProxy( $ip ) {
        wfDeprecated( __METHOD__, '1.24' );
        return IP::isConfiguredProxy( $ip );
 }
+
+/**
+ * Returns true if these thumbnail parameters match one that MediaWiki
+ * requests from file description pages and/or parser output.
+ *
+ * $params is considered non-standard if they involve a non-standard
+ * width or any non-default parameters aside from width and page number.
+ * The number of possible files with standard parameters is far less than
+ * that of all combinations; rate-limiting for them can thus be more generious.
+ *
+ * @param File $file
+ * @param array $params
+ * @return bool
+ * @since 1.24 Moved from thumb.php to GlobalFunctions in 1.25
+ */
+function wfThumbIsStandard( File $file, array $params ) {
+       global $wgThumbLimits, $wgImageLimits, $wgResponsiveImages;
+
+       $multipliers = array( 1 );
+       if ( $wgResponsiveImages ) {
+               // These available sizes are hardcoded currently elsewhere in MediaWiki.
+               // @see Linker::processResponsiveImages
+               $multipliers[] = 1.5;
+               $multipliers[] = 2;
+       }
+
+       $handler = $file->getHandler();
+       if ( !$handler || !isset( $params['width'] ) ) {
+               return false;
+       }
+
+       $basicParams = array();
+       if ( isset( $params['page'] ) ) {
+               $basicParams['page'] = $params['page'];
+       }
+
+       $thumbLimits = array();
+       $imageLimits = array();
+       // Expand limits to account for multipliers
+       foreach ( $multipliers as $multiplier ) {
+               $thumbLimits = array_merge( $thumbLimits, array_map(
+                       function ( $width ) use ( $multiplier ) {
+                               return round( $width * $multiplier );
+                       }, $wgThumbLimits )
+               );
+               $imageLimits = array_merge( $imageLimits, array_map(
+                       function ( $pair ) use ( $multiplier ) {
+                               return array(
+                                       round( $pair[0] * $multiplier ),
+                                       round( $pair[1] * $multiplier ),
+                               );
+                       }, $wgImageLimits )
+               );
+       }
+
+       // Check if the width matches one of $wgThumbLimits
+       if ( in_array( $params['width'], $thumbLimits ) ) {
+               $normalParams = $basicParams + array( 'width' => $params['width'] );
+               // Append any default values to the map (e.g. "lossy", "lossless", ...)
+               $handler->normaliseParams( $file, $normalParams );
+       } else {
+               // If not, then check if the width matchs one of $wgImageLimits
+               $match = false;
+               foreach ( $imageLimits as $pair ) {
+                       $normalParams = $basicParams + array( 'width' => $pair[0], 'height' => $pair[1] );
+                       // Decide whether the thumbnail should be scaled on width or height.
+                       // Also append any default values to the map (e.g. "lossy", "lossless", ...)
+                       $handler->normaliseParams( $file, $normalParams );
+                       // Check if this standard thumbnail size maps to the given width
+                       if ( $normalParams['width'] == $params['width'] ) {
+                               $match = true;
+                               break;
+                       }
+               }
+               if ( !$match ) {
+                       return false; // not standard for description pages
+               }
+       }
+
+       // Check that the given values for non-page, non-width, params are just defaults
+       foreach ( $params as $key => $value ) {
+               if ( !isset( $normalParams[$key] ) || $normalParams[$key] != $value ) {
+                       return false;
+               }
+       }
+
+       return true;
+}