Merge "Add a Uri class."
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 7 Aug 2012 03:37:30 +0000 (03:37 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 7 Aug 2012 03:37:30 +0000 (03:37 +0000)
1  2 
includes/AutoLoader.php
includes/GlobalFunctions.php

diff --combined includes/AutoLoader.php
@@@ -252,6 -252,7 +252,7 @@@ $wgAutoloadLocalClasses = array
        'UnlistedSpecialPage' => 'includes/SpecialPage.php',
        'UploadSourceAdapter' => 'includes/Import.php',
        'UppercaseCollation' => 'includes/Collation.php',
+       'Uri' => 'includes/Uri.php',
        'User' => 'includes/User.php',
        'UserArray' => 'includes/UserArray.php',
        'UserArrayFromResult' => 'includes/UserArray.php',
        'ApiResult' => 'includes/api/ApiResult.php',
        'ApiRollback' => 'includes/api/ApiRollback.php',
        'ApiRsd' => 'includes/api/ApiRsd.php',
 +      'ApiSetNotificationTimestamp' => 'includes/api/ApiSetNotificationTimestamp.php',
        'ApiTokens' => 'includes/api/ApiTokens.php',
        'ApiUnblock' => 'includes/api/ApiUnblock.php',
        'ApiUndelete' => 'includes/api/ApiUndelete.php',
        'XCacheBagOStuff' => 'includes/objectcache/XCacheBagOStuff.php',
  
        # includes/parser
 -      'CacheTime' => 'includes/parser/ParserOutput.php',
 +      'CacheTime' => 'includes/parser/CacheTime.php',
        'CoreLinkFunctions' => 'includes/parser/CoreLinkFunctions.php',
        'CoreParserFunctions' => 'includes/parser/CoreParserFunctions.php',
        'CoreTagHooks' => 'includes/parser/CoreTagHooks.php',
@@@ -391,7 -391,7 +391,7 @@@ function wfArrayToCgi( $array1, $array
  
        $cgi = '';
        foreach ( $array1 as $key => $value ) {
-               if ( !is_null($value) && $value !== false ) {
+               if ( $value !== false ) {
                        if ( $cgi != '' ) {
                                $cgi .= '&';
                        }
                        } 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 +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 );
   * 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
   * @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 +572,13 @@@ function wfExpandUrl( $url, $defaultPro
   * @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,58 -725,13 +725,13 @@@ 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
   */
  function wfParseUrl( $url ) {
-       global $wgUrlProtocols; // Allow all protocols defined in DefaultSettings/LocalSettings.php
-       // Protocol-relative URLs are handled really badly by parse_url(). It's so bad that the easiest
-       // way to handle them is to just prepend 'http:' and strip the protocol out later
-       $wasRelative = substr( $url, 0, 2 ) == '//';
-       if ( $wasRelative ) {
-               $url = "http:$url";
-       }
-       wfSuppressWarnings();
-       $bits = parse_url( $url );
-       wfRestoreWarnings();
-       // parse_url() returns an array without scheme for some invalid URLs, e.g.
-       // parse_url("%0Ahttp://example.com") == array( 'host' => '%0Ahttp', 'path' => 'example.com' )
-       if ( !$bits || !isset( $bits['scheme'] ) ) {
-               return false;
-       }
-       // most of the protocols are followed by ://, but mailto: and sometimes news: not, check for it
-       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'] ) ) {
-                       $bits['host'] = $bits['path'];
-                       $bits['path'] = '';
-               }
-       } else {
-               return false;
-       }
-       /* Provide an empty host for eg. file:/// urls (see bug 28627) */
-       if ( !isset( $bits['host'] ) ) {
-               $bits['host'] = '';
-               /* parse_url loses the third / for file:///c:/ urls (but not on variants) */
-               if ( substr( $bits['path'], 0, 1 ) !== '/' ) {
-                       $bits['path'] = '/' . $bits['path'];
-               }
-       }
-       // If the URL was protocol-relative, fix scheme and delimiter
-       if ( $wasRelative ) {
-               $bits['scheme'] = '';
-               $bits['delimiter'] = '//';
-       }
-       return $bits;
+       $obj = new Uri( $url );
+       return $obj->getComponents();
  }
  
  /**
@@@ -1458,8 -1373,6 +1373,8 @@@ function wfMessageFallback( /*...*/ ) 
   * Use wfMsgForContent() instead if the message should NOT
   * change depending on the user preferences.
   *
 + * @deprecated since 1.18
 + *
   * @param $key String: lookup key for the message, usually
   *    defined in languages/Language.php
   *
@@@ -1480,8 -1393,6 +1395,8 @@@ function wfMsg( $key ) 
  /**
   * Same as above except doesn't transform the message
   *
 + * @deprecated since 1.18
 + *
   * @param $key String
   * @return String
   */
@@@ -1510,8 -1421,6 +1425,8 @@@ function wfMsgNoTrans( $key ) 
   * customize potentially hundreds of messages in
   * order to, e.g., fix a link in every possible language.
   *
 + * @deprecated since 1.18
 + *
   * @param $key String: lookup key for the message, usually
   *     defined in languages/Language.php
   * @return String
@@@ -1532,8 -1441,6 +1447,8 @@@ function wfMsgForContent( $key ) 
  /**
   * Same as above except doesn't transform the message
   *
 + * @deprecated since 1.18
 + *
   * @param $key String
   * @return String
   */
@@@ -1553,8 -1460,6 +1468,8 @@@ function wfMsgForContentNoTrans( $key 
  /**
   * Really get a message
   *
 + * @deprecated since 1.18
 + *
   * @param $key String: key to get.
   * @param $args
   * @param $useDB Boolean
@@@ -1573,8 -1478,6 +1488,8 @@@ function wfMsgReal( $key, $args, $useD
  /**
   * Fetch a message string value, but don't replace any keys yet.
   *
 + * @deprecated since 1.18
 + *
   * @param $key String
   * @param $useDB Bool
   * @param $langCode String: Code of the language to get the message for, or
@@@ -1598,8 -1501,6 +1513,8 @@@ function wfMsgGetKey( $key, $useDB = tr
  /**
   * Replace message parameter keys on the given formatted output.
   *
 + * @deprecated since 1.18
 + *
   * @param $message String
   * @param $args Array
   * @return string
@@@ -1632,8 -1533,6 +1547,8 @@@ function wfMsgReplaceArgs( $message, $a
   * to pre-escape them if you really do want plaintext, or just wrap
   * the whole thing in htmlspecialchars().
   *
 + * @deprecated since 1.18
 + *
   * @param $key String
   * @param string ... parameters
   * @return string
@@@ -1651,8 -1550,6 +1566,8 @@@ function wfMsgHtml( $key ) 
   * to pre-escape them if you really do want plaintext, or just wrap
   * the whole thing in htmlspecialchars().
   *
 + * @deprecated since 1.18
 + *
   * @param $key String
   * @param string ... parameters
   * @return string
@@@ -1668,9 -1565,6 +1583,9 @@@ function wfMsgWikiHtml( $key ) 
  
  /**
   * Returns message in the requested format
 + *
 + * @deprecated since 1.18
 + *
   * @param $key String: key of the message
   * @param $options Array: processing rules. Can take the following options:
   *   <i>parse</i>: parses wikitext to HTML
@@@ -1763,8 -1657,6 +1678,8 @@@ function wfMsgExt( $key, $options ) 
   * looked up didn't exist but a XHTML string, this function checks for the
   * nonexistance of messages by checking the MessageCache::get() result directly.
   *
 + * @deprecated since 1.18
 + *
   * @param $key      String: the message key looked up
   * @return Boolean True if the message *doesn't* exist.
   */