onExtensionFunction()   C
last analyzed

Complexity

Conditions 12
Paths 130

Size

Total Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 6.0715
c 0
b 0
f 0
cc 12
nc 130
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use SESP\HookRegistry;
4
5
/**
6
 * Extension SemanticExtraSpecialProperties - Adds some extra special properties to all pages.
7
 *
8
 * @see https://github.com/SemanticMediaWiki/SemanticExtraSpecialProperties
9
 *
10
 * @defgroup SemanticExtraSpecialProperties Semantic Extra Special Properties
11
 */
12
if ( !defined( 'MEDIAWIKI' ) ) {
13
	die( 'This file is part of the SemanticExtraSpecialProperties extension, it is not a valid entry point.' );
14
}
15
16
if ( defined( 'SESP_VERSION' ) ) {
17
	// Do not initialize more than once.
18
	return 1;
19
}
20
21
SemanticExtraSpecialProperties::load();
22
23
/**
24
 * @codeCoverageIgnore
25
 */
26
class SemanticExtraSpecialProperties {
27
28
	/**
29
	 * @since 1.4
30
	 *
31
	 * @note It is expected that this function is loaded before LocalSettings.php
32
	 * to ensure that settings and global functions are available by the time
33
	 * the extension is activated.
34
	 */
35
	public static function load() {
36
37
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
38
			include_once __DIR__ . '/vendor/autoload.php';
39
		}
40
41
		foreach ( include __DIR__ . '/DefaultSettings.php' as $key => $value ) {
42
			if ( !isset( $GLOBALS[$key] ) ) {
43
				$GLOBALS[$key] = $value;
44
			}
45
		}
46
	}
47
48
	/**
49
	 * @since 1.4
50
	 */
51
	public static function initExtension( $credits = [] ) {
52
53
		// See https://phabricator.wikimedia.org/T151136
54
		define( 'SESP_VERSION', isset( $credits['version'] ) ? $credits['version'] : 'UNKNOWN' );
55
56
		$GLOBALS['wgMessagesDirs']['SemanticExtraSpecialProperties'] = __DIR__ . '/i18n';
57
58
		// Register hooks that require to be listed as soon as possible and preferable
59
		// before the execution of onExtensionFunction
60
		HookRegistry::initExtension( $GLOBALS );
61
	}
62
63
	/**
64
	 * @since 1.4
65
	 */
66
	public static function onExtensionFunction() {
67
68
		if ( !defined( 'SMW_VERSION' ) ) {
69
			if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
70
				die( "\nThe 'Semantic Extra Special Properties' extension requires 'Semantic MediaWiki' to be installed and enabled.\n" );
71
			} else {
72
				die( '<b>Error:</b> The <a href="https://github.com/SemanticMediaWiki/SemanticExtraSpecialProperties/">Semantic Extra Special Properties</a> extension requires <a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki">Semantic MediaWiki</a> to be installed and enabled.<br />' );
73
			}
74
		}
75
76
		// Cover legacy settings
77
		$deprecationNotices = [];
78
79
		if ( isset( $GLOBALS['sespUseAsFixedTables'] ) ) {
80
			$GLOBALS['sespgUseFixedTables'] = $GLOBALS['sespUseAsFixedTables'];
81
			$deprecationNotices['replacement']['sespUseAsFixedTables'] = 'sespgUseFixedTables';
82
		}
83
84
		if ( isset( $GLOBALS['sespPropertyDefinitionFile'] ) ) {
85
			$GLOBALS['sespgDefinitionsFile'] = $GLOBALS['sespPropertyDefinitionFile'];
86
			$deprecationNotices['replacement']['sespPropertyDefinitionFile'] = 'sespgDefinitionsFile';
87
		}
88
89
		if ( isset( $GLOBALS['sespLocalPropertyDefinitions'] ) ) {
90
			$GLOBALS['sespgLocalDefinitions'] = $GLOBALS['sespLocalPropertyDefinitions'];
91
			$deprecationNotices['replacement']['sespLocalPropertyDefinitions'] = 'sespgLocalDefinitions';
92
		}
93
94
		if ( isset( $GLOBALS['sespSpecialProperties'] ) ) {
95
			$GLOBALS['sespgEnabledPropertyList'] = $GLOBALS['sespSpecialProperties'];
96
			$deprecationNotices['replacement']['sespSpecialProperties'] = 'sespgEnabledPropertyList';
97
		}
98
99
		if ( isset( $GLOBALS['sespLabelCacheVersion'] ) ) {
100
			$GLOBALS['sespgLabelCacheVersion'] = $GLOBALS['sespLabelCacheVersion'];
101
			$deprecationNotices['replacement']['sespLabelCacheVersion'] = 'sespgLabelCacheVersion';
102
		}
103
104
		if ( isset( $GLOBALS['wgSESPExcludeBots'] ) ) {
105
			$GLOBALS['sespgExcludeBotEdits'] = $GLOBALS['wgSESPExcludeBots'];
106
			$deprecationNotices['replacement']['wgSESPExcludeBots'] = 'sespgExcludeBotEdits';
107
		}
108
109
		// Allow deprecated settings to appear on the `Special:SemanticMediaWiki`
110
		// "Deprecation notices" section
111
		if ( $deprecationNotices !== [] && !isset( $GLOBALS['smwgDeprecationNotices']['sesp'] ) ) {
112
			$GLOBALS['smwgDeprecationNotices']['sesp'] = [ 'replacement' => $deprecationNotices['replacement'] ];
113
		}
114
115
		$config = [
116
			'sespgUseFixedTables'      => $GLOBALS['sespgUseFixedTables'],
117
			'sespgEnabledPropertyList' => $GLOBALS['sespgEnabledPropertyList'],
118
			'sespgExcludeBotEdits'     => $GLOBALS['sespgExcludeBotEdits'],
119
			'sespgDefinitionsFile'     => $GLOBALS['sespgDefinitionsFile'],
120
			'sespgLocalDefinitions'    => $GLOBALS['sespgLocalDefinitions'],
121
			'sespgLabelCacheVersion'   => $GLOBALS['sespgLabelCacheVersion'],
122
123
			// Non-SESP settings
124
			'wgDisableCounters'        => $GLOBALS['wgDisableCounters'] ?? null,
125
			'wgShortUrlPrefix'         => $GLOBALS['wgShortUrlPrefix'],
126
		];
127
128
		$hookRegistry = new HookRegistry(
129
			$config
130
		);
131
132
		$hookRegistry->register();
133
	}
134
135
	/**
136
	 * @since 1.4
137
	 *
138
	 * @return string|null
139
	 */
140
	public static function getVersion() {
141
142
		if ( !defined( 'SESP_VERSION' ) ) {
143
			return null;
144
		}
145
146
		return SESP_VERSION;
147
	}
148
149
}
150