summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Exporter/DataItemMatchFinder.php
blob: 24cd2450790b5b932e08962d837ee64d9c4c0ef6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php

namespace SMW\Exporter;

use SMW\DIWikiPage;
use SMW\Exporter\Element\ExpElement;
use SMW\Exporter\Element\ExpResource;
use SMW\Localizer;
use SMW\Store;
use SMWDataItem as DataItem;
use Title;

/**
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class DataItemMatchFinder {

	/**
	 * @var Store
	 */
	private $store;

	/**
	 * @var string
	 */
	private $wikiNamespace;

	/**
	 * @since 2.4
	 *
	 * @param Store $store
	 * @param string $wikiNamespace
	 */
	public function __construct( Store $store, $wikiNamespace = '' ) {
		$this->store = $store;
		$this->wikiNamespace = $wikiNamespace;
	}

	/**
	 * Try to map an ExpElement to a representative DataItem which may return null
	 * if the attempt fails.
	 *
	 * @since 2.4
	 *
	 * @param ExpElement $expElement
	 *
	 * @return DataItem|null
	 */
	public function matchExpElement( ExpElement $expElement ) {

		$dataItem = null;

		if ( !$expElement instanceof ExpResource ) {
			return $dataItem;
		}

		$uri = $expElement->getUri();

		if ( strpos( $uri, $this->wikiNamespace ) !== false ) {
			$dataItem = $this->matchToWikiNamespaceUri( $uri );
		} else {
			 // Not in wikiNamespace therefore most likely an imported URI
			$dataItem = $this->matchToUnknownWikiNamespaceUri( $uri );
		}

		return $dataItem;
	}

	private function matchToWikiNamespaceUri( $uri ) {

		$dataItem = null;
		$localName = substr( $uri, strlen( $this->wikiNamespace ) );

		$dbKey = rawurldecode( Escaper::decodeUri( $localName ) );
		$parts = explode( '#', $dbKey, 2 );

		if ( count( $parts ) == 2 ) {
			$dbKey = $parts[0];
			$subobjectname = $parts[1];
		} else {
			$subobjectname = '';
		}

		$parts = explode( ':', $dbKey, 2 );

		// No extra NS
		if ( count( $parts ) == 1 ) {
			return new DIWikiPage( $dbKey, NS_MAIN, '', $subobjectname );
		}

		$namespaceId = $this->matchToNamespaceName( $parts[0] );

		if ( $namespaceId != -1 && $namespaceId !== false ) {
			$dataItem = new DIWikiPage( $parts[1], $namespaceId, '', $subobjectname );
		} else {
			$title = Title::newFromDBkey( $dbKey );

			if ( $title !== null ) {
				$dataItem = new DIWikiPage( $title->getDBkey(), $title->getNamespace(), $title->getInterwiki(), $subobjectname );
			}
		}

		return $dataItem;
	}

	private function matchToNamespaceName( $name ) {
		// try the by far most common cases directly before using Title
		$namespaceName = str_replace( '_', ' ', $name );

		if ( ( $namespaceId = Localizer::getInstance()->getNamespaceIndexByName( $name ) ) !== false ) {
			return $namespaceId;
		}

		foreach ( [ SMW_NS_PROPERTY, NS_CATEGORY, NS_USER, NS_HELP ] as $nsId ) {
			if ( $namespaceName == Localizer::getInstance()->getNamespaceTextById( $nsId ) ) {
				$namespaceId = $nsId;
				break;
			}
		}

		return $namespaceId;
	}

	private function matchToUnknownWikiNamespaceUri( $uri ) {

		$dataItem = null;

		// Sesame: Not a valid (absolute) URI: _node1abjt1k9bx17
		if ( filter_var( $uri, FILTER_VALIDATE_URL ) === false ) {
			return $dataItem;
		}

		$respositoryResult = $this->store->getConnection( 'sparql' )->select(
			'?v1 ?v2',
			"<$uri> rdfs:label ?v1 . <$uri> swivt:wikiNamespace ?v2",
			[ 'LIMIT' => 1 ]
		);

		$expElements = $respositoryResult->current();

		if ( $expElements !== false ) {

			// ?v1
			if ( isset( $expElements[0] ) ) {
				$dbKey = $expElements[0]->getLexicalForm();
			} else {
				$dbKey = 'UNKNOWN';
			}

			// ?v2
			if ( isset( $expElements[1] ) ) {
				$namespace = strval( $expElements[1]->getLexicalForm() );
			} else {
				$namespace = NS_MAIN;
			}

			$dataItem = new DIWikiPage(
				$this->getFittingDBKey( $dbKey, $namespace ),
				$namespace
			);
		}

		return $dataItem;
	}

	private function getFittingDBKey( $dbKey, $namespace ) {

		// https://www.mediawiki.org/wiki/Manual:$wgCapitalLinks
		// https://www.mediawiki.org/wiki/Manual:$wgCapitalLinkOverrides
		if ( $GLOBALS['wgCapitalLinks'] || ( isset( $GLOBALS['wgCapitalLinkOverrides'][$namespace] ) && $GLOBALS['wgCapitalLinkOverrides'][$namespace] ) ) {
			return mb_strtoupper( mb_substr( $dbKey, 0, 1 ) ) . mb_substr( $dbKey, 1 );
		}

		return $dbKey;
	}

}