Clean up search results if scrolled of the end
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index cb5b7fd..b40b007 100644 (file)
@@ -35,16 +35,6 @@ if ( !defined( 'MEDIAWIKI' ) ) {
  * PHP extensions may be included here.
  */
 
-if ( !function_exists( 'iconv' ) ) {
-       /**
-        * @codeCoverageIgnore
-        * @return string
-        */
-       function iconv( $from, $to, $string ) {
-               return Fallback::iconv( $from, $to, $string );
-       }
-}
-
 if ( !function_exists( 'mb_substr' ) ) {
        /**
         * @codeCoverageIgnore
@@ -226,7 +216,7 @@ function wfAppendToArrayIfNotDefault( $key, $value, $default, &$changed ) {
  * @deprecated since 1.22; use array_replace()
  *
  * @param array $array1 Initial array to merge.
- * @param array [$array2,...] Variable list of arrays to merge.
+ * @param array $array2,... Variable list of arrays to merge.
  * @return array
  */
 function wfArrayMerge( $array1 /*...*/ ) {
@@ -256,7 +246,7 @@ function wfArrayMerge( $array1 /*...*/ ) {
  *             array( 'y' )
  *     )
  *
- * @param array [$array1,...]
+ * @param array $array1,...
  * @return array
  */
 function wfMergeErrorArrays( /*...*/ ) {
@@ -1189,7 +1179,7 @@ function wfDeprecated( $function, $version = false, $component = false, $callerO
  * Send a warning either to the debug log or in a PHP error depending on
  * $wgDevelopmentWarnings. To log warnings in production, use wfLogWarning() instead.
  *
- * @param string $msg message to send
+ * @param string $msg Message to send
  * @param int $callerOffset Number of items to go back in the backtrace to
  *        find the correct caller (1 = function calling wfWarn, ...)
  * @param int $level PHP error level; defaults to E_USER_NOTICE;
@@ -1448,7 +1438,7 @@ function wfGetLangObj( $langcode = false ) {
  * This function replaces all old wfMsg* functions.
  *
  * @param string $key Message key
- * @param mixed [$params,...] Normal message parameters
+ * @param mixed $params,... Normal message parameters
  * @return Message
  *
  * @since 1.17
@@ -1469,7 +1459,7 @@ function wfMessage( $key /*...*/ ) {
  * for the first message which is non-empty. If all messages are empty then an
  * instance of the first message key is returned.
  *
- * @param string|string[] [$keys,...] Message keys
+ * @param string|string[] $keys,... Message keys
  * @return Message
  *
  * @since 1.18
@@ -1489,7 +1479,7 @@ function wfMessageFallback( /*...*/ ) {
  *
  * @deprecated since 1.18
  *
- * @param string $key lookup key for the message, usually
+ * @param string $key Lookup key for the message, usually
  *    defined in languages/Language.php
  *
  * Parameters to the message, which can be used to insert variable text into
@@ -1674,7 +1664,7 @@ function wfMsgReplaceArgs( $message, $args ) {
  * @deprecated since 1.18
  *
  * @param string $key
- * @param string [$args,...] Parameters
+ * @param string $args,... Parameters
  * @return string
  */
 function wfMsgHtml( $key ) {
@@ -1695,7 +1685,7 @@ function wfMsgHtml( $key ) {
  * @deprecated since 1.18
  *
  * @param string $key
- * @param string [$args,...] Parameters
+ * @param string $args,... Parameters
  * @return string
  */
 function wfMsgWikiHtml( $key ) {
@@ -1883,7 +1873,7 @@ function wfReportTime() {
        $responseTime = round( ( microtime( true ) - $wgRequestTime ) * 1000 );
        $reportVars = array( 'wgBackendResponseTime' => $responseTime );
        if ( $wgShowHostnames ) {
-               $reportVars[ 'wgHostname' ] = wfHostname();
+               $reportVars['wgHostname'] = wfHostname();
        }
        return Skin::makeVariablesScript( $reportVars );
 }
@@ -2028,36 +2018,6 @@ function wfShowingResults( $offset, $limit ) {
        return wfMessage( 'showingresults' )->numParams( $limit, $offset + 1 )->parse();
 }
 
-/**
- * Generate (prev x| next x) (20|50|100...) type links for paging
- *
- * @param string $offset
- * @param int $limit
- * @param string $link
- * @param string $query Optional URL query parameter string
- * @param bool $atend Optional param for specified if this is the last page
- * @return string
- * @deprecated since 1.19; use Language::viewPrevNext() instead
- */
-function wfViewPrevNext( $offset, $limit, $link, $query = '', $atend = false ) {
-       wfDeprecated( __METHOD__, '1.19' );
-
-       global $wgLang;
-
-       $query = wfCgiToArray( $query );
-
-       if ( is_object( $link ) ) {
-               $title = $link;
-       } else {
-               $title = Title::newFromText( $link );
-               if ( is_null( $title ) ) {
-                       return false;
-               }
-       }
-
-       return $wgLang->viewPrevNext( $title, $offset, $limit, $query, $atend );
-}
-
 /**
  * @todo document
  * @todo FIXME: We may want to blacklist some broken browsers
@@ -2316,7 +2276,7 @@ function wfClearOutputBuffers() {
  * factors
  *
  * @param string $accept
- * @param string $def default
+ * @param string $def Default
  * @return float[] Associative array of string => float pairs
  */
 function wfAcceptToPrefs( $accept, $def = '*/*' ) {
@@ -2741,7 +2701,7 @@ 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 $args,...
  * @return string
  */
 function wfEscapeShellArg( /*...*/ ) {
@@ -2825,7 +2785,9 @@ function wfShellExecDisabled() {
  * Execute a shell command, with time and memory limits mirrored from the PHP
  * configuration if supported.
  *
- * @param string $cmd Command line, properly escaped for shell.
+ * @param string|string[] $cmd If string, a properly shell-escaped command line,
+ *   or an array of unescaped arguments, in which case each value will be escaped
+ *   Example:   [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
  * @param null|mixed &$retval Optional, will receive the program's exit code.
  *   (non-zero is usually failure). If there is an error from
  *   read, select, or proc_open(), this will be set to -1.
@@ -2874,6 +2836,15 @@ function wfShellExec( $cmd, &$retval = null, $environ = array(),
                        $envcmd .= "$k=" . escapeshellarg( $v ) . ' ';
                }
        }
+       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 = $envcmd . $cmd;
 
        $useLogPipe = false;
@@ -2950,19 +2921,28 @@ function wfShellExec( $cmd, &$retval = null, $environ = array(),
                $fds[(int)$pipe] = $fd;
        }
 
-       while ( true ) {
-               $status = proc_get_status( $proc );
-               if ( !$status['running'] ) {
-                       break;
+       $running = true;
+       $timeout = null;
+       $numReadyPipes = 0;
+
+       while ( $running === true || $numReadyPipes !== 0 ) {
+               if ( $running ) {
+                       $status = proc_get_status( $proc );
+                       // If the process has terminated, switch to nonblocking selects
+                       // for getting any data still waiting to be read.
+                       if ( !$status['running'] ) {
+                               $running = false;
+                               $timeout = 0;
+                       }
                }
-               $status = false;
 
                $readyPipes = $pipes;
 
                // Clear last error
                // @codingStandardsIgnoreStart Generic.PHP.NoSilencedErrors.Discouraged
                @trigger_error( '' );
-               if ( @stream_select( $readyPipes, $emptyArray, $emptyArray, null ) === false ) {
+               $numReadyPipes = @stream_select( $readyPipes, $emptyArray, $emptyArray, $timeout );
+               if ( $numReadyPipes === false ) {
                        // @codingStandardsIgnoreEnd
                        $error = error_get_last();
                        if ( strncmp( $error['message'], $eintrMessage, strlen( $eintrMessage ) ) == 0 ) {
@@ -3010,7 +2990,7 @@ function wfShellExec( $cmd, &$retval = null, $environ = array(),
 
        // Use the status previously collected if possible, since proc_get_status()
        // just calls waitpid() which will not return anything useful the second time.
-       if ( $status === false ) {
+       if ( $running ) {
                $status = proc_get_status( $proc );
        }
 
@@ -3051,7 +3031,7 @@ function wfShellExec( $cmd, &$retval = null, $environ = array(),
  * @param string $cmd Command line, properly escaped for shell.
  * @param null|mixed &$retval Optional, will receive the program's exit code.
  *   (non-zero is usually failure)
- * @param array $environ optional environment variables which should be
+ * @param array $environ Optional environment variables which should be
  *   added to the executed command environment.
  * @param array $limits Optional array with limits(filesize, memory, time, walltime)
  *   this overwrites the global wgMaxShell* limits.
@@ -3499,7 +3479,7 @@ function wfBaseConvert( $input, $sourceBase, $destBase, $pad = 1,
 /**
  * Check if there is sufficient entropy in php's built-in session generation
  *
- * @return bool true = there is sufficient entropy
+ * @return bool True = there is sufficient entropy
  */
 function wfCheckEntropy() {
        return (
@@ -3605,7 +3585,7 @@ function wfGetPrecompiledData( $name ) {
 /**
  * Get a cache key
  *
- * @param string [$args,...]
+ * @param string $args,...
  * @return string
  */
 function wfMemcKey( /*...*/ ) {
@@ -3622,7 +3602,7 @@ function wfMemcKey( /*...*/ ) {
  *
  * @param string $db
  * @param string $prefix
- * @param string [$args,...]
+ * @param string $args,...
  * @return string
  */
 function wfForeignMemcKey( $db, $prefix /*...*/ ) {
@@ -3694,7 +3674,7 @@ function &wfGetDB( $db, $groups = array(), $wiki = false ) {
 /**
  * Get a load balancer object.
  *
- * @param string|bool $wiki wiki ID, or false for the current wiki
+ * @param string|bool $wiki Wiki ID, or false for the current wiki
  * @return LoadBalancer
  */
 function wfGetLB( $wiki = false ) {
@@ -3714,7 +3694,7 @@ function &wfGetLBFactory() {
  * Find a file.
  * Shortcut for RepoGroup::singleton()->findFile()
  *
- * @param string $title or Title object
+ * @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
@@ -3781,7 +3761,7 @@ function wfScript( $script = 'index' ) {
 /**
  * Get the script URL.
  *
- * @return string script URL
+ * @return string Script URL
  */
 function wfGetScriptUrl() {
        if ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
@@ -3828,11 +3808,14 @@ function wfGetNull() {
  * in maintenance scripts, to avoid causing too much lag.  Of course, this is
  * a no-op if there are no slaves.
  *
- * @param int|bool $maxLag (deprecated)
+ * @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.
  */
-function wfWaitForSlaves( $maxLag = false, $wiki = false, $cluster = false ) {
+function wfWaitForSlaves( $ifWritesSince = false, $wiki = false, $cluster = false ) {
+       // B/C: first argument used to be "max seconds of lag"; ignore such values
+       $ifWritesSince = ( $ifWritesSince > 1e9 ) ? $ifWritesSince : false;
+
        if ( $cluster !== false ) {
                $lb = wfGetLBFactory()->getExternalLB( $cluster );
        } else {
@@ -3842,7 +3825,13 @@ function wfWaitForSlaves( $maxLag = false, $wiki = false, $cluster = false ) {
        // 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; // assume no writes done
+               }
                $dbw = $lb->getConnection( DB_MASTER, array(), $wiki );
+               if ( $ifWritesSince && $dbw->lastDoneWrites() < $ifWritesSince ) {
+                       return; // 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).
@@ -4053,7 +4042,7 @@ function wfRunHooks( $event, array $args = array(), $deprecatedVersion = null )
  * Also be careful when using this function to read unsigned 32 bit integer
  * because php might make it negative.
  *
- * @throws MWException if $data not long enough, or if unpack fails
+ * @throws MWException If $data not long enough, or if unpack fails
  * @return array Associative array of the extracted data
  */
 function wfUnpack( $format, $data, $length = false ) {