jenkins-bot has submitted this change and it was merged. Change subject: Correctly format commons links with spaces ......................................................................
Correctly format commons links with spaces Spaces should be replaced with underscores in URLs, underscores should be replaced with spaces for link texts, plus signs should stay what they are. Using Title here is probably not completely sane since it takes the local configuration into account. It works, though. Bug: 61738 Bug: 45046 Change-Id: I4cac81188e5a09bd1a4cbf5ccdf3c579fce9821a (cherry picked from commit 24984f6f2e13c5e006c4f9ec1c57c0a194845a6a) --- M lib/includes/formatters/CommonsLinkFormatter.php M lib/tests/phpunit/formatters/CommonsLinkFormatterTest.php M lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php M repo/tests/phpunit/includes/api/FormatSnakValueTest.php 4 files changed, 33 insertions(+), 7 deletions(-) Approvals: Aude: Looks good to me, approved WikidataJenkins: Verified jenkins-bot: Verified diff --git a/lib/includes/formatters/CommonsLinkFormatter.php b/lib/includes/formatters/CommonsLinkFormatter.php index 254246c..d406216 100644 --- a/lib/includes/formatters/CommonsLinkFormatter.php +++ b/lib/includes/formatters/CommonsLinkFormatter.php @@ -5,6 +5,7 @@ use DataValues\StringValue; use Html; use InvalidArgumentException; +use Title; use ValueFormatters\FormatterOptions; use ValueFormatters\ValueFormatter; @@ -49,9 +50,14 @@ } $fileName = $value->getValue(); + // We are using NS_MAIN only because makeTitleSafe requires a valid namespace + // We cannot use makeTitle because it does not secureAndSplit() + $title = Title::makeTitleSafe( NS_MAIN, $fileName ); - $attributes = array_merge( $this->attributes, array( 'href' => '//commons.wikimedia.org/wiki/' . wfUrlencode( 'File:' . $fileName ) ) ); - $html = Html::element( 'a', $attributes, $fileName ); + $attributes = array_merge( $this->attributes, array( + 'href' => '//commons.wikimedia.org/wiki/' . 'File:' . $title->getPartialURL() + ) ); + $html = Html::element( 'a', $attributes, $title->getText() ); return $html; } diff --git a/lib/tests/phpunit/formatters/CommonsLinkFormatterTest.php b/lib/tests/phpunit/formatters/CommonsLinkFormatterTest.php index f816ffb..7fd4a0d 100644 --- a/lib/tests/phpunit/formatters/CommonsLinkFormatterTest.php +++ b/lib/tests/phpunit/formatters/CommonsLinkFormatterTest.php @@ -25,9 +25,29 @@ return array( array( - new StringValue( 'example.jpg' ), + new StringValue( 'example.jpg' ), // Lower-case file name $options, - '@<a .*href="//commons.wikimedia.org/wiki/File:example.jpg".*>.*example.jpg.*</a>@' + '@<a .*href="//commons.wikimedia.org/wiki/File:Example.jpg".*>.*Example.jpg.*</a>@' + ), + array( + new StringValue( 'Example.jpg' ), + $options, + '@<a .*href="//commons.wikimedia.org/wiki/File:Example.jpg".*>.*Example.jpg.*</a>@' + ), + array( + new StringValue( 'Example space.jpg' ), + $options, + '@<a .*href="//commons.wikimedia.org/wiki/File:Example_space.jpg".*>.*Example space.jpg.*</a>@' + ), + array( + new StringValue( 'Example_underscore.jpg' ), + $options, + '@<a .*href="//commons.wikimedia.org/wiki/File:Example_underscore.jpg".*>.*Example underscore.jpg.*</a>@' + ), + array( + new StringValue( 'Example+plus.jpg' ), + $options, + '@<a .*href="//commons.wikimedia.org/wiki/File:Example%2Bplus.jpg".*>.*Example\+plus.jpg.*</a>@' ), ); } diff --git a/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php b/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php index ccfde58..2a268db 100644 --- a/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php +++ b/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php @@ -125,8 +125,8 @@ 'commons link' => array( SnakFormatter::FORMAT_HTML, $options, - new StringValue( 'example.jpg' ), - '@^<a class="extiw" href="//commons\\.wikimedia\\.org/wiki/File:example\\.jpg">example\\.jpg</a>$@', + new StringValue( 'Example.jpg' ), + '@^<a class="extiw" href="//commons\\.wikimedia\\.org/wiki/File:Example\\.jpg">Example\\.jpg</a>$@', 'commonsMedia' ), ); diff --git a/repo/tests/phpunit/includes/api/FormatSnakValueTest.php b/repo/tests/phpunit/includes/api/FormatSnakValueTest.php index 3f4e196..4c70191 100644 --- a/repo/tests/phpunit/includes/api/FormatSnakValueTest.php +++ b/repo/tests/phpunit/includes/api/FormatSnakValueTest.php @@ -91,7 +91,7 @@ 'commonsMedia', SnakFormatter::FORMAT_HTML, null, - '@commons\.wikimedia\.org\/wiki\/File:example\.jpg@' ), + '@commons\.wikimedia\.org\/wiki\/File:Example\.jpg@' ), // FIXME: This test uses the production environment, but it should have its own mock data array( new EntityIdValue( new ItemId( 'Q200000' ) ), -- To view, visit https://gerrit.wikimedia.org/r/115172 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I4cac81188e5a09bd1a4cbf5ccdf3c579fce9821a Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: mw1.23-wmf15 Gerrit-Owner: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Adrian Lang <adrian.l...@wikimedia.de> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: WikidataJenkins <wikidata-servi...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits