jenkins-bot has submitted this change and it was merged. Change subject: (bug 51113) Introducing HtmlUrlFormatter ......................................................................
(bug 51113) Introducing HtmlUrlFormatter Change-Id: I616ae769ebb9f4e673503a32f45e82c89185a6aa --- M lib/WikibaseLib.classes.php A lib/includes/formatters/HtmlUrlFormatter.php M lib/includes/formatters/WikibaseValueFormatterBuilders.php A lib/tests/phpunit/formatters/HtmlUrlFormatterTest.php M lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php 5 files changed, 124 insertions(+), 7 deletions(-) Approvals: Addshore: Looks good to me, approved jenkins-bot: Verified diff --git a/lib/WikibaseLib.classes.php b/lib/WikibaseLib.classes.php index 66cfa33..4d61156 100644 --- a/lib/WikibaseLib.classes.php +++ b/lib/WikibaseLib.classes.php @@ -115,6 +115,7 @@ 'Wikibase\Lib\MwTimeIsoFormatter' => 'includes/formatters/MwTimeIsoFormatter.php', 'Wikibase\Lib\EscapingValueFormatter' => 'includes/formatters/EscapingValueFormatter.php', 'Wikibase\Lib\FormattingException' => 'includes/formatters/FormattingException.php', + 'Wikibase\Lib\HtmlUrlFormatter' => 'includes/formatters/HtmlUrlFormatter.php', 'Wikibase\Lib\MessageSnakFormatter' => 'includes/formatters/MessageSnakFormatter.php', 'Wikibase\Lib\PropertyValueSnakFormatter' => 'includes/formatters/PropertyValueSnakFormatter.php', 'Wikibase\Lib\SnakFormatter' => 'includes/formatters/SnakFormatter.php', diff --git a/lib/includes/formatters/HtmlUrlFormatter.php b/lib/includes/formatters/HtmlUrlFormatter.php new file mode 100644 index 0000000..617ede2 --- /dev/null +++ b/lib/includes/formatters/HtmlUrlFormatter.php @@ -0,0 +1,59 @@ +<?php + +namespace Wikibase\Lib; + +use DataValues\IriValue; +use DataValues\StringValue; +use Html; +use InvalidArgumentException; +use ValueFormatters\FormatterOptions; +use ValueFormatters\ValueFormatter; + +/** + * Formats a StringValue as an HTML link. + * + * @since 0.5 + * + * @licence GNU GPL v2+ + * @author Daniel Kinzler + */ +class HtmlUrlFormatter implements ValueFormatter { + + /** + * @var array HTML attributes to use on the generated <a> tags. + */ + protected $attributes; + + public function __construct( FormatterOptions $options ) { + //TODO: configure from options + $this->attributes = array( + 'rel' => 'nofollow', + 'class' => 'external free' + ); + } + + /** + * Formats the given URL as an HTML link + * + * @since 0.5 + * + * @param StringValue|IriValue $value The URL to turn into a link + * + * @return string + * + * @throws InvalidArgumentException + */ + public function format( $value ) { + if ( !( $value instanceof StringValue ) ) { + throw new InvalidArgumentException( 'Data value type mismatch. Expected a StringValue.' ); + } + + $url = $value->getValue(); + + $attributes = array_merge( $this->attributes, array( 'href' => $url ) ); + $html = Html::element( 'a', $attributes, $url ); + + return $html; + } + +} diff --git a/lib/includes/formatters/WikibaseValueFormatterBuilders.php b/lib/includes/formatters/WikibaseValueFormatterBuilders.php index b5b4ff7..e224120 100644 --- a/lib/includes/formatters/WikibaseValueFormatterBuilders.php +++ b/lib/includes/formatters/WikibaseValueFormatterBuilders.php @@ -80,7 +80,7 @@ // Formatters to use for HTML display. // Falls back to plain text formatters (plus escaping). SnakFormatter::FORMAT_HTML => array( - //'PT:url' => 'Wikibase\Lib\LinkFormatter', // TODO + 'PT:url' => 'Wikibase\Lib\HtmlUrlFormatter', //'PT:commonsMedia' => 'Wikibase\Lib\CommonsLinkFormatter', // TODO //'PT:wikibase-item' => 'Wikibase\Lib\ItemLinkFormatter', // TODO ), diff --git a/lib/tests/phpunit/formatters/HtmlUrlFormatterTest.php b/lib/tests/phpunit/formatters/HtmlUrlFormatterTest.php new file mode 100644 index 0000000..a31c7f4 --- /dev/null +++ b/lib/tests/phpunit/formatters/HtmlUrlFormatterTest.php @@ -0,0 +1,60 @@ +<?php + +namespace Wikibase\Lib\Test; + +use DataValues\IriValue; +use DataValues\NumberValue; +use DataValues\StringValue; +use ValueFormatters\FormatterOptions; +use Wikibase\Lib\HtmlUrlFormatter; + +/** + * @covers Wikibase\Lib\HtmlUrlFormatter + * + * @since 0.5 + * + * @group ValueFormatters + * @group DataValueExtensions + * @group WikibaseLib + * @group Wikibase + * + * @licence GNU GPL v2+ + * @author Daniel Kinzler + */ +class HtmlUrlFormatterTest extends \PHPUnit_Framework_TestCase { + + /** + * @dataProvider urlFormatProvider + * + * @covers HtmlUrlFormatterTest::format() + */ + public function testFormat( $value, $options, $pattern ) { + $formatter = new HtmlUrlFormatter( $options ); + + $html = $formatter->format( $value ); + $this->assertRegExp( $pattern, $html ); + } + + public function urlFormatProvider() { + $options = new FormatterOptions(); + + return array( + array( + new StringValue( 'http://acme.com' ), + $options, + '@<a .*href="http://acme\.com".*>.*http://acme\.com.*</a>@' + ), + ); + } + + /** + * @covers HtmlUrlFormatterTest::format() + */ + public function testFormatError() { + $formatter = new HtmlUrlFormatter( new FormatterOptions() ); + $value = new NumberValue( 23 ); + + $this->setExpectedException( 'InvalidArgumentException' ); + $formatter->format( $value ); + } +} diff --git a/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php b/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php index 0135411..81c8d97 100644 --- a/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php +++ b/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php @@ -2,9 +2,7 @@ namespace Wikibase\Lib\Test; -use DataValues\GlobeCoordinateValue; use DataValues\StringValue; -use DataValues\TimeValue; use DataValues\UnDeserializableValue; use Language; use ValueFormatters\FormatterOptions; @@ -12,7 +10,6 @@ use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\EntityIdValue; use Wikibase\DataModel\Entity\ItemId; -use Wikibase\DataModel\Entity\PropertyId; use Wikibase\EntityFactory; use Wikibase\Lib\SnakFormatter; use Wikibase\Lib\OutputFormatSnakFormatterFactory; @@ -145,12 +142,12 @@ new PropertyValueSnak( 7, new EntityIdValue( new ItemId( 'Q5' ) ) ), 'Label for Q5' // compare mock object created in newBuilders() ), - 'diff <url>' => array( + 'diff url' => array( SnakFormatter::FORMAT_HTML_DIFF, $options, 'url', - new PropertyValueSnak( 7, new StringValue( '<http://acme.com/>' ) ), - '<http://acme.com/>' + new PropertyValueSnak( 7, new StringValue( 'http://acme.com/' ) ), + '<a rel="nofollow" class="external free" href="http://acme.com/">http://acme.com/</a>' ), 'bad value' => array( SnakFormatter::FORMAT_PLAIN, -- To view, visit https://gerrit.wikimedia.org/r/98121 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I616ae769ebb9f4e673503a32f45e82c89185a6aa Gerrit-PatchSet: 5 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com> Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits