From efd83c55fcfa0930590855ba106a7ca34e6a4d02 Mon Sep 17 00:00:00 2001 From: Antoine Musso Date: Fri, 13 May 2011 11:41:17 +0000 Subject: [PATCH] merge in prettyURL patch This is basicly a merge of r84386 & r84491, see their commit messages for more details. r84386 makes wikilinks nicer by updating the URL forge implemented by r2621 r84491 fix an issue with the (un)?watch links. getParamValue should not be used to guess 'title' or 'action' --- includes/Setup.php | 6 + includes/Title.php | 58 ++-- .../mediawiki.action.watch.ajax.js | 4 +- resources/mediawiki.util/mediawiki.util.js | 65 ++++- tests/parser/parserTests.txt | 250 +++++++++--------- tests/phpunit/includes/TitleTest.php | 105 ++++++++ 6 files changed, 343 insertions(+), 145 deletions(-) diff --git a/includes/Setup.php b/includes/Setup.php index 2310155174..36a617215b 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -42,6 +42,12 @@ if ( $wgArticlePath === false ) { } } +if ( !empty($wgActionPaths) && !isset($wgActionPaths['view']) ) { + # 'view' is assumed the default action path everywhere in the code + # but is rarely filled in $wgActionPaths + $wgActionPaths['view'] = $wgArticlePath ; +} + if ( $wgStylePath === false ) $wgStylePath = "$wgScriptPath/skins"; if ( $wgLocalStylePath === false ) $wgLocalStylePath = "$wgScriptPath/skins"; if ( $wgStyleDirectory === false ) $wgStyleDirectory = "$IP/skins"; diff --git a/includes/Title.php b/includes/Title.php index d1597258da..8f9e9163e0 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -876,29 +876,21 @@ class Title { $url = str_replace( '$1', $dbkey, $wgArticlePath ); } } else { - global $wgActionPaths; $url = false; - $matches = array(); - if ( !empty( $wgActionPaths ) && - preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) ) - { - $action = urldecode( $matches[2] ); - if ( isset( $wgActionPaths[$action] ) ) { - $query = $matches[1]; - if ( isset( $matches[4] ) ) { - $query .= $matches[4]; - } - $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] ); - if ( $query != '' ) { - $url = wfAppendQuery( $url, $query ); - } - } + + global $wgActionPaths; + if( !empty( $wgActionPaths ) ) { + $url = Title::resolveActionPath( $dbkey, $query ); } + if ( $url === false ) { if ( $query == '-' ) { $query = ''; } - $url = "{$wgScript}?title={$dbkey}&{$query}"; + #$url = "{$wgScript}?title={$dbkey}&{$query}"; + # forge a nice URL (ex: /wiki/Special:Foo?q=1&r=2 ) + $baseurl = str_replace( '$1', $dbkey, $wgArticlePath ); + $url = wfAppendQuery( $baseurl, $query ); } } @@ -912,6 +904,38 @@ class Title { return $url; } + /** + * Helper for getLocalUrl() to handles $wgActionPaths + * + * @param $dbkey string Title in database key format + * @param $query string request parameters in CGI format (p=1&q=2&..) + * @return Url resolved or boolean false + */ + private static function resolveActionPath( $dbkey, $query ) { + $url = ''; + + # query parameters are easier to handle using an array: + $queryArray = wfCGIToArray( $query ); + + global $wgActionPaths; + if( !array_key_exists( 'action', $queryArray ) ) { + // Makes the default action 'view' and points to $wgArticlePath + // FIXME: this should be handled in Setup or Wiki! + global $wgArticlePath; + $url = str_replace( '$1', $dbkey, $wgArticlePath ); + } elseif( isset( $wgActionPaths[$queryArray['action']] ) ) { + $url = str_replace( '$1', $dbkey, $wgActionPaths[$queryArray['action']] ); + } else { + # No path found + return false; + } + + # No need to append the action since we have embed it in the path + unset( $queryArray['action'] ); + $url = wfAppendQuery( $url, wfArrayToCGI( $queryArray ) ); + return $url; + } + /** * Get a URL that's the simplest URL that will be valid to link, locally, * to the current Title. It includes the fragment, but does not include diff --git a/resources/mediawiki.action/mediawiki.action.watch.ajax.js b/resources/mediawiki.action/mediawiki.action.watch.ajax.js index c0fc40539d..0d08acd817 100644 --- a/resources/mediawiki.action/mediawiki.action.watch.ajax.js +++ b/resources/mediawiki.action/mediawiki.action.watch.ajax.js @@ -65,8 +65,8 @@ $( document ).ready( function() { var link = this; $link .data( 'icon', $link.closest( 'li' ).hasClass( 'icon' ) ) - .data( 'action', mw.util.getParamValue( 'action', link.href ) == 'unwatch' ? 'unwatch' : 'watch' ); - var title = mw.util.getParamValue( 'title', link.href ); + .data( 'action', mw.util.getActionFrom( link.href ) == 'unwatch' ? 'unwatch' : 'watch' ); + var title = mw.util.getTitleFrom( link.href ); $link.data( 'target', title.replace( /_/g, ' ' ) ); }); diff --git a/resources/mediawiki.util/mediawiki.util.js b/resources/mediawiki.util/mediawiki.util.js index dd53c43446..7e64225f74 100644 --- a/resources/mediawiki.util/mediawiki.util.js +++ b/resources/mediawiki.util/mediawiki.util.js @@ -207,6 +207,12 @@ /** * Grab the URL parameter value for the given parameter. * Returns null if not found. + * Beware! When action paths are enabled (wgActionPaths) using this function + * to retrieve the 'action' or 'title' parameter will probably fail since + * those parameters are hidden in the path. + * To safely query for: + * 'action' use getActionFrom( url ) + * 'title' use getTitleFrom( url ) * * @param param The parameter name * @param url URL to search through (optional) @@ -222,6 +228,63 @@ return null; }, + /** + * Try to find the wiki action for a given URL with actions paths support + * + * @param url URL to search for a wiki action + */ + 'getActionFrom' : function( url ) { + // attempt to get the action from the parameter [&?]action= + var action = mw.util.getParamValue( 'action', url ); + if( action !== null ) { + return action; + } + + // now from the action paths + var actionPaths = mw.config.get( 'wgActionPaths' ); + if( actionPaths.length == 0 ) { + actionPaths['view'] = mw.config.get( 'wgArticlePath' ); + } + var action = ''; + for ( action in actionPaths ) { + var actionRe = new RegExp( actionPaths[action].replace( '$1', '.*?' ) ); + if( url.match( actionRe ) ) { + return action; + } + } + + return null; + }, + + /** + * Try to find the wiki title for a given URL with actions paths support + * + * @param url URL to search for a title + */ + 'getTitleFrom' : function( url ) { + // attempt to get the title from the parameter [&?]title= + var title = mw.util.getParamValue( 'title', url ); + if( title !== null ) { + return title; + } + + // now from the action paths + var actionPaths = mw.config.get( 'wgActionPaths' ); + if( actionPaths.length == 0 ) { + actionPaths['view'] = mw.config.get( 'wgArticlePath' ); + } + var action = ''; + for ( action in actionPaths ) { + var actionRe = new RegExp( '.*' + actionPaths[action].replace( '$1', '([^&?#]+)' ) ); + var title = url.match( actionRe ); + if( title !== null ) { + return title[1]; + } + } + + return null; + }, + // Access key prefix. // Will be re-defined based on browser/operating system detection in // mw.util.init(). @@ -548,4 +611,4 @@ mw.util.init(); -} )( jQuery, mediaWiki ); \ No newline at end of file +} )( jQuery, mediaWiki ); diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt index fc05d5b148..cdde746a0d 100644 --- a/tests/parser/parserTests.txt +++ b/tests/parser/parserTests.txt @@ -538,7 +538,7 @@ Definition list with wikilink containing colon !! input ; [[Help:FAQ]]: The least-read page on Wikipedia !! result -
Help:FAQ
The least-read page on Wikipedia +
Help:FAQ
The least-read page on Wikipedia
!! end @@ -971,7 +971,7 @@ External links: wiki links within external link (Bug 3695) !! input [http://example.com [[wikilink]] embedded in ext link] !! result -

wikilink embedded in ext link +

wikilink embedded in ext link

!! end @@ -1138,7 +1138,7 @@ External link containing double-single-quotes with no space separating the url f !! input [http://www.musee-picasso.fr/pages/page_id18528_u1l2.htm''La muerte de Casagemas'' (1901) en el sitio de [[Museo Picasso (París)|Museo Picasso]].] !! result -

La muerte de Casagemas (1901) en el sitio de Museo Picasso. +

La muerte de Casagemas (1901) en el sitio de Museo Picasso.

!! end @@ -1147,7 +1147,7 @@ URL-encoding in URL functions (single parameter) !! input {{localurl:Some page|amp=&}} !! result -

/index.php?title=Some_page&amp=& +

/wiki/Some_page?amp=&

!! end @@ -1156,7 +1156,7 @@ URL-encoding in URL functions (multiple parameters) !! input {{localurl:Some page|q=?&=&}} !! result -

/index.php?title=Some_page&q=?&amp=& +

/wiki/Some_page?q=?&amp=&

!! end @@ -1757,7 +1757,7 @@ Heading inside table (affected by r85922)
-

[edit] Heading

+

[edit] Heading

@@ -1938,7 +1938,7 @@ Broken link !! input [[Zigzagzogzagzig]] !! result -

Zigzagzogzagzig +

Zigzagzogzagzig

!! end @@ -1947,7 +1947,7 @@ Broken link with fragment !! input [[Zigzagzogzagzig#zug]] !! result -

Zigzagzogzagzig#zug +

Zigzagzogzagzig#zug

!! end @@ -2019,7 +2019,7 @@ Link to namespaces !! input [[Talk:Parser testing]], [[Meta:Disclaimers]] !! result -

Talk:Parser testing, Meta:Disclaimers +

Talk:Parser testing, Meta:Disclaimers

!! end @@ -2028,7 +2028,7 @@ Piped link to namespace !! input [[Meta:Disclaimers|The disclaimers]] !! result -

The disclaimers +

The disclaimers

!! end @@ -2046,7 +2046,7 @@ Link containing % (not as a hex sequence) !! input [[7% Solution]] !! result -

7% Solution +

7% Solution

!! end @@ -2055,7 +2055,7 @@ Link containing % as a single hex sequence interpreted to char !! input [[7%25 Solution]] !! result -

7% Solution +

7% Solution

!!end @@ -2092,7 +2092,7 @@ Link containing double-single-quotes '' (bug 4598) !! input [[Lista d''e paise d''o munno]] !! result -

Lista d''e paise d''o munno +

Lista d''e paise d''o munno

!! end @@ -2101,7 +2101,7 @@ Link containing double-single-quotes '' in text (bug 4598 sanity check) !! input Some [[Link|pretty ''italics'' and stuff]]! !! result -

Some pretty italics and stuff! +

Some pretty italics and stuff!

!! end @@ -2110,7 +2110,7 @@ Link containing double-single-quotes '' in text embedded in italics (bug 4598 sa !! input ''Some [[Link|pretty ''italics'' and stuff]]! !! result -

Some pretty italics and stuff! +

Some pretty italics and stuff!

!! end @@ -2125,10 +2125,10 @@ Link with double quotes in title part (literal) and alternate part (interpreted) [[''Pentecoste''|''Pentecoste'']] !! result -

File:Denys Savchenko Pentecoste.jpg -

''Pentecoste'' -

Pentecoste -

Pentecoste +

File:Denys Savchenko Pentecoste.jpg +

''Pentecoste'' +

Pentecoste +

Pentecoste

!! end @@ -2148,7 +2148,7 @@ Plain link to URL # ---- # I'm changing it to match the current output--it arguably makes more # sense in the light of the test above. Old expected result was: -#

Piped link to URL: an example URL +#

Piped link to URL: an example URL #

# But I think this test is bordering on "garbage in, garbage out" anyway. # -- wtm @@ -2267,7 +2267,7 @@ language=en !! input [[Something]]'nice !! result -

Something'nice +

Something'nice

!! end @@ -2278,7 +2278,7 @@ language=ca !! input [[Something]]'nice !! result -

Something'nice +

Something'nice

!! end @@ -2289,7 +2289,7 @@ language=kaa !! input [[Something]]'nice !! result -

Something'nice +

Something'nice

!! end @@ -2909,7 +2909,7 @@ Magic links: internal link to RFC (bug 479) !! input [[RFC 123]] !! result -

RFC 123 +

RFC 123

!! end @@ -2949,7 +2949,7 @@ Nonexistent template !! input {{thistemplatedoesnotexist}} !! result -

Template:Thistemplatedoesnotexist +

Template:Thistemplatedoesnotexist

!! end @@ -3131,7 +3131,7 @@ Template with thumb image (with link in description) {{paramtest| param =[[Image:noimage.png|thumb|[[no link|link]] [[no link|caption]]]]}} !! result -This is a test template with parameter +This is a test template with parameter !! end @@ -3435,8 +3435,8 @@ Bug 6563: Edit link generation for section shown by !! input {{includeonly section}} !! result -

[edit] Includeonly section

-

[edit] Section T-1

+

[edit] Includeonly section

+

[edit] Section T-1

!! end @@ -3462,7 +3462,7 @@ Bug 6563: Edit link generation for section suppressed by ==Section 1== !! result -

[edit] Section 1

+

[edit] Section 1

!! end @@ -4103,7 +4103,7 @@ Add test with existing image page !! input [[:Image:test]] !! result -

Image:test +

Image:test

!! end @@ -4112,7 +4112,7 @@ bug 18784 Link to non-existent image page with caption should use caption as li !! input [[:Image:test|caption]] !! result -

caption +

caption

!! end @@ -4239,7 +4239,7 @@ Image caption containing another image !! input [[Image:Foobar.jpg|thumb|This is a caption with another [[Image:icon.png|image]] inside it!]] !! result -
This is a caption with another image inside it!
+
This is a caption with another image inside it!
!! end @@ -4326,7 +4326,7 @@ Disabled subpages !! input [[/subpage]] !! result -

/subpage +

/subpage

!! end @@ -4337,7 +4337,7 @@ subpage title=[[Page]] !! input {{/Subpage}} !! result -

Page/Subpage +

Page/Subpage

!! end @@ -4407,13 +4407,13 @@ More ===Smaller headline=== Blah blah !! result -

[edit] Headline 1

+

[edit] Headline 1

Some text

-

[edit] Headline 2

+

[edit] Headline 2

More

-

[edit] Smaller headline

+

[edit] Smaller headline

Blah blah

!! end @@ -4452,14 +4452,14 @@ Some text -

[edit] Headline 1

-

[edit] Subheadline 1

-
[edit] Skipping a level
-
[edit] Skipping a level
-

[edit] Headline 2

+

[edit] Headline 1

+

[edit] Subheadline 1

+
[edit] Skipping a level
+
[edit] Skipping a level
+

[edit] Headline 2

Some text

-

[edit] Another headline

+

[edit] Another headline

!! end @@ -4507,16 +4507,16 @@ Handling of sections up to level 6 and beyond -

[edit] Level 1 Heading

-

[edit] Level 2 Heading

-

[edit] Level 3 Heading

-

[edit] Level 4 Heading

-
[edit] Level 5 Heading
-
[edit] Level 6 Heading
-
[edit] = Level 7 Heading=
-
[edit] == Level 8 Heading==
-
[edit] === Level 9 Heading===
-
[edit] ==== Level 10 Heading====
+

[edit] Level 1 Heading

+

[edit] Level 2 Heading

+

[edit] Level 3 Heading

+

[edit] Level 4 Heading

+
[edit] Level 5 Heading
+
[edit] Level 6 Heading
+
[edit] = Level 7 Heading=
+
[edit] == Level 8 Heading==
+
[edit] === Level 9 Heading===
+
[edit] ==== Level 10 Heading====
!! end @@ -4549,12 +4549,12 @@ TOC regression (bug 9764) -

[edit] title 1

-

[edit] title 1.1

-

[edit] title 1.1.1

-

[edit] title 1.2

-

[edit] title 2

-

[edit] title 2.1

+

[edit] title 1

+

[edit] title 1.1

+

[edit] title 1.1.1

+

[edit] title 1.2

+

[edit] title 2

+

[edit] title 2.1

!! end @@ -4585,12 +4585,12 @@ wgMaxTocLevel=3 -

[edit] title 1

-

[edit] title 1.1

-

[edit] title 1.1.1

-

[edit] title 1.2

-

[edit] title 2

-

[edit] title 2.1

+

[edit] title 1

+

[edit] title 1.1

+

[edit] title 1.1.1

+

[edit] title 1.2

+

[edit] title 2

+

[edit] title 2.1

!! end @@ -4615,11 +4615,11 @@ wgMaxTocLevel=3
  • 2 Section 2
  • -

    [edit] Section 1

    -

    [edit] Section 1.1

    -

    [edit] Section 1.1.1

    -

    [edit] Section 1.1.1.1

    -

    [edit] Section 2

    +

    [edit] Section 1

    +

    [edit] Section 1.1

    +

    [edit] Section 1.1.1

    +

    [edit] Section 1.1.1.1

    +

    [edit] Section 2

    !! end @@ -4630,8 +4630,8 @@ Resolving duplicate section names == Foo bar == == Foo bar == !! result -

    [edit] Foo bar

    -

    [edit] Foo bar

    +

    [edit] Foo bar

    +

    [edit] Foo bar

    !! end @@ -4641,8 +4641,8 @@ Resolving duplicate section names with differing case (bug 10721) == Foo bar == == Foo Bar == !! result -

    [edit] Foo bar

    -

    [edit] Foo Bar

    +

    [edit] Foo bar

    +

    [edit] Foo Bar

    !! end @@ -4661,10 +4661,10 @@ __NOTOC__ {{sections}} ==Section 4== !! result -

    [edit] Section 0

    -

    [edit] Section 1

    -

    [edit] Section 2

    -

    [edit] Section 4

    +

    [edit] Section 0

    +

    [edit] Section 1

    +

    [edit] Section 2

    +

    [edit] Section 4

    !! end @@ -4685,7 +4685,7 @@ Link inside a section heading !! input ==Section with a [[Main Page|link]] in it== !! result -

    [edit] Section with a link in it

    +

    [edit] Section with a link in it

    !! end @@ -4707,9 +4707,9 @@ __TOC__
  • 2 title 2
  • -

    [edit] title 1

    -

    [edit] title 1.1

    -

    [edit] title 2

    +

    [edit] title 1

    +

    [edit] title 1.1

    +

    [edit] title 2

    !! end @@ -4731,10 +4731,10 @@ The line above must have a trailing space! --> But just in case it doesn't... !! result -

    [edit] =

    +

    [edit] =

    The line above must have a trailing space!

    -

    [edit] =

    +

    [edit] =

    But just in case it doesn't...

    !! end @@ -4770,19 +4770,19 @@ section 5
  • 5 text " text
  • -

    [edit] text > text

    +

    [edit] text > text

    section 1

    -

    [edit] text < text

    +

    [edit] text < text

    section 2

    -

    [edit] text & text

    +

    [edit] text & text

    section 3

    -

    [edit] text ' text

    +

    [edit] text ' text

    section 4

    -

    [edit] text " text

    +

    [edit] text " text

    section 5

    !! end @@ -4971,7 +4971,7 @@ Media link to nonexistent file (bug 1702) !! input [[Media:No such.jpg]] !! result -

    Media:No such.jpg +

    Media:No such.jpg

    !! end @@ -4980,7 +4980,7 @@ Image link to nonexistent file (bug 1850 - good) !! input [[Image:No such.jpg]] !! result -

    File:No such.jpg +

    File:No such.jpg

    !! end @@ -4989,7 +4989,7 @@ Image link to nonexistent file (bug 1850 - good) !! input [[:Image:No such.jpg]] !! result -

    Image:No such.jpg +

    Image:No such.jpg

    !! end @@ -5988,7 +5988,7 @@ Fuzz testing: Parser14 == onmouseover= == http://__TOC__ !! result -

    [edit] onmouseover=

    +

    [edit] onmouseover=

    http://

    Contents

    • 1 onmouseover=
    • @@ -6004,7 +6004,7 @@ Fuzz testing: Parser14-table {| STYLE=__TOC__ |foo !! result -

      [edit] a

      +

      [edit] a

      foo @@ -6325,7 +6325,7 @@ Transclusion of nonexistent MediaWiki message !! input {{MediaWiki:Mainpagexxx}} !!result -

      MediaWiki:Mainpagexxx +

      MediaWiki:Mainpagexxx

      !! end @@ -7243,7 +7243,7 @@ Say the magic word
    • Talk
    • -
    • Template:Dynamic +
    • Template:Dynamic
    • !! end @@ -7546,7 +7546,7 @@ Double RFC with a wiki link !! input RFC [[RFC 1234]] !! result -

      RFC RFC 1234 +

      RFC RFC 1234

      !! end @@ -7680,7 +7680,7 @@ subpage title=[[Subpage test/L1/L2/L3]] !! input [[../|L2]] !! result -

      L2 +

      L2

      !! end @@ -7692,7 +7692,7 @@ subpage title=[[Subpage test/L1/L2/L3]] !! input [[../]] !! result -

      Subpage test/L1/L2 +

      Subpage test/L1/L2

      !! end @@ -7707,8 +7707,8 @@ subpage title=[[Subpage test/L1/L2/L3]] [[../../|L1]]l !! result -

      L12 -

      L1l +

      L12 +

      L1l

      !! end @@ -7730,7 +7730,7 @@ subpage title=[[Subpage test/L1/L2/L3]] !! input [[../../////]] !! result -

      /// +

      ///

      !! end @@ -7771,7 +7771,7 @@ RAW magic word !! input {{RAW:QUERTY}} !! result -

      Template:QUERTY +

      Template:QUERTY

      !! end @@ -7808,7 +7808,7 @@ Inclusion of !userCanEdit() content !! input {{MediaWiki:Fake}} !! result -

      [edit] header

      +

      [edit] header

      !! end @@ -7839,12 +7839,12 @@ Out-of-order TOC heading levels
      -

      [edit] 2

      -
      [edit] 6
      -

      [edit] 3

      -

      [edit] 1

      -
      [edit] 5
      -

      [edit] 2

      +

      [edit] 2

      +
      [edit] 6
      +

      [edit] 3

      +

      [edit] 1

      +
      [edit] 5
      +

      [edit] 2

      !! end @@ -8142,7 +8142,7 @@ language=sr variant=sr-ec !! input == -{Naslov}- == !! result -

      [уреди] Naslov

      +

      [уреди] Naslov

      !! end @@ -8390,7 +8390,7 @@ Morwen/13: Unclosed link followed by heading !! result

      [[link

      -

      [edit] heading

      +

      [edit] heading

      !! end @@ -8414,7 +8414,7 @@ HHP2.2: Heuristics for headings in preprocessor parenthetical structures !! result

      {{foo|

      -

      [edit] heading

      +

      [edit] heading

      !! end @@ -8688,7 +8688,7 @@ wgUseDynamicDates=1 !! input [[2009-03-24]] !! result -

      2009-03-24 +

      2009-03-24

      !!end @@ -8717,7 +8717,7 @@ wgUseDynamicDates=false !! input [[2009-03-24]] !! result -

      2009-03-24 +

      2009-03-24

      !! end @@ -8737,7 +8737,7 @@ wgUseDynamicDates=true !! input [[January 15]] !! result -

      January 15 +

      January 15

      !! end @@ -8831,7 +8831,7 @@ title=[[Subpage test]] !! input Poked at a [[/subpage]] here... !! result -Poked at a /subpage here... +Poked at a /subpage here... !!end !! test @@ -8893,7 +8893,7 @@ comment !!input [[ABC%33D% ++]] [[ABC%33D% ++|+%20]] !! result -ABC3D% ++ +%20 +ABC3D% ++ +%20 !! end !! test @@ -8994,7 +8994,7 @@ this is not the the title !! result Screen

      this is not the the title -Template:DISPLAYTITLE:screen +Template:DISPLAYTITLE:screen

      !! end @@ -9090,10 +9090,10 @@ percent-encoding and + signs in internal links (Bug 26410) [[%]] [[+]] [[image:%+abc%39|foo|[[bar]]]] [[%33%45]] [[%33%45+]] !! result -

      User:+% Page+title% -%+ %20 %+ %+r -% + bar -3E 3E+ +

      User:+% Page+title% +%+ %20 %+ %+r +% + bar +3E 3E+

      !! end @@ -9103,8 +9103,8 @@ Special characters in embedded file links (bug 27679) [[File:Contains & ampersand.jpg]] [[File:Does not exist.jpg|Title with & ampersand]] !! result -

      File:Contains & ampersand.jpg -Title with & ampersand +

      File:Contains & ampersand.jpg +Title with & ampersand

      !! end diff --git a/tests/phpunit/includes/TitleTest.php b/tests/phpunit/includes/TitleTest.php index 70925610bc..37044bc5a3 100644 --- a/tests/phpunit/includes/TitleTest.php +++ b/tests/phpunit/includes/TitleTest.php @@ -14,4 +14,109 @@ class TitleTest extends MediaWikiTestCase { } } } + + /** + * Helper to test getLocalURL + * + * @cover Title:getLocalURL + */ + function assertGetLocalURL( $expected, $dbkey, $query, $articlepath, $actionpaths = array(), $variant = false ) + { + global $wgArticlePath; + $wgArticlePath = $articlepath; + global $wgActionPaths; + $wgActionPaths = $actionpaths; + + $t = Title::newFromDBKey( $dbkey ); + + $this->assertEquals( $expected, $t->getLocalURL( $query, $variant ) ); + } + + /** + * @dataProvider provider1 + * @cover Title:getLocalURL + */ + function testGetLocalUrl( $expected, $dbkey, $query ) + { + $this->assertGetLocalURL( $expected, $dbkey, $query, '/wiki/$1' ); + } + + function provider1() + { + return array( + array( '/wiki/Recentchanges', 'Recentchanges', '' ), + array( '/wiki/Recentchanges?foo=2', 'Recentchanges', 'foo=2' ), + array( '/wiki/Recentchanges?foo=A&bar=1', 'Recentchanges', 'foo=A&bar=1' ), + ); + } + + + /** + * @dataProvider provider2 + * @cover Title:getLocalURL + */ + function testGetLocalUrlWithArticlePath( $expected, $dbkey, $query, $actions ) + { + $this->assertGetLocalURL( $expected, $dbkey, $query, '/wiki/view/$1', $actions ); + } + + function provider2() + { + return array( + array( '/wiki/view/Recentchanges', 'Recentchanges', '', array( ) ), + array( '/wiki/view/Recentchanges', 'Recentchanges', '', array( 'view' => 'OH MEN' ) ), + array( '/wiki/view/Recentchanges', 'Recentchanges', '', array( 'view' => '/wiki/view/$1' ) ), + array( '/wiki/view/Recentchanges?foo=2', 'Recentchanges', 'foo=2', array( 'view' => '/wiki/view/$1' ) ), + array( '/wiki/view/Recentchanges?foo=A&bar=1', 'Recentchanges', 'foo=A&bar=1', array() ), + ); + } + + + /** + * @dataProvider provider3 + * @cover Title:getLocalURL + */ + function testGetLocalUrlWithActionPaths( $expected, $dbkey, $query ) + { + $actions = array( 'edit' => '/wiki/edit/$1' ); + $this->assertGetLocalURL( $expected, $dbkey, $query, '/wiki/view/$1', $actions ); + } + + function provider3() { + return array( + array( '/wiki/view/Recentchanges', 'Recentchanges', ''), + array( '/wiki/edit/Recentchanges', 'Recentchanges', 'action=edit' ), + array( '/wiki/edit/Recentchanges?foo=2', 'Recentchanges', 'action=edit&foo=2' ), + array( '/wiki/edit/Recentchanges?foo=2', 'Recentchanges', 'foo=2&action=edit' ), + array( '/wiki/view/Recentchanges?foo=A&bar=1', 'Recentchanges', 'foo=A&bar=1' ), + array( '/wiki/edit/Recentchanges?foo=A&bar=1', 'Recentchanges', 'foo=A&bar=1&action=edit' ), + array( '/wiki/edit/Recentchanges?foo=A&bar=1', 'Recentchanges', 'foo=A&action=edit&bar=1' ), + array( '/wiki/edit/Recentchanges?foo=A&bar=1', 'Recentchanges', 'action=edit&foo=A&bar=1' ), + + # FIXME The next two are equals but need investigation: + array( '/wiki/edit/Recentchanges', 'Recentchanges', 'action=view&action=edit' ), + array( '/wiki/view/Recentchanges?action=edit&action=view', 'Recentchanges', 'action=edit&action=view' ), + ); + } + + /** + * @dataProvider provider4 + * @cover Title:getLocalURL + */ + function testGetLocalUrlWithVariantArticlePaths( $expected, $dbkey, $query ) + { + # FIXME find a language with variants! + $this->markTestIncomplete(); + + $actions = array( 'edit' => '/wiki/edit/$1' ); + $this->assertGetLocalURL( $expected, $dbkey, $query, '/wiki/view/$1', array(), '/$2/$1' ); + } + + function provider4() { + return array( + array( '/wiki/view/Recentchanges', 'Recentchanges', '', ), + ); + } + + } -- 2.20.1