Completed
Pull Request — master (#67)
by Karsten
18:54
created

SemanticInterlanguageLinks::onExtensionFunction()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.2986
c 0
b 0
f 0
cc 7
nc 6
nop 0
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 19 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
14
SemanticInterlanguageLinks::load();
15
16
/**
17
 * @codeCoverageIgnore
18
 */
19
class SemanticInterlanguageLinks {
20
21
	/**
22
	 * @since 1.4
23
	 *
24
	 * @note It is expected that this function is loaded before LocalSettings.php
25
	 * to ensure that settings and global functions are available by the time
26
	 * the extension is activated.
27
	 */
28
	public static function load() {
29
30
		// Load DefaultSettings
31
		require_once __DIR__ . '/DefaultSettings.php';
32
33
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
34
			include_once __DIR__ . '/vendor/autoload.php';
35
		}
36
	}
37
38
	/**
39
	 * @since 1.3
40
	 */
41
	public static function initExtension( $credits = [] ) {
42
43
		define( 'SIL_VERSION', isset( $credits['version'] ) ? $credits['version'] : 'UNKNOWN' );
44
45
		// Register message files
46
		$GLOBALS['wgMessagesDirs']['SemanticInterlanguageLinks'] = __DIR__ . '/i18n';
47
		$GLOBALS['wgExtensionMessagesFiles']['SemanticInterlanguageLinksMagic'] = __DIR__ . '/i18n/SemanticInterlanguageLinks.magic.php';
48
49
		self::onBeforeExtensionFunction();
50
	}
51
52
	/**
53
	 * Register hooks that require to be listed as soon as possible and preferable
54
	 * before the execution of onExtensionFunction
55
	 *
56
	 * @since 1.4
57
	 */
58
	public static function onBeforeExtensionFunction() {
59
		$GLOBALS['wgHooks']['SMW::Config::BeforeCompletion'][] = '\SIL\HookRegistry::onBeforeConfigCompletion';
60
	}
61
62
	/**
63
	 * @since 1.4
64
	 */
65
	public static function checkRequirements() {
66
67
		if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.27', 'lt' ) ) {
68
			die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticInterlanguageLinks/">Semantic Interlanguage Links</a> is only compatible with MediaWiki 1.27 or above. You need to upgrade MediaWiki first.' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkRequirements() 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...
69
		}
70
71
		if ( !defined( 'SMW_VERSION' ) ) {
72
			die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticInterlanguageLinks/">Semantic Interlanguage Links</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 checkRequirements() 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...
73
		}
74
	}
75
76
	/**
77
	 * @since 1.3
78
	 */
79
	public static function onExtensionFunction() {
80
81
		if ( !defined( 'SMW_VERSION' ) ) {
82
83
			if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
84
				die( "\nThe 'Semantic Interlanguage Links' extension requires 'Semantic MediaWiki' to be installed and enabled.\n" );
0 ignored issues
show
Coding Style Compatibility introduced by
The method onExtensionFunction() 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...
85
			} else {
86
				die(
0 ignored issues
show
Coding Style Compatibility introduced by
The method onExtensionFunction() 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...
87
					'<b>Error:</b> The <a href="https://www.semantic-mediawiki.org/wiki/Extension:Semantic_Interlanguage_Links">Semantic Interlanguage Links</a> ' .
88
					'extension requires <a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki">Semantic MediaWiki</a> to be ' .
89
					'installed and enabled.<br>'
90
				);
91
			}
92
		}
93
94
		// Legacy
95
		if ( isset( $GLOBALS['egSILEnabledCategoryFilterByLanguage'] ) ) {
96
			$GLOBALS['silgEnabledCategoryFilterByLanguage'] = $GLOBALS['egSILEnabledCategoryFilterByLanguage'];
97
		}
98
99
		if ( isset( $GLOBALS['egSILCacheType'] ) ) {
100
			$GLOBALS['silgCacheType'] = $GLOBALS['egSILCacheType'];
101
		}
102
103
		$cacheFactory = new CacheFactory();
104
105
		$compositeCache = $cacheFactory->newCompositeCache( [
106
			$cacheFactory->newFixedInMemoryLruCache( 500 ),
107
			$cacheFactory->newMediaWikiCache( ObjectCache::getInstance( $GLOBALS['silgCacheType'] ) )
108
		] );
109
110
		$cacheKeyProvider = new CacheKeyProvider(
111
			$GLOBALS['wgCachePrefix'] === false ? wfWikiID() : $GLOBALS['wgCachePrefix']
112
		);
113
114
		$hookRegistry = new HookRegistry(
115
			ApplicationFactory::getInstance()->getStore(),
116
			$compositeCache,
117
			$cacheKeyProvider
118
		);
119
120
		$hookRegistry->register();
121
	}
122
123
	/**
124
	 * @since 1.3
125
	 *
126
	 * @return string|null
127
	 */
128
	public static function getVersion() {
129
		return SIL_VERSION;
130
	}
131
132
}
133