Completed
Push — master ( 28790c...69b7b6 )
by mw
02:44
created

SemanticBreadcrumbLinks.php (3 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 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 Semantic Breadcrumb Links 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 {
27
28
	/**
29
	 * @since 1.3
30
	 */
31
	public static function load() {
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
41
	/**
42
	 * @since 1.3
43
	 */
44
	public static function initExtension( $credits = [] ) {
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...
45
46
		// See https://phabricator.wikimedia.org/T151136
47
		define( 'SBL_VERSION', isset( $credits['version'] ) ? $credits['version'] : 'UNKNOWN' );
48
		define( 'SBL_PROP_PARENTPAGE', 'Has parent page' );
49
50
		// Register message files
51
		$GLOBALS['wgMessagesDirs']['SemanticBreadcrumbLinks'] = __DIR__ . '/i18n';
52
		$GLOBALS['wgExtensionMessagesFiles']['SemanticBreadcrumbLinksMagic'] = __DIR__ . '/i18n/SemanticBreadcrumbLinks.magic.php';
53
54
		// Register resource files
55
		$GLOBALS['wgResourceModules']['ext.semanticbreadcrumblinks.styles'] = [
56
			'styles'  => 'res/sbl.styles.css',
57
			'localBasePath' => __DIR__ ,
58
			'remoteExtPath' => 'SemanticBreadcrumbLinks',
59
			'position' => 'top',
60
			'group'    => 'ext.smw',
61
			'targets' => [
62
				'mobile',
63
				'desktop'
64
			]
65
		];
66
67
		$GLOBALS['wgResourceModules']['ext.semanticbreadcrumblinks'] = [
68
			'scripts' => 'res/sbl.tooltip.js',
69
			'localBasePath' => __DIR__ ,
70
			'remoteExtPath' => 'SemanticBreadcrumbLinks',
71
			'position' => 'top',
72
			'group'    => 'ext.smw',
73
			'dependencies'  => [
74
				'ext.semanticbreadcrumblinks.styles',
75
				'onoi.qtip'
76
			],
77
			'targets' => [
78
				'mobile',
79
				'desktop'
80
			]
81
		];
82
	}
83
84
	/**
85
	 * @since 1.3
86
	 */
87
	public static function onExtensionFunction() {
88
89
		if ( !defined( 'SMW_VERSION' ) ) {
90
			if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
91
				die( "\nThe 'Semantic Breadcrumb Links' extension requires 'Semantic MediaWiki' to be installed and enabled.\n" );
0 ignored issues
show
Coding Style Compatibility introduced by
The method onExtensionFunction() 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...
92
			} else {
93
				die( '<b>Error:</b> The <a href="https://github.com/SemanticMediaWiki/SemanticBreadcrumbLinks/">Semantic Breadcrumb Links</a> extension requires <a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki">Semantic MediaWiki</a> to be installed and enabled.<br />' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method onExtensionFunction() 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...
94
			}
95
		}
96
97
		// Default values are defined at this point to ensure
98
		// NS contants are specified prior
99
		$defaultPropertySearchPatternByNamespace = [
100
			NS_CATEGORY => [
101
				'_SUBC',
102
				'_SUBC',
103
				'_SUBC'
104
			],
105
			SMW_NS_PROPERTY => [
106
				'_SUBP',
107
				'_SUBP',
108
				'_SUBP'
109
			],
110
			NS_MAIN => [
111
				SBL_PROP_PARENTPAGE,
112
				SBL_PROP_PARENTPAGE,
113
				SBL_PROP_PARENTPAGE
114
			],
115
			NS_HELP => [
116
				SBL_PROP_PARENTPAGE,
117
				SBL_PROP_PARENTPAGE,
118
				SBL_PROP_PARENTPAGE
119
			]
120
		];
121
122
		// Cover legacy settings
123
		$deprecationNotices = [];
124
125
		if ( isset( $GLOBALS['egSBLBreadcrumbTrailStyleClass'] ) ) {
126
			$GLOBALS['sblgBreadcrumbTrailStyleClass'] = $GLOBALS['egSBLBreadcrumbTrailStyleClass'];
127
			$deprecationNotices['replacement']['egSBLBreadcrumbTrailStyleClass'] = 'sblgBreadcrumbTrailStyleClass';
128
		}
129
130
		if ( $deprecationNotices !== [] && !isset( $GLOBALS['smwgDeprecationNotices']['sbl'] ) ) {
131
			$GLOBALS['smwgDeprecationNotices']['sbl'] = [ 'replacement' => $deprecationNotices['replacement'] ];
132
		}
133
134
		$configuration = [
135
			'hideSubpageParent' => $GLOBALS['egSBLPageTitleToHideSubpageParent'],
136
			'breadcrumbTrailStyleClass' => $GLOBALS['sblgBreadcrumbTrailStyleClass'],
137
			'breadcrumbDividerStyleClass' => $GLOBALS['egSBLBreadcrumbDividerStyleClass'],
138
			'tryToFindClosestDescendant' => $GLOBALS['egSBLTryToFindClosestDescendant'],
139
			'useSubpageFinderFallback' => $GLOBALS['egSBLUseSubpageFinderFallback'],
140
			'enabledSubpageParentAnnotation' => $GLOBALS['egSBLEnabledSubpageParentAnnotation'],
141
			'disableTranslationSubpageAnnotation' => $GLOBALS['egSBLDisableTranslationSubpageAnnotation'],
142
			'wgNamespacesWithSubpages' => $GLOBALS['wgNamespacesWithSubpages'],
143
			'propertySearchPatternByNamespace' => $GLOBALS['egSBLPropertySearchPatternByNamespace'] + $defaultPropertySearchPatternByNamespace
144
		];
145
146
		$hookRegistry = new HookRegistry(
147
			ApplicationFactory::getInstance()->getStore(),
148
			new Options( $configuration )
149
		);
150
151
		$hookRegistry->register();
152
	}
153
154
	/**
155
	 * @since 1.3
156
	 *
157
	 * @return string|null
158
	 */
159
	public static function getVersion() {
160
		return SBL_VERSION;
161
	}
162
163
}
164