Completed
Push — master ( df86c0...0c7ab9 )
by
unknown
09:12
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
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 34 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 SemanticCite extension, it is not a valid entry point.' );
14
}
15
16
if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.24', 'lt' ) ) {
17
	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.' );
18
}
19
20
if ( defined( 'SCI_VERSION' ) ) {
21
	// Do not initialize more than once.
22
	return 1;
23
}
24
25
SemanticCite::initExtension();
26
27
$GLOBALS['wgExtensionFunctions'][] = function() {
28
	SemanticCite::onExtensionFunction();
29
};
30
31
/**
32
 * @codeCoverageIgnore
33
 */
34
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...
35
36
	/**
37
	 * @since 1.1
38
	 */
39
	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...
40
41
		// Load DefaultSettings
42
		require_once __DIR__ . '/DefaultSettings.php';
43
44
		define( 'SCI_VERSION', '1.1.1' );
45
46
		// Register the extension
47
		$GLOBALS['wgExtensionCredits']['semantic'][ ] = array(
48
			'path'           => __FILE__,
49
			'name'           => 'Semantic Cite',
50
			'author'         => array( 'James Hong Kong' ),
51
			'url'            => 'https://github.com/SemanticMediaWiki/SemanticCite/',
52
			'descriptionmsg' => 'sci-desc',
53
			'version'        => SCI_VERSION,
54
			'license-name'   => 'GPL-2.0+',
55
		);
56
57
		// Register message files
58
		$GLOBALS['wgMessagesDirs']['SemanticCite'] = __DIR__ . '/i18n';
59
		$GLOBALS['wgExtensionMessagesFiles']['SemanticCiteMagic'] = __DIR__ . '/i18n/SemanticCite.magic.php';
60
		$GLOBALS['wgExtensionMessagesFiles']['SemanticCiteAlias'] = __DIR__ . '/i18n/SemanticCite.alias.php';
61
62
		$GLOBALS['wgSpecialPages']['FindCitableMetadata'] = '\SCI\Specials\SpecialFindCitableMetadata';
63
64
		// Restrict access to the meta search for registered users only
65
		$GLOBALS['wgAvailableRights'][] = 'sci-metadatasearch';
66
		$GLOBALS['wgGroupPermissions']['user']['sci-metadatasearch'] = true;
67
68
		// Register resource files
69
		$GLOBALS['wgResourceModules']['ext.scite.styles'] = array(
70
			'styles'  => 'res/scite.styles.css',
71
			'localBasePath' => __DIR__ ,
72
			'remoteExtPath' => 'SemanticCite',
73
			'position' => 'top',
74
			'group'    => 'ext.smw',
75
			'targets' => array(
76
				'mobile',
77
				'desktop'
78
			)
79
		);
80
81
		$GLOBALS['wgResourceModules']['ext.scite.metadata'] = array(
82
			'scripts' => array(
83
				'res/scite.text.selector.js',
84
				'res/scite.page.creator.js'
85
			),
86
			'localBasePath' => __DIR__ ,
87
			'remoteExtPath' => 'SemanticCite',
88
			'position' => 'top',
89
			'group'    => 'ext.smw',
90
			'dependencies'  => array(
91
				'ext.scite.styles',
92
				'mediawiki.api'
93
			),
94
			'targets' => array(
95
				'mobile',
96
				'desktop'
97
			)
98
		);
99
100
		$GLOBALS['wgResourceModules']['ext.scite.tooltip'] = array(
101
			'scripts' => array(
102
				'res/scite.tooltip.js'
103
			),
104
			'localBasePath' => __DIR__ ,
105
			'remoteExtPath' => 'SemanticCite',
106
			'dependencies'  => array(
107
				'onoi.qtip',
108
				'onoi.blobstore',
109
				'onoi.util',
110
				'ext.scite.styles',
111
				'mediawiki.api.parse'
112
			),
113
			'messages' => array(
114
				'sci-tooltip-citation-lookup-failure',
115
				'sci-tooltip-citation-lookup-failure-multiple'
116
			),
117
			'targets' => array(
118
				'mobile',
119
				'desktop'
120
			)
121
		);
122
	}
123
124
	/**
125
	 * @since 1.1
126
	 */
127
	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...
128
129
		// Require a global because MW's Special page is missing an interface
130
		// to inject dependencies
131
		$GLOBALS['scigCachePrefix'] = $GLOBALS['wgCachePrefix'] === false ? wfWikiID() : $GLOBALS['wgCachePrefix'];
132
133
		$configuration = array(
134
			'numberOfReferenceListColumns'       => $GLOBALS['scigNumberOfReferenceListColumns'],
135
			'browseLinkToCitationResource'       => $GLOBALS['scigBrowseLinkToCitationResource'],
136
			'showTooltipForCitationReference'    => $GLOBALS['scigShowTooltipForCitationReference'],
137
			'tooltipRequestCacheTTL'             => $GLOBALS['scigTooltipRequestCacheTTLInSeconds'],
138
			'citationReferenceCaptionFormat'     => $GLOBALS['scigCitationReferenceCaptionFormat'],
139
			'referenceListType'                  => $GLOBALS['scigReferenceListType'],
140
			'strictParserValidationEnabled'      => $GLOBALS['scigStrictParserValidationEnabled'],
141
			'cachePrefix'                        => $GLOBALS['scigCachePrefix'],
142
			'enabledCitationTextChangeUpdateJob' => $GLOBALS['scigEnabledCitationTextChangeUpdateJob']
143
		);
144
145
		$applicationFactory = ApplicationFactory::getInstance();
146
147
		$hookRegistry = new HookRegistry(
148
			$applicationFactory->getStore(),
149
			$applicationFactory->newCacheFactory()->newMediaWikiCompositeCache( $GLOBALS['scigReferenceListCacheType'] ),
150
			new Options( $configuration )
151
		);
152
153
		$hookRegistry->register();
154
	}
155
156
	/**
157
	 * @since 1.1
158
	 *
159
	 * @return string|null
160
	 */
161
	public static function getVersion() {
162
		return SCI_VERSION;
163
	}
164
165
}
166