Completed
Push — master ( ad9ce2...b11c6d )
by mw
02:04
created

SemanticBreadcrumbLinks::checkRequirements()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
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 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 SBL\HookRegistry;
4
use SBL\Options;
5
use SMW\ApplicationFactory;
6
7
/**
8
 * @see https://github.com/SemanticMediaWiki/SemanticBreadcrumbLinks/
9
 *
10
 * @defgroup SBL Semantic Breadcrumb Links
11
 */
12
if ( !defined( 'MEDIAWIKI' ) ) {
13
	die( 'This file is part of the SemanticBreadcrumbLinks extension, it is not a valid entry point.' );
14
}
15
16
if ( defined( 'SBL_VERSION' ) ) {
17
	// Do not initialize more than once.
18
	return 1;
19
}
20
21
SemanticBreadcrumbLinks::load();
22
23
/**
24
 * @codeCoverageIgnore
25
 */
26
class SemanticBreadcrumbLinks {
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
	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...
32
33
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
34
			include_once __DIR__ . '/vendor/autoload.php';
35
		}
36
37
		// Load DefaultSettings
38
		require_once __DIR__ . '/DefaultSettings.php';
39
40
		// In case extension.json is being used, the the succeeding steps will
41
		// be handled by the ExtensionRegistry
42
		self::initExtension();
43
44
		$GLOBALS['wgExtensionFunctions'][] = function() {
45
			self::onExtensionFunction();
46
		};
47
	}
48
49
	/**
50
	 * @since 1.3
51
	 */
52
	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...
53
54
		define( 'SBL_VERSION', '1.3.1-alpha' );
55
		define( 'SBL_PROP_PARENTPAGE', 'Has parent page' );
56
57
		// Register the extension
58
		$GLOBALS['wgExtensionCredits']['semantic'][ ] = array(
59
			'path'           => __FILE__,
60
			'name'           => 'Semantic Breadcrumb Links',
61
			'author'         => array( 'James Hong Kong' ),
62
			'url'            => 'https://github.com/SemanticMediaWiki/SemanticBreadcrumbLinks/',
63
			'descriptionmsg' => 'sbl-desc',
64
			'version'        => SBL_VERSION,
65
			'license-name'   => 'GPL-2.0+',
66
		);
67
68
		// Register message files
69
		$GLOBALS['wgMessagesDirs']['SemanticBreadcrumbLinks'] = __DIR__ . '/i18n';
70
71
		// Register resource files
72
		$GLOBALS['wgResourceModules']['ext.semanticbreadcrumblinks.styles'] = array(
73
			'styles'  => 'res/sbl.styles.css',
74
			'localBasePath' => __DIR__ ,
75
			'remoteExtPath' => 'SemanticBreadcrumbLinks',
76
			'position' => 'top',
77
			'group'    => 'ext.smw',
78
			'targets' => array(
79
				'mobile',
80
				'desktop'
81
			)
82
		);
83
84
		$GLOBALS['wgResourceModules']['ext.semanticbreadcrumblinks'] = array(
85
			'scripts' => 'res/sbl.tooltip.js',
86
			'localBasePath' => __DIR__ ,
87
			'remoteExtPath' => 'SemanticBreadcrumbLinks',
88
			'position' => 'top',
89
			'group'    => 'ext.smw',
90
			'dependencies'  => array(
91
				'ext.semanticbreadcrumblinks.styles',
92
				'onoi.qtip'
93
			),
94
			'targets' => array(
95
				'mobile',
96
				'desktop'
97
			)
98
		);
99
	}
100
101
	/**
102
	 * @since 1.3
103
	 */
104
	public static function checkRequirements() {
0 ignored issues
show
Coding Style introduced by
checkRequirements 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...
105
106
		if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.23', 'lt' ) ) {
107
			die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticBreadcrumbLinks/">Semantic Breadcrumb Links</a> is only compatible with MediaWiki 1.23 or above. You need to upgrade MediaWiki first.' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkRequirements() 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...
108
		}
109
110
		if ( !defined( 'SMW_VERSION' ) ) {
111
			die( '<b>Error:</b> <a href="https://github.com/SemanticMediaWiki/SemanticBreadcrumbLinks/">Semantic Breadcrumb Links</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 checkRequirements() 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...
112
		}
113
	}
114
115
	/**
116
	 * @since 1.3
117
	 */
118
	public static function onExtensionFunction() {
0 ignored issues
show
Coding Style introduced by
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...
119
120
		// Check requirements after LocalSetting.php has been processed
121
		self::checkRequirements();
122
123
		// Default values are defined at this point to ensure
124
		// NS contants are specified prior
125
		$defaultPropertySearchPatternByNamespace = array(
126
			NS_CATEGORY => array(
127
				'_SUBC',
128
				'_SUBC',
129
				'_SUBC'
130
			),
131
			SMW_NS_PROPERTY => array(
132
				'_SUBP',
133
				'_SUBP',
134
				'_SUBP'
135
			),
136
			NS_MAIN => array(
137
				SBL_PROP_PARENTPAGE,
138
				SBL_PROP_PARENTPAGE,
139
				SBL_PROP_PARENTPAGE
140
			),
141
			NS_HELP => array(
142
				SBL_PROP_PARENTPAGE,
143
				SBL_PROP_PARENTPAGE,
144
				SBL_PROP_PARENTPAGE
145
			)
146
		);
147
148
		$configuration = array(
149
			'hideSubpageParent' => $GLOBALS['egSBLPageTitleToHideSubpageParent'],
150
			'breadcrumbTrailStyleClass' => $GLOBALS['egSBLBreadcrumbTrailStyleClass'],
151
			'breadcrumbDividerStyleClass' => $GLOBALS['egSBLBreadcrumbDividerStyleClass'],
152
			'tryToFindClosestDescendant' => $GLOBALS['egSBLTryToFindClosestDescendant'],
153
			'useSubpageFinderFallback' => $GLOBALS['egSBLUseSubpageFinderFallback'],
154
			'enabledSubpageParentAnnotation' => $GLOBALS['egSBLEnabledSubpageParentAnnotation'],
155
			'wgNamespacesWithSubpages' => $GLOBALS['wgNamespacesWithSubpages'],
156
			'propertySearchPatternByNamespace' => $GLOBALS['egSBLPropertySearchPatternByNamespace'] + $defaultPropertySearchPatternByNamespace
157
		);
158
159
		$hookRegistry = new HookRegistry(
160
			ApplicationFactory::getInstance()->getStore(),
161
			new Options( $configuration )
162
		);
163
164
		$hookRegistry->register();
165
	}
166
167
	/**
168
	 * @since 1.3
169
	 *
170
	 * @return string|null
171
	 */
172
	public static function getVersion() {
173
		return SBL_VERSION;
174
	}
175
176
}
177