Mwjames has uploaded a new change for review. https://gerrit.wikimedia.org/r/117159
Change subject: Add 'SG\PropertyRegistry' ...................................................................... Add 'SG\PropertyRegistry' - Fixed Deprecated use of wfMsg - Added unit test - Fixed wrong invocation for "registerPropertyAliases" Change-Id: I9e9a0b6fbeef1b26ec4cb0bce2a752a2de693d04 --- M SemanticGlossary.php M SemanticGlossaryBackend.php A src/PropertyRegistry.php A tests/phpunit/PropertyRegistryTest.php 4 files changed, 167 insertions(+), 28 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticGlossary refs/changes/59/117159/1 diff --git a/SemanticGlossary.php b/SemanticGlossary.php index 36bf63c..8ac90eb 100644 --- a/SemanticGlossary.php +++ b/SemanticGlossary.php @@ -60,15 +60,13 @@ $autoloadClasses = array( 'SemanticGlossaryBackend' => $dir . '/SemanticGlossaryBackend.php', 'SemanticGlossaryCacheHandling' => $dir . '/SemanticGlossaryCacheHandling.php', + 'SG\PropertyRegistry' => $dir . '/src/PropertyRegistry.php', ); $GLOBALS[ 'wgAutoloadClasses' ] = array_merge( $GLOBALS[ 'wgAutoloadClasses' ], $autoloadClasses ); // register hook handlers $hooks = array( - 'smwInitProperties' => array( 'SemanticGlossaryBackend::registerProperties' ), - 'smwInitDatatypes' => array( 'SemanticGlossaryBackend::registerPropertyAliases' ), - 'SMWStore::updateDataBefore' => array( 'SemanticGlossaryCacheHandling::purgeCacheForData' ), // invalidate on update 'smwDeleteSemanticData' => array( 'SemanticGlossaryCacheHandling::purgeCacheForSubject' ), // invalidate on delete 'TitleMoveComplete' => array( 'SemanticGlossaryCacheHandling::purgeCacheForTitle' ), // move annotations @@ -81,4 +79,13 @@ define( 'SG_PROP_GLL', 'Glossary-Link' ); define( 'SG_PROP_GLS', 'Glossary-Style' ); + /** + * Register properties + * + * @since 1.0 + */ + $GLOBALS['wgHooks']['smwInitProperties'][] = function () { + return \SG\PropertyRegistry::getInstance()->registerPropertiesAndAliases(); + }; + } ); diff --git a/SemanticGlossaryBackend.php b/SemanticGlossaryBackend.php index 2164a11..68fc0dc 100644 --- a/SemanticGlossaryBackend.php +++ b/SemanticGlossaryBackend.php @@ -193,29 +193,4 @@ return true; } - /** - * Hook handler for registering semantic properties - * - * @return bool - */ - static function registerProperties() { - SMWDIProperty::registerProperty( '___glt', '_str', SG_PROP_GLT, true ); - SMWDIProperty::registerProperty( '___gld', '_txt', SG_PROP_GLD, true ); - SMWDIProperty::registerProperty( '___gll', '_str', SG_PROP_GLL, true ); - SMWDIProperty::registerProperty( '___gls', '_txt', SG_PROP_GLS, true ); - return true; - } - - /** - * Hook handler for registering property aliases - * - * @return bool - */ - static function registerPropertyAliases() { - SMWDIProperty::registerPropertyAlias( '___glt', wfMsg( 'semanticglossary-prop-glt' ) ); - SMWDIProperty::registerPropertyAlias( '___gld', wfMsg( 'semanticglossary-prop-gld' ) ); - SMWDIProperty::registerPropertyAlias( '___gll', wfMsg( 'semanticglossary-prop-gll' ) ); - SMWDIProperty::registerPropertyAlias( '___gls', wfMsg( 'semanticglossary-prop-gls' ) ); - return true; - } } diff --git a/src/PropertyRegistry.php b/src/PropertyRegistry.php new file mode 100644 index 0000000..99181df --- /dev/null +++ b/src/PropertyRegistry.php @@ -0,0 +1,98 @@ +<?php + +namespace SG; + +use SMW\DIProperty; + +/** + * @ingroup SG + * + * @licence GNU GPL v2+ + * @since 1.0 + * + * @author mwjames + */ +class PropertyRegistry { + + const SG_TERM = '___glt'; + const SG_DEFINITION = '___gld'; + const SG_LINK = '___gll'; + const SG_STYLE = '___gls'; + + protected static $instance = null; + + /** + * @since 1.0 + * + * @return PropertyRegistry + */ + public static function getInstance() { + + if ( self::$instance === null ) { + self::$instance = new self(); + } + + return self::$instance; + } + + /** + * @since 1.0 + */ + public static function clear() { + self::$instance = null; + } + + /** + * @since 1.0 + * + * @return boolean + */ + public function registerPropertiesAndAliases() { + + $propertyDefinitions = array( + self::SG_TERM => array( + 'label' => SG_PROP_GLT, + 'type' => '_txt', + 'alias' => wfMessage( 'semanticglossary-prop-glt' )->text() + ), + self::SG_DEFINITION => array( + 'label' => SG_PROP_GLD, + 'type' => '_txt', + 'alias' => wfMessage( 'semanticglossary-prop-gld' )->text() + ), + self::SG_LINK => array( + 'label' => SG_PROP_GLL, + 'type' => '_txt', + 'alias' => wfMessage( 'semanticglossary-prop-gll' )->text() + ), + self::SG_STYLE => array( + 'label' => SG_PROP_GLS, + 'type' => '_txt', + 'alias' => wfMessage( 'semanticglossary-prop-gls' )->text() + ) + ); + + return $this->registerPropertiesFromList( $propertyDefinitions ); + } + + protected function registerPropertiesFromList( array $propertyList ) { + + foreach ( $propertyList as $propertyId => $definition ) { + + DIProperty::registerProperty( + $propertyId, + $definition['type'], + $definition['label'], + true + ); + + DIProperty::registerPropertyAlias( + $propertyId, + $definition['alias'] + ); + } + + return true; + } + +} diff --git a/tests/phpunit/PropertyRegistryTest.php b/tests/phpunit/PropertyRegistryTest.php new file mode 100644 index 0000000..49b3a3c --- /dev/null +++ b/tests/phpunit/PropertyRegistryTest.php @@ -0,0 +1,59 @@ +<?php + +namespace SG\Tests; + +use SG\PropertyRegistry; +use SMW\DIProperty; + +/** + * @covers \SG\PropertyRegistry + * + * @ingroup Test + * + * @group SG + * @group SGExtension + * + * @licence GNU GPL v2+ + * @since 1.0 + * + * @author mwjames + */ +class PropertyRegistryTest extends \PHPUnit_Framework_TestCase { + + public function testCanConstruct() { + $this->assertInstanceOf( + '\SG\PropertyRegistry', + PropertyRegistry::getInstance() + ); + } + + public function testRegisterPropertiesAndAliases() { + PropertyRegistry::clear(); + $this->assertTrue( PropertyRegistry::getInstance()->registerPropertiesAndAliases() ); + } + + /** + * @dataProvider propertyDefinitionDataProvider + */ + public function testRegisteredPropertyById( $id, $label ) { + + $property = new DIProperty( $id ); + + $this->assertInstanceOf( '\SMW\DIProperty', $property ); + $this->assertEquals( $label, $property->getLabel() ); + $this->assertTrue( $property->isShown() ); + } + + public function propertyDefinitionDataProvider() { + + $provider = array(); + + $provider[] = array( PropertyRegistry::SG_TERM, SG_PROP_GLT ); + $provider[] = array( PropertyRegistry::SG_DEFINITION, SG_PROP_GLD ); + $provider[] = array( PropertyRegistry::SG_LINK, SG_PROP_GLL ); + $provider[] = array( PropertyRegistry::SG_STYLE, SG_PROP_GLS ); + + return $provider; + } + +} -- To view, visit https://gerrit.wikimedia.org/r/117159 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I9e9a0b6fbeef1b26ec4cb0bce2a752a2de693d04 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/SemanticGlossary Gerrit-Branch: master Gerrit-Owner: Mwjames <jamesin.hongkon...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits