Lucas Werkmeister (WMDE) has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/378944 )

Change subject: Fix CommonsLinkChecker for Creator namespace
......................................................................


Fix CommonsLinkChecker for Creator namespace

The TitleParser we use is configured with the namespaces of the local
wiki (on Wikidata, wikidatawiki), which are not the same as the
namespaces of Wikimedia commons – for instance, the Creator namespace is
commonswiki-specific (namespace number 100). Due to the way Wikimedia’s
MediaWiki config is set up, there’s no proper way to fix this, so we
just hard-code the few commonswiki-specific namespace numbers.

We now use the TitleParser for two purposes, depending on namespace: if
we know the namespace (e. g. Creator), we just use it to turn the title
into the corresponding database key, otherwise we also have it resolve
the namespace. In both cases, TitleParser processes interwiki prefix,
namespace and proper title, which is more than we want (e. g. for a
hypothetical creator “File:Foo” – full title “Creator:File:Foo” – we
would check in the “File” namespace again), but until TitleParser is
refactored to publicly expose just the parts of its functionality we
need, I see no way around this problem.

Bug: T176062
Change-Id: Ib54cc1a42acc569e4c433e9f793e3c945cf6a549
---
M includes/ConstraintCheck/Checker/CommonsLinkChecker.php
M tests/phpunit/Checker/CommonsLinkChecker/CommonsLinkCheckerTest.php
2 files changed, 49 insertions(+), 2 deletions(-)

Approvals:
  Jonas Kress (WMDE): Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/ConstraintCheck/Checker/CommonsLinkChecker.php 
b/includes/ConstraintCheck/Checker/CommonsLinkChecker.php
index a960af1..44c5473 100644
--- a/includes/ConstraintCheck/Checker/CommonsLinkChecker.php
+++ b/includes/ConstraintCheck/Checker/CommonsLinkChecker.php
@@ -56,6 +56,33 @@
        }
 
        /**
+        * Get the number of a namespace on Wikimedia Commons (commonswiki).
+        * All namespaces not known to this function will be looked up by the 
TitleParser.
+        *
+        * @param string $namespace
+        * @return array first element is the namespace number (default 
namespace for TitleParser),
+        * second element is a string to prepend to the title before giving it 
to the TitleParser
+        */
+       private function getCommonsNamespace( $namespace ) {
+               // for namespace numbers see mediawiki-config repo, 
wmf-config/InitialiseSettings.php,
+               // 'wgExtraNamespaces' key, 'commonswiki' subkey
+               switch ( $namespace ) {
+                       case '':
+                               return [ NS_MAIN, '' ];
+                       case 'Creator':
+                               return [ 100, '' ];
+                       case 'TimedText':
+                               return [ 102, '' ];
+                       case 'Sequence':
+                               return [ 104, '' ];
+                       case 'Institution':
+                               return [ 106, '' ];
+                       default:
+                               return [ NS_MAIN, $namespace . ':' ];
+               }
+       }
+
+       /**
         * Checks 'Commons link' constraint.
         *
         * @param Context $context
@@ -99,8 +126,8 @@
                        if ( !$this->commonsLinkIsWellFormed( $commonsLink ) ) {
                                throw new MalformedTitleException( 
'wbqc-violation-message-commons-link-not-well-formed', $commonsLink ); // 
caught below
                        }
-                       $prefix = $namespace === '' ? '' : $namespace . ':';
-                       $title = $this->titleParser->parseTitle( $prefix . 
$commonsLink, NS_MAIN );
+                       list ( $defaultNamespace, $prefix ) = 
$this->getCommonsNamespace( $namespace );
+                       $title = $this->titleParser->parseTitle( $prefix . 
$commonsLink, $defaultNamespace );
                        if ( $this->pageExists( $title ) ) {
                                $message = '';
                                $status = CheckResult::STATUS_COMPLIANCE;
diff --git 
a/tests/phpunit/Checker/CommonsLinkChecker/CommonsLinkCheckerTest.php 
b/tests/phpunit/Checker/CommonsLinkChecker/CommonsLinkCheckerTest.php
index 2a92ac8..eb74bde 100644
--- a/tests/phpunit/Checker/CommonsLinkChecker/CommonsLinkCheckerTest.php
+++ b/tests/phpunit/Checker/CommonsLinkChecker/CommonsLinkCheckerTest.php
@@ -79,6 +79,14 @@
                                'page_title' => 'Test_gallery'
                        ]
                );
+               $this->db->insert(
+                       'page',
+                       [
+                               'page_id' => '4',
+                               'page_namespace' => 100,
+                               'page_title' => 'Test_creator'
+                       ]
+               );
        }
 
        public function testCommonsLinkConstraintValid() {
@@ -142,6 +150,18 @@
                $this->assertCompliance( $result );
        }
 
+       public function testCommonsLinkConstraintValidCreator() {
+               $value = new StringValue( 'test creator' );
+               $snak = new PropertyValueSnak( new PropertyId( 'P1' ), $value );
+
+               $result = $this->commonsLinkChecker->checkConstraint(
+                       new FakeSnakContext( $snak ),
+                       $this->getConstraintMock( $this->namespaceParameter( 
'Creator' ) )
+               );
+
+               $this->assertCompliance( $result );
+       }
+
        public function testCommonsLinkConstraintNotExistent() {
                $value = new StringValue( 'no image.jpg' );
                $snak = new PropertyValueSnak( new PropertyId( 'P1' ), $value );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib54cc1a42acc569e4c433e9f793e3c945cf6a549
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: DCausse <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to