Per siebrand and Nikerabbit (tweak for r47535):
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index f47a41e..c9a7693 100644 (file)
@@ -9,6 +9,7 @@ if ( !defined( 'MEDIAWIKI' ) ) {
  */
 
 require_once dirname(__FILE__) . '/normal/UtfNormalUtil.php';
+require_once dirname(__FILE__) . '/XmlFunctions.php';
 
 // Hide compatibility functions from Doxygen
 /// @cond
@@ -88,6 +89,19 @@ if ( !function_exists( 'array_diff_key' ) ) {
        }
 }
 
+// Support for Wietse Venema's taint feature
+if ( !function_exists( 'istainted' ) ) {
+       function istainted( $var ) {
+               return 0;
+       }
+       function taint( $var, $level = 0 ) {}
+       function untaint( $var, $level = 0 ) {}
+       define( 'TC_HTML', 1 );
+       define( 'TC_SHELL', 1 );
+       define( 'TC_MYSQL', 1 );
+       define( 'TC_PCRE', 1 );
+       define( 'TC_SELF', 1 );
+}
 /// @endcond
 
 
@@ -430,7 +444,7 @@ function wfGetLangObj( $langcode = false ){
                return Language::factory( $langcode );
 
        # $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.' );
+       wfDebug( "Invalid language code passed to wfGetLangObj, falling back to content language.\n" );
        return $wgContLang;
 }
 
@@ -748,6 +762,46 @@ function wfMsgExt( $key, $options ) {
        return $string;
 }
 
+
+/**
+ * Just like exit() but makes a note of it.
+ * Commits open transactions except if the error parameter is set
+ *
+ * @deprecated Please return control to the caller or throw an exception
+ */
+function wfAbruptExit( $error = false ){
+       static $called = false;
+       if ( $called ){
+               exit( -1 );
+       }
+       $called = true;
+
+       $bt = wfDebugBacktrace();
+       if( $bt ) {
+               for($i = 0; $i < count($bt) ; $i++){
+                       $file = isset($bt[$i]['file']) ? $bt[$i]['file'] : "unknown";
+                       $line = isset($bt[$i]['line']) ? $bt[$i]['line'] : "unknown";
+                       wfDebug("WARNING: Abrupt exit in $file at line $line\n");
+               }
+       } else {
+               wfDebug("WARNING: Abrupt exit\n");
+       }
+
+       wfLogProfilingData();
+
+       if ( !$error ) {
+               wfGetLB()->closeAll();
+       }
+       exit( -1 );
+}
+
+/**
+ * @deprecated Please return control the caller or throw an exception
+ */
+function wfErrorExit() {
+       wfAbruptExit( true );
+}
+
 /**
  * Print a simple message and die, returning nonzero to the shell if any.
  * Plain die() fails to return nonzero to the shell if you pass a string.
@@ -796,19 +850,21 @@ function wfHostname() {
        return $host;
 }
 
-/**
- * Returns a HTML comment with the elapsed time since request.
- * This method has no side effects.
- * @return string
- */
-function wfReportTime() {
-       global $wgRequestTime, $wgShowHostnames;
-       $now = wfTime();
-       $elapsed = $now - $wgRequestTime;
-       return $wgShowHostnames
-               ? sprintf( "<!-- Served by %s in %01.3f secs. -->", wfHostname(), $elapsed )
-               : sprintf( "<!-- Served in %01.3f secs. -->", $elapsed );
-}
+       /**
+        * Returns a HTML comment with the elapsed time since request.
+        * This method has no side effects.
+        * @return string
+        */
+       function wfReportTime() {
+               global $wgRequestTime, $wgShowHostnames;
+
+               $now = wfTime();
+               $elapsed = $now - $wgRequestTime;
+
+               return $wgShowHostnames
+                       ? sprintf( "<!-- Served by %s in %01.3f secs. -->", wfHostname(), $elapsed )
+                       : sprintf( "<!-- Served in %01.3f secs. -->", $elapsed );
+       }
 
 /**
  * Safety wrapper for debug_backtrace().
@@ -930,11 +986,11 @@ function wfViewPrevNext( $offset, $limit, $link, $query = '', $atend = false ) {
        } else {
                $nlink = '<a href="' . $title->escapeLocalUrl( $q ) . "\" class=\"mw-nextlink\">{$next}</a>";
        }
-       $nums = wfNumLink( $offset, 20, $title, $query ) . ' | ' .
-         wfNumLink( $offset, 50, $title, $query ) . ' | ' .
-         wfNumLink( $offset, 100, $title, $query ) . ' | ' .
-         wfNumLink( $offset, 250, $title, $query ) . ' | ' .
-         wfNumLink( $offset, 500, $title, $query );
+       $nums = $wgLang->pipeList( array( wfNumLink( $offset, 20, $title, $query ),
+         wfNumLink( $offset, 50, $title, $query ),
+         wfNumLink( $offset, 100, $title, $query ),
+         wfNumLink( $offset, 250, $title, $query ),
+         wfNumLink( $offset, 500, $title, $query ) ) );
 
        return wfMsg( 'viewprevnext', $plink, $nlink, $nums );
 }
@@ -1164,6 +1220,14 @@ function wfExpandUrl( $url ) {
        }
 }
 
+/**
+ * This is obsolete, use SquidUpdate::purge()
+ * @deprecated
+ */
+function wfPurgeSquidServers ($urlArr) {
+       SquidUpdate::purge( $urlArr );
+}
+
 /**
  * Windows-compatible version of escapeshellarg()
  * Windows doesn't recognise single-quotes in the shell, but the escapeshellarg()
@@ -1641,6 +1705,11 @@ define('TS_ORACLE', 6);
  */
 define('TS_POSTGRES', 7);
 
+/**
+ * DB2 format time
+ */
+define('TS_DB2', 8);
+
 /**
  * @param mixed $outputtype A timestamp in one of the supported formats, the
  *                          function will autodetect which format is supplied
@@ -1702,6 +1771,8 @@ function wfTimestamp($outputtype=TS_UNIX,$ts=0) {
                        return gmdate( 'd-M-y h.i.s A', $uts) . ' +00:00';
                case TS_POSTGRES:
                        return gmdate( 'Y-m-d H:i:s', $uts) . ' GMT';
+               case TS_DB2:
+                       return gmdate( 'Y-m-d H:i:s', $uts);
                default:
                        throw new MWException( 'wfTimestamp() called with illegal output type.');
        }
@@ -1786,7 +1857,7 @@ function wfGetCachedNotice( $name ) {
                        $parserMemc->set( $key, array( 'html' => $parsed, 'hash' => md5( $notice ) ), 600 );
                        $notice = $parsed;
                } else {
-                       wfDebug( 'wfGetCachedNotice called for ' . $name . ' with no $wgOut available' );
+                       wfDebug( 'wfGetCachedNotice called for ' . $name . ' with no $wgOut available'."\n" );
                        $notice = '';
                }
        }
@@ -2050,11 +2121,26 @@ function wfIniGetBool( $setting ) {
 function wfShellExec( $cmd, &$retval=null ) {
        global $IP, $wgMaxShellMemory, $wgMaxShellFileSize, $wgMaxShellTime;
 
-       if( wfIniGetBool( 'safe_mode' ) ) {
-               wfDebug( "wfShellExec can't run in safe_mode, PHP's exec functions are too broken.\n" );
+       static $disabled;
+       if ( is_null( $disabled ) ) {
+               $disabled = false;
+               if( wfIniGetBool( 'safe_mode' ) ) {
+                       wfDebug( "wfShellExec can't run in safe_mode, PHP's exec functions are too broken.\n" );
+                       $disabled = true;
+               }
+               $functions = explode( ',', ini_get( 'disable_functions' ) );
+               $functions = array_map( 'trim', $functions );
+               $functions = array_map( 'strtolower', $functions );
+               if ( in_array( 'passthru', $functions ) ) {
+                       wfDebug( "passthru is in disabled_functions\n" );
+                       $disabled = true;
+               }
+       }
+       if ( $disabled ) {
                $retval = 1;
                return "Unable to run external programs in safe mode.";
        }
+
        wfInitShellLocale();
 
        if ( php_uname( 's' ) == 'Linux' ) {
@@ -2266,9 +2352,16 @@ function wfMergeErrorArrays(/*...*/) {
 }
 
 /**
- * Make a URL index, appropriate for the el_index field of externallinks.
+ * parse_url() work-alike, but non-broken.  Differences:
+ *
+ * 1) Does not raise warnings on bad URLs (just returns false)
+ * 2) Handles protocols that don't use :// (e.g., mailto: and news:) correctly
+ * 3) Adds a "delimiter" element to the array, either '://' or ':' (see (2))
+ *
+ * @param string $url A URL to parse
+ * @return array Bits of the URL in an associative array, per PHP docs
  */
