Linker: Add ability for Special page links to parse subpage and params

Change-Id: Ibcd2c5365a1f581eda43970dd12a1708880144a1
This commit is contained in:
James D. Forrester 2022-11-29 16:48:59 -05:00
parent 81ab58ed82
commit 3255f37af1
3 changed files with 54 additions and 2 deletions

View file

@ -76,6 +76,8 @@ For notes on 1.39.x and older releases, see HISTORY.
- ContentHandler::getDataForSearchIndex
- the SearchDataForIndex hook is deprecated in favor of SearchDataForIndex2
* Vue development mode is enabled by default in DevelopmentSettings.php
* The Linker::specialLink() method can now link to a Special page's with a sub-
page or action parameter set, e.g. [[Special:Contributions/JohnDoe]].
* …
=== External library changes in 1.40 ===

View file

@ -978,11 +978,29 @@ class Linker {
* @return string
*/
public static function specialLink( $name, $key = '' ) {
$subpage = false;
if ( str_contains( $name, '/' ) ) {
$subpage = substr( $name, strpos( $name, '/' ) + 1 );
$name = substr( $name, 0, strpos( $name, '/' ) );
}
$getParams = [];
if ( str_contains( $name, '?' ) ) {
$getParams = wfCgiToArray( substr( $name, strpos( $name, '?' ) + 1 ) );
$name = substr( $name, 0, strpos( $name, '?' ) );
}
if ( $key == '' ) {
$key = strtolower( $name );
}
return self::linkKnown( SpecialPage::getTitleFor( $name ), wfMessage( $key )->escaped() );
return self::linkKnown(
SpecialPage::getTitleFor( $name, $subpage ),
wfMessage( $key )->escaped(),
[],
$getParams
);
}
/**

View file

@ -792,7 +792,10 @@ class LinkerTest extends MediaWikiLangTestCase {
* @dataProvider provideSpecialLink
*/
public function testSpecialLink( $expected, $target, $key = null ) {
$this->overrideConfigValue( MainConfigNames::ArticlePath, '/wiki/$1' );
$this->overrideConfigValues( [
MainConfigNames::Script => '/w/index.php',
MainConfigNames::ArticlePath => '/wiki/$1',
] );
$this->assertEquals( $expected, Linker::specialLink( $target, $key ) );
}
@ -803,6 +806,11 @@ class LinkerTest extends MediaWikiLangTestCase {
'Recentchanges'
];
yield 'Recent Changes, only for a given tag' => [
'<a href="/w/index.php?title=Special:RecentChanges&amp;tagfilter=blanking" title="Special:RecentChanges">Recent changes</a>',
'Recentchanges?tagfilter=blanking'
];
yield 'Contributions' => [
'<a href="/wiki/Special:Contributions" title="Special:Contributions">User contributions</a>',
'Contributions'
@ -814,10 +822,34 @@ class LinkerTest extends MediaWikiLangTestCase {
'made-up-display-key'
];
yield 'Contributions, targetted' => [
'<a href="/wiki/Special:Contributions/JohnDoe" title="Special:Contributions/JohnDoe">User contributions</a>',
'Contributions/JohnDoe'
];
yield 'Contributions, targetted, topOnly' => [
'<a href="/wiki/Special:Contributions/JohnDoe%3FtopOnly%3D1" title="Special:Contributions/JohnDoe?topOnly=1">User contributions</a>',
'Contributions/JohnDoe?topOnly=1'
];
yield 'Userlogin' => [
'<a href="/wiki/Special:UserLogin" title="Special:UserLogin">Log in</a>',
'Userlogin',
'login'
];
yield 'Userlogin, returnto' => [
'<a href="/w/index.php?title=Special:UserLogin&amp;returnto=Main+Page" title="Special:UserLogin">Log in</a>',
'Userlogin?returnto=Main+Page',
'login'
];
yield 'Userlogin, targetted' => [
// Note that this special page doesn't have any support for and doesn't do anything with
// the subtitle; this is here as demonstration that Linker doesn't care.
'<a href="/wiki/Special:UserLogin/JohnDoe" title="Special:UserLogin/JohnDoe">Log in</a>',
'Userlogin/JohnDoe',
'login'
];
}
}