Skip to content

Commit

Permalink
Add extension.json support (#62)
Browse files Browse the repository at this point in the history
* Add extension.json support
* Update extension.json
* Update composer.json
  • Loading branch information
mwjames authored and kghbln committed Oct 6, 2018
1 parent 1dd58af commit 87fd773
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 70 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
83 changes: 25 additions & 58 deletions SemanticCite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/**
Expand All @@ -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';
Expand Down Expand Up @@ -134,40 +113,23 @@ 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( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticCite/">Semantic Cite</a> is only compatible with MediaWiki 1.27 or above. You need to upgrade MediaWiki first.' );
}

if ( !defined( 'SMW_VERSION' ) ) {
die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticCite/">Semantic Cite</a> requires the <a href="https://github.com/SemanticMediaWiki/SemanticMediaWiki/">Semantic MediaWiki</a> 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 );
}

/**
* @since 1.1
*/
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( '<b>Error:</b> The <a href="https://github.com/SemanticMediaWiki/SemanticCite/">Semantic Cite</a> extension requires <a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki">Semantic MediaWiki</a> to be installed and enabled.<br />' );
}
}

// Require a global because MW's Special page is missing an interface
// to inject dependencies
Expand Down Expand Up @@ -203,6 +165,11 @@ public static function onExtensionFunction() {
* @return string|null
*/
public static function getVersion() {

if ( !defined( 'SCI_VERSION' ) ) {
return null;
}

return SCI_VERSION;
}

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
],
Expand All @@ -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",
Expand All @@ -44,7 +44,6 @@
},
"autoload": {
"files" : [
"DefaultSettings.php",
"SemanticCite.php"
],
"psr-4": {
Expand All @@ -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"
}
}
25 changes: 25 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
@@ -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
}
36 changes: 36 additions & 0 deletions src/HookRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 12 additions & 5 deletions tests/phpunit/Unit/HookRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
1 change: 1 addition & 0 deletions tests/travis/install-semantic-cite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function updateConfiguration {

# SMW#1732
echo 'wfLoadExtension( "SemanticMediaWiki" );' >> LocalSettings.php
echo 'wfLoadExtension( "SemanticCite" );' >> LocalSettings.php

php maintenance/update.php --quick
}
Expand Down

0 comments on commit 87fd773

Please sign in to comment.