Completed
Push — master ( feeac5...f27c97 )
by mw
12s
created

getFittingKeyByCapitalLinks()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.25

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
ccs 3
cts 4
cp 0.75
crap 4.25
rs 9.2
1
<?php
2
3
namespace SMW\Exporter;
4
5
use SMW\DIWikiPage;
6
use SMW\Exporter\Element\ExpElement;
7
use SMW\Exporter\Element\ExpResource;
8
use SMW\Localizer;
9
use SMW\Store;
10
use SMWDataItem as DataItem;
11
use Title;
12
13
/**
14
 * @license GNU GPL v2+
15
 * @since 2.4
16
 *
17
 * @author mwjames
18
 */
19
class DataItemByExpElementMatchFinder {
20
21
	/**
22
	 * @var Store
23
	 */
24
	private $store;
25
26
	/**
27
	 * @var string
28
	 */
29
	private $wikiNamespace;
30
31
	/**
32
	 * @since 2.4
33
	 *
34
	 * @param Store $store
35
	 * @param string $wikiNamespace
36
	 */
37 251
	public function __construct( Store $store, $wikiNamespace = '' ) {
38 251
		$this->store = $store;
39 251
		$this->wikiNamespace = $wikiNamespace;
40 251
	}
41
42
	/**
43
	 * Try to map an ExpElement to a representative DataItem which may return null
44
	 * if the attempt fails.
45
	 *
46
	 * @since 2.4
47
	 *
48
	 * @param ExpElement $expElement
49
	 *
50
	 * @return DataItem|null
51
	 */
52 4
	public function tryToFindDataItemForExpElement( ExpElement $expElement ) {
53
54 4
		$dataItem = null;
55
56 4
		if ( !$expElement instanceof ExpResource ) {
57
			return $dataItem;
58
		}
59
60 4
		$uri = $expElement->getUri();
61
62 4
		if ( strpos( $uri, $this->wikiNamespace ) !== false ) {
63 2
			$dataItem = $this->tryToMatchDataItemForWikiNamespaceUri( $uri );
64
		} else {
65
			 // Not in wikiNamespace therefore most likely an imported URI
66 2
			$dataItem = $this->tryToMatchDataItemForUnmatchedWikiNamespaceUri( $uri );
67
		}
68
69 4
		return $dataItem;
70
	}
71
72 2
	private function tryToMatchDataItemForWikiNamespaceUri( $uri ) {
73
74 2
		$dataItem = null;
75 2
		$localName = substr( $uri, strlen( $this->wikiNamespace ) );
76
77 2
		$dbKey = rawurldecode( Escaper::decodeUri( $localName ) );
78 2
		$parts = explode( '#', $dbKey, 2 );
79
80 2
		if ( count( $parts ) == 2 ) {
81 2
			$dbKey = $parts[0];
82 2
			$subobjectname = $parts[1];
83
		} else {
84
			$subobjectname = '';
85
		}
86
87 2
		$parts = explode( ':', $dbKey, 2 );
88
89
		// No extra NS
90 2
		if ( count( $parts ) == 1 ) {
91 1
			return new DIWikiPage( $dbKey, NS_MAIN, '', $subobjectname );
92
		}
93
94 1
		$namespaceId = $this->tryToMatchNamespaceName( $parts[0] );
95
96 1
		if ( $namespaceId != -1 && $namespaceId !== false ) {
97 1
			$dataItem = new DIWikiPage( $parts[1], $namespaceId, '', $subobjectname );
98
		} else {
99
			$title = Title::newFromDBkey( $dbKey );
100
101
			if ( $title !== null ) {
102
				$dataItem = new DIWikiPage( $title->getDBkey(), $title->getNamespace(), $title->getInterwiki(), $subobjectname );
103
			}
104
		}
105
106 1
		return $dataItem;
107
	}
108
109 1
	private function tryToMatchNamespaceName( $name ) {
110
		// try the by far most common cases directly before using Title
111 1
		$namespaceName = str_replace( '_', ' ', $name );
112
113 1
		if ( ( $namespaceId = Localizer::getInstance()->getNamespaceIndexByName( $name ) ) !== false ) {
114 1
			return $namespaceId;
115
		}
116
117
		foreach ( array( SMW_NS_PROPERTY, NS_CATEGORY, NS_USER, NS_HELP ) as $nsId ) {
118
			if ( $namespaceName == Localizer::getInstance()->getNamespaceTextById( $nsId ) ) {
119
				$namespaceId = $nsId;
120
				break;
121
			}
122
		}
123
124
		return $namespaceId;
125
	}
126
127 2
	private function tryToMatchDataItemForUnmatchedWikiNamespaceUri( $uri ) {
128
129 2
		$dataItem = null;
130
131
		// Sesame: Not a valid (absolute) URI: _node1abjt1k9bx17
132 2
		if ( filter_var( $uri, FILTER_VALIDATE_URL ) === false ) {
133 1
			return $dataItem;
134
		}
135
136 1
		$respositoryResult = $this->store->getConnection( 'sparql' )->select(
137 1
			'?v1 ?v2',
138 1
			"<$uri> rdfs:label ?v1 . <$uri> swivt:wikiNamespace ?v2",
139 1
			array( 'LIMIT' => 1 )
140
		);
141
142 1
		$expElements = $respositoryResult->current();
143
144 1
		if ( $expElements !== false ) {
145
146
			// ?v1
147 1
			if ( isset( $expElements[0] ) ) {
148
				$dbKey = $expElements[0]->getLexicalForm();
149
			} else {
150 1
				$dbKey = 'UNKNOWN';
151
			}
152
153
			// ?v2
154 1
			if ( isset( $expElements[1] ) ) {
155
				$namespace = strval( $expElements[1]->getLexicalForm() );
156
			} else {
157 1
				$namespace = NS_MAIN;
158
			}
159
160 1
			$dataItem = new DIWikiPage(
161 1
				$this->getFittingKeyByCapitalLinks( $dbKey, $namespace ),
162
				$namespace
163
			);
164
		}
165
166 1
		return $dataItem;
167
	}
168
169 1
	private function getFittingKeyByCapitalLinks( $dbKey, $namespace ) {
0 ignored issues
show
Coding Style introduced by
getFittingKeyByCapitalLinks 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...
170
171
		// https://www.mediawiki.org/wiki/Manual:$wgCapitalLinks
172
		// https://www.mediawiki.org/wiki/Manual:$wgCapitalLinkOverrides
173 1
		if ( $GLOBALS['wgCapitalLinks'] || ( isset( $GLOBALS['wgCapitalLinkOverrides'][$namespace] ) && $GLOBALS['wgCapitalLinkOverrides'][$namespace] ) ) {
174 1
			return mb_strtoupper( mb_substr( $dbKey, 0, 1 ) ) . mb_substr( $dbKey, 1 );
175
		}
176
177
		return $dbKey;
178
	}
179
180
}
181