skins: Correctly index the copyright icon, caused failure in MF

On mobile and possibly desktop via (`$wgFooterIcons`), trying to acccess
the copyright icon returns an array instead of a string. An exception is thrown
since the indexing is done on a string.

Throwing an error: TypeError: Return value of BaseTemplate::getCopyrightIconHTML()
must be of the type string, array returned.

The returned array looks like this (when I test):

["copyright"]=> array(1) {
    ["copyright"]=> array(3) {
        ["url"]=> string(47) "https://creativecommons.org/licenses/by-sa/4.0/"
        ["src"]=> string(44) "/core/resources/assets/licenses/cc-by-sa.png"
        ["alt"]=> string(39) "Creative Commons Attribution-ShareAlike"
    }
}

So getting the icon, we need to index src, hence the patch. Let me know
if I'm wrong here. Also, have a look [[Manual:$wgFooterIcons]], you'll see that
default value is an empty array instead of a string:

$wgFooterIcons = [
	"copyright" => [
		"copyright" => [],
	],
]

This made MF to not be able to load locally and the patch fixes it.

In addition, we can just pass `$config` as we have above rather than request
the for it again and again.

Bug: T291325
Change-Id: Icb42342e83f2bc61922ab991bcec66aa5e7b5646
This commit is contained in:
Derick Alangi 2021-09-17 09:56:17 +01:00
parent 3d9a1a158c
commit caf0591902
4 changed files with 31 additions and 12 deletions

View file

@ -35,16 +35,16 @@ abstract class BaseTemplate extends QuickTemplate {
/**
* @internal for usage by BaseTemplate or SkinTemplate.
* @param Config $config
* @param Skin $skin
* @return string
*/
public static function getCopyrightIconHTML( Config $config ): string {
public static function getCopyrightIconHTML( Config $config, Skin $skin ): string {
$out = '';
$footerIcons = $config->get( 'FooterIcons' );
if (
isset( $footerIcons['copyright']['copyright'] ) &&
$footerIcons['copyright']['copyright']
) {
$out = $footerIcons['copyright']['copyright'];
$copyright = $footerIcons['copyright']['copyright'] ?? null;
// T291325: $wgFooterIcons['copyright']['copyright'] can return an array.
if ( $copyright !== null ) {
$out = $skin->makeFooterIcon( $copyright );
} elseif ( $config->get( 'RightsIcon' ) ) {
$icon = htmlspecialchars( $config->get( 'RightsIcon' ) );
$url = $config->get( 'RightsUrl' );

View file

@ -940,7 +940,7 @@ abstract class Skin extends ContextSource {
*/
protected function getCopyrightIcon() {
wfDeprecated( __METHOD__, '1.37' );
return BaseTemplate::getCopyrightIconHTML( $this->getConfig() );
return BaseTemplate::getCopyrightIconHTML( $this->getConfig(), $this );
}
/**

View file

@ -379,8 +379,8 @@ class SkinTemplate extends Skin {
$tpl->set( 'credits', $footerData['info']['credits'] ?? false );
$tpl->set( 'numberofwatchingusers', false );
$tpl->set( 'copyrightico', BaseTemplate::getCopyrightIconHTML( $this->getConfig() ) );
$poweredBy = BaseTemplate::getPoweredByHTML( $this->getConfig() );
$tpl->set( 'copyrightico', BaseTemplate::getCopyrightIconHTML( $config, $this ) );
$poweredBy = BaseTemplate::getPoweredByHTML( $config );
// Run deprecated hook.
$this->getHookRunner()->onSkinGetPoweredBy( $poweredBy, $this );
$tpl->set( 'poweredbyico', $poweredBy );

View file

@ -16,18 +16,35 @@ class BaseTemplateTest extends MediaWikiUnitTestCase {
'',
'Returns empty string when no configuration'
],
// When $wgFooterIcons['copyright']['copyright'] returns an array (T291325)
[
[
'FooterIcons' => [
'copyright' => [
'copyright' => '<img src="footer-copyright.png">'
'copyright' => [
'src' => 'footer-copyright.png',
'url' => 'https://t.ui',
'alt' => 'Alt text',
]
]
],
'RightsIcon' => 'copyright.png',
'RightsUrl' => 'https://',
'RightsText' => 'copyright'
],
'<img src="footer-copyright.png">',
'<a href="https://t.ui"><img src="footer-copyright.png" alt="Alt text" width="88" height="31" /></a>',
'Reads from FooterIcons first'
],
// When $wgFooterIcons['copyright']['copyright'] returns a string (T291325)
[
[
'FooterIcons' => [
'copyright' => [
'copyright' => 'footer-copyright.png'
]
]
],
'<img src="footer-copyright.png" width="88" height="31" />',
'Reads from FooterIcons first'
],
[
@ -58,8 +75,10 @@ class BaseTemplateTest extends MediaWikiUnitTestCase {
* @dataProvider provideGetCopyrightIconHTML
*/
public function testGetCopyrightIconHTML( $globals, $expected, $msg ) {
$mockSkin = $this->createMock( Skin::class );
$mockSkin->method( 'makeFooterIcon' )->willReturn( $expected );
$hashConfig = new HashConfig( $globals );
$icon = BaseTemplate::getCopyrightIconHTML( $hashConfig );
$icon = BaseTemplate::getCopyrightIconHTML( $hashConfig, $mockSkin );
$this->assertEquals( $expected, $icon, $msg );
}
}