Completed
Push — master ( 5013dd...decfe1 )
by mw
03:55
created

SemanticCite::getVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
use SCI\HookRegistry;
4
use SCI\Options;
5
use SMW\ApplicationFactory;
6
7
/**
8
 * @see https://github.com/SemanticMediaWiki/SemanticCite/
9
 *
10
 * @defgroup SCI Semantic Cite
11
 */
12
SemanticCite::load();
13
14
/**
15
 * @codeCoverageIgnore
16
 */
17
class SemanticCite {
18
19
	/**
20
	 * @since 1.3
21
	 *
22
	 * @note It is expected that this function is loaded before LocalSettings.php
23
	 * to ensure that settings and global functions are available by the time
24
	 * the extension is activated.
25
	 */
26
	public static function load() {
27
28
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
29
			include_once __DIR__ . '/vendor/autoload.php';
30
		}
31
32
		// Load DefaultSettings
33
		require_once __DIR__ . '/DefaultSettings.php';
34
	}
35
36
	/**
37
	 * @since 1.1
38
	 */
39
	public static function initExtension( $credits = [] ) {
40
41
		// See https://phabricator.wikimedia.org/T151136
42
		define( 'SCI_VERSION', isset( $credits['version'] ) ? $credits['version'] : 'UNKNOWN' );
43
44
		// Extend the upgrade key provided by SMW to ensure that an DB
45
		// schema is updated accordingly before using the extension
46
		if ( isset( $GLOBALS['smwgUpgradeKey'] ) ) {
47
		//	$GLOBALS['smwgUpgradeKey'] .= ':scite:2018-09';
48
		}
49
50
		// Register message files
51
		$GLOBALS['wgMessagesDirs']['SemanticCite'] = __DIR__ . '/i18n';
52
		$GLOBALS['wgExtensionMessagesFiles']['SemanticCiteMagic'] = __DIR__ . '/i18n/SemanticCite.magic.php';
53
		$GLOBALS['wgExtensionMessagesFiles']['SemanticCiteAlias'] = __DIR__ . '/i18n/SemanticCite.alias.php';
54
55
		$GLOBALS['wgSpecialPages']['FindCitableMetadata'] = '\SCI\Specials\SpecialFindCitableMetadata';
56
57
		// Restrict access to the meta search for registered users only
58
		$GLOBALS['wgAvailableRights'][] = 'sci-metadatasearch';
59
		$GLOBALS['wgGroupPermissions']['user']['sci-metadatasearch'] = true;
60
61
		// Register resource files
62
		$GLOBALS['wgResourceModules']['ext.scite.styles'] = [
63
			'styles'  => 'res/scite.styles.css',
64
			'localBasePath' => __DIR__ ,
65
			'remoteExtPath' => 'SemanticCite',
66
			'position' => 'top',
67
			'group'    => 'ext.smw',
68
			'targets' => [
69
				'mobile',
70
				'desktop'
71
			]
72
		];
73
74
		$GLOBALS['wgResourceModules']['ext.scite.metadata'] = [
75
			'scripts' => [
76
				'res/scite.text.selector.js',
77
				'res/scite.page.creator.js'
78
			],
79
			'localBasePath' => __DIR__ ,
80
			'remoteExtPath' => 'SemanticCite',
81
			'position' => 'top',
82
			'group'    => 'ext.smw',
83
			'dependencies'  => [
84
				'ext.scite.styles',
85
				'mediawiki.api'
86
			],
87
			'targets' => [
88
				'mobile',
89
				'desktop'
90
			]
91
		];
92
93
		// #71
94
		if ( version_compare( $GLOBALS['wgVersion'], '1.32', '<' ) ) {
95
			$dependencies = [
96
				'onoi.qtip',
97
				'onoi.blobstore',
98
				'onoi.util',
99
				'ext.scite.styles',
100
				'mediawiki.api.parse'
101
			];
102
		} else {
103
			$dependencies = [
104
				'onoi.qtip',
105
				'onoi.blobstore',
106
				'onoi.util',
107
				'ext.scite.styles',
108
				'mediawiki.api'
109
			];
110
		}
111
112
		$GLOBALS['wgResourceModules']['ext.scite.tooltip'] = [
113
			'scripts' => [
114
				'res/scite.tooltip.js'
115
			],
116
			'localBasePath' => __DIR__ ,
117
			'remoteExtPath' => 'SemanticCite',
118
			'dependencies'  => $dependencies,
119
			'messages' => [
120
				'sci-tooltip-citation-lookup-failure',
121
				'sci-tooltip-citation-lookup-failure-multiple'
122
			],
123
			'targets' => [
124
				'mobile',
125
				'desktop'
126
			]
127
		];
128
129
		// Register hooks that require to be listed as soon as possible and preferable
130
		// before the execution of onExtensionFunction
131
		HookRegistry::initExtension( $GLOBALS );
132
	}
133
134
	/**
135
	 * @since 1.1
136
	 */
137
	public static function onExtensionFunction() {
138
139
		if ( !defined( 'SMW_VERSION' ) ) {
140
			if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
141
				die( "\nThe 'Semantic Cite' extension requires 'Semantic MediaWiki' to be installed and enabled.\n" );
142
			} else {
143
				die( '<b>Error:</b> The <a href="https://github.com/SemanticMediaWiki/SemanticCite/">Semantic Cite</a> extension requires <a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki">Semantic MediaWiki</a> to be installed and enabled.<br />' );
144
			}
145
		}
146
147
		// Require a global because MW's Special page is missing an interface
148
		// to inject dependencies
149
		$GLOBALS['scigCachePrefix'] = $GLOBALS['wgCachePrefix'] === false ? wfWikiID() : $GLOBALS['wgCachePrefix'];
150
151
		$configuration = [
152
			'numberOfReferenceListColumns'       => $GLOBALS['scigNumberOfReferenceListColumns'],
153
			'browseLinkToCitationResource'       => $GLOBALS['scigBrowseLinkToCitationResource'],
154
			'showTooltipForCitationReference'    => $GLOBALS['scigShowTooltipForCitationReference'],
155
			'tooltipRequestCacheTTL'             => $GLOBALS['scigTooltipRequestCacheTTLInSeconds'],
156
			'citationReferenceCaptionFormat'     => $GLOBALS['scigCitationReferenceCaptionFormat'],
157
			'referenceListType'                  => $GLOBALS['scigReferenceListType'],
158
			'enabledStrictParserValidation'      => $GLOBALS['scigEnabledStrictParserValidation'],
159
			'cachePrefix'                        => $GLOBALS['scigCachePrefix'],
160
			'enabledCitationTextChangeUpdateJob' => $GLOBALS['scigEnabledCitationTextChangeUpdateJob'],
161
			'responsiveMonoColumnCharacterBoundLength' => $GLOBALS['scigResponsiveMonoColumnCharacterBoundLength']
162
		];
163
164
		$applicationFactory = ApplicationFactory::getInstance();
165
166
		$hookRegistry = new HookRegistry(
167
			$applicationFactory->getStore(),
168
			$applicationFactory->newCacheFactory()->newMediaWikiCompositeCache( $GLOBALS['scigReferenceListCacheType'] ),
169
			new Options( $configuration )
170
		);
171
172
		$hookRegistry->register();
173
	}
174
175
}
176