jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/397825 )
Change subject: Create tracking category for 3D uploads lacking {{3dpatent}} ...................................................................... Create tracking category for 3D uploads lacking {{3dpatent}} Bug: T182684 Change-Id: I03dfa614f12a71b63c76fd22e41cb28bf9345250 --- M extension.json M i18n/en.json M i18n/qqq.json M src/DataCollector.php M src/HookHandler.php M tests/phpunit/DataCollectorTest.php M tests/phpunit/HookHandlerTest.php M tests/phpunit/ParserTestHelper.php 8 files changed, 85 insertions(+), 23 deletions(-) Approvals: jenkins-bot: Verified Cparle: Looks good to me, approved diff --git a/extension.json b/extension.json index 63e9062..51b6457 100644 --- a/extension.json +++ b/extension.json @@ -35,7 +35,8 @@ "commonsmetadata-trackingcategory-no-license", "commonsmetadata-trackingcategory-no-description", "commonsmetadata-trackingcategory-no-author", - "commonsmetadata-trackingcategory-no-source" + "commonsmetadata-trackingcategory-no-source", + "commonsmetadata-trackingcategory-no-patent" ], "manifest_version": 1 } diff --git a/i18n/en.json b/i18n/en.json index 8ab2650..04ce64f 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -11,5 +11,7 @@ "commonsmetadata-trackingcategory-no-author": "Files with no machine-readable author", "commonsmetadata-trackingcategory-no-author-desc": "The file does not have a [{{MediaWiki:Commonsmetadata-doc-url}} machine-readable] information template, or its author field is not filled out.", "commonsmetadata-trackingcategory-no-source": "Files with no machine-readable source", - "commonsmetadata-trackingcategory-no-source-desc": "The file does not have a [{{MediaWiki:Commonsmetadata-doc-url}} machine-readable] information template, or its source field is not filled out." + "commonsmetadata-trackingcategory-no-source-desc": "The file does not have a [{{MediaWiki:Commonsmetadata-doc-url}} machine-readable] information template, or its source field is not filled out.", + "commonsmetadata-trackingcategory-no-patent": "Files with no machine-readable patent", + "commonsmetadata-trackingcategory-no-patent-desc": "The file does not have a [{{MediaWiki:Commonsmetadata-doc-url}} machine-readable] patent template." } diff --git a/i18n/qqq.json b/i18n/qqq.json index 3f62d3d..e91c64b 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -14,5 +14,7 @@ "commonsmetadata-trackingcategory-no-author": "Name of the tracking category for files with no machine-readable author", "commonsmetadata-trackingcategory-no-author-desc": "Description of the inclusion criteria for the tracking category for files with no machine-readable license", "commonsmetadata-trackingcategory-no-source": "Name of the tracking category for files with no machine-readable source", - "commonsmetadata-trackingcategory-no-source-desc": "Description of the the inclusion criteria for tracking category for files with no machine-readable license" + "commonsmetadata-trackingcategory-no-source-desc": "Description of the the inclusion criteria for tracking category for files with no machine-readable license", + "commonsmetadata-trackingcategory-no-patent": "Name of the tracking category for files with no machine-readable patent", + "commonsmetadata-trackingcategory-no-patent-desc": "Description of the the inclusion criteria for tracking category for files with no machine-readable patent" } diff --git a/src/DataCollector.php b/src/DataCollector.php index bfa5ad0..fbe0ffb 100644 --- a/src/DataCollector.php +++ b/src/DataCollector.php @@ -7,6 +7,7 @@ use File; use LocalFile; use ForeignAPIFile; +use ParserOutput; use WikiFilePage; use Wikimedia\ScopedCallback; @@ -108,14 +109,18 @@ /** * Checks for the presence of metadata needed for attributing the file (author, source, license) * and returns a list of keys corresponding to problems. - * @param string $descriptionText HTML code of the file description + * @param ParserOutput $parserOutput + * @param File $file * @return array one or more of the following keys: * - no-license - failed to detect a license * - no-description - failed to detect any image description * - no-author - failed to detect author name or a custom attribution text * - no-source - failed to detect the source of the image or a custom attribution text */ - public function verifyAttributionMetadata( $descriptionText ) { + public function verifyAttributionMetadata( ParserOutput $parserOutput, File $file ) { + // HTML code of the file description + $descriptionText = $parserOutput->getText(); + $templateData = $this->templateParser->parsePage( $descriptionText ); $problems = $licenseData = $informationData = []; @@ -148,6 +153,16 @@ ( !isset( $informationData['Attribution'] ) || $informationData['Attribution'] === '' ) ) { $problems[] = 'no-source'; + } + + // Certain uploads (3D objects) need a patent license + $templates = $parserOutput->getTemplates(); + $templates = isset( $templates[NS_TEMPLATE] ) ? $templates[NS_TEMPLATE] : []; + if ( + !array_key_exists( '3dpatent', $templates ) && + $file->getMimeType() === 'application/sla' + ) { + $problems[] = 'no-patent'; } return $problems; @@ -396,9 +411,9 @@ /** * Receives the list of licenses found by the template parser and selects which one to use. - * @param array $licenses an array of licenses, each is an array of metdata fields + * @param array $licenses an array of licenses, each is an array of metadata fields * in fieldname => value form - * @return array an array of metdata fields in fieldname => value form + * @return array an array of metadata fields in fieldname => value form */ protected function selectLicense( array $licenses ) { if ( !$licenses ) { diff --git a/src/HookHandler.php b/src/HookHandler.php index 4fc08cb..484607d 100644 --- a/src/HookHandler.php +++ b/src/HookHandler.php @@ -128,20 +128,21 @@ * * then fallback to DB, for files that have just been renamed */ $repo = RepoGroup::singleton()->getLocalRepo(); - if ( - $title->isRedirect() - || ( - !$repo->findFile( $title, [ 'ignoreRedirect' => true ] ) - && !$repo->findFile( $title, [ 'ignoreRedirect' => true, 'latest' => true ] ) - ) - ) { + if ( $title->isRedirect() ) { return true; + } + $file = $repo->findFile( $title, [ 'ignoreRedirect' => true ] ); + if ( $file === false ) { + $file = $repo->findFile( $title, [ 'ignoreRedirect' => true, 'latest' => true ] ); + if ( $file === false ) { + return true; + } } $language = $content->getContentHandler()->getPageViewLanguage( $title, $content ); $dataCollector = self::getDataCollector( $language, true ); - $categoryKeys = $dataCollector->verifyAttributionMetadata( $parserOutput->getText() ); + $categoryKeys = $dataCollector->verifyAttributionMetadata( $parserOutput, $file ); foreach ( $categoryKeys as $key ) { $parserOutput->addTrackingCategory( 'commonsmetadata-trackingcategory-' . $key, $title ); diff --git a/tests/phpunit/DataCollectorTest.php b/tests/phpunit/DataCollectorTest.php index 33ae0c5..7e78691 100644 --- a/tests/phpunit/DataCollectorTest.php +++ b/tests/phpunit/DataCollectorTest.php @@ -2,6 +2,9 @@ namespace CommonsMetadata; +use ParserOutput; +use Title; + /** * @covers CommonsMetadata\DataCollector * @group Extensions/CommonsMetadata @@ -241,7 +244,7 @@ $this->licenseParser->expects( $this->any() )->method( 'parseLicenseString' ) ->will( $this->returnValue( null ) ); - $problems = $this->dataCollector->verifyAttributionMetadata( '' ); + $problems = $this->dataCollector->verifyAttributionMetadata( new ParserOutput(), $this->file ); $this->assertEmpty( $problems ); } @@ -257,7 +260,7 @@ $this->licenseParser->expects( $this->any() )->method( 'parseLicenseString' ) ->will( $this->returnValue( null ) ); - $problems = $this->dataCollector->verifyAttributionMetadata( '' ); + $problems = $this->dataCollector->verifyAttributionMetadata( new ParserOutput(), $this->file ); $this->assertEmpty( $problems ); } @@ -272,7 +275,7 @@ $this->licenseParser->expects( $this->any() )->method( 'parseLicenseString' ) ->will( $this->returnValue( null ) ); - $problems = $this->dataCollector->verifyAttributionMetadata( '' ); + $problems = $this->dataCollector->verifyAttributionMetadata( new ParserOutput(), $this->file ); $this->assertContains( 'no-license', $problems ); $this->assertContains( 'no-description', $problems ); @@ -286,7 +289,7 @@ $this->licenseParser->expects( $this->any() )->method( 'parseLicenseString' ) ->will( $this->returnValue( null ) ); - $problems = $this->dataCollector->verifyAttributionMetadata( '' ); + $problems = $this->dataCollector->verifyAttributionMetadata( new ParserOutput(), $this->file ); $this->assertContains( 'no-license', $problems ); $this->assertContains( 'no-description', $problems ); @@ -300,7 +303,7 @@ $this->licenseParser->expects( $this->any() )->method( 'parseLicenseString' ) ->will( $this->returnValue( null ) ); - $problems = $this->dataCollector->verifyAttributionMetadata( '' ); + $problems = $this->dataCollector->verifyAttributionMetadata( new ParserOutput(), $this->file ); $this->assertContains( 'no-license', $problems ); $this->assertContains( 'no-description', $problems ); @@ -308,6 +311,40 @@ $this->assertContains( 'no-source', $problems ); } + public function testVerifyPatentProvided() { + $title = Title::newFromText( '3dpatent', NS_TEMPLATE ); + $parserOutput = new ParserOutput(); + $parserOutput->addTemplate( $title, 1, 1 ); + + $parserTestHelper = new ParserTestHelper(); + $parserTestHelper->setTestCase( $this ); + $file = $parserTestHelper->getLocalFile( '3D file with patent template', [], 'application/sla' ); + + $problems = $this->dataCollector->verifyAttributionMetadata( $parserOutput, $file ); + + $this->assertNotContains( 'no-patent', $problems ); + } + + public function testVerifyPatentMissing() { + $parserTestHelper = new ParserTestHelper(); + $parserTestHelper->setTestCase( $this ); + $file = $parserTestHelper->getLocalFile( '3D file w/o patent template', [], 'application/sla' ); + + $problems = $this->dataCollector->verifyAttributionMetadata( new ParserOutput(), $file ); + + $this->assertContains( 'no-patent', $problems ); + } + + public function testVerifyNoPatentNeeded() { + $parserTestHelper = new ParserTestHelper(); + $parserTestHelper->setTestCase( $this ); + $file = $parserTestHelper->getLocalFile( 'Not 3D = no patent needed', [], 'image/jpeg' ); + + $problems = $this->dataCollector->verifyAttributionMetadata( new ParserOutput(), $file ); + + $this->assertNotContains( 'no-patent', $problems ); + } + /*------------------------------- Helpers --------------------------*/ protected function assertMetadataValue( $field, $expected, $metadata, $message = '' ) { diff --git a/tests/phpunit/HookHandlerTest.php b/tests/phpunit/HookHandlerTest.php index 1ef11bb..e07bdfb 100644 --- a/tests/phpunit/HookHandlerTest.php +++ b/tests/phpunit/HookHandlerTest.php @@ -82,7 +82,7 @@ /*----------------------------------------------------------*/ /** - * @dataProvider provideDesctiptionData + * @dataProvider provideDescriptionData * @param string $testName a test name from ParserTestHelper::$testHTMLFiles */ public function testDescription( $testName ) { @@ -102,7 +102,7 @@ } } - public function provideDesctiptionData() { + public function provideDescriptionData() { return [ [ 'noinfo' ], [ 'simple' ], diff --git a/tests/phpunit/ParserTestHelper.php b/tests/phpunit/ParserTestHelper.php index b89dcf2..2e49cd0 100644 --- a/tests/phpunit/ParserTestHelper.php +++ b/tests/phpunit/ParserTestHelper.php @@ -109,9 +109,10 @@ /** * @param string $description file page text * @param string[] $categories list of category names, without namespace + * @param string $mime mimetype of the image * @return \LocalFile */ - public function getLocalFile( $description, $categories ) { + public function getLocalFile( $description, $categories, $mime = 'image/jpeg' ) { $file = $this->testCase->getMockBuilder( 'LocalFile' ) ->setMockClassName( 'LocalFileMock' ) ->disableOriginalConstructor() @@ -125,6 +126,9 @@ $file->expects( $this->testCase->any() ) ->method( 'getDescriptionTouched' ) ->will( $this->testCase->returnValue( time() ) ); + $file->expects( $this->testCase->any() ) + ->method( 'getMimeType' ) + ->will( $this->testCase->returnValue( $mime ) ); $file->mockedCategories = $categories; return $file; } -- To view, visit https://gerrit.wikimedia.org/r/397825 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I03dfa614f12a71b63c76fd22e41cb28bf9345250 Gerrit-PatchSet: 3 Gerrit-Project: mediawiki/extensions/CommonsMetadata Gerrit-Branch: master Gerrit-Owner: Matthias Mullie <mmul...@wikimedia.org> Gerrit-Reviewer: Cparle <cpa...@wikimedia.org> Gerrit-Reviewer: Siebrand <siebr...@kitano.nl> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits