Completed
Pull Request — master (#95)
by Mark A.
02:59
created

SemanticExtraSpecialProperties.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 26 and the first side effect is on line 13.

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 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
		 * In case extension.json is being used, the succeeding steps are
49
		 * expected to be handled by the ExtensionRegistry aka extension.json
50
		 * ...
51
		 *
52
		 * 	"callback": "SemanticExtraSpecialProperties::initExtension",
53
		 * 	"ExtensionFunctions": [
54
		 * 		"SemanticExtraSpecialProperties::onExtensionFunction"
55
		 * 	],
56
		 */
57
		self::initExtension();
58
59
		$GLOBALS['wgExtensionFunctions'][] = function() {
60
			self::onExtensionFunction();
61
		};
62
	}
63
64
	/**
65
	 * @since 1.4
66
	 */
67
	public static function initExtension() {
68
69
		define( 'SESP_VERSION', '2.0.0-alpha' );
70
71
		// Register extension info
72
		$GLOBALS['wgExtensionCredits']['semantic'][] = [
73
			'path'           => __FILE__,
74
			'name'           => 'Semantic Extra Special Properties',
75
			'author'         => [
76
				'[https://www.semantic-mediawiki.org/wiki/User:MWJames James Hong Kong]',
77
				'...'
78
			],
79
			'version'        => SESP_VERSION,
80
			'url'            => 'https://www.mediawiki.org/wiki/Extension:SemanticExtraSpecialProperties',
81
			'descriptionmsg' => 'sesp-desc',
82
			'license-name'   => 'GPL-2.0+'
83
		];
84
85
		$GLOBALS['wgMessagesDirs']['SemanticExtraSpecialProperties'] = __DIR__ . '/i18n';
86
87
		self::onBeforeExtensionFunction();
88
	}
89
90
	/**
91
	 * Register hooks that require to be listed as soon as possible and preferable
92
	 * before the execution of onExtensionFunction
93
	 *
94
	 * @since 1.4
95
	 */
96
	public static function onBeforeExtensionFunction() {
97
		$GLOBALS['wgHooks']['SMW::Config::BeforeCompletion'][] = '\SESP\HookRegistry::onBeforeConfigCompletion';
98
	}
99
100
	/**
101
	 * @since 2.0
102
	 */
103
	public static function checkRequirements() {
104
105
		if ( version_compare( $GLOBALS['wgVersion'], '1.27', '<' ) ) {
106
			die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticExtraSpecialProperties/">Semantic Extra Special Properties</a> requires MediaWiki 1.27 or above.' );
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...
107
		}
108
109
		if ( !defined( 'SMW_VERSION' ) ) {
110
			die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticExtraSpecialProperties/">Semantic Extra Special Properties</a> requires <a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki">Semantic MediaWiki</a> installed.<br />' );
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...
111
		}
112
	}
113
114
	/**
115
	 * @since 1.4
116
	 */
117
	public static function onExtensionFunction() {
118
119
		// Check requirements after LocalSetting.php has been processed
120
		self::checkRequirements();
121
122
		$configuration = [
123
			'wgDisableCounters'       => $GLOBALS['wgDisableCounters'],
124
			'sespUseAsFixedTables'    => $GLOBALS['sespUseAsFixedTables'],
125
			'sespSpecialProperties'   => $GLOBALS['sespSpecialProperties'],
126
			'wgSESPExcludeBots'       => $GLOBALS['wgSESPExcludeBots'],
127
			'wgShortUrlPrefix'        => $GLOBALS['wgShortUrlPrefix'],
128
			'sespPropertyDefinitionFile' => $GLOBALS['sespPropertyDefinitionFile'],
129
			'sespLocalPropertyDefinitions' => $GLOBALS['sespLocalPropertyDefinitions'],
130
			'sespLabelCacheVersion' => $GLOBALS['sespLabelCacheVersion'],
131
			'sespPropertyDefinitions' => [],
132
		];
133
134
		$hookRegistry = new HookRegistry(
135
			$configuration
136
		);
137
138
		$hookRegistry->register();
139
	}
140
141
	/**
142
	 * @since 1.4
143
	 *
144
	 * @return string|null
145
	 */
146
	public static function getVersion() {
147
		return SESP_VERSION;
148
	}
149
150
}
151