Completed
Push — master ( deaef9...81d09c )
by mw
06:04
created

SemanticInterlanguageLinks   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 114
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 17 2
A initExtension() 0 21 1
A onBeforeExtensionFunction() 0 3 1
A doCheckRequirements() 0 10 3
B onExtensionFunction() 0 24 2
A getVersion() 0 3 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
use SIL\HookRegistry;
4
use SIL\CacheKeyProvider;
5
use SMW\ApplicationFactory;
6
use Onoi\Cache\CacheFactory;
7
8
/**
9
 * @see https://github.com/SemanticMediaWiki/SemanticInterlanguageLinks/
10
 *
11
 * @defgroup SIL Semantic Interlanguage Links
12
 */
13
if ( !defined( 'MEDIAWIKI' ) ) {
14
	die( 'This file is part of the SemanticInterlanguageLinks extension, it is not a valid entry point.' );
15
}
16
17
if ( defined( 'SIL_VERSION' ) ) {
18
	// Do not initialize more than once.
19
	return 1;
20
}
21
22
SemanticInterlanguageLinks::load();
23
24
/**
25
 * @codeCoverageIgnore
26
 */
27
class SemanticInterlanguageLinks {
28
29
	/**
30
	 * @since 1.4
31
	 *
32
	 * @note It is expected that this function is loaded before LocalSettings.php
33
	 * to ensure that settings and global functions are available by the time
34
	 * the extension is activated.
35
	 */
36
	public static function load() {
37
38
		// Load DefaultSettings
39
		require_once __DIR__ . '/DefaultSettings.php';
40
41
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
42
			include_once __DIR__ . '/vendor/autoload.php';
43
		}
44
45
		// In case extension.json is being used, the the succeeding steps will
46
		// be handled by the ExtensionRegistry
47
		self::initExtension();
48
49
		$GLOBALS['wgExtensionFunctions'][] = function() {
50
			self::onExtensionFunction();
51
		};
52
	}
53
54
	/**
55
	 * @since 1.3
56
	 */
57
	public static function initExtension() {
58
59
		define( 'SIL_VERSION', '1.4.0-alpha' );
60
61
		// Register extension info
62
		$GLOBALS[ 'wgExtensionCredits' ][ 'semantic' ][ ] = array(
63
			'path'           => __FILE__,
64
			'name'           => 'Semantic Interlanguage Links',
65
			'author'         => array( 'James Hong Kong' ),
66
			'url'            => 'https://github.com/SemanticMediaWiki/SemanticInterlanguageLinks/',
67
			'descriptionmsg' => 'sil-desc',
68
			'version'        => SIL_VERSION,
69
			'license-name'   => 'GPL-2.0+',
70
		);
71
72
		// Register message files
73
		$GLOBALS['wgMessagesDirs']['SemanticInterlanguageLinks'] = __DIR__ . '/i18n';
74
		$GLOBALS['wgExtensionMessagesFiles']['SemanticInterlanguageLinksMagic'] = __DIR__ . '/i18n/SemanticInterlanguageLinks.magic.php';
75
76
		self::onBeforeExtensionFunction();
77
	}
78
79
	/**
80
	 * Register hooks that require to be listed as soon as possible and preferable
81
	 * before the execution of onExtensionFunction
82
	 *
83
	 * @since 1.4
84
	 */
85
	public static function onBeforeExtensionFunction() {
86
		$GLOBALS['wgHooks']['SMW::Config::BeforeCompletion'][] = '\SIL\HookRegistry::onBeforeConfigCompletion';
87
	}
88
89
	/**
90
	 * @since 1.4
91
	 */
92
	public static function doCheckRequirements() {
93
94
		if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.23', 'lt' ) ) {
95
			die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticInterlanguageLinks/">Semantic InterlanguageLinks</a> is only compatible with MediaWiki 1.23 or above. You need to upgrade MediaWiki first.' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method doCheckRequirements() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
96
		}
97
98
		if ( !defined( 'SMW_VERSION' ) ) {
99
			die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticInterlanguageLinks/">Semantic InterlanguageLinks</a> requires <a href="https://github.com/SemanticMediaWiki/SemanticMediaWiki/">Semantic MediaWiki</a>, please enable or install the extension first.' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method doCheckRequirements() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
100
		}
101
	}
102
103
	/**
104
	 * @since 1.3
105
	 */
106
	public static function onExtensionFunction() {
107
108
		// Check requirements after LocalSetting.php has been processed
109
		self::doCheckRequirements();
110
111
		$cacheFactory = new CacheFactory();
112
113
		$compositeCache = $cacheFactory->newCompositeCache( array(
114
			$cacheFactory->newFixedInMemoryLruCache( 500 ),
115
			$cacheFactory->newMediaWikiCache( ObjectCache::getInstance( $GLOBALS['egSILCacheType'] ) )
116
		) );
117
118
		$cacheKeyProvider = new CacheKeyProvider(
119
			$GLOBALS['wgCachePrefix'] === false ? wfWikiID() : $GLOBALS['wgCachePrefix']
120
		);
121
122
		$hookRegistry = new HookRegistry(
123
			ApplicationFactory::getInstance()->getStore(),
124
			$compositeCache,
125
			$cacheKeyProvider
126
		);
127
128
		$hookRegistry->register();
129
	}
130
131
	/**
132
	 * @since 1.3
133
	 *
134
	 * @return string|null
135
	 */
136
	public static function getVersion() {
137
		return SIL_VERSION;
138
	}
139
140
}
141