Completed
Push — master ( 57be7d...d19112 )
by mw
79:35 queued 68:10
created

NamespaceManager::init()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 0
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SMW;
4
5
/**
6
 * @license GNU GPL v2+
7
 * @since 1.9
8
 *
9
 * @author mwjames
10
 * @author others
11
 */
12
class NamespaceManager {
13
14
	/**
15
	 * @var array
16
	 */
17
	protected $globalVars;
18
19
	/**
20
	 * @since 1.9
21
	 *
22
	 * @param array &$globalVars
23
	 */
24 6
	public function __construct( &$globalVars ) {
25 6
		$this->globalVars =& $globalVars;
26 6
	}
27
28
	/**
29
	 * @since 1.9
30
	 */
31 4
	public function init() {
32
33 4
		if ( !$this->isDefinedConstant( 'SMW_NS_PROPERTY' ) ) {
34 1
			$this->initCustomNamespace( $this->globalVars );
35
		}
36
37 4
		if ( empty( $this->globalVars['smwgContLang'] ) ) {
38 4
			$this->globalVars['smwgContLang'] = ExtraneousLanguage::getInstance()->fetchByLanguageCode( $this->globalVars['wgLanguageCode'] );
39
		}
40
41 4
		$this->addNamespaceSettings();
42
43 4
		return true;
44
	}
45
46
	/**
47
	 * @since 2.4
48
	 *
49
	 * @param string $languageCode
50
	 *
51
	 * @return array
52
	 */
53 233
	public static function getNamespacesByLanguageCode( $languageCode ) {
0 ignored issues
show
Coding Style introduced by
getNamespacesByLanguageCode 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...
54 233
		$GLOBALS['smwgContLang'] = ExtraneousLanguage::getInstance()->fetchByLanguageCode( $languageCode );
55 233
		return $GLOBALS['smwgContLang']->getNamespaces();
56
	}
57
58
	/**
59
	 * @see Hooks:CanonicalNamespaces
60
	 *
61
	 * @since 1.9
62
	 *
63
	 * @return array
64
	 */
65 270
	public static function getCanonicalNames() {
66
67
		$canonicalNames = array(
68 270
			SMW_NS_PROPERTY      => 'Property',
69 270
			SMW_NS_PROPERTY_TALK => 'Property_talk',
70 270
			SMW_NS_TYPE          => 'Type',
71 270
			SMW_NS_TYPE_TALK     => 'Type_talk',
72 270
			SMW_NS_CONCEPT       => 'Concept',
73 270
			SMW_NS_CONCEPT_TALK  => 'Concept_talk'
74
		);
75
76 270
		return $canonicalNames;
77
	}
78
79
	/**
80
	 * @since 1.9
81
	 *
82
	 * @param integer offset
83
	 *
84
	 * @return array
85
	 */
86 4
	public static function buildNamespaceIndex( $offset ) {
87
88
		$namespaceIndex = array(
89 4
			'SMW_NS_PROPERTY'      => $offset + 2,
90 4
			'SMW_NS_PROPERTY_TALK' => $offset + 3,
91 4
			'SMW_NS_TYPE'          => $offset + 4,
92 4
			'SMW_NS_TYPE_TALK'     => $offset + 5,
93 4
			'SF_NS_FORM'           => $offset + 6,
94 4
			'SF_NS_FORM_TALK'      => $offset + 7,
95 4
			'SMW_NS_CONCEPT'       => $offset + 8,
96 4
			'SMW_NS_CONCEPT_TALK'  => $offset + 9,
97
		);
98
99 4
		return $namespaceIndex;
100
	}
101
102
	/**
103
	 * 100 and 101 used to be occupied by SMW's now obsolete namespaces
104
	 * "Relation" and "Relation_Talk"
105
	 *
106
	 * 106 and 107 are occupied by the Semantic Forms, we define them here
107
	 * to offer some (easy but useful) support to SF
108
	 *
109
	 * @since 1.9
110
	 *
111
	 * @param array $globalVars
112
	 */
113 2
	public static function initCustomNamespace( &$globalVars ) {
114
115 2
		$instance = new self( $globalVars );
116
117 2
		if ( !isset( $globalVars['smwgNamespaceIndex'] ) ) {
118 2
			$globalVars['smwgNamespaceIndex'] = 100;
119
		}
120
121 2
		foreach ( $instance->buildNamespaceIndex( $globalVars['smwgNamespaceIndex'] ) as $ns => $index ) {
122 2
			if ( !$instance->isDefinedConstant( $ns ) ) {
123 2
				define( $ns, $index );
124
			};
125
		}
126
127 2
		$globalVars['wgExtraNamespaces'] = ( isset( $globalVars['wgExtraNamespaces'] ) ? $globalVars['wgExtraNamespaces'] : array() ) + self::getCanonicalNames();
128 2
	}
129
130 4
	protected function addNamespaceSettings() {
131
132 4
		$this->isValidConfigurationOrSetDefault( 'wgExtraNamespaces', array() );
133 4
		$this->isValidConfigurationOrSetDefault( 'wgNamespaceAliases', array() );
134
135
		/**
136
		 * @var SMWLanguage $smwgContLang
137
		 */
138 4
		$this->globalVars['wgExtraNamespaces'] = $this->globalVars['smwgContLang']->getNamespaces() + $this->globalVars['wgExtraNamespaces'];
139 4
		$this->globalVars['wgNamespaceAliases'] = array_flip( $this->globalVars['smwgContLang']->getNamespaces() ) + $this->globalVars['wgNamespaceAliases'];
140
141
		// Support subpages only for talk pages by default
142 4
		$this->globalVars['wgNamespacesWithSubpages'] = $this->globalVars['wgNamespacesWithSubpages'] + array(
143 4
			SMW_NS_PROPERTY_TALK => true,
144 4
			SMW_NS_TYPE_TALK => true,
145 4
			SMW_NS_CONCEPT_TALK => true,
146
		);
147
148
		// not modified for Semantic MediaWiki
149
		/* $this->globalVars['wgNamespacesToBeSearchedDefault'] = array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
150
			NS_MAIN           => true,
151
			);
152
		*/
153
154
		/**
155
		 * Default settings for the SMW specific NS which can only
156
		 * be defined after SMW_NS_PROPERTY is declared
157
		 */
158
		$smwNamespacesSettings = array(
159 4
			SMW_NS_PROPERTY  => true,
160 4
			SMW_NS_PROPERTY_TALK  => false,
161 4
			SMW_NS_TYPE => true,
162 4
			SMW_NS_TYPE_TALK => false,
163 4
			SMW_NS_CONCEPT => true,
164 4
			SMW_NS_CONCEPT_TALK => false,
165
		);
166
167
		// Combine default values with values specified in other places
168
		// (LocalSettings etc.)
169 4
		$this->globalVars['smwgNamespacesWithSemanticLinks'] = array_replace(
170
			$smwNamespacesSettings,
171 4
			$this->globalVars['smwgNamespacesWithSemanticLinks']
172
		);
173
174 4
	}
175
176 4
	protected function isValidConfigurationOrSetDefault( $element, $default ) {
177 4
		if ( !isset( $this->globalVars[$element] ) || !is_array( $this->globalVars[$element] ) ) {
178 1
			$this->globalVars[$element] = $default;
179
		}
180 4
	}
181
182 5
	protected function isDefinedConstant( $constant ) {
183 5
		return defined( $constant );
184
	}
185
186
}
187