Adrian Lang has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/114746

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
---
M lib/includes/formatters/CommonsLinkFormatter.php
M lib/tests/phpunit/formatters/CommonsLinkFormatterTest.php
M lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
3 files changed, 30 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/46/114746/1

diff --git a/lib/includes/formatters/CommonsLinkFormatter.php 
b/lib/includes/formatters/CommonsLinkFormatter.php
index 254246c..4fa4813 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,12 @@
                }
 
                $fileName = $value->getValue();
+               $title = Title::newFromText( $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'
                        ),
                );

-- 
To view, visit https://gerrit.wikimedia.org/r/114746
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4cac81188e5a09bd1a4cbf5ccdf3c579fce9821a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Adrian Lang <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to