-function wfMakeUrlIndex( $url ) {
+function wfParseUrl( $url ) {
        global $wgUrlProtocols; // Allow all protocols defined in DefaultSettings/LocalSettings.php
        wfSuppressWarnings();
        $bits = parse_url( $url );
@@ -2276,12 +2369,12 @@ function wfMakeUrlIndex( $url ) {
        if ( !$bits ) {
                return false;
        }
+
        // most of the protocols are followed by ://, but mailto: and sometimes news: not, check for it
-       $delimiter = '';
-       if ( in_array( $bits['scheme'] . '://' , $wgUrlProtocols ) ) {
-               $delimiter = '://';
-       } elseif ( in_array( $bits['scheme'] .':' , $wgUrlProtocols ) ) {
-               $delimiter = ':';
+       if ( in_array( $bits['scheme'] . '://', $wgUrlProtocols ) ) {
+               $bits['delimiter'] = '://';
+       } elseif ( in_array( $bits['scheme'] . ':', $wgUrlProtocols ) ) {
+               $bits['delimiter'] = ':';
                // parse_url detects for news: and mailto: the host part of an url as path
                // We have to correct this wrong detection
                if ( isset ( $bits['path'] ) ) {
@@ -2292,6 +2385,15 @@ function wfMakeUrlIndex( $url ) {
                return false;
        }
 
+       return $bits;
+}
+
+/**
+ * Make a URL index, appropriate for the el_index field of externallinks.
+ */
+function wfMakeUrlIndex( $url ) {
+       $bits = wfParseUrl( $url );
+
        // Reverse the labels in the hostname, convert to lower case
        // For emails reverse domainpart only
        if ( $bits['scheme'] == 'mailto' ) {
@@ -2313,7 +2415,7 @@ function wfMakeUrlIndex( $url ) {
        }
        // Reconstruct the pseudo-URL
        $prot = $bits['scheme'];
-       $index = "$prot$delimiter$reversedHost";
+       $index = $prot . $bits['delimiter'] . $reversedHost;
        // Leave out user and password. Add the port, path, query and fragment
        if ( isset( $bits['port'] ) )      $index .= ':' . $bits['port'];
        if ( isset( $bits['path'] ) ) {
@@ -2343,6 +2445,13 @@ function wfDoUpdates()
        $wgPostCommitUpdateList = array();
 }
 
+/**
+ * @deprecated use StringUtils::explodeMarkup
+ */
+function wfExplodeMarkup( $separator, $text ) {
+       return StringUtils::explodeMarkup( $separator, $text );
+}
+
 /**
  * Convert an arbitrarily-long digit string from one numeric base
  * to another, optionally zero-padding to a minimum column width.
@@ -2457,10 +2566,29 @@ function wfCreateObject( $name, $p ){
        }
 }
 
+/**
+ * Alias for modularized function
+ * @deprecated Use Http::get() instead
+ */
+function wfGetHTTP( $url, $timeout = 'default' ) {
+       wfDeprecated(__FUNCTION__);
+       return Http::get( $url, $timeout );
+}
+
+/**
+ * Alias for modularized function
+ * @deprecated Use Http::isLocalURL() instead
+ */
+function wfIsLocalURL( $url ) {
+       wfDeprecated(__FUNCTION__);
+       return Http::isLocalURL( $url );
+}
+
 function wfHttpOnlySafe() {
        global $wgHttpOnlyBlacklist;
        if( !version_compare("5.2", PHP_VERSION, "<") )
                return false;
+
        if( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
                foreach( $wgHttpOnlyBlacklist as $regex ) {
                        if( preg_match( $regex, $_SERVER['HTTP_USER_AGENT'] ) ) {
@@ -2468,6 +2596,7 @@ function wfHttpOnlySafe() {
                        }
                }
        }
+
        return true;
 }
 
@@ -2644,7 +2773,7 @@ function &wfGetLBFactory() {
  * @return File, or false if the file does not exist
  */
 function wfFindFile( $title, $time = false, $flags = 0, $bypass = false ) {
-       if( !$time && !$flags && !$bypass ) {
+        if( !$time && !$flags && !$bypass ) {
                return FileCache::singleton()->findFile( $title );
        } else {
                return RepoGroup::singleton()->findFile( $title, $time, $flags );
@@ -2826,6 +2955,21 @@ function wfWaitForSlaves( $maxLag ) {
        }
 }
 
+/**
+ * Output some plain text in command-line mode or in the installer (updaters.inc).
+ * Do not use it in any other context, its behaviour is subject to change.
+ */
+function wfOut( $s ) {
+       static $lineStarted = false;
+       global $wgCommandLineMode;
+       if ( $wgCommandLineMode && !defined( 'MEDIAWIKI_INSTALL' ) ) {
+               echo $s;
+       } else {
+               echo htmlspecialchars( $s );
+       }
+       flush();
+}
+
 /** Generate a random 32-character hexadecimal token.
  * @param mixed $salt Some sort of salt, if necessary, to add to random characters before hashing.
  */