Henning Snater has uploaded a new change for review. https://gerrit.wikimedia.org/r/102643
Change subject: Display qualifiers in non-JavaScript UI ...................................................................... Display qualifiers in non-JavaScript UI Change-Id: Ic2d3c32718c40f8ed809a7297aa7c3caa7a0044a --- M repo/includes/ClaimHtmlGenerator.php M repo/includes/EntityView.php M repo/tests/phpunit/includes/ClaimHtmlGeneratorTest.php 3 files changed, 182 insertions(+), 36 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/43/102643/1 diff --git a/repo/includes/ClaimHtmlGenerator.php b/repo/includes/ClaimHtmlGenerator.php index f8623b8..e49e8c2 100644 --- a/repo/includes/ClaimHtmlGenerator.php +++ b/repo/includes/ClaimHtmlGenerator.php @@ -14,8 +14,7 @@ * @since 0.4 * @licence GNU GPL v2+ * - * @author H. Snater < mediawiki at snater.com > - * @author Daniel Werner + * @author H. Snater < [email protected] > * @author Pragunbhutani * @author Katie Filbert < [email protected]> */ @@ -29,28 +28,51 @@ protected $snakFormatter; /** + * @since 0.5 + * + * @var EntityTitleLookup + */ + protected $entityTitleLookup; + + /** + * Array of property labels indexed by serialized property ids + * @since 0.5 + * + * @var string[] + */ + protected $propertyLabels; + + /** * Constructor. * * @param SnakFormatter $snakFormatter + * @param EntityTitleLookup $entityTitleLookup + * @param string[] $propertyLabels */ - public function __construct( SnakFormatter $snakFormatter ) { + public function __construct( + SnakFormatter $snakFormatter, + EntityTitleLookup $entityTitleLookup, + $propertyLabels = array() + ) { $this->snakFormatter = $snakFormatter; + $this->entityTitleLookup = $entityTitleLookup; + $this->propertyLabels = $propertyLabels; } /** * Returns the Html for the main Snak. * - * @param DataValue $value + * @param string $formattedValue * @return string */ - protected function getMainSnakHtml( $value ) { + protected function getMainSnakHtml( $formattedValue ) { $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 + ( $formattedValue === '' ) ? ' ' : $formattedValue ); return $mainSnakHtml; @@ -66,21 +88,12 @@ * * @return string */ - public function getHtmlForClaim( - Claim $claim, - $editSectionHtml = null - ) { + 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 ); + $mainSnakHtml = $this->getMainSnakHtml( + $this->getFormattedSnakValue( $claim->getMainSnak() ) + ); $rankHtml = ''; @@ -100,7 +113,7 @@ $rankHtml, $claim->getGuid(), $mainSnakHtml, - '', // TODO: Qualifiers + $this->getHtmlForQualifiers( $claim->getQualifiers() ), // TODO: Qualifiers $editSectionHtml, '', // TODO: References heading '' // TODO: References @@ -109,4 +122,65 @@ wfProfileOut( __METHOD__ ); return $claimHtml; } + + /** + * Generates and returns the HTML representing a claim's qualifiers. + * + * @param Snaks $qualifiers + * @return string + */ + protected function getHtmlForQualifiers( Snaks $qualifiers ) { + $qualifiersByProperty = new ByPropertyIdArray( $qualifiers ); + $qualifiersByProperty->buildIndex(); + + $snaklistviewsHtml = ''; + + foreach( $qualifiersByProperty->getPropertyIds() as $propertyId ) { + $snaksHtml = ''; + $i = 0; + + foreach( $qualifiersByProperty->getByPropertyId( $propertyId ) as $snak ) { + + $propertyKey = $propertyId->getSerialization(); + $propertyLabel = isset( $this->propertyLabels[$propertyKey] ) + ? $this->propertyLabels[$propertyKey] + : $propertyKey; + $propertyLink = \Linker::link( + $this->entityTitleLookup->getTitleForId( $propertyId ), + htmlspecialchars( $propertyLabel ) + ); + + $snaksHtml .= wfTemplate( 'wb-snak', + 'wb-snakview', + // Display property link only once for snaks featuring the same property: + $i++ === 0 ? $propertyLink : '', + '', + ( $snak->getType() === 'value' ) ? $this->getFormattedSnakValue( $snak ) : '' + ); + } + + $snaklistviewsHtml .= wfTemplate( + 'wb-snaklistview', + $snaksHtml + ); + } + + return wfTemplate( 'wb-listview', + $snaklistviewsHtml + ); + } + + /** + * @param Snak $snak + * @return string + */ + protected function getFormattedSnakValue( $snak ) { + try { + return $this->snakFormatter->formatSnak( $snak ); + } catch ( FormattingException $ex ) { + return '?'; // XXX: perhaps show error message? + } catch ( PropertyNotFoundException $ex ) { + return '?'; // XXX: perhaps show error message? + } + } } diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php index a3833d2..5d33abf 100644 --- a/repo/includes/EntityView.php +++ b/repo/includes/EntityView.php @@ -572,7 +572,7 @@ $claimsByProperty[$propertyId->getNumericId()][] = $claim; } - $labels = $this->getPropertyLabels( $entity, $languageCode ); + $propertyLabels = $this->getPropertyLabels( $entity, $languageCode ); /** * @var string $claimsHtml @@ -584,7 +584,9 @@ $propertyId = $claims[0]->getMainSnak()->getPropertyId(); $propertyKey = $propertyId->getSerialization(); - $propertyLabel = isset( $labels[$propertyKey] ) ? $labels[$propertyKey] : $propertyKey; + $propertyLabel = isset( $propertyLabels[$propertyKey] ) + ? $propertyLabels[$propertyKey] + : $propertyKey; $propertyLink = \Linker::link( $this->entityTitleLookup->getTitleForId( $propertyId ), htmlspecialchars( $propertyLabel ) @@ -593,7 +595,9 @@ $htmlForEditSection = $this->getHtmlForEditSection( '', 'span' ); // TODO: add link to SpecialPage $claimHtmlGenerator = new ClaimHtmlGenerator( - $this->snakFormatter + $this->snakFormatter, + $this->entityTitleLookup, + $propertyLabels ); foreach( $claims as $claim ) { diff --git a/repo/tests/phpunit/includes/ClaimHtmlGeneratorTest.php b/repo/tests/phpunit/includes/ClaimHtmlGeneratorTest.php index a029011..30d1d17 100644 --- a/repo/tests/phpunit/includes/ClaimHtmlGeneratorTest.php +++ b/repo/tests/phpunit/includes/ClaimHtmlGeneratorTest.php @@ -2,9 +2,16 @@ namespace Wikibase\Test; +use DataValues\StringValue; +use Title; use Wikibase\Claim; use Wikibase\ClaimHtmlGenerator; +use Wikibase\DataModel\Entity\EntityId; +use Wikibase\EntityTitleLookup; +use Wikibase\Lib\DispatchingSnakFormatter; use Wikibase\PropertySomeValueSnak; +use Wikibase\PropertyValueSnak; +use Wikibase\SnakList; /** * @covers Wikibase\ClaimHtmlGenerator @@ -16,21 +23,15 @@ * * @licence GNU GPL v2+ * @author Katie Filbert < [email protected] > + * @author Daniel Kinzler + * @author H. Snater < [email protected] > */ class ClaimHtmlGeneratorTest extends \PHPUnit_Framework_TestCase { /** - * @dataProvider getHtmlForClaimProvider + * @return DispatchingSnakFormatter */ - 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!/'; - + protected function getSnakFormatterMock() { $snakFormatter = $this->getMockBuilder( 'Wikibase\Lib\DispatchingSnakFormatter' ) ->disableOriginalConstructor() ->getMock(); @@ -39,11 +40,78 @@ ->method( 'formatSnak' ) ->will( $this->returnValue( 'a snak!' ) ); - $claim = new Claim( new PropertySomeValueSnak( 42 ) ); + return $snakFormatter; + } - return array( - array( $expected, $snakFormatter, $claim ) + /** + * @param EntityId $id + * @return Title + */ + public function getTitleForId( EntityId $id ) { + $name = $id->getEntityType() . ':' . $id->getPrefixedId(); + return Title::makeTitle( NS_MAIN, $name ); + } + + /** + * @return EntityTitleLookup + */ + protected function getEntityTitleLookupMock() { + $lookup = $this->getMock( 'Wikibase\EntityTitleLookup' ); + $lookup->expects( $this->any() ) + ->method( 'getTitleForId' ) + ->will( $this->returnCallback( array( $this, 'getTitleForId' ) ) ); + + return $lookup; + } + + /** + * @dataProvider getHtmlForClaimProvider + */ + public function testGetHtmlForClaim( + $snakFormatter, + $entityTitleLookup, + $propertyLabels, + $claim, + $pattern + ) { + $claimHtmlGenerator = new ClaimHtmlGenerator( + $snakFormatter, + $entityTitleLookup, + $propertyLabels ); + $html = $claimHtmlGenerator->getHtmlForClaim( $claim, 'edit' ); + $this->assertRegExp( $pattern, $html ); + } + + public function getHtmlForClaimProvider() { + $snakFormatter = $this->getSnakFormatterMock(); + + $entityTitleLookupMock = $this->getEntityTitleLookupMock(); + + $testCases = array(); + + $testCases[] = array( + $snakFormatter, + $entityTitleLookupMock, + array(), + new Claim( new PropertySomeValueSnak( 42 ) ), + '/a snak!/' + ); + + $testCases[] = array( + $snakFormatter, + $entityTitleLookupMock, + array(), + new Claim( + new PropertySomeValueSnak( 42 ), + new SnakList( array( + new PropertyValueSnak( 50, new StringValue( 'second snak' ) ), + ) ) + ), + '/a snak!.*a snak!/s' + ); + + return $testCases; } } -- To view, visit https://gerrit.wikimedia.org/r/102643 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic2d3c32718c40f8ed809a7297aa7c3caa7a0044a Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Henning Snater <[email protected]> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
