jenkins-bot has submitted this change and it was merged.
Change subject: Factor out getHtmlForClaim from EntityView
......................................................................
Factor out getHtmlForClaim from EntityView
Bug: 50578
Change-Id: I1b0825aabf5d7dd498119eec4f45dee19c83e8e3
---
M repo/Wikibase.classes.php
A repo/includes/ClaimHtmlGenerator.php
M repo/includes/EntityView.php
A repo/tests/phpunit/includes/ClaimHtmlGeneratorTest.php
4 files changed, 162 insertions(+), 66 deletions(-)
Approvals:
Daniel Kinzler: Looks good to me, approved
jenkins-bot: Verified
diff --git a/repo/Wikibase.classes.php b/repo/Wikibase.classes.php
index 9d7058b..0d31b3b 100644
--- a/repo/Wikibase.classes.php
+++ b/repo/Wikibase.classes.php
@@ -49,6 +49,7 @@
'Wikibase\Summary' => 'includes/Summary.php',
'Wikibase\SummaryFormatter' => 'includes/SummaryFormatter.php',
'Wikibase\Repo\WikibaseRepo' => 'includes/WikibaseRepo.php',
+ 'Wikibase\ClaimHtmlGenerator' =>
'includes/ClaimHtmlGenerator.php',
// includes/ChangeOp
'Wikibase\ChangeOp\ChangeOps' =>
'includes/ChangeOp/ChangeOps.php',
diff --git a/repo/includes/ClaimHtmlGenerator.php
b/repo/includes/ClaimHtmlGenerator.php
new file mode 100644
index 0000000..dae57e9
--- /dev/null
+++ b/repo/includes/ClaimHtmlGenerator.php
@@ -0,0 +1,103 @@
+<?php
+
+namespace Wikibase;
+
+use Html;
+use Language;
+use MWException;
+use Wikibase\Lib\EntityIdFormatter;
+use Wikibase\Lib\SnakFormatter;
+
+/**
+ * Base class for generating the HTML for a Claim in Entity View.
+ *
+ * @since 0.4
+ * @licence GNU GPL v2+
+ *
+ * @author H. Snater < mediawiki at snater.com >
+ * @author Daniel Werner
+ * @author Pragunbhutani
+ * @author Katie Filbert < [email protected]>
+ */
+class ClaimHtmlGenerator {
+
+ /**
+ * @since 0.4
+ *
+ * @var SnakFormatter
+ */
+ protected $snakFormatter;
+
+ /**
+ * Constructor.
+ *
+ * @param SnakFormatter $snakFormatter
+ */
+ public function __construct( SnakFormatter $snakFormatter ) {
+ $this->snakFormatter = $snakFormatter;
+ }
+
+ /**
+ * Returns the Html for the main Snak.
+ *
+ * @param DataValue $value
+ * @return string
+ */
+ protected function getMainSnakHtml( $value ) {
+ $mainSnakHtml = wfTemplate( 'wb-snak',
+ 'wb-mainsnak',
+ '', // Link to property. NOTE: we don't display this
ever (instead, we generate it on
+ // Claim group level) If this was a public
function, this should be generated
+ // anyhow since important when displaying a
Claim on its own.
+ '', // type selector, JS only
+ ( $value === '' ) ? ' ' : $value
+ );
+
+ return $mainSnakHtml;
+ }
+
+ /**
+ * Builds and returns the HTML representing a single WikibaseEntity's
claim.
+ *
+ * @since 0.4
+ *
+ * @param EntityContent $entity the entity related to the claim
+ * @param Claim $claim the claim to render
+ * @param Language|null $lang the language to use for rendering. if not
given, the local
+ * context will be used.
+ * @param bool $editable whether editing is allowed (enabled edit links)
+ * @param editSectionHtml has the html for the edit section
+ * @return string
+ */
+ public function getHtmlForClaim(
+ Claim $claim,
+ $editSectionHtml = null
+ ) {
+ wfProfileIn( __METHOD__ );
+
+ try {
+ $snakValueHtml = $this->snakFormatter->formatSnak(
$claim->getMainSnak() );
+ } catch ( FormattingException $ex ) {
+ $snakValueHtml = '?'; // XXX: perhaps show error
message?
+ } catch ( PropertyNotFoundException $ex ) {
+ $snakValueHtml = '?'; // XXX: perhaps show error
message?
+ }
+
+ $mainSnakHtml = $this->getMainSnakHtml( $snakValueHtml );
+
+ // @todo: Use 'wb-claim' or 'wb-statement' template accordingly
+ // @todo: get rid of usage of global wfTemplate function
+ $claimHtml = wfTemplate( 'wb-statement',
+ '', // additional classes
+ $claim->getGuid(),
+ $mainSnakHtml,
+ '', // TODO: Qualifiers
+ $editSectionHtml,
+ '', // TODO: References heading
+ '' // TODO: References
+ );
+
+ wfProfileOut( __METHOD__ );
+ return $claimHtml;
+ }
+}
diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php
index 8d6bee7..cbc3c99 100644
--- a/repo/includes/EntityView.php
+++ b/repo/includes/EntityView.php
@@ -15,6 +15,7 @@
use FormatJson;
use User;
use ValueParsers\FormattingException;
+use Wikibase\ClaimHtmlGenerator;
use Wikibase\Lib\EntityIdFormatter;
use Wikibase\Lib\PropertyDataTypeLookup;
use Wikibase\Lib\PropertyNotFoundException;
@@ -25,6 +26,7 @@
use Wikibase\Lib\SnakFormatter;
use ValueFormatters\FormatterOptions;
use ValueFormatters\ValueFormatter;
+use ValueFormatters\ValueFormatterFactory;
use ValueFormatters\TimeFormatter;
use Wikibase\Repo\WikibaseRepo;
use Wikibase\Lib\MwTimeIsoFormatter;
@@ -576,9 +578,14 @@
);
}
- $i = 0;
+ $htmlForEditSection = $this->getHtmlForEditSection(
$entity, $lang, '', 'span' ); // TODO: add link to SpecialPage
+
+ $claimHtmlGenerator = new ClaimHtmlGenerator(
+ $this->snakFormatter
+ );
+
foreach( $claims as $claim ) {
- $propertyHtml .= $this->getHtmlForClaim(
$entity, $claim, $lang, $editable );
+ $propertyHtml .=
$claimHtmlGenerator->getHtmlForClaim( $claim, $htmlForEditSection );
}
$toolbarHtml = wfTemplate( 'wikibase-toolbar',
@@ -601,70 +608,6 @@
wfProfileOut( __METHOD__ );
return $html;
- }
-
- /**
- * Builds and returns the HTML representing a single WikibaseEntity's
claim.
- *
- * @since 0.4
- *
- * @param Entity $entity the entity related to the claim
- * @param Claim $claim the claim to render
- * @param Language $lang the language to use for rendering. if not
given, the local
- * context will be used.
- * @param bool $editable whether editing is allowed (enabled edit links)
- * @return string
- *
- * @throws MWException If a claim's value can't be displayed because
the related value formatter
- * is not yet implemented or provided in the constructor. (Also
see related todo)
- */
- protected function getHtmlForClaim(
- Entity $entity,
- Claim $claim,
- Language $lang,
- $editable = true
- ) {
- wfProfileIn( __METHOD__ );
-
- $languageCode = $lang->getCode();
-
- $valueFormatterOptions = new FormatterOptions( array(
- ValueFormatter::OPT_LANG => $languageCode,
- TimeFormatter::OPT_TIME_ISO_FORMATTER => new
MwTimeIsoFormatter(
- new FormatterOptions( array(
ValueFormatter::OPT_LANG => $languageCode ) )
- ),
- ) );
-
- try {
- $snakValueHtml = $this->snakFormatter->formatSnak(
$claim->getMainSnak() );
- } catch ( FormattingException $ex ) {
- $snakValueHtml = '?'; // XXX: perhaps show error
message?
- } catch ( PropertyNotFoundException $ex ) {
- $snakValueHtml = '?'; // XXX: perhaps show error
message?
- }
-
- $mainSnakHtml = wfTemplate( 'wb-snak',
- 'wb-mainsnak',
- '', // Link to property. NOTE: we don't display this
ever (instead, we generate it on
- // Claim group level) If this was a public
function, this should be generated
- // anyhow since important when displaying a
Claim on its own.
- '', // type selector, JS only
- ( $snakValueHtml === '' ) ? ' ' : $snakValueHtml
- );
-
- // TODO: Use 'wb-claim' or 'wb-statement' template accordingly
- $claimHtml = wfTemplate( 'wb-statement',
- '', // additional classes
- $claim->getGuid(),
- $mainSnakHtml,
- '', // TODO: Qualifiers
- $this->getHtmlForEditSection( $entity, $lang, '',
'span' ), // TODO: add link to SpecialPage
- '', // TODO: References heading
- '' // TODO: References
- );
-
- wfProfileOut( __METHOD__ );
- return $claimHtml;
}
/**
diff --git a/repo/tests/phpunit/includes/ClaimHtmlGeneratorTest.php
b/repo/tests/phpunit/includes/ClaimHtmlGeneratorTest.php
new file mode 100644
index 0000000..a029011
--- /dev/null
+++ b/repo/tests/phpunit/includes/ClaimHtmlGeneratorTest.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\Claim;
+use Wikibase\ClaimHtmlGenerator;
+use Wikibase\PropertySomeValueSnak;
+
+/**
+ * @covers Wikibase\ClaimHtmlGenerator
+ *
+ * @since 0.4
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < [email protected] >
+ */
+class ClaimHtmlGeneratorTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @dataProvider getHtmlForClaimProvider
+ */
+ public function testGetHtmlForClaim( $pattern, $snakFormatter, $claim )
{
+ $claimHtmlGenerator = new ClaimHtmlGenerator( $snakFormatter );
+ $html = $claimHtmlGenerator->getHtmlForClaim( $claim, 'edit' );
+ $this->assertRegExp( $pattern, $html );
+ }
+
+ public function getHtmlForClaimProvider() {
+ $expected = '/a snak!/';
+
+ $snakFormatter = $this->getMockBuilder(
'Wikibase\Lib\DispatchingSnakFormatter' )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $snakFormatter->expects( $this->any() )
+ ->method( 'formatSnak' )
+ ->will( $this->returnValue( 'a snak!' ) );
+
+ $claim = new Claim( new PropertySomeValueSnak( 42 ) );
+
+ return array(
+ array( $expected, $snakFormatter, $claim )
+ );
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/72909
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I1b0825aabf5d7dd498119eec4f45dee19c83e8e3
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Pragunbhutani <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits