From 87fd7731ebb69ca03d965b49fdb2a1fbb987302d Mon Sep 17 00:00:00 2001 From: mwjames Date: Sat, 6 Oct 2018 20:15:46 +0000 Subject: [PATCH] Add extension.json support (#62) * Add extension.json support * Update extension.json * Update composer.json --- .travis.yml | 4 +- SemanticCite.php | 83 ++++++++----------------- composer.json | 10 +-- extension.json | 25 ++++++++ src/HookRegistry.php | 36 +++++++++++ tests/phpunit/Unit/HookRegistryTest.php | 17 +++-- tests/travis/install-semantic-cite.sh | 1 + 7 files changed, 106 insertions(+), 70 deletions(-) create mode 100644 extension.json diff --git a/.travis.yml b/.travis.yml index c7ea4d3..50d8cf6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,11 +9,11 @@ matrix: - env: DB=mysql; MW=REL1_29; TYPE=coverage; PHPUNIT=4.8.* php: 5.6 - env: DB=mysql; MW=REL1_27; - php: 5.5 + php: 5.6 - env: DB=sqlite; MW=REL1_28; SITELANG=ja php: 5.6 - env: DB=sqlite; MW=master; PHPUNIT=4.8.* - php: '7' + php: 7.1 exclude: - env: THENEEDFORTHIS=FAIL allow_failures: diff --git a/SemanticCite.php b/SemanticCite.php index 88fe1e9..6ed9a1b 100644 --- a/SemanticCite.php +++ b/SemanticCite.php @@ -9,15 +9,6 @@ * * @defgroup SCI Semantic Citation */ -if ( !defined( 'MEDIAWIKI' ) ) { - die( 'This file is part of the Semantic Cite extension, it is not a valid entry point.' ); -} - -if ( defined( 'SCI_VERSION' ) ) { - // Do not initialize more than once. - return 1; -} - SemanticCite::load(); /** @@ -40,33 +31,21 @@ public static function load() { // Load DefaultSettings require_once __DIR__ . '/DefaultSettings.php'; - - // In case extension.json is being used, the the succeeding steps are - // expected to be handled by the ExtensionRegistry - self::initExtension(); - - $GLOBALS['wgExtensionFunctions'][] = function() { - self::onExtensionFunction(); - }; } /** * @since 1.1 */ - public static function initExtension() { - - define( 'SCI_VERSION', '1.4.0-alpha' ); - - // Register the extension - $GLOBALS['wgExtensionCredits']['semantic'][ ] = [ - 'path' => __FILE__, - 'name' => 'Semantic Cite', - 'author' => [ 'James Hong Kong' ], - 'url' => 'https://github.com/SemanticMediaWiki/SemanticCite/', - 'descriptionmsg' => 'sci-desc', - 'version' => SCI_VERSION, - 'license-name' => 'GPL-2.0-or-later' - ]; + public static function initExtension( $credits = array() ) { + + // See https://phabricator.wikimedia.org/T151136 + define( 'SCI_VERSION', isset( $credits['version'] ) ? $credits['version'] : 'UNKNOWN' ); + + // Extend the upgrade key provided by SMW to ensure that an DB + // schema is updated accordingly before using the extension + if ( isset( $GLOBALS['smwgUpgradeKey'] ) ) { + // $GLOBALS['smwgUpgradeKey'] .= ':scite:2018-09'; + } // Register message files $GLOBALS['wgMessagesDirs']['SemanticCite'] = __DIR__ . '/i18n'; @@ -134,31 +113,9 @@ public static function initExtension() { ] ]; - self::onBeforeExtensionFunction(); - } - - /** - * Register hooks that require to be listed as soon as possible and preferable - * before the execution of onExtensionFunction - * - * @since 1.3 - */ - public static function onBeforeExtensionFunction() { - $GLOBALS['wgHooks']['SMW::Config::BeforeCompletion'][] = '\SCI\HookRegistry::onBeforeConfigCompletion'; - } - - /** - * @since 1.3 - */ - public static function doCheckRequirements() { - - if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.27', 'lt' ) ) { - die( 'Error: This version of Semantic Cite is only compatible with MediaWiki 1.27 or above. You need to upgrade MediaWiki first.' ); - } - - if ( !defined( 'SMW_VERSION' ) ) { - die( 'Error: Semantic Cite requires the Semantic MediaWiki extension. Please enable or install the extension first.' ); - } + // Register hooks that require to be listed as soon as possible and preferable + // before the execution of onExtensionFunction + HookRegistry::initExtension( $GLOBALS ); } /** @@ -166,8 +123,13 @@ public static function doCheckRequirements() { */ public static function onExtensionFunction() { - // Check requirements after LocalSetting.php has been processed - self::doCheckRequirements(); + if ( !defined( 'SMW_VERSION' ) ) { + if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) { + die( "\nThe 'Semantic Cite' extension requires 'Semantic MediaWiki' to be installed and enabled.\n" ); + } else { + die( 'Error: The Semantic Cite extension requires Semantic MediaWiki to be installed and enabled.
' ); + } + } // Require a global because MW's Special page is missing an interface // to inject dependencies @@ -203,6 +165,11 @@ public static function onExtensionFunction() { * @return string|null */ public static function getVersion() { + + if ( !defined( 'SCI_VERSION' ) ) { + return null; + } + return SCI_VERSION; } diff --git a/composer.json b/composer.json index 28cfbe1..7e593a4 100644 --- a/composer.json +++ b/composer.json @@ -9,11 +9,11 @@ "mediawiki", "citation" ], - "homepage": "https://semantic-mediawiki.org/wiki/Extension:SemanticCite", + "homepage": "https://semantic-mediawiki.org/wiki/Extension:Semantic_Cite", "license": "GPL-2.0-or-later", "authors": [ { - "name": "mwjames", + "name": "James Hong Kong", "role": "Developer" } ], @@ -26,7 +26,7 @@ "source": "https://github.com/SemanticMediaWiki/SemanticCite" }, "require": { - "php": ">=5.5.0", + "php": ">=5.6.0", "composer/installers": "^1.0.12", "mediawiki/semantic-media-wiki": "~2.5|~3.0", "onoi/cache": "~1.2", @@ -44,7 +44,6 @@ }, "autoload": { "files" : [ - "DefaultSettings.php", "SemanticCite.php" ], "psr-4": { @@ -55,6 +54,7 @@ "process-timeout": 0 }, "scripts":{ - "phpunit": "php ../../tests/phpunit/phpunit.php -c phpunit.xml.dist" + "phpunit": "php ../../tests/phpunit/phpunit.php -c phpunit.xml.dist", + "test": "php ../../tests/phpunit/phpunit.php -c phpunit.xml.dist" } } diff --git a/extension.json b/extension.json new file mode 100644 index 0000000..c47c218 --- /dev/null +++ b/extension.json @@ -0,0 +1,25 @@ +{ + "name": "SemanticCite", + "version": "2.0.0-alpha", + "author": [ + "James Hong Kong" + ], + "url": "https://github.com/SemanticMediaWiki/SemanticCite/", + "descriptionmsg": "sci-desc", + "license-name": "GPL-2.0-or-later", + "type": "semantic", + "requires": { + "MediaWiki": ">= 1.27" + }, + "MessagesDirs": { + "SemanticCite": [ + "i18n" + ] + }, + "callback": "SemanticCite::initExtension", + "ExtensionFunctions": [ + "SemanticCite::onExtensionFunction" + ], + "load_composer_autoloader":true, + "manifest_version": 1 +} diff --git a/src/HookRegistry.php b/src/HookRegistry.php index 5ad6c53..b7ff08f 100644 --- a/src/HookRegistry.php +++ b/src/HookRegistry.php @@ -114,6 +114,42 @@ public static function onBeforeConfigCompletion( &$config ) { ); } + /** + * @since 2.0 + * + * @param array &$vars + */ + public static function initExtension( &$vars ) { + + $vars['wgHooks']['SMW::Config::BeforeCompletion'][] = function( &$config ) { + + $exemptionlist = [ + PropertyRegistry::SCI_CITE + ]; + + // Exclude listed properties from indexing + if ( isset( $config['smwgFulltextSearchPropertyExemptionList'] ) ) { + $config['smwgFulltextSearchPropertyExemptionList'] = array_merge( + $config['smwgFulltextSearchPropertyExemptionList'], + $exemptionlist + ); + } + + // Exclude listed properties from dependency detection as each of the + // selected object would trigger an automatic change without the necessary + // human intervention and can therefore produce unwanted query updates + + if ( isset( $config['smwgQueryDependencyPropertyExemptionList'] ) ) { + $config['smwgQueryDependencyPropertyExemptionList'] = array_merge( + $config['smwgQueryDependencyPropertyExemptionList'], + $exemptionlist + ); + } + + return true; + }; + } + private function addCallbackHandlers( $store, $cache, $options ) { $propertyRegistry = new PropertyRegistry(); diff --git a/tests/phpunit/Unit/HookRegistryTest.php b/tests/phpunit/Unit/HookRegistryTest.php index b0b41d7..cf89a04 100644 --- a/tests/phpunit/Unit/HookRegistryTest.php +++ b/tests/phpunit/Unit/HookRegistryTest.php @@ -84,21 +84,28 @@ public function testRegister() { $this->doTestRegisteredAfterDeleteSubjectComplete( $instance ); } - public function testOnBeforeConfigCompletion() { + public function testInitExtension() { - $config = [ - 'smwgFulltextSearchPropertyExemptionList' => [] - ]; + $vars = []; + HookRegistry::initExtension( $vars ); $propertyExemptionList = [ '__sci_cite' ]; - HookRegistry::onBeforeConfigCompletion( $config ); + $config = [ + 'smwgFulltextSearchPropertyExemptionList' => [], + 'smwgQueryDependencyPropertyExemptionList' => [] + ]; + + foreach ( $vars['wgHooks']['SMW::Config::BeforeCompletion'] as $callback ) { + call_user_func_array( $callback, [ &$config ] ); + } $this->assertEquals( [ 'smwgFulltextSearchPropertyExemptionList' => $propertyExemptionList, + 'smwgQueryDependencyPropertyExemptionList' => $propertyExemptionList, ], $config ); diff --git a/tests/travis/install-semantic-cite.sh b/tests/travis/install-semantic-cite.sh index 7f7e8e6..296b3ca 100644 --- a/tests/travis/install-semantic-cite.sh +++ b/tests/travis/install-semantic-cite.sh @@ -55,6 +55,7 @@ function updateConfiguration { # SMW#1732 echo 'wfLoadExtension( "SemanticMediaWiki" );' >> LocalSettings.php + echo 'wfLoadExtension( "SemanticCite" );' >> LocalSettings.php php maintenance/update.php --quick }