jenkins-bot has submitted this change and it was merged.

Change subject: Record usage of commons media files
......................................................................


Record usage of commons media files

This patch creates a new ValuesFinder class to find all
data values for a specified data type in an array of snaks.

This class also replaces the ReferencedUrlFinder class.

Change-Id: Ib2c4e49bb63e2ac574b1a2c2224c0a0a8238a860
---
D lib/includes/ReferencedUrlFinder.php
A lib/includes/ValuesFinder.php
D lib/tests/phpunit/ReferencedUrlFinderTest.php
A lib/tests/phpunit/ValuesFinderTest.php
M repo/includes/EntityView.php
M repo/tests/phpunit/includes/EntityViewTest.php
M repo/tests/phpunit/includes/ItemViewTest.php
M repo/tests/phpunit/includes/View/ClaimsViewTest.php
8 files changed, 265 insertions(+), 181 deletions(-)

Approvals:
  Daniel Kinzler: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/includes/ReferencedUrlFinder.php 
b/lib/includes/ReferencedUrlFinder.php
deleted file mode 100644
index 0396dd0..0000000
--- a/lib/includes/ReferencedUrlFinder.php
+++ /dev/null
@@ -1,85 +0,0 @@
-<?php
-
-namespace Wikibase;
-
-use DataValues\StringValue;
-use Wikibase\DataModel\Entity\PropertyId;
-use Wikibase\Lib\PropertyDataTypeLookup;
-use Wikibase\Lib\PropertyNotFoundException;
-
-/**
- * Finds URLs given a list of snaks.
- *
- * If a snaks property is not found or the type of DataValue
- * does not match the expected one for URLs, the snak is ignored
- * silently.
- *
- * @since 0.4
- *
- * @licence GNU GPL v2+
- * @author Daniel Kinzler
- * @author Jeroen De Dauw < [email protected] >
- */
-class ReferencedUrlFinder {
-
-       /**
-        * @since 0.4
-        *
-        * @var PropertyDataTypeLookup
-        */
-       protected $propertyDataTypeLookup;
-
-       /**
-        * @var string[]
-        */
-       protected $foundURLs;
-
-       /**
-        * @since 0.4
-        *
-        * @param PropertyDataTypeLookup $propertyDataTypeLookup
-        */
-       public function __construct( PropertyDataTypeLookup 
$propertyDataTypeLookup ) {
-               $this->propertyDataTypeLookup = $propertyDataTypeLookup;
-       }
-
-       /**
-        * @param Snak[] $snaks
-        *
-        * @return string[]
-        */
-       public function findSnakLinks( array $snaks ) {
-               $this->foundURLs = array();
-
-               foreach ( $snaks as $snak ) {
-                       if( $snak instanceof PropertyValueSnak ) {
-                               if ( $this->isUrlProperty( 
$snak->getPropertyId() ) ) {
-                                       $this->findPropertyValueSnakLinks( 
$snak );
-                               }
-                       }
-               }
-
-               return array_unique( $this->foundURLs );
-       }
-
-       protected function findPropertyValueSnakLinks( PropertyValueSnak $snak 
) {
-               $snakValue = $snak->getDataValue();
-
-               if ( $snakValue instanceof StringValue ) {
-                       $this->foundURLs[] = $snakValue->getValue();
-               }
-       }
-
-       protected function isUrlProperty( PropertyId $propertyId ) {
-               try {
-                       $type = 
$this->propertyDataTypeLookup->getDataTypeIdForProperty( $propertyId );
-               } catch ( PropertyNotFoundException $ex ) {
-                       return false;
-               }
-
-               return $type === 'url';
-       }
-
-}
-
-
diff --git a/lib/includes/ValuesFinder.php b/lib/includes/ValuesFinder.php
new file mode 100644
index 0000000..cb46f68
--- /dev/null
+++ b/lib/includes/ValuesFinder.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace Wikibase;
+
+use Wikibase\Lib\PropertyDataTypeLookup;
+use Wikibase\Lib\PropertyNotFoundException;
+
+/**
+ * Find all data values for a specified data type in an array of snaks.
+ *
+ * @since 0.5
+ *
+ * @license GNU GPL v2+
+ * @author Bene* < [email protected] >
+ */
+class ValuesFinder {
+
+       /**
+        * @var PropertyDataTypeLookup
+        */
+       private $propertyDataTypeLookup;
+
+       public function __construct( PropertyDataTypeLookup 
$propertyDataTypeLookup ) {
+               $this->propertyDataTypeLookup = $propertyDataTypeLookup;
+       }
+
+       /**
+        * Find all data values for the specified data type in the array of 
snaks.
+        *
+        * @param Snak[] $snaks
+        * @param string $dataType
+        *
+        * @return DataValue[]
+        */
+       public function findFromSnaks( array $snaks, $dataType ) {
+               $found = array();
+
+               foreach ( $snaks as $snak ) {
+                       if ( $this->isMatchingSnak( $snak, $dataType ) ) {
+                               $dataValue = $snak->getDataValue();
+                               $found[$dataValue->getHash()] = $dataValue;
+                       }
+               }
+
+               return $found;
+       }
+
+       private function isMatchingSnak( Snak $snak, $dataType ) {
+               if ( !$snak instanceof PropertyValueSnak ) {
+                       return false;
+               }
+
+               try {
+                       $type = 
$this->propertyDataTypeLookup->getDataTypeIdForProperty( $snak->getPropertyId() 
);
+               } catch ( PropertyNotFoundException $ex ) {
+                       return false;
+               }
+
+               if ( $type !== $dataType ) {
+                       return false;
+               }
+
+               return true;
+       }
+
+}
diff --git a/lib/tests/phpunit/ReferencedUrlFinderTest.php 
b/lib/tests/phpunit/ReferencedUrlFinderTest.php
deleted file mode 100644
index c7b0f2c..0000000
--- a/lib/tests/phpunit/ReferencedUrlFinderTest.php
+++ /dev/null
@@ -1,86 +0,0 @@
-<?php
-
-namespace Wikibase\Lib\Test;
-
-use DataValues\StringValue;
-use Wikibase\DataModel\Entity\EntityId;
-use Wikibase\DataModel\Entity\PropertyId;
-use Wikibase\DataModel\Snak\PropertyNoValueSnak;
-use Wikibase\DataModel\Snak\PropertySomeValueSnak;
-use Wikibase\DataModel\Snak\PropertyValueSnak;
-use Wikibase\DataModel\Snak\Snak;
-use Wikibase\Lib\InMemoryDataTypeLookup;
-use Wikibase\ReferencedUrlFinder;
-
-/**
- * @covers Wikibase\ReferencedUrlFinder
- *
- * @group Wikibase
- * @group WikibaseLib
- *
- * @licence GNU GPL v2+
- * @author Daniel Kinzler
- */
-class ReferencedUrlFinderTest extends \MediaWikiTestCase {
-
-       public function snaksProvider() {
-               $argLists = array();
-
-               $p23 = new PropertyId( 'p23' );
-               $p42 = new PropertyId( 'p42' );
-
-               $argLists["empty"] = array(
-                       array(),
-                       array() );
-
-               $argLists["PropertyNoValueSnak"] = array(
-                       array( new PropertyNoValueSnak( $p42 ) ),
-                       array());
-
-               $argLists["PropertySomeValueSnak"] = array(
-                       array( new PropertySomeValueSnak( $p42 ) ),
-                       array() );
-
-               $argLists["PropertyValueSnak with string value"] = array(
-                       array( new PropertyValueSnak( $p23, new StringValue( 
'http://not/a/url' )  ) ),
-                       array() );
-
-               $argLists["PropertyValueSnak with EntityId"] = array(
-                       array( new PropertyValueSnak( $p42, new StringValue( 
'http://acme.com/test' )  ) ),
-                       array( 'http://acme.com/test' ) );
-
-               return $argLists;
-       }
-
-       /**
-        * @dataProvider snaksProvider
-        *
-        * @param Snak[] $snaks
-        * @param EntityId[] $expected
-        */
-       public function testFindSnakLinks( array $snaks, array $expected ) {
-               $p23 = new PropertyId( 'p23' );
-               $p42 = new PropertyId( 'p42' );
-
-               $dataTypeLookup = new InMemoryDataTypeLookup();
-               $dataTypeLookup->setDataTypeForProperty( $p23, 'string' );
-               $dataTypeLookup->setDataTypeForProperty( $p42, 'url' );
-
-               $linkFinder = new ReferencedUrlFinder( $dataTypeLookup );
-               $actual = $linkFinder->findSnakLinks( $snaks );
-
-               $this->assertArrayEquals( $expected, $actual ); // 
assertArrayEquals doesn't take a message :(
-       }
-
-       public function testFindSnakLinksForUnknownProperty() {
-               $dataTypeLookup = new InMemoryDataTypeLookup();
-               $linkFinder = new ReferencedUrlFinder( $dataTypeLookup );
-
-               $p42 = new PropertyId( 'p42' );
-               $snaks = array( new PropertyValueSnak( $p42, new StringValue( 
'http://acme.com/test' )  ) );
-
-               $actual = $linkFinder->findSnakLinks( $snaks );
-               $this->assertEmpty( $actual ); // since $p42 isn't know, this 
should return nothing
-       }
-
-}
diff --git a/lib/tests/phpunit/ValuesFinderTest.php 
b/lib/tests/phpunit/ValuesFinderTest.php
new file mode 100644
index 0000000..32eace2
--- /dev/null
+++ b/lib/tests/phpunit/ValuesFinderTest.php
@@ -0,0 +1,124 @@
+<?php
+
+namespace Wikibase\Lib\Test;
+
+use DataValues\BooleanValue;
+use DataValues\DataValue;
+use DataValues\StringValue;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\DataModel\Snak\PropertyNoValueSnak;
+use Wikibase\DataModel\Snak\PropertySomeValueSnak;
+use Wikibase\DataModel\Snak\PropertyValueSnak;
+use Wikibase\DataModel\Snak\Snak;
+use Wikibase\Lib\InMemoryDataTypeLookup;
+use Wikibase\ValuesFinder;
+
+/**
+ * @covers Wikibase\ReferencedUrlFinder
+ *
+ * @group Wikibase
+ * @group WikibaseLib
+ *
+ * @licence GNU GPL v2+
+ * @author Bene* < [email protected] >
+ */
+class ValuesFinderTest extends \MediaWikiTestCase {
+
+       static $propertyDataTypes = array(
+               'P23' => 'string',
+               'P42' => 'url',
+               'P44' => 'boolean'
+       );
+
+       public function snaksProvider() {
+               $argLists = array();
+
+               $p23 = new PropertyId( 'p23' );
+               $p42 = new PropertyId( 'p42' );
+               $p44 = new PropertyId( 'p44' );
+               $p404 = new PropertyId( 'P404' );
+
+               $argLists["empty"] = array(
+                       array(),
+                       'url',
+                       array() );
+
+               $argLists["PropertyNoValueSnak"] = array(
+                       array( new PropertyNoValueSnak( $p42 ) ),
+                       'url',
+                       array());
+
+               $argLists["PropertySomeValueSnak"] = array(
+                       array( new PropertySomeValueSnak( $p42 ) ),
+                       'url',
+                       array() );
+
+               $argLists["PropertyValueSnak with string value and unknown data 
type"] = array(
+                       array( new PropertyValueSnak( $p404, new StringValue( 
'not an url' ) ) ),
+                       'url',
+                       array() );
+
+               $argLists["PropertyValueSnak with string value and wrong data 
type"] = array(
+                       array( new PropertyValueSnak( $p23, new StringValue( 
'not an url' ) ) ),
+                       'url',
+                       array() );
+
+               $argLists["PropertyValueSnak with string value and correct data 
type"] = array(
+                       array( new PropertyValueSnak( $p42, new StringValue( 
'http://acme.com/test' ) ) ),
+                       'url',
+                       array( 'http://acme.com/test' ) );
+
+               $argLists["PropertyValueSnak with boolean value"] = array(
+                       array( new PropertyValueSnak( $p42, new BooleanValue( 
true ) ) ),
+                       'url',
+                       array( true ) );
+
+               $argLists["PropertyValueSnak with string values and correct 
data type"] = array(
+                       array( new PropertyValueSnak( $p42, new StringValue( 
'http://acme.com/test' ) ),
+                                       new PropertyValueSnak( $p42, new 
StringValue( 'http://foo.bar/' ) ) ),
+                       'url',
+                       array( 'http://acme.com/test', 'http://foo.bar/' ) );
+
+               $argLists["PropertyValueSnak with boolean value and correct 
data type"] = array(
+                       array( new PropertyValueSnak( $p44, new BooleanValue( 
false ) ) ),
+                       'boolean',
+                       array( false ) );
+
+               $argLists["PropertyValueSnak with boolean value and wrong data 
type"] = array(
+                       array( new PropertyValueSnak( $p44, new BooleanValue( 
false ) ) ),
+                       'url',
+                       array() );
+
+               return $argLists;
+       }
+
+       /**
+        * @dataProvider snaksProvider
+        *
+        * @param Snak[] $snaks
+        * @param string $dataType
+        * @param string[] $expected
+        */
+       public function testFindFromSnaks( array $snaks, $dataType, array 
$expected ) {
+               $valuesFinder = $this->getValuesFinder();
+
+               $actual = $valuesFinder->findFromSnaks( $snaks, $dataType );
+
+               $actual = array_map( function( DataValue $dataValue ) {
+                       return $dataValue->getValue();
+               }, $actual );
+
+               $this->assertArrayEquals( $expected, $actual ); // 
assertArrayEquals doesn't take a message :(
+       }
+
+       private function getValuesFinder() {
+               $dataTypeLookup = new InMemoryDataTypeLookup();
+
+               foreach ( self::$propertyDataTypes as $propertyId => $dataType 
) {
+                       $dataTypeLookup->setDataTypeForProperty( new 
PropertyId( $propertyId ), $dataType );
+               }
+
+               return new ValuesFinder( $dataTypeLookup );
+       }
+
+}
diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php
index 2469862..0c9c268 100644
--- a/repo/includes/EntityView.php
+++ b/repo/includes/EntityView.php
@@ -372,12 +372,20 @@
                        $pout->addLink( 
$this->entityTitleLookup->getTitleForId( $entityId ) );
                }
 
+               $valuesFinder = new ValuesFinder( $this->dataTypeLookup );
+
                // treat URL values as external links ------
-               $urlFinder = new ReferencedUrlFinder( $this->dataTypeLookup );
-               $usedUrls = $urlFinder->findSnakLinks( $allSnaks );
+               $usedUrls = $valuesFinder->findFromSnaks( $allSnaks, 'url' );
 
                foreach ( $usedUrls as $url ) {
-                       $pout->addExternalLink( $url );
+                       $pout->addExternalLink( $url->getValue() );
+               }
+
+               // treat CommonsMedia values as file transclusions ------
+               $usedImages = $valuesFinder->findFromSnaks( $allSnaks, 
'commonsMedia' );
+
+               foreach( $usedImages as $image ) {
+                       $pout->addImage( $image->getValue() );
                }
 
                if ( $generateHtml ) {
diff --git a/repo/tests/phpunit/includes/EntityViewTest.php 
b/repo/tests/phpunit/includes/EntityViewTest.php
index 931171b..55443cd 100644
--- a/repo/tests/phpunit/includes/EntityViewTest.php
+++ b/repo/tests/phpunit/includes/EntityViewTest.php
@@ -192,6 +192,7 @@
                        $mockRepo->putEntity( $this->makeProperty( 'P11', 
'wikibase-item' ) );
                        $mockRepo->putEntity( $this->makeProperty( 'P23', 
'string' ) );
                        $mockRepo->putEntity( $this->makeProperty( 'P42', 'url' 
) );
+                       $mockRepo->putEntity( $this->makeProperty( 'P43', 
'commonsMedia' ) );
                        $mockRepo->putEntity( $this->makeProperty( 'P44', 
'wikibase-item' ) );
 
                        self::$mockRepo = $mockRepo;
diff --git a/repo/tests/phpunit/includes/ItemViewTest.php 
b/repo/tests/phpunit/includes/ItemViewTest.php
index e9449df..5f8da4b 100644
--- a/repo/tests/phpunit/includes/ItemViewTest.php
+++ b/repo/tests/phpunit/includes/ItemViewTest.php
@@ -166,4 +166,55 @@
                $this->assertEquals( $expectedLinks, $links );
        }
 
