Completed
Push — master ( 3fbb63...83072e )
by mw
39:37 queued 06:51
created

GlobalFunctions.php ➔ smwfNormalTitleText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use SMW\CompatibilityMode;
4
use SMW\NamespaceManager;
5
use SMW\NumberFormatter;
6
use SMW\SPARQLStore\SparqlDBConnectionProvider;
7
8
/**
9
 * Global functions specified and used by Semantic MediaWiki. In general, it is
10
 * tried to fit functions in suitable classes as static methods if they clearly
11
 * belong to some particular sub-function of SMW. Most functions here are used
12
 * in diverse contexts so that they do not have fonud a place in any such class
13
 * yet.
14
 * @ingroup SMW
15
 */
16
17
/**
18
 * @see NamespaceExaminer
19
 *
20
 * @return boolean
21
 * @deprecated since 1.9 and will be removed in 1.11
22
 */
23
function smwfIsSemanticsProcessed( $namespace ) {
24
	return \SMW\NamespaceExaminer::getInstance()->isSemanticEnabled( $namespace );
25
}
26
27
/**
28
 * Takes a title text and turns it safely into its DBKey. This function
29
 * reimplements most of the title normalization as done in Title.php in order
30
 * to achieve conversion with less overhead. The official code could be called
31
 * here if more advanced normalization is needed.
32
 *
33
 * @param string $text
34
 */
35
function smwfNormalTitleDBKey( $text ) {
36 1
	global $wgCapitalLinks;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
37
38 1
	$text = trim( $text );
39
40 1
	if ( $wgCapitalLinks ) {
41 1
		$text = ucfirst( $text );
42
	}
43
44 1
	return str_replace( ' ', '_', $text );
45
}
46
47
/**
48
 * Takes a text and turns it into a normalised version. This function
49
 * reimplements the title normalization as done in Title.php in order to
50
 * achieve conversion with less overhead. The official code could be called
51
 * here if more advanced normalization is needed.
52
 *
53
 * @param string $text
54
 */
55
function smwfNormalTitleText( $text ) {
56 186
	global $wgCapitalLinks, $wgContLang;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
57
58 186
	$text = trim( $text );
59
60 186
	if ( $wgCapitalLinks ) {
61 185
		$text = $wgContLang->ucfirst( $text );
62
	}
63
64 186
	return str_replace( '_', ' ', $text );
65
}
66
67
/**
68
 * Escapes text in a way that allows it to be used as XML content (e.g. as a
69
 * string value for some property).
70
 *
71
 * @param string $text
72
 */
73
function smwfXMLContentEncode( $text ) {
74 46
	return str_replace( array( '&', '<', '>' ), array( '&amp;', '&lt;', '&gt;' ), Sanitizer::decodeCharReferences( $text ) );
75
}
76
77
/**
78
 * Decodes character references and inserts Unicode characters instead, using
79
 * the MediaWiki Sanitizer.
80
 *
81
 * @param string $text
82
 */
83
function smwfHTMLtoUTF8( $text ) {
84 12
	return Sanitizer::decodeCharReferences( $text );
85
}
86
87
/**
88
 * @deprecated since 2.1, use NumberFormatter instead
89
 */
90
function smwfNumberFormat( $value, $decplaces = 3 ) {
91
	return NumberFormatter::getInstance()->getLocalizedFormattedNumber( $value, $decplaces );
92
}
93
94
/**
95
 * Formats an array of message strings so that it appears as a tooltip.
96
 * $icon should be one of: 'warning' (default), 'info'.
97
 *
98
 * @param array $messages
99
 * @param string $icon Acts like an enum. Callers must ensure safety, since this value is used directly in the output.
0 ignored issues
show
Bug introduced by
There is no parameter named $icon. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
100
 * @param string $seperator
101
 * @param boolean $escape Should the messages be escaped or not (ie when they already are)
102
 *
103
 * @return string
104
 */
105
function smwfEncodeMessages( array $messages, $type = 'warning', $seperator = ' <!--br-->', $escape = true ) {
106 78
	if (  $messages !== array() ) {
107
108 25
		if ( $escape ) {
109 24
			$messages = array_map( 'htmlspecialchars', $messages );
110
		}
111
112 25
		if ( count( $messages ) == 1 )  {
113 17
			$errorList = $messages[0];
114
		}
115
		else {
116 9
			foreach ( $messages as &$message ) {
117 9
				$message = '<li>' . $message . '</li>';
118
			}
119
120 9
			$errorList = '<ul>' . implode( $seperator, $messages ) . '</ul>';
121
		}
122
123
		// Type will be converted internally
124 25
		$highlighter = SMW\Highlighter::factory( $type );
125 25
		$highlighter->setContent( array (
126 25
			'caption'   => null,
127 25
			'content'   => $errorList
128
		) );
129
130 25
		return $highlighter->getHtml();
131
	} else {
132 58
		return '';
133
	}
134
}
135
136
/**
137
 * Returns an instance for the storage back-end
138
 *
139
 * @return SMWStore
140
 */
