Aude has uploaded a new change for review.
https://gerrit.wikimedia.org/r/118688
Change subject: Move isTitleInEntityNamespace to NamespaceUtils with tests
......................................................................
Move isTitleInEntityNamespace to NamespaceUtils with tests
Change-Id: I15c48fc0e3b4c20504081b9ecdbaf8eda6611e39
---
M repo/Wikibase.hooks.php
M repo/includes/NamespaceUtils.php
M repo/tests/phpunit/includes/NamespaceUtilsTest.php
3 files changed, 68 insertions(+), 8 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/88/118688/1
diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index 5023977..7b9f655 100644
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -46,13 +46,6 @@
*/
final class RepoHooks {
- private static function isTitleInEntityNamespace( Title $title ) {
- $entityNamespaces = array_flip(
NamespaceUtils::getEntityNamespaces() );
- $namespace = $title->getNamespace();
-
- return array_key_exists( $namespace, $entityNamespaces );
- }
-
/**
* Handler for the BeforePageDisplay hook, simply injects
wikibase.ui.entitysearch module
* replacing the native search box with the entity selector widget.
@@ -1144,7 +1137,10 @@
* @return boolean
*/
public static function onOutputPageBeforeHtmlRegisterConfig( OutputPage
$out, &$html ) {
- if ( !self::isTitleInEntityNamespace( $out->getTitle() ) ) {
+ $settings = WikibaseRepo::getDefaultInstance()->getSettings();
+ $namespaceUtils = new NamespaceUtils( $settings->getSetting(
'entityNamespaces' ) );
+
+ if ( !$namespaceUtils->isTitleInEntityNamespace(
$out->getTitle() ) ) {
return true;
}
diff --git a/repo/includes/NamespaceUtils.php b/repo/includes/NamespaceUtils.php
index 97e09be..24beb5e 100644
--- a/repo/includes/NamespaceUtils.php
+++ b/repo/includes/NamespaceUtils.php
@@ -1,6 +1,8 @@
<?php
namespace Wikibase;
+
+use Title;
use Wikibase\Repo\WikibaseRepo;
/**
@@ -16,6 +18,36 @@
final class NamespaceUtils {
/**
+ * @var array
+ */
+ private $entityNamespaces;
+
+ /**
+ * @param array $entityNamespaces
+ */
+ public function __construct( array $entityNamespaces = null ) {
+ $this->entityNamespaces = is_array( $entityNamespaces )
+ ? $entityNamespaces
+ :
WikibaseRepo::getDefaultInstance()->getSettings()->getSetting(
'entityNamespaces' );
+
+ if ( !is_array( $this->entityNamespaces ) ) {
+ throw new UnexpectedValueException( 'entityNamespaces
must be an array' );
+ }
+ }
+
+ /**
+ * @param Title $title
+ *
+ * @return boolean
+ */
+ public function isTitleInEntityNamespace( Title $title ) {
+ $entityNamespaces = array_flip( $this->entityNamespaces );
+ $namespace = $title->getNamespace();
+
+ return array_key_exists( $namespace, $entityNamespaces );
+ }
+
+ /**
* Returns a list of entity content model ids pointing to the ids of
the namespaces in which they reside.
*
* @since 0.4
diff --git a/repo/tests/phpunit/includes/NamespaceUtilsTest.php
b/repo/tests/phpunit/includes/NamespaceUtilsTest.php
index 5cdb7c0..27467bc 100644
--- a/repo/tests/phpunit/includes/NamespaceUtilsTest.php
+++ b/repo/tests/phpunit/includes/NamespaceUtilsTest.php
@@ -2,6 +2,7 @@
namespace Wikibase\Test;
+use Title;
use Wikibase\NamespaceUtils;
/**
@@ -30,4 +31,35 @@
$this->assertFalse( NamespaceUtils::isEntityNamespace(
720101010 ) );
}
+ /**
+ * @dataProvider isTitleInEntityNamespaceProvider
+ */
+ public function testIsTitleInEntityNamespace( $expected, array
$namespaces, Title $title ) {
+ $namespaceUtils = new NamespaceUtils( $namespaces );
+
+ $this->assertEquals( $expected,
$namespaceUtils->isTitleInEntityNamespace( $title ) );
+ }
+
+ public function isTitleInEntityNamespaceProvider() {
+ $namespaces = array(
+ 'wikibase-item' => 0,
+ 'wikibase-property' => 102,
+ );
+
+ $namespaces2 = array(
+ 'wikibase-item' => 120,
+ 'wikibase-property' => 122
+ );
+
+ return array(
+ array( true, $namespaces, Title::makeTitle( 0, 'Cat' )
),
+ array( false, $namespaces, Title::makeTitle( 2, 'Cat' )
),
+ array( true, $namespaces, Title::makeTitle( 102, 'Cat'
) ),
+ array( false, $namespaces2, Title::makeTitle( 0, 'Cat'
) ),
+ array( false, $namespaces2, Title::makeTitle( 2, 'Cat'
) ),
+ array( true, $namespaces2, Title::makeTitle( 120, 'Cat'
) ),
+ array( true, $namespaces2, Title::makeTitle( 122, 'Cat'
) )
+ );
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/118688
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I15c48fc0e3b4c20504081b9ecdbaf8eda6611e39
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits