Completed
Pull Request — master (#83)
by Jeroen De
02:46
created

SemanticWatchlist::checkRequirements()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 3
nop 0
1
<?php
2
3
namespace SWL;
4
5
/**
6
 * @codeCoverageIgnore
7
 */
8
class SemanticWatchlist {
9
10
	/**
11
	 * @since 1.2
12
	 *
13
	 * @note It is expected that this function is loaded before LocalSettings.php
14
	 * to ensure that settings and global functions are available by the time
15
	 * the extension is activated.
16
	 */
17
	public static function load() {
18
		// Load DefaultSettings
19
		require_once __DIR__ . '/../DefaultSettings.php';
20
21
		/**
22
		 * In case extension.json is being used, the succeeding steps are
23
		 * expected to be handled by the ExtensionRegistry aka extension.json
24
		 * ...
25
		 *
26
		 * 	"callback": "SemanticWatchlist::initExtension",
27
		 * 	"ExtensionFunctions": [
28
		 * 		"SemanticWatchlist::onExtensionFunction"
29
		 * 	],
30
		 */
31
		self::initExtension();
32
33
		$GLOBALS['wgExtensionFunctions'][] = function() {
34
			SemanticWatchlist::onExtensionFunction();
35
		};
36
	}
37
38
	/**
39
	 * @since 1.2
40
	 */
41
	public static function initExtension() {
42
		// Register the extension
43
		$GLOBALS['wgExtensionCredits']['semantic'][] = array(
44
			'path' => __FILE__,
45
			'name' => 'Semantic Watchlist',
46
			'version' => SWL_VERSION,
47
			'author' => array(
48
				'[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw] for [http://www.wikiworks.com/ WikiWorks]',
49
				'...'
50
			),
51
			'url' => 'https://www.mediawiki.org/wiki/Extension:Semantic_Watchlist',
52
			'descriptionmsg' => 'semanticwatchlist-desc',
53
			'license-name'   => 'GPL-3.0+'
54
		);
55
56
		$GLOBALS['egSwlSqlDatabaseSchemaPath'] = __DIR__ . '/../src/swl-table-schema.sql';
57
58
		// Register message files
59
		$GLOBALS['wgMessagesDirs']['SemanticWatchlist'] = __DIR__ . '/../i18n';
60
		$GLOBALS['wgExtensionMessagesFiles']['SemanticWatchlistAlias'] = __DIR__ . '/../SemanticWatchlist.i18n.alias.php';
61
62
		$GLOBALS['egSWLScriptPath'] = $GLOBALS['wgExtensionAssetsPath'] === false ? $GLOBALS['wgScriptPath'] . '/extensions/SemanticWatchlist' : $GLOBALS['wgExtensionAssetsPath'] . '/SemanticWatchlist';
63
64
		// wgSpecialPages
65
		$GLOBALS['wgSpecialPages']['SemanticWatchlist'] = 'SpecialSemanticWatchlist';
66
		$GLOBALS['wgSpecialPageGroups']['SemanticWatchlist'] = 'changes';
67
68
		$GLOBALS['wgSpecialPages']['WatchlistConditions'] = 'SpecialWatchlistConditions';
69
		$GLOBALS['wgSpecialPageGroups']['WatchlistConditions'] = 'changes';
70
71
		// wgAPIModules
72
		$GLOBALS['wgAPIModules']['addswlgroup'] = 'ApiAddWatchlistGroup';
73
		$GLOBALS['wgAPIModules']['deleteswlgroup'] = 'ApiDeleteWatchlistGroup';
74
		$GLOBALS['wgAPIModules']['editswlgroup'] = 'ApiEditWatchlistGroup';
75
		$GLOBALS['wgAPIListModules']['semanticwatchlist'] = 'ApiQuerySemanticWatchlist';
76
77
		// wgAvailableRights
78
		$GLOBALS['wgAvailableRights'][] = 'semanticwatch';
79
		$GLOBALS['wgAvailableRights'][] = 'semanticwatchgroups';
80
81
		$moduleTemplate = array(
82
			'localBasePath' => __DIR__ . '/..',
83
			'remoteBasePath' => $GLOBALS['egSWLScriptPath']
84
		);
85
86
		$GLOBALS['wgResourceModules']['ext.swl.watchlist'] = $moduleTemplate + array(
87
				'styles' => array( 'specials/ext.swl.watchlist.css' ),
88
				'scripts' => array(),
89
				'dependencies' => array(),
90
				'messages' => array()
91
			);
92
93
		$GLOBALS['wgResourceModules']['ext.swl.watchlistconditions'] = $moduleTemplate + array(
94
				'styles' => array( 'specials/ext.swl.watchlistconditions.css' ),
95
				'scripts' => array(
96
					'specials/jquery.watchlistcondition.js',
97
					'specials/ext.swl.watchlistconditions.js'
98
				),
99
				'dependencies' => array(),
100
				'messages' => array(
101
					'swl-group-name',
102
					'swl-group-legend',
103
					'swl-group-properties',
104
					'swl-properties-list',
105
					'swl-group-remove-property',
106
					'swl-group-add-property',
107
					'swl-group-page-selection',
108
					'swl-group-save',
109
					'swl-group-saved',
110
					'swl-group-saving',
111
					'swl-group-remove',
112
					'swl-group-category',
113
					'swl-group-namespace',
114
					'swl-group-concept',
115
					'swl-group-confirm-remove',
116
					'swl-custom-legend',
117
					'swl-custom-remove-property',
118
					'swl-custom-text-add',
119
					'swl-custom-input',
120
				)
121
			);
122
	}
123
124
	/**
125
	 * @since 1.2
126
	 */
127
	public static function onExtensionFunction() {
128
129
		// Check requirements after LocalSetting.php has been processed, thid has
130
		// be done here to ensure SMW is loaded in case
131
		// wfLoadExtension( 'SemanticMediaWiki' ) is used
132
		self::checkRequirements();
133
134
		$configuration = array(
135
			'egSWLEnableTopLink'         => $GLOBALS['egSWLEnableTopLink'],
136
			'egSWLEnableEmailNotify'     => $GLOBALS['egSWLEnableEmailNotify'],
137
			'egSwlSqlDatabaseSchemaPath' => $GLOBALS['egSwlSqlDatabaseSchemaPath']
138
		);
139
140
		$hookRegistry = new HookRegistry(
141
			$configuration
142
		);
143
144
		$hookRegistry->register( $GLOBALS['wgHooks'] );
145
	}
146
147
	private static function checkRequirements() {
148
149
		if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.23', 'lt' ) ) {
150
			die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticWatchlist/">Semantic Watchlist</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 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...
151
		}
152
153
		if ( !defined( 'SMW_VERSION' ) ) {
154
			die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticWatchlist/">Semantic Watchlist</a> requires the <a href="https://github.com/SemanticMediaWiki/SemanticMediaWiki/">Semantic MediaWiki</a> extension, 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...
155
		}
156
	}
157
158
	/**
159
	 * @since 1.2
160
	 *
161
	 * @return string|null
162
	 */
163
	public static function getVersion() {
164
		return SWL_VERSION;
165
	}
166
167
}
168