Completed
Push — master ( 1a6bd7...3cf9c2 )
by mw
08:54
created

SemanticScribunto::initExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
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 24 and the first side effect is on line 11.

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 SMW\Scribunto\HookRegistry;
4
5
/**
6
 * @see https://github.com/SemanticMediaWiki/SemanticScribunto/
7
 *
8
 * @defgroup SemanticScribunto Semantic Scribunto
9
 */
10
if ( !defined( 'MEDIAWIKI' ) ) {
11
	die( 'This file is part of the Semantic Scribunto extension, it is not a valid entry point.' );
12
}
13
14
if ( defined( 'SMW_SCRIBUNTO_VERSION' ) ) {
15
	// Do not initialize more than once.
16
	return 1;
17
}
18
19
SemanticScribunto::load();
20
21
/**
22
 * @codeCoverageIgnore
23
 */
24
class SemanticScribunto {
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...
25
26
	/**
27
	 * @since 1.0
28
	 *
29
	 * @note It is expected that this function is loaded before LocalSettings.php
30
	 * to ensure that settings and global functions are available by the time
31
	 * the extension is activated.
32
	 */
33
	public static function load() {
0 ignored issues
show
Coding Style introduced by
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...
34
35
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
36
			include_once __DIR__ . '/vendor/autoload.php';
37
		}
38
39
		// In case extension.json is being used, the the succeeding steps will
40
		// be handled by the ExtensionRegistry
41
		self::initExtension();
42
43
		$GLOBALS['wgExtensionFunctions'][] = function() {
44
			self::onExtensionFunction();
45
		};
46
	}
47
48
	/**
49
	 * @since 1.0
50
	 */
51
	public static function initExtension() {
0 ignored issues
show
Coding Style introduced by
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...
52
53
		define( 'SMW_SCRIBUNTO_VERSION', '1.1.0-alpha' );
54
55
		// Register extension info
56
		$GLOBALS['wgExtensionCredits']['semantic'][] = [
57
			'path'           => __FILE__,
58
			'name'           => 'Semantic Scribunto',
59
			'author'         => [
60
				'James Hong Kong',
61
				'[https://www.semantic-mediawiki.org/wiki/User:Oetterer Tobias Oetterer]',
62
			],
63
			'url'            => 'https://github.com/SemanticMediaWiki/SemanticScribunto/',
64
			'descriptionmsg' => 'smw-scribunto-desc',
65
			'version'        => SMW_SCRIBUNTO_VERSION,
66
			'license-name'   => 'GPL-2.0+',
67
		];
68
69
		// Register message files
70
		$GLOBALS['wgMessagesDirs']['SemanticScribunto'] = __DIR__ . '/i18n';
71
	}
72
73
	/**
74
	 * @since 1.0
75
	 */
76
	public static function doCheckRequirements() {
0 ignored issues
show
Coding Style introduced by
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...
77
78
		if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.27', 'lt' ) ) {
79
			die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticScribunto/">Semantic Scribunto</a> is only compatible with MediaWiki 1.26 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...
80
		}
81
82
		// Using the constant as indicator to avoid class_exists
83
		if ( !defined( 'CONTENT_MODEL_SCRIBUNTO' ) ) {
84
			die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticScribunto/">Semantic Scribunto</a> requires <a href="https://www.mediawiki.org/wiki/Extension:Scribunto">Scribunto</a>, 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...
85
		}
86
87
		if ( !defined( 'SMW_VERSION' ) ) {
88
			die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticScribunto/">Semantic Scribunto</a> requires <a href="https://github.com/SemanticMediaWiki/SemanticMediaWiki/">Semantic MediaWiki</a>, 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...
89
		}
90
	}
91
92
	/**
93
	 * @since 1.0
94
	 */
95
	public static function onExtensionFunction() {
96
97
		// Check requirements after LocalSetting.php has been processed
98
		self::doCheckRequirements();
99
100
		$hookRegistry = new HookRegistry();
101
		$hookRegistry->register();
102
	}
103
104
	/**
105
	 * @since 1.0
106
	 *
107
	 * @param string|null $dependency
108
	 *
109
	 * @return string|null
110
	 */
111
	public static function getVersion( $dependency = null ) {
112
113
		if ( $dependency === null && defined( 'SMW_SCRIBUNTO_VERSION' ) ) {
114
			return SMW_SCRIBUNTO_VERSION;
115
		}
116
117
		if ( $dependency === 'SMW' && defined( 'SMW_VERSION' ) ) {
118
			return SMW_VERSION;
119
		}
120
121
		if ( $dependency === 'Lua' && method_exists( 'Scribunto_LuaStandaloneInterpreter', 'getLuaVersion' ) ) {
122
			return Scribunto_LuaStandaloneInterpreter::getLuaVersion( [ 'luaPath' => null ] );
123
		}
124
125
		return null;
126
	}
127
128
}
129