jenkins-bot has submitted this change and it was merged.
Change subject: SMW\NamespaceExaminer class (deprecates
smwfIsSemanticsProcessed())
......................................................................
SMW\NamespaceExaminer class (deprecates smwfIsSemanticsProcessed())
Coverage: 100%
CRAP: 9
smwfIsSemanticsProcessed is deprecated with 1.9 and will be removed in 1.11
Change-Id: I3dedb0c465f4005d52a3f76bd696271443501d49
---
M includes/GlobalFunctions.php
A includes/NamespaceExaminer.php
M includes/Setup.php
A includes/exceptions/InvalidNamespaceException.php
A tests/phpunit/includes/NamespaceExaminerTest.php
5 files changed, 347 insertions(+), 6 deletions(-)
Approvals:
Mwjames: Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php
index e9c8c18..071beed 100644
--- a/includes/GlobalFunctions.php
+++ b/includes/GlobalFunctions.php
@@ -10,13 +10,13 @@
*/
/**
- * Return true if semantic data should be processed and displayed for a page
- * in the given namespace.
+ * @see NamespaceExaminer
+ *
* @return boolean
+ * @deprecated since 1.9 and will be removed in 1.11
*/
function smwfIsSemanticsProcessed( $namespace ) {
- global $smwgNamespacesWithSemanticLinks;
- return !empty( $smwgNamespacesWithSemanticLinks[$namespace] );
+ return \SMW\NamespaceExaminer::getInstance()->isSemanticEnabled(
$namespace );
}
/**
diff --git a/includes/NamespaceExaminer.php b/includes/NamespaceExaminer.php
new file mode 100644
index 0000000..bd64f61
--- /dev/null
+++ b/includes/NamespaceExaminer.php
@@ -0,0 +1,140 @@
+<?php
+
+namespace SMW;
+
+use MWNamespace;
+
+/**
+ * This Class examines if a specific namespace is enabled for the usage of the
+ * Semantic MediaWiki extension
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 1.9
+ *
+ * @file
+ *
+ * @licence GNU GPL v2+
+ * @author mwjames
+ */
+
+/**
+ * This class examines if a specific namespace is enabled for the usage of the
+ * Semantic MediaWiki extension
+ *
+ * @ingroup SMW
+ */
+final class NamespaceExaminer {
+
+ /** @var array */
+ private static $instance = null;
+
+ /** @var array */
+ private $registeredNamespaces = array();
+
+ /**
+ * @since 1.9
+ *
+ * @param array $registeredNamespaces
+ */
+ public function __construct( array $registeredNamespaces ) {
+ $this->registeredNamespaces = $registeredNamespaces;
+ }
+
+ /**
+ * Returns a static instance with an invoked global settings array
+ *
+ * @par Example:
+ * @code
+ * \SMW\NamespaceExaminer::getInstance()->isSemanticEnabled( NS_MAIN )
+ * @endcode
+ *
+ * @note Used in smwfIsSemanticsProcessed
+ *
+ * @since 1.9
+ *
+ * @return NamespaceExaminer
+ */
+ public static function getInstance() {
+
+ if ( self::$instance === null ) {
+ self::$instance = self::newFromArray(
Settings::newFromGlobals()->get( 'smwgNamespacesWithSemanticLinks' ) );
+ }
+
+ return self::$instance;
+ }
+
+ /**
+ * Registers an array of available namespaces
+ *
+ * @par Example:
+ * @code
+ * \SMW\NamespaceExaminer::newFromArray( array( ... )
)->isSemanticEnabled( NS_MAIN )
+ * @endcode
+ *
+ * @since 1.9
+ *
+ * @return NamespaceExaminer
+ */
+ public static function newFromArray( $registeredNamespaces ) {
+ return new self( $registeredNamespaces );
+ }
+
+ /**
+ * Resets static instance
+ *
+ * @since 1.9
+ */
+ public static function reset() {
+ self::$instance = null;
+ }
+
+ /**
+ * Returns if a namespace is enabled for semantic processing
+ *
+ * @since 1.9
+ *
+ * @param integer $namespace
+ *
+ * @return boolean
+ * @throws InvalidNamespaceException
+ */
+ public function isSemanticEnabled( $namespace ) {
+
+ if ( !is_int( $namespace ) ) {
+ throw new InvalidNamespaceException( "{$namespace} is
not a number" );
+ }
+
+ if ( !in_array( $namespace, MWNamespace::getValidNamespaces() )
) {
+ throw new InvalidNamespaceException( "{$namespace} is
not a valid namespace" );
+ }
+
+ return $this->isEnabled( $namespace );
+ }
+
+ /**
+ * Asserts if a namespace is enabled
+ *
+ * @since 1.9
+ *
+ * @param integer $namespace
+ *
+ * @return boolean
+ */
+ protected function isEnabled( $namespace ) {
+ return !empty( $this->registeredNamespaces[$namespace] );
+ }
+}
diff --git a/includes/Setup.php b/includes/Setup.php
index 1392b6e..883a43b 100644
--- a/includes/Setup.php
+++ b/includes/Setup.php
@@ -146,19 +146,21 @@
$wgAutoloadClasses['SMW\RecurringEvents'] = $incDir .
'RecurringEvents.php';
$wgAutoloadClasses['SMW\Settings'] = $incDir .
'Settings.php';
+ $wgAutoloadClasses['SMW\NamespaceExaminer'] = $incDir .
'NamespaceExaminer.php';
+
+ $wgAutoloadClasses['SMW\CacheHandler'] = $incDir .
'/handlers/CacheHandler.php';
// Formatters
$wgAutoloadClasses['SMW\ArrayFormatter'] = $incDir .
'formatters/ArrayFormatter.php';
$wgAutoloadClasses['SMW\ParserParameterFormatter'] = $incDir .
'formatters/ParserParameterFormatter.php';
$wgAutoloadClasses['SMW\MessageFormatter'] = $incDir .
'formatters/MessageFormatter.php';
- $wgAutoloadClasses['SMW\CacheHandler'] = $incDir .
'/handlers/CacheHandler.php';
-
// Exceptions
$wgAutoloadClasses['SMW\StoreInstanceException'] = $incDir .
'/exceptions/StoreInstanceException.php';
$wgAutoloadClasses['SMW\SettingsArgumentException'] = $incDir .
'/exceptions/SettingsArgumentException.php';
$wgAutoloadClasses['SMW\PredefinedPropertyException'] = $incDir .
'/exceptions/PredefinedPropertyException.php';
$wgAutoloadClasses['SMW\InvalidSemanticDataException'] = $incDir .
'/exceptions/InvalidSemanticDataException.php';
+ $wgAutoloadClasses['SMW\InvalidNamespaceException'] = $incDir .
'/exceptions/InvalidNamespaceException.php';
// Article pages
$apDir = $smwgIP . 'includes/articlepages/';
diff --git a/includes/exceptions/InvalidNamespaceException.php
b/includes/exceptions/InvalidNamespaceException.php
new file mode 100644
index 0000000..7a17237
--- /dev/null
+++ b/includes/exceptions/InvalidNamespaceException.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace SMW;
+
+use MWException;
+
+/**
+ * Exception for an invalid namespace
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 1.9
+ *
+ * @file
+ * @ingroup Exception
+ *
+ * @author mwjames
+ */
+
+/**
+ * Exception for an invalid semantic data object
+ *
+ * @ingroup Exception
+ * @codeCoverageIgnore
+ */
+class InvalidNamespaceException extends MWException {}
\ No newline at end of file
diff --git a/tests/phpunit/includes/NamespaceExaminerTest.php
b/tests/phpunit/includes/NamespaceExaminerTest.php
new file mode 100644
index 0000000..c116fd2
--- /dev/null
+++ b/tests/phpunit/includes/NamespaceExaminerTest.php
@@ -0,0 +1,160 @@
+<?php
+
+namespace SMW\Test;
+
+use SMW\NamespaceExaminer;
+use SMW\Settings;
+
+/**
+ * Tests for the NamespaceExaminer class
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 1.9
+ *
+ * @file
+ *
+ * @licence GNU GPL v2+
+ * @author mwjames
+ */
+
+/**
+ * @covers \SMW\NamespaceExaminer
+ *
+ * @ingroup Test
+ *
+ * @group SMW
+ * @group SMWExtension
+ */
+class NamespaceExaminerTest extends SemanticMediaWikiTestCase {
+
+ /**
+ * Returns the name of the class to be tested
+ *
+ * @return string
+ */
+ public function getClass() {
+ return '\SMW\NamespaceExaminer';
+ }
+
+ /**
+ * Helper method that returns a NamespaceExaminer object
+ *
+ * @param array $namespaces
+ *
+ * @return NamespaceExaminer
+ */
+ private function getInstance( array $namespaces = array() ) {
+ return new NamespaceExaminer( $namespaces );
+ }
+
+ /**
+ * @test NamespaceExaminer::__construct
+ *
+ * @since 1.9
+ */
+ public function testConstructor() {
+ $instance = $this->getInstance( array( NS_MAIN => true ) );
+ $this->assertInstanceOf( $this->getClass(), $instance );
+ }
+
+ /**
+ * @test NamespaceExaminer::isSemanticEnabled
+ *
+ * @since 1.9
+ */
+ public function testIsSemanticEnabled() {
+
+ $instance = $this->getInstance( array( NS_MAIN => true ) );
+ $this->assertTrue( $instance->isSemanticEnabled( NS_MAIN ) );
+
+ $instance = $this->getInstance( array( NS_MAIN => false ) );
+ $this->assertFalse( $instance->isSemanticEnabled( NS_MAIN ) );
+
+ $instance = $this->getInstance();
+ $this->assertFalse( $instance->isSemanticEnabled( NS_MAIN ) );
+
+ }
+
+ /**
+ * @test NamespaceExaminer::isSemanticEnabled
+ *
+ * @since 1.9
+ */
+ public function testNoNumberException() {
+ $this->setExpectedException( '\SMW\InvalidNamespaceException' );
+
+ $instance = $this->getInstance( array( NS_MAIN => true ) );
+ $this->assertTrue( $instance->isSemanticEnabled( 'lula' ) );
+ }
+
+ /**
+ * @test NamespaceExaminer::isSemanticEnabled
+ *
+ * @since 1.9
+ */
+ public function testNoValidNamespaceException() {
+ $this->setExpectedException( '\SMW\InvalidNamespaceException' );
+
+ $instance = $this->getInstance( array( NS_MAIN => true ) );
+ $this->assertTrue( $instance->isSemanticEnabled( 99991001 ) );
+ }
+
+ /**
+ * @test NamespaceExaminer::getInstance
+ *
+ * @since 1.9
+ */
+ public function testGetInstance() {
+
+ $instance = NamespaceExaminer::getInstance();
+ $this->assertInstanceOf( $this->getClass(), $instance );
+
+ // Static instance
+ $this->assertTrue( $instance ===
NamespaceExaminer::getInstance() );
+
+ // Reset static instance
+ NamespaceExaminer::reset();
+ $this->assertFalse( $instance ===
NamespaceExaminer::getInstance() );
+
+ }
+
+ /**
+ * @test NamespaceExaminer::newFromArray
+ *
+ * @since 1.9
+ */
+ public function testNewFromArray() {
+ $instance = NamespaceExaminer::newFromArray( array( NS_MAIN =>
true ) );
+
+ $this->assertInstanceOf( $this->getClass(), $instance );
+ $this->assertTrue( $instance->isSemanticEnabled( NS_MAIN ) );
+ }
+
+ /**
+ * @see smwfIsSemanticsProcessed
+ *
+ * FIXME Delete this test in 1.11
+ *
+ * @since 1.9
+ */
+ public function testSmwfIsSemanticsProcessed() {
+ $result = smwfIsSemanticsProcessed( NS_MAIN );
+
+ $this->assertInternalType( 'boolean', $result );
+ $this->assertTrue( $result );
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/65008
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I3dedb0c465f4005d52a3f76bd696271443501d49
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>
Gerrit-Reviewer: Mwjames <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits