Completed
Push — master ( f96a9e...aacabf )
by mw
08:09 queued 06:22
created

SemanticMetaTags::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
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 SMT\HookRegistry;
4
use SMT\Options;
5
use SMW\ApplicationFactory;
6
7
/**
8
 * @see https://github.com/SemanticMediaWiki/SemanticMetaTags/
9
 *
10
 * @defgroup SMT Semantic Meta Tags
11
 */
12
if ( !defined( 'MEDIAWIKI' ) ) {
13
	die( 'This file is part of the SemanticMetaTags extension, it is not a valid entry point.' );
14
}
15
16
if ( defined( 'SMT_VERSION' ) ) {
17
	// Do not initialize more than once.
18
	return 1;
19
}
20
21
SemanticMetaTags::load();
22
23
/**
24
 * @codeCoverageIgnore
25
 */
26
class SemanticMetaTags {
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.4
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
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...
36
37
		// Load DefaultSettings
38
		require_once __DIR__ . '/DefaultSettings.php';
39
40
		if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
41
			include_once __DIR__ . '/vendor/autoload.php';
42
		}
43
44
		// In case extension.json is being used, the the succeeding steps will
45
		// be handled by the ExtensionRegistry
46
		self::initExtension();
47
48
		$GLOBALS['wgExtensionFunctions'][] = function() {
49
			self::onExtensionFunction();
50
		};
51
	}
52
53
	/**
54
	 * @since 1.0
55
	 */
56
	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...
57
58
		define( 'SMT_VERSION', '1.4.0-alpha' );
59
60
		// Register extension info
61
		$GLOBALS['wgExtensionCredits']['semantic'][] = array(
62
			'path'           => __FILE__,
63
			'name'           => 'Semantic Meta Tags',
64
			'author'         => array( 'James Hong Kong' ),
65
			'url'            => 'https://github.com/SemanticMediaWiki/SemanticMetaTags/',
66
			'descriptionmsg' => 'smt-desc',
67
			'version'        => SMT_VERSION,
68
			'license-name'   => 'GPL-2.0+',
69
		);
70
71
		// Register message files
72
		$GLOBALS['wgMessagesDirs']['SemanticMetaTags'] = __DIR__ . '/i18n';
73
	}
74
75
	/**
76
	 * @since 1.4
77
	 */
78
	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...
79
80
		if ( version_compare( $GLOBALS[ 'wgVersion' ], '1.27', 'lt' ) ) {
81
			die( '<b>Error:</b> This version of <a href="https://github.com/SemanticMediaWiki/SemanticMetaTags/">SemanticMetaTags</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...
82
		}
83
	}
84
85
	/**
86
	 * @since 1.0
87
	 */
88
	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...
89
90
		// Check requirements after LocalSetting.php has been processed
91
		self::doCheckRequirements();
92
93
		$configuration = array(
94
			'metaTagsContentPropertySelector' => $GLOBALS['smtgTagsProperties'],
95
			'metaTagsStaticContentDescriptor' => $GLOBALS['smtgTagsStrings'],
96
			'metaTagsBlacklist' => $GLOBALS['smtgTagsBlacklist'],
97
			'metaTagsFallbackUseForMultipleProperties' => $GLOBALS['smtgTagsPropertyFallbackUsage'],
98
			'metaTagsMetaPropertyPrefixes' => $GLOBALS['smtgMetaPropertyPrefixes']
99
		);
100
101
		$hookRegistry = new HookRegistry(
102
			ApplicationFactory::getInstance()->getStore(),
103
			new Options( $configuration )
104
		);
105
106
		$hookRegistry->register();
107
	}
108
109
	/**
110
	 * @since 1.0
111
	 *
112
	 * @return string|null
113
	 */
114
	public static function getVersion() {
115
		return SMT_VERSION;
116
	}
117
118
}
119