Completed
Pull Request — master (#38)
by mw
04:34
created

SemanticCite.php (4 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
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 Citation
11
 */
12
if ( !defined( 'MEDIAWIKI' ) ) {
13
	die( 'This file is part of the SemanticCite extension, it is not a valid entry point.' );
14
}
15
16
if ( defined( 'SCI_VERSION' ) ) {
17
	// Do not initialize more than once.
18
	return 1;
19
}
20
21
SemanticCite::load();
22
23
/**
24
 * @codeCoverageIgnore
25
 */
26
class SemanticCite {
27
28
	/**
29
	 * @since 1.3
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() {
0 ignored issues
show
load uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
36
37
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
38
			include_once __DIR__ . '/vendor/autoload.php';
39
		}
40
41
		// Load DefaultSettings
42
		require_once __DIR__ . '/DefaultSettings.php';
43
44
		// In case extension.json is being used, the the succeeding steps are
45
		// expected to be handled by the ExtensionRegistry
46
		self::initExtension();
47
48
		$GLOBALS['wgExtensionFunctions'][] = function() {
49
			self::onExtensionFunction();
50
		};
51
	}
52
53
	/**
54
	 * @since 1.1
55
	 */
56
	public static function initExtension() {
57
58
		define( 'SCI_VERSION', '1.3.0-alpha' );
59
60
		// Register the extension
61
		$GLOBALS['wgExtensionCredits']['semantic'][ ] = array(
62
			'path'           => __FILE__,
63
			'name'           => 'Semantic Cite',
64
			'author'         => array( 'James Hong Kong' ),
65
			'url'            => 'https://github.com/SemanticMediaWiki/SemanticCite/',
66
			'descriptionmsg' => 'sci-desc',
67
			'version'        => SCI_VERSION,
68
			'license-name'   => 'GPL-2.0+',
69
		);
70
71
		// Register message files
72
		$GLOBALS['wgMessagesDirs']['SemanticCite'] = __DIR__ . '/i18n';
73
		$GLOBALS['wgExtensionMessagesFiles']['SemanticCiteMagic'] = __DIR__ . '/i18n/SemanticCite.magic.php';
74
		$GLOBALS['wgExtensionMessagesFiles']['SemanticCiteAlias'] = __DIR__ . '/i18n/SemanticCite.alias.php';
75
76
		$GLOBALS['wgSpecialPages']['FindCitableMetadata'] = '\SCI\Specials\SpecialFindCitableMetadata';
77
78
		// Restrict access to the meta search for registered users only
79
		$GLOBALS['wgAvailableRights'][] = 'sci-metadatasearch';
80
		$GLOBALS['wgGroupPermissions']['user']['sci-metadatasearch'] = true;
81
82
		// Register resource files
83
		$GLOBALS['wgResourceModules']['ext.scite.styles'] = array(
84
			'styles'  => 'res/scite.styles.css',
85
			'localBasePath' => __DIR__ ,
86
			'remoteExtPath' => 'SemanticCite',
87
			'position' => 'top',
88
			'group'    => 'ext.smw',
89
			'targets' => array(
90
				'mobile',
91
				'desktop'
92
			)
93
		);
94
95
		$GLOBALS['wgResourceModules']['ext.scite.metadata'] = array(
96
			'scripts' => array(
97
				'res/scite.text.selector.js',
98
				'res/scite.page.creator.js'
99
			),
100
			'localBasePath' => __DIR__ ,
101
			'remoteExtPath' => 'SemanticCite',
102
			'position' => 'top',
103
			'group'    => 'ext.smw',
104
			'dependencies'  => array(
105
				'ext.scite.styles',
106
				'mediawiki.api'
107
			),
108
			'targets' => array(
109
				'mobile',
110
				'desktop'
111
			)
112
		);
113
114
		$GLOBALS['wgResourceModules']['ext.scite.tooltip'] = array(
115
			'scripts' => array(
116
				'res/scite.tooltip.js'
117
			),
118
			'localBasePath' => __DIR__ ,
119
			'remoteExtPath' => 'SemanticCite',
120
			'dependencies'  => array(
121
				'onoi.qtip',
122
				'onoi.blobstore',
123
				'onoi.util',
124
				'ext.scite.styles',
125
				'mediawiki.api.parse'
126
			),
127
			'messages' => array(
128
				'sci-tooltip-citation-lookup-failure',
129
				'sci-tooltip-citation-lookup-failure-multiple'
130
			),
131
			'targets' => array(
132
				'mobile',
133
				'desktop'
134
			)
135
		);
136
	}
137
138
	/**
139
	 * @since 1.3
140
	 */
141
	public static function doCheckRequirements() {
0 ignored issues
show
doCheckRequirements uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
142
143
		if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.24', 'lt' ) ) {
144
			die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticCite/">SemanticCite</a> is only compatible with MediaWiki 1.24 or above. You need to upgrade MediaWiki first.' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method doCheckRequirements() 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...
145
		}
146
147
		if ( !defined( 'SMW_VERSION' ) ) {
148
			die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticCite/">Semantic Cite</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 doCheckRequirements() 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...
149
		}
150
	}
151
152
	/**
153
	 * @since 1.1
154
	 */
155
	public static function onExtensionFunction() {
156
157
		// Check requirements after LocalSetting.php has been processed
158
		self::doCheckRequirements();
159
160
		// Require a global because MW's Special page is missing an interface
161
		// to inject dependencies
162
		$GLOBALS['scigCachePrefix'] = $GLOBALS['wgCachePrefix'] === false ? wfWikiID() : $GLOBALS['wgCachePrefix'];
163
164
		$configuration = array(
165
			'numberOfReferenceListColumns'       => $GLOBALS['scigNumberOfReferenceListColumns'],
166
			'browseLinkToCitationResource'       => $GLOBALS['scigBrowseLinkToCitationResource'],
167
			'showTooltipForCitationReference'    => $GLOBALS['scigShowTooltipForCitationReference'],
168
			'tooltipRequestCacheTTL'             => $GLOBALS['scigTooltipRequestCacheTTLInSeconds'],
169
			'citationReferenceCaptionFormat'     => $GLOBALS['scigCitationReferenceCaptionFormat'],
170
			'referenceListType'                  => $GLOBALS['scigReferenceListType'],
171
			'enabledStrictParserValidation'      => $GLOBALS['scigEnabledStrictParserValidation'],
172
			'cachePrefix'                        => $GLOBALS['scigCachePrefix'],
173
			'enabledCitationTextChangeUpdateJob' => $GLOBALS['scigEnabledCitationTextChangeUpdateJob'],
174
			'responsiveMonoColumnCharacterBoundLength' => $GLOBALS['scigResponsiveMonoColumnCharacterBoundLength']
175
		);
176
177
		$applicationFactory = ApplicationFactory::getInstance();
178
179
		$hookRegistry = new HookRegistry(
180
			$applicationFactory->getStore(),
181
			$applicationFactory->newCacheFactory()->newMediaWikiCompositeCache( $GLOBALS['scigReferenceListCacheType'] ),
182
			new Options( $configuration )
183
		);
184
185
		$hookRegistry->register();
186
	}
187
188
	/**
189
	 * @since 1.1
190
	 *
191
	 * @return string|null
192
	 */
193
	public static function getVersion() {
194
		return SCI_VERSION;
195
	}
196
197
}
198