+       public function getParserOutputImageLinksProvider() {
+               $argLists = array();
+
+               $p23 = new PropertyId( 'P23' );
+               $p43 = new PropertyId( 'P43' );
+
+               $argLists["empty"] = array(
+                       array(),
+                       array() );
+
+               $argLists["PropertyNoValueSnak"] = array(
+                       array( $this->makeStatement( new PropertyNoValueSnak( 
$p43 ) ) ),
+                       array() );
+
+               $argLists["PropertySomeValueSnak"] = array(
+                       array( $this->makeStatement( new PropertySomeValueSnak( 
$p43 ) ) ),
+                       array() );
+
+               $argLists["PropertyValueSnak with string value"] = array(
+                       array( $this->makeStatement( new PropertyValueSnak( 
$p23, new StringValue( 'not an image' )  ) ) ),
+                       array() );
+
+               $argLists["PropertyValueSnak with image"] = array(
+                       array( $this->makeStatement( new PropertyValueSnak( 
$p43, new StringValue( 'File:Image.jpg' ) ) ) ),
+                       array( 'File:Image.jpg' ) );
+
+               return $argLists;
+       }
+
+       /**
+        * @dataProvider getParserOutputImageLinksProvider
+        *
+        * @param Statement[] $statements
+        * @param string[] $expectedImages
+        */
+       public function testGetParserOutputImageLinks( array $statements, array 
$expectedImages ) {
+               $entityRevision = $this->newEntityRevisionForStatements( 
$statements );
+               $entityView = $this->newEntityView( 
$entityRevision->getEntity()->getType() );
+
+               $out = $entityView->getParserOutput( $entityRevision, true, 
false );
+               $images = $out->getImages();
+
+               $expectedImages = array_values( $expectedImages );
+               sort( $expectedImages );
+
+               $images = array_keys( $images );
+               sort( $images );
+
+               $this->assertEquals( $expectedImages, $images );
+       }
+
 }
