SemanticBreadcrumbLinks::onExtensionFunction()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 7.8084
c 0
b 0
f 0
cc 7
nc 6
nop 0

How to fix   Long Method   

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 SBL\HookRegistry;
4
use SBL\Options;
5
use SMW\ApplicationFactory;
6
7
/**
8
 * @see https://github.com/SemanticMediaWiki/SemanticBreadcrumbLinks/
9
 *
10
 * @defgroup SBL Semantic Breadcrumb Links
11
 */
12
if ( !defined( 'MEDIAWIKI' ) ) {
13
	die( 'This file is part of the Semantic Breadcrumb Links extension. It is not a valid entry point.' );
14
}
15
16
if ( defined( 'SBL_VERSION' ) ) {
17
	// Do not initialize more than once.
18
	return 1;
19
}
20
21
SemanticBreadcrumbLinks::load();
22
23
/**
24
 * @codeCoverageIgnore
25
 */
26
class SemanticBreadcrumbLinks {
27
28
	/**
29
	 * @since 1.3
30
	 */
31
	public static function load() {
32
33
		if ( !defined( 'MEDIAWIKI' ) ) {
34
			return;
35
		}
36
37
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
38
			include_once __DIR__ . '/vendor/autoload.php';
39
		}
40
41
		// #56 Ensure the constant is defined before `LocalSettings.php` is
42
		// loaded in order to make it available for use in `LocalSettings.php`
43
		define( 'SBL_PROP_PARENTPAGE', 'Has parent page' );
44
45
		// Load DefaultSettings
46
		require_once __DIR__ . '/DefaultSettings.php';
47
	}
48
49
	/**
50
	 * @since 1.3
51
	 */
52
	public static function initExtension( $credits = [] ) {
53
54
		// See https://phabricator.wikimedia.org/T151136
55
		define( 'SBL_VERSION', isset( $credits['version'] ) ? $credits['version'] : 'UNKNOWN' );
56
57
		// Register message files
58
		$GLOBALS['wgMessagesDirs']['SemanticBreadcrumbLinks'] = __DIR__ . '/i18n';
59
		$GLOBALS['wgExtensionMessagesFiles']['SemanticBreadcrumbLinksMagic'] = __DIR__ . '/i18n/SemanticBreadcrumbLinks.magic.php';
60
61
		// Register resource files
62
		$GLOBALS['wgResourceModules']['ext.semanticbreadcrumblinks.styles'] = [
63
			'styles'  => 'res/sbl.styles.css',
64
			'localBasePath' => __DIR__ ,
65
			'remoteExtPath' => 'SemanticBreadcrumbLinks',
66
			'position' => 'top',
67
			'group'    => 'ext.smw',
68
			'targets' => [
69
				'mobile',
70
				'desktop'
71
			]
72
		];
73
74
		$GLOBALS['wgResourceModules']['ext.semanticbreadcrumblinks'] = [
75
			'scripts' => 'res/sbl.tooltip.js',
76
			'localBasePath' => __DIR__ ,
77
			'remoteExtPath' => 'SemanticBreadcrumbLinks',
78
			'position' => 'top',
79
			'group'    => 'ext.smw',
80
			'dependencies'  => [
81
				'ext.semanticbreadcrumblinks.styles',
82
				'onoi.qtip'
83
			],
84
			'targets' => [
85
				'mobile',
86
				'desktop'
87
			]
88
		];
89
	}
90
91
	/**
92
	 * @since 1.3
93
	 */
94
	public static function onExtensionFunction() {
95
96
		if ( !defined( 'SMW_VERSION' ) ) {
97
			if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
98
				die( "\nThe 'Semantic Breadcrumb Links' extension requires 'Semantic MediaWiki' to be installed and enabled.\n" );
99
			} else {
100
				die( '<b>Error:</b> The <a href="https://github.com/SemanticMediaWiki/SemanticBreadcrumbLinks/">Semantic Breadcrumb Links</a> extension requires <a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki">Semantic MediaWiki</a> to be installed and enabled.<br />' );
101
			}
102
		}
103
104
		// Default values are defined at this point to ensure
105
		// NS contants are specified prior
106
		$defaultPropertySearchPatternByNamespace = [
107
			NS_CATEGORY => [
108
				'_SUBC',
109
				'_SUBC',
110
				'_SUBC'
111
			],
112
			SMW_NS_PROPERTY => [
113
				'_SUBP',
114
				'_SUBP',
115
				'_SUBP'
116
			],
117
			NS_MAIN => [
118
				SBL_PROP_PARENTPAGE,
119
				SBL_PROP_PARENTPAGE,
120
				SBL_PROP_PARENTPAGE
121
			],
122
			NS_HELP => [
123
				SBL_PROP_PARENTPAGE,
124
				SBL_PROP_PARENTPAGE,
125
				SBL_PROP_PARENTPAGE
126
			]
127
		];
128
129
		// Cover legacy settings
130
		$deprecationNotices = [];
131
132
		if ( isset( $GLOBALS['egSBLBreadcrumbTrailStyleClass'] ) ) {
133
			$GLOBALS['sblgBreadcrumbTrailStyleClass'] = $GLOBALS['egSBLBreadcrumbTrailStyleClass'];
134
			$deprecationNotices['replacement']['egSBLBreadcrumbTrailStyleClass'] = 'sblgBreadcrumbTrailStyleClass';
135
		}
136
137
		if ( $deprecationNotices !== [] && !isset( $GLOBALS['smwgDeprecationNotices']['sbl'] ) ) {
138
			$GLOBALS['smwgDeprecationNotices']['sbl'] = [ 'replacement' => $deprecationNotices['replacement'] ];
139
		}
140
141
		$configuration = [
142
			'hideSubpageParent' => $GLOBALS['egSBLPageTitleToHideSubpageParent'],
143
			'breadcrumbTrailStyleClass' => $GLOBALS['sblgBreadcrumbTrailStyleClass'],
144
			'breadcrumbDividerStyleClass' => $GLOBALS['egSBLBreadcrumbDividerStyleClass'],
145
			'tryToFindClosestDescendant' => $GLOBALS['egSBLTryToFindClosestDescendant'],
146
			'useSubpageFinderFallback' => $GLOBALS['egSBLUseSubpageFinderFallback'],
147
			'enabledSubpageParentAnnotation' => $GLOBALS['egSBLEnabledSubpageParentAnnotation'],
148
			'disableTranslationSubpageAnnotation' => $GLOBALS['egSBLDisableTranslationSubpageAnnotation'],
149
			'wgNamespacesWithSubpages' => $GLOBALS['wgNamespacesWithSubpages'],
150
			'propertySearchPatternByNamespace' => $GLOBALS['egSBLPropertySearchPatternByNamespace'] + $defaultPropertySearchPatternByNamespace
151
		];
152
153
		$hookRegistry = new HookRegistry(
154
			ApplicationFactory::getInstance()->getStore(),
155
			new Options( $configuration )
156
		);
157
158
		$hookRegistry->register();
159
	}
160
161
	/**
162
	 * @since 1.3
163
	 *
164
	 * @return string|null
165
	 */
166
	public static function getVersion() {
167
		return SBL_VERSION;
168
	}
169
170
}
171