Completed
Pull Request — master (#64)
by Karsten
34:07 queued 14:06
created

SemanticCite.php (9 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 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 Semantic Cite 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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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() {
0 ignored issues
show
initExtension 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...
57
58
		define( 'SCI_VERSION', '1.4.0-alpha' );
59
60
		// Register the extension
61
		$GLOBALS['wgExtensionCredits']['semantic'][ ] = [
62
			'path'           => __FILE__,
63
			'name'           => 'Semantic Cite',
64
			'author'         => [ 'James Hong Kong' ],
65
			'url'            => 'https://github.com/SemanticMediaWiki/SemanticCite/',
66
			'descriptionmsg' => 'sci-desc',
67
			'version'        => SCI_VERSION,
68
			'license-name'   => 'GPL-2.0-or-later'
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'] = [
84
			'styles'  => 'res/scite.styles.css',
85
			'localBasePath' => __DIR__ ,
86
			'remoteExtPath' => 'SemanticCite',
87
			'position' => 'top',
88
			'group'    => 'ext.smw',
89
			'targets' => [
90
				'mobile',
91
				'desktop'
92
			]
93
		];
94
95
		$GLOBALS['wgResourceModules']['ext.scite.metadata'] = [
96
			'scripts' => [
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'  => [
105
				'ext.scite.styles',
106
				'mediawiki.api'
107
			],
108
			'targets' => [
109
				'mobile',
110
				'desktop'
111
			]
112
		];
113
114
		$GLOBALS['wgResourceModules']['ext.scite.tooltip'] = [
115
			'scripts' => [
116
				'res/scite.tooltip.js'
117
			],
118
			'localBasePath' => __DIR__ ,
119
			'remoteExtPath' => 'SemanticCite',
120
			'dependencies'  => [
121
				'onoi.qtip',
122
				'onoi.blobstore',
123
				'onoi.util',
124
				'ext.scite.styles',
125
				'mediawiki.api.parse'
126
			],
127
			'messages' => [
128
				'sci-tooltip-citation-lookup-failure',
129
				'sci-tooltip-citation-lookup-failure-multiple'
130
			],
131
			'targets' => [
132
				'mobile',
133
				'desktop'
134
			]
135
		];
136
137
		self::onBeforeExtensionFunction();
138
	}
139
140
	/**
141
	 * Register hooks that require to be listed as soon as possible and preferable
142
	 * before the execution of onExtensionFunction
143
	 *
144
	 * @since 1.3
145
	 */
146
	public static function onBeforeExtensionFunction() {
0 ignored issues
show
onBeforeExtensionFunction 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...
147
		$GLOBALS['wgHooks']['SMW::Config::BeforeCompletion'][] = '\SCI\HookRegistry::onBeforeConfigCompletion';
148
	}
149
150
	/**
151
	 * @since 1.3
152
	 */
153
	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...
154
155
		if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.27', 'lt' ) ) {
156
			die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticCite/">Semantic Cite</a> is only compatible with MediaWiki 1.27 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...
157
		}
158
159
		if ( !defined( 'SMW_VERSION' ) ) {
160
			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...
161
		}
162
	}
163
164
	/**
165
	 * @since 1.1
166
	 */
167
	public static function onExtensionFunction() {
0 ignored issues
show
onExtensionFunction 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...
168
169
		// Check requirements after LocalSetting.php has been processed
170
		self::doCheckRequirements();
171
172
		// Require a global because MW's Special page is missing an interface
173
		// to inject dependencies
174
		$GLOBALS['scigCachePrefix'] = $GLOBALS['wgCachePrefix'] === false ? wfWikiID() : $GLOBALS['wgCachePrefix'];
175
176
		$configuration = [
177
			'numberOfReferenceListColumns'       => $GLOBALS['scigNumberOfReferenceListColumns'],
178
			'browseLinkToCitationResource'       => $GLOBALS['scigBrowseLinkToCitationResource'],
179
			'showTooltipForCitationReference'    => $GLOBALS['scigShowTooltipForCitationReference'],
180
			'tooltipRequestCacheTTL'             => $GLOBALS['scigTooltipRequestCacheTTLInSeconds'],
181
			'citationReferenceCaptionFormat'     => $GLOBALS['scigCitationReferenceCaptionFormat'],
182
			'referenceListType'                  => $GLOBALS['scigReferenceListType'],
183
			'enabledStrictParserValidation'      => $GLOBALS['scigEnabledStrictParserValidation'],
184
			'cachePrefix'                        => $GLOBALS['scigCachePrefix'],
185
			'enabledCitationTextChangeUpdateJob' => $GLOBALS['scigEnabledCitationTextChangeUpdateJob'],
186
			'responsiveMonoColumnCharacterBoundLength' => $GLOBALS['scigResponsiveMonoColumnCharacterBoundLength']
187
		];
188
189
		$applicationFactory = ApplicationFactory::getInstance();
190
191
		$hookRegistry = new HookRegistry(
192
			$applicationFactory->getStore(),
193
			$applicationFactory->newCacheFactory()->newMediaWikiCompositeCache( $GLOBALS['scigReferenceListCacheType'] ),
194
			new Options( $configuration )
195
		);
196
197
		$hookRegistry->register();
198
	}
199
200
	/**
201
	 * @since 1.1
202
	 *
203
	 * @return string|null
204
	 */
205
	public static function getVersion() {
206
		return SCI_VERSION;
207
	}
208
209
}
210