141
function &smwfGetStore() {
142
	$store = \SMW\StoreFactory::getStore();
143
	return $store;
144
}
145
146
/**
147
 * @codeCoverageIgnore
148
 *
149
 * Get the SMWSparqlDatabase object to use for connecting to a SPARQL store,
150
 * or null if no SPARQL backend has been set up.
151
 *
152
 * Currently, it just returns one globally defined object, but the
153
 * infrastructure allows to set up load balancing and task-dependent use of
154
 * stores (e.g. using other stores for fast querying than for storing new
155
 * facts), somewhat similar to MediaWiki's DB implementation.
156
 *
157
 * @since 1.6
158
 *
159
 * @return SMWSparqlDatabase or null
160
 */
161
function &smwfGetSparqlDatabase() {
0 ignored issues
show
Coding Style introduced by
smwfGetSparqlDatabase 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...
162
163
	if ( !isset( $GLOBALS['smwgSparqlDatabaseMaster'] ) ) {
164
		$connectionProvider = new SparqlDBConnectionProvider();
165
		$GLOBALS['smwgSparqlDatabaseMaster'] = $connectionProvider->getConnection();
166
	}
167
168
	return $GLOBALS['smwgSparqlDatabaseMaster'];
169
}
170
171
/**
172
 * Compatibility helper for using Linker methods.
173
 * MW 1.16 has a Linker with non-static methods,
174
 * where in MW 1.19 they are static, and a DummyLinker
175
 * class is introduced, which can be instantiated for
176
 * compat reasons. As of MW 1.28, DummyLinker is being
177
 * deprecated, so always use Linker.
178
 *
179
 * @since 1.6
180
 *
181
 * @return Linker
182
 */
183
function smwfGetLinker() {
184 8
	static $linker = false;
185
186 8
	if ( $linker === false ) {
187
		$linker = new Linker();
188
	}
189
190 8
	return $linker;
191
}
192
193
/**
194
 * Function to switch on Semantic MediaWiki. This function must be called in
195
 * LocalSettings.php after including SMW_Settings.php. It is used to ensure
196
 * that required parameters for SMW are really provided explicitly. For
197
 * readability, this is the only global function that does not adhere to the
198
 * naming conventions.
199
 *
200
 * This function also sets up all autoloading, such that all SMW classes are
201
 * available as early on. Moreover, jobs and special pages are registered.
202
 *
203
 * @param mixed $namespace
204
 * @param boolean $complete
205
 *
206
 * @return true
207
 *
208
 * @codeCoverageIgnore
209
 */
210
function enableSemantics( $namespace = null, $complete = false ) {
0 ignored issues
show
Coding Style introduced by
enableSemantics 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...
211
	global $smwgNamespace;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
212
213
	// $GLOBALS ought to be injected from the top-level but that would require
214
	// to change the interface which would bring no benefits for the end user
215
	// as enableSemantics() is only targeted to be included in LocalSettings
216
	NamespaceManager::initCustomNamespace( $GLOBALS );
217
218
	if ( !$complete && ( $smwgNamespace !== '' ) ) {
219
		// The dot tells that the domain is not complete. It will be completed
220
		// in the Export since we do not want to create a title object here when
221
		// it is not needed in many cases.
222
		$smwgNamespace = '.' . $namespace;
223
	} else {
224
		$smwgNamespace = $namespace;
225
	}
226
227
	$GLOBALS['smwgSemanticsEnabled'] = true;
228
229
	return true;
230
}
231
232
/**
233
 * To disable Semantic MediaWiki's operational functionality
234
 *
235
 * @note This function can be used to temporary disable SMW but it is paramount
236
 * that after SMW is re-enabled to run `rebuildData.php` in order for data to
237
 * represent a state that mirrors the actual environment (deleted, moved pages
238
 * are not tracked when disabled).
239
 */
240
function disableSemantics() {
241
	CompatibilityMode::disableSemantics();
242
}
243