merge latest master into Wikidata branch
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 0a60518..3184eb7 100644 (file)
@@ -391,7 +391,7 @@ function wfArrayToCgi( $array1, $array2 = null, $prefix = '' ) {
 
        $cgi = '';
        foreach ( $array1 as $key => $value ) {
-               if ( !is_null($value) && $value !== false ) {
+               if ( $value !== false ) {
                        if ( $cgi != '' ) {
                                $cgi .= '&';
                        }
@@ -412,8 +412,11 @@ function wfArrayToCgi( $array1, $array2 = null, $prefix = '' ) {
                        } else {
                                if ( is_object( $value ) ) {
                                        $value = $value->__toString();
+                               } elseif( !is_null( $value ) ) {
+                                       $cgi .= urlencode( $key ) . '=' . urlencode( $value );
+                               } else {
+                                       $cgi .= urlencode( $key );
                                }
-                               $cgi .= urlencode( $key ) . '=' . urlencode( $value );
                        }
                }
        }
@@ -440,14 +443,15 @@ function wfCgiToArray( $query ) {
                        continue;
                }
                if ( strpos( $bit, '=' ) === false ) {
-                       // Pieces like &qwerty become 'qwerty' => '' (at least this is what php does)
-                       $key = $bit;
-                       $value = '';
+                       // Pieces like &qwerty become 'qwerty' => null
+                       $key = urldecode( $bit );
+                       $value = null;
                } else {
                        list( $key, $value ) = explode( '=', $bit );
+                       $key = urldecode( $key );
+                       $value = urldecode( $value );
                }
-               $key = urldecode( $key );
-               $value = urldecode( $value );
+
                if ( strpos( $key, '[' ) !== false ) {
                        $keys = array_reverse( explode( '[', $key ) );
                        $key = array_pop( $keys );
@@ -472,23 +476,15 @@ function wfCgiToArray( $query ) {
  * Append a query string to an existing URL, which may or may not already
  * have query string parameters already. If so, they will be combined.
  *
+ * @deprecated in 1.20. Use Uri class.
  * @param $url String
  * @param $query Mixed: string or associative array
  * @return string
  */
 function wfAppendQuery( $url, $query ) {
-       if ( is_array( $query ) ) {
-               $query = wfArrayToCgi( $query );
-       }
-       if( $query != '' ) {
-               if( false === strpos( $url, '?' ) ) {
-                       $url .= '?';
-               } else {
-                       $url .= '&';
-               }
-               $url .= $query;
-       }
-       return $url;
+       $obj = new Uri( $url );
+       $obj->extendQuery( $query );
+       return $obj->toString();
 }
 
 /**
@@ -576,49 +572,13 @@ function wfExpandUrl( $url, $defaultProto = PROTO_CURRENT ) {
  * @todo Need to integrate this into wfExpandUrl (bug 32168)
  *
  * @since 1.19
+ * @deprecated
  * @param $urlParts Array URL parts, as output from wfParseUrl
  * @return string URL assembled from its component parts
  */
 function wfAssembleUrl( $urlParts ) {
-       $result = '';
-
-       if ( isset( $urlParts['delimiter'] ) ) {
-               if ( isset( $urlParts['scheme'] ) ) {
-                       $result .= $urlParts['scheme'];
-               }
-
-               $result .= $urlParts['delimiter'];
-       }
-
-       if ( isset( $urlParts['host'] ) ) {
-               if ( isset( $urlParts['user'] ) ) {
-                       $result .= $urlParts['user'];
-                       if ( isset( $urlParts['pass'] ) ) {
-                               $result .= ':' . $urlParts['pass'];
-                       }
-                       $result .= '@';
-               }
-
-               $result .= $urlParts['host'];
-
-               if ( isset( $urlParts['port'] ) ) {
-                       $result .= ':' . $urlParts['port'];
-               }
-       }
-
-       if ( isset( $urlParts['path'] ) ) {
-               $result .= $urlParts['path'];
-       }
-
-       if ( isset( $urlParts['query'] ) ) {
-               $result .= '?' . $urlParts['query'];
-       }
-
-       if ( isset( $urlParts['fragment'] ) ) {
-               $result .= '#' . $urlParts['fragment'];
-       }
-
-       return $result;
+       $obj = new Uri( $urlParts );
+       return $obj->toString();
 }
 
 /**
@@ -765,6 +725,7 @@ function wfUrlProtocolsWithoutProtRel() {
  * 2) Handles protocols that don't use :// (e.g., mailto: and news: , as well as protocol-relative URLs) correctly
  * 3) Adds a "delimiter" element to the array, either '://', ':' or '//' (see (2))
  *
+ * @deprecated
  * @param $url String: a URL to parse
  * @return Array: bits of the URL in an associative array, per PHP docs
  */
@@ -786,6 +747,9 @@ function wfParseUrl( $url ) {
                return false;
        }
 
+       // parse_url() incorrectly handles schemes case-sensitively. Convert it to lowercase.
+       $bits['scheme'] = strtolower( $bits['scheme'] );
+
        // most of the protocols are followed by ://, but mailto: and sometimes news: not, check for it
        if ( in_array( $bits['scheme'] . '://', $wgUrlProtocols ) ) {
                $bits['delimiter'] = '://';
@@ -1061,6 +1025,7 @@ function wfLogDBError( $text ) {
                } else {
                        $d = date_create( "now", $logDBErrorTimeZoneObject );
                }
+               $date = $d->format( 'D M j G:i:s T Y' );
 
                $date = $d->format( 'D M j G:i:s T Y' );
 
@@ -1539,8 +1504,6 @@ function wfMsgGetKey( $key, $useDB = true, $langCode = false, $transform = true
 /**
  * Replace message parameter keys on the given formatted output.
  *
- * @deprecated since 1.18
- *
  * @param $message String
  * @param $args Array
  * @return string
@@ -2409,8 +2372,13 @@ define( 'TS_ISO_8601_BASIC', 9 );
  * @return Mixed: String / false The same date in the format specified in $outputtype or false
  */
 function wfTimestamp( $outputtype = TS_UNIX, $ts = 0 ) {
-       $timestamp = new MWTimestamp( $ts );
-       return $timestamp->getTimestamp( $outputtype );
+       try {
+               $timestamp = new MWTimestamp( $ts );
+               return $timestamp->getTimestamp( $outputtype );
+       } catch( TimestampException $e ) {
+               wfDebug("wfTimestamp() fed bogus time value: TYPE=$outputtype; VALUE=$ts\n");
+               return false;
+       }
 }
 
 /**
@@ -3546,16 +3514,6 @@ function wfBoolToStr( $value ) {
        return $value ? 'true' : 'false';
 }
 
-/**
- * Load an extension messages file
- *
- * @deprecated since 1.16, warnings in 1.18, remove in 1.20
- * @codeCoverageIgnore
- */
-function wfLoadExtensionMessages() {
-       wfDeprecated( __FUNCTION__, '1.16' );
-}
-
 /**
  * Get a platform-independent path to the null file, e.g. /dev/null
  *