diff --git a/repo/tests/phpunit/includes/View/ClaimsViewTest.php 
b/repo/tests/phpunit/includes/View/ClaimsViewTest.php
index ae42f96..8d49988 100644
--- a/repo/tests/phpunit/includes/View/ClaimsViewTest.php
+++ b/repo/tests/phpunit/includes/View/ClaimsViewTest.php
@@ -3,26 +3,20 @@
 namespace Wikibase\Test;
 
 use DataValues\StringValue;
-use DOMDocument;
 use Title;
-use ValueFormatters\FormatterOptions;
 use Wikibase\ClaimHtmlGenerator;
 use Wikibase\DataModel\Claim\Claim;
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\EntityIdValue;
-use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
-use Wikibase\DataModel\Entity\Property;
 use Wikibase\DataModel\Entity\PropertyId;
 use Wikibase\DataModel\Snak\PropertyNoValueSnak;
+use Wikibase\DataModel\Snak\PropertySomeValueSnak;
 use Wikibase\DataModel\Snak\PropertyValueSnak;
 use Wikibase\DataModel\Snak\Snak;
-use Wikibase\Lib\SnakFormatter;
 use Wikibase\Lib\Store\EntityTitleLookup;
 use Wikibase\Repo\View\ClaimsView;
 use Wikibase\Repo\View\SectionEditLinkGenerator;
-use Wikibase\Repo\View\SnakHtmlGenerator;
-use Wikibase\Repo\WikibaseRepo;
 
 /**
  * @covers Wikibase\Repo\View\ClaimsView
@@ -60,6 +54,17 @@
                                new PropertyId( 'P23' ),
                                new StringValue( 'test' )
                        ) ),
+                       $this->makeClaim( new PropertyValueSnak(
+                               new PropertyId( 'P43' ),
+                               new StringValue( 'File:Image.jpg' )
+                       ) ),
+                       $this->makeClaim( new PropertySomeValueSnak(
+                               new PropertyId( 'P44' )
+                       ) ),
+                       $this->makeClaim( new PropertyValueSnak(
+                               new PropertyId( 'P100' ),
+                               new EntityIdValue( new ItemId( 'Q555' ) )
+                       ) ),
                );
 
                return array(

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib2c4e49bb63e2ac574b1a2c2224c0a0a8238a860
Gerrit-PatchSet: 12
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Bene <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Bene <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: WikidataJenkins <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to