Completed
Pull Request — master (#83)
by Jeroen De
05:44 queued 03:24
created

SemanticWatchlist::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
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 20
rs 9.4285
cc 1
eloc 5
nc 1
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
43
		define( 'SWL_VERSION', '1.2.0-alpha' );
44
45
		// Register the extension
46
		$GLOBALS['wgExtensionCredits']['semantic'][] = array(
47
			'path' => __FILE__,
48
			'name' => 'Semantic Watchlist',
49
			'version' => SWL_VERSION,
50
			'author' => array(
51
				'[https://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw] for [http://www.wikiworks.com/ WikiWorks]',
52
				'...'
53
			),
54
			'url' => 'https://www.mediawiki.org/wiki/Extension:Semantic_Watchlist',
55
			'descriptionmsg' => 'semanticwatchlist-desc',
56
			'license-name'   => 'GPL-3.0+'
57
		);
58
59
		$GLOBALS['egSwlSqlDatabaseSchemaPath'] = __DIR__ . '/../src/swl-table-schema.sql';
60
61
		// Register message files
62
		$GLOBALS['wgMessagesDirs']['SemanticWatchlist'] = __DIR__ . '/../i18n';
63
		$GLOBALS['wgExtensionMessagesFiles']['SemanticWatchlistAlias'] = __DIR__ . '/../SemanticWatchlist.i18n.alias.php';
64
65
		$GLOBALS['egSWLScriptPath'] = $GLOBALS['wgExtensionAssetsPath'] === false ? $GLOBALS['wgScriptPath'] . '/extensions/SemanticWatchlist' : $GLOBALS['wgExtensionAssetsPath'] . '/SemanticWatchlist';
66
67
		// wgSpecialPages
68
		$GLOBALS['wgSpecialPages']['SemanticWatchlist'] = 'SpecialSemanticWatchlist';
69
		$GLOBALS['wgSpecialPageGroups']['SemanticWatchlist'] = 'changes';
70
71
		$GLOBALS['wgSpecialPages']['WatchlistConditions'] = 'SpecialWatchlistConditions';
72
		$GLOBALS['wgSpecialPageGroups']['WatchlistConditions'] = 'changes';
73
74
		// wgAPIModules
75
		$GLOBALS['wgAPIModules']['addswlgroup'] = 'ApiAddWatchlistGroup';
76
		$GLOBALS['wgAPIModules']['deleteswlgroup'] = 'ApiDeleteWatchlistGroup';
77
		$GLOBALS['wgAPIModules']['editswlgroup'] = 'ApiEditWatchlistGroup';
78
		$GLOBALS['wgAPIListModules']['semanticwatchlist'] = 'ApiQuerySemanticWatchlist';
79
80
		// wgAvailableRights
81
		$GLOBALS['wgAvailableRights'][] = 'semanticwatch';
82
		$GLOBALS['wgAvailableRights'][] = 'semanticwatchgroups';
83
84
		$moduleTemplate = array(
85
			'localBasePath' => __DIR__ . '/..',
86
			'remoteBasePath' => $GLOBALS['egSWLScriptPath']
87
		);
88
89
		$GLOBALS['wgResourceModules']['ext.swl.watchlist'] = $moduleTemplate + array(
90
				'styles' => array( 'specials/ext.swl.watchlist.css' ),
91
				'scripts' => array(),
92
				'dependencies' => array(),
93
				'messages' => array()
94
			);
95
96
		$GLOBALS['wgResourceModules']['ext.swl.watchlistconditions'] = $moduleTemplate + array(
97
				'styles' => array( 'specials/ext.swl.watchlistconditions.css' ),
98
				'scripts' => array(
99
					'specials/jquery.watchlistcondition.js',
100
					'specials/ext.swl.watchlistconditions.js'
101
				),
102
				'dependencies' => array(),
103
				'messages' => array(
104
					'swl-group-name',
105
					'swl-group-legend',
106
					'swl-group-properties',
107
					'swl-properties-list',
108
					'swl-group-remove-property',
109
					'swl-group-add-property',
110
					'swl-group-page-selection',
111
					'swl-group-save',
112
					'swl-group-saved',
113
					'swl-group-saving',
114
					'swl-group-remove',
115
					'swl-group-category',
116
					'swl-group-namespace',
117
					'swl-group-concept',
118
					'swl-group-confirm-remove',
119
					'swl-custom-legend',
120
					'swl-custom-remove-property',
121
					'swl-custom-text-add',
122
					'swl-custom-input',
123
				)
124
			);
125
	}
126
127
	/**
128
	 * @since 1.2
129
	 */
130
	public static function checkRequirements() {
131
132
		if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.23', 'lt' ) ) {
133
			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...
134
		}
135
136
		if ( !defined( 'SMW_VERSION' ) ) {
137
			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...
138
		}
139
	}
140
141
	/**
142
	 * @since 1.2
143
	 */
144
	public static function onExtensionFunction() {
145
146
		// Check requirements after LocalSetting.php has been processed, thid has
147
		// be done here to ensure SMW is loaded in case
148
		// wfLoadExtension( 'SemanticMediaWiki' ) is used
149
		self::checkRequirements();
150
151
		$configuration = array(
152
			'egSWLEnableTopLink'         => $GLOBALS['egSWLEnableTopLink'],
153
			'egSWLEnableEmailNotify'     => $GLOBALS['egSWLEnableEmailNotify'],
154
			'egSwlSqlDatabaseSchemaPath' => $GLOBALS['egSwlSqlDatabaseSchemaPath']
155
		);
156
157
		$hookRegistry = new HookRegistry(
158
			$configuration
159
		);
160
161
		$hookRegistry->register( $GLOBALS['wgHooks'] );
162
	}
163
164
	/**
165
	 * @since 1.2
166
	 *
167
	 * @return string|null
168
	 */
169
	public static function getVersion() {
170
		return SWL_VERSION;
171
	}
172
173
}
174