Bug 22474, urlencode now takes a parameter to change escaping style
authorConrad Irwin <conrad@users.mediawiki.org>
Thu, 8 Apr 2010 00:29:58 +0000 (00:29 +0000)
committerConrad Irwin <conrad@users.mediawiki.org>
Thu, 8 Apr 2010 00:29:58 +0000 (00:29 +0000)
RELEASE-NOTES
includes/parser/CoreParserFunctions.php
languages/messages/MessagesEn.php
maintenance/parserTests.txt

index fd0935c..3da77e6 100644 (file)
@@ -41,6 +41,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
   changes list
 * (bug 22925) "sp-contributions-blocked-notice-anon" message now displayed when
   viewing contributions of a blocked IP address
+* (bug 22474) {{urlencode:}} now takes an optional second paramter for type of
+  escaping.
 
 === Bug fixes in 1.17 ===
 * (bug 17560) Half-broken deletion moved image files to deletion archive
index fa073e9..c3e9884 100644 (file)
@@ -124,8 +124,37 @@ class CoreParserFunctions {
                return wfUrlencode( str_replace( ' ', '_', self::ns( $parser, $part1 ) ) );
        }
 
-       static function urlencode( $parser, $s = '' ) {
-               return urlencode( $s );
+       /**
+        * urlencodes a string according to one of three patterns: (bug 22474)
+        *
+        * By default (for HTTP "query" strings), spaces are encoded as '+'.
+        * Or to encode a value for the HTTP "path", spaces are encoded as '%20'.
+        * For links to "wiki"s, or similar software, spaces are encoded as '_',
+        *
+        * @param $parser.
+        * @param $s String: The text to encode.
+        * @param $arg String (optional): The type of encoding.
+        */
+       static function urlencode( $parser, $s = '', $arg = null ) {
+               static $magicWords = null;
+               if ( is_null( $magicWords ) ) {
+                       $magicWords = new MagicWordArray( array( 'url_path', 'url_query', 'url_wiki' ) );
+               }
+               switch( $magicWords->matchStartToEnd( $arg ) ) {
+
+                       // Encode as though it's a wiki page, '_' for ' '.
+                       case 'url_wiki':
+                               return wfUrlencode( str_replace( ' ', '_', $s ) );
+
+                       // Encode for an HTTP Path, '%20' for ' '.
+                       case 'url_path':
+                               return rawurlencode( $s );
+
+                       // Encode for HTTP query, '+' for ' '.
+                       case 'url_query':
+                       default:
+                               return urlencode( $s );
+               }
        }
 
        static function lcfirst( $parser, $s = '' ) {
index a44ef99..de47be4 100644 (file)
@@ -352,6 +352,9 @@ $magicWords = array(
        'staticredirect'         => array( 1,    '__STATICREDIRECT__'     ),
        'protectionlevel'        => array( 1,    'PROTECTIONLEVEL'        ),
        'formatdate'             => array( 0,    'formatdate', 'dateformat' ),
+       'url_path'               => array( 0,    'PATH' ),
+       'url_wiki'               => array( 0,    'WIKI' ),
+       'url_query'              => array( 0,    'QUERY' ),
 );
 
 /**
index e34d126..460b2ab 100644 (file)
@@ -2177,6 +2177,21 @@ language=de
 !! end
 
 
+!! test
+Urlencode
+!! input
+{{urlencode:hi world?!}}
+{{urlencode:hi world?!|WIKI}}
+{{urlencode:hi world?!|PATH}}
+{{urlencode:hi world?!|QUERY}}
+!! result
+<p>hi+world%3F%21
+hi_world%3F!
+hi%20world%3F%21
+hi+world%3F%21
+</p>
+!! end
+
 ###
 ### Magic links
 ###