From 481ddf27f9d37d9abe3e57e9e6ce33dee8b8cac4 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 8 Apr 2010 00:29:58 +0000 Subject: [PATCH] Bug 22474, urlencode now takes a parameter to change escaping style --- RELEASE-NOTES | 2 ++ includes/parser/CoreParserFunctions.php | 33 +++++++++++++++++++++++-- languages/messages/MessagesEn.php | 3 +++ maintenance/parserTests.txt | 15 +++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index fd0935cff7..3da77e6b46 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 diff --git a/includes/parser/CoreParserFunctions.php b/includes/parser/CoreParserFunctions.php index fa073e9cdd..c3e9884eac 100644 --- a/includes/parser/CoreParserFunctions.php +++ b/includes/parser/CoreParserFunctions.php @@ -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 = '' ) { diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index a44ef992b6..de47be4073 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -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' ), ); /** diff --git a/maintenance/parserTests.txt b/maintenance/parserTests.txt index e34d12699f..460b2ab92a 100644 --- a/maintenance/parserTests.txt +++ b/maintenance/parserTests.txt @@ -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 +

hi+world%3F%21 +hi_world%3F! +hi%20world%3F%21 +hi+world%3F%21 +

+!! end + ### ### Magic links ### -- 2.20.1