Completed
Push — master ( 9e1461...cdd4b1 )
by
unknown
13:17
created

getQueryResultForInterlanguageLink()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 24
cts 24
cp 1
rs 9.264
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
namespace SIL;
4
5
use SMW\Query\Language\Conjunction;
6
use SMW\Query\Language\SomeProperty;
7
use SMW\Query\Language\ValueDescription;
8
use SMW\DataValueFactory;
9
use SMW\Store;
10
use SMW\DIWikiPage;
11
use SMW\DIProperty;
12
use SMWPrintRequest as PrintRequest;
13
use SMWPropertyValue as PropertyValue;
14
use SMWQuery as Query;
15
use SMWDIBlob as DIBlob;
16
use Title;
17
18
/**
19
 * This class is the most critical component of SIL as it combines the store
20
 * interface with the cache interface.
21
 *
22
 * Any request either for a target link or language code lookup are channelled
23
 * through this class in order to make a decision whether to use an existing
24
 * cache entry or to make a "fresh" query request to the storage back-end.
25
 *
26
 * No other component of SIL should communicate to the store directly and let
27
 * the lookup class to handle those requests.
28
 *
29
 * @license GNU GPL v2+
30
 * @since 1.0
31
 *
32
 * @author mwjames
33
 */
34
class InterlanguageLinksLookup {
35
36
	const NO_LANG = '';
37
38
	/**
39
	 * @var LanguageTargetLinksCache
40
	 */
41
	private $languageTargetLinksCache;
42
43
	/**
44
	 * @var Store
45
	 */
46
	private $store;
47
48
	/**
49
	 * @since 1.0
50
	 *
51
	 * @param LanguageTargetLinksCache $languageTargetLinksCache
52
	 */
53 16
	public function __construct( LanguageTargetLinksCache $languageTargetLinksCache ) {
54 16
		$this->languageTargetLinksCache = $languageTargetLinksCache;
55 16
	}
56
57
	/**
58
	 * @since 1.0
59
	 *
60
	 * @param Store $store
61
	 */
62 21
	public function setStore( Store $store ) {
63 21
		$this->store = $store;
64 21
	}
65
66
67
	/**
68
	 * @since 1.2
69
	 *
70
	 * @param Title $title
71
	 *
72
	 * @return Title
73
	 */
74 8
	public function getRedirectTargetFor( Title $title ) {
75 8
		return $this->store->getRedirectTarget( DIWikiPage::newFromTitle( $title ) )->getTitle();
76
	}
77
78
	/**
79
	 * @since 1.0
80
	 *
81
	 * @param Title $title
82
	 */
83 9
	public function resetLookupCacheBy( Title $title ) {
84
85 9
		$this->languageTargetLinksCache->deleteLanguageTargetLinksFromCache(
86 9
			$this->findFullListOfReferenceTargetLinks( $title )
87 9
		);
88
89 9
		$this->languageTargetLinksCache->deletePageLanguageForTargetFromCache(
90
			$title
91 9
		);
92 9
	}
93
94
	/**
95
	 * @since 1.0
96
	 *
97
	 * @param Title|null $title
98
	 * @param string $languageCode
99
	 */
100 12
	public function pushPageLanguageToLookupCache( Title $title = null, $languageCode ) {
101
102 12
		if ( $title !== null && $this->languageTargetLinksCache->getPageLanguageFromCache( $title ) === $languageCode ) {
103 7
			return;
104
		}
105
106 12
		$this->languageTargetLinksCache->pushPageLanguageToCache(
107 12
			$title,
108
			$languageCode
109 12
		);
110 12
	}
111
112
	/**
113
	 * @since 1.0
114
	 *
115
	 * @param InterlanguageLink $interlanguageLink
116
	 * @param Title|null $target
117
	 *
118
	 * @return array
119
	 */
120 11
	public function queryLanguageTargetLinks( InterlanguageLink $interlanguageLink, Title $target = null ) {
121
122 11
		$languageTargetLinks = $this->languageTargetLinksCache->getLanguageTargetLinksFromCache(
123
			$interlanguageLink
124 11
		);
125
126 11
		if ( is_array( $languageTargetLinks ) && $languageTargetLinks !== array() ) {
127 2
			return $languageTargetLinks;
128
		}
129
130 10
		$languageTargetLinks = array();
131
132 10
		if ( $target !== null && $interlanguageLink->getLanguageCode() !== '' ) {
133 8
			$languageTargetLinks[ $interlanguageLink->getLanguageCode() ] = $target;
134 8
		}
135
136 10
		$queryResult = $this->getQueryResultForInterlanguageLink( $interlanguageLink );
137
138 10
		$this->iterateQueryResultToFindLanguageTargetLinks(
139 10
			$queryResult,
140
			$languageTargetLinks
141 10
		);
142
143 10
		$this->languageTargetLinksCache->saveLanguageTargetLinksToCache(
144 10
			$interlanguageLink,
145
			$languageTargetLinks
146 10
		);
147
148 10
		return $languageTargetLinks;
149
	}
150
151
	/**
152
	 * @since 1.0
153
	 *
154
	 * @param Title $title
155
	 *
156
	 * @return string
157
	 */
158 14
	public function findPageLanguageForTarget( Title $title ) {
159
160
		// @note $title->getPageLanguage()->getLanguageCode() cannot be called
161
		// here as this would cause a recursive chain
162
163 14
		$lookupLanguageCode = $this->languageTargetLinksCache->getPageLanguageFromCache( $title );
164
165 14
		if ( $lookupLanguageCode !== null && $lookupLanguageCode !== false ) {
166 3
			return $lookupLanguageCode;
167
		}
168
169 12
		$lookupLanguageCode = $this->lookupLastPageLanguageForTarget( $title );
170
171 12
		$this->pushPageLanguageToLookupCache(
172 12
			$title,
173
			$lookupLanguageCode
174 12
		);
175
176 12
		return $lookupLanguageCode;
177
	}
178
179
	/**
180
	 * @since 1.1
181
	 *
182
	 * @param Title $title
183
	 *
184
	 * @return boolean
185
	 */
186 2
	public function hasSilAnnotationFor( Title $title ) {
187
188 2
		$propertyValues = $this->store->getPropertyValues(
189 2
			DIWikiPage::newFromTitle( $title ),
190 2
			new DIProperty( PropertyRegistry::SIL_CONTAINER )
191 2
		);
192
193 2
		return $propertyValues !== array();
194
	}
195
196
	/**
197
	 * @since 1.0
198
	 *
199
	 * @param Title $title
200
	 *
201
	 * @return DIWikiPage[]|[]
202
	 */
203 12
	public function findFullListOfReferenceTargetLinks( Title $title ) {
204
205 12
		$linkReferences = array();
206
207
		try{
208 12
			$property = new DIProperty( PropertyRegistry::SIL_CONTAINER );
209 12
		} catch ( \Exception $e ) {
210
			return $linkReferences;
211
		}
212
213 12
		$propertyValues = $this->store->getPropertyValues(
214 12
			DIWikiPage::newFromTitle( $title ),
215
			$property
216 12
		);
217
218 12
		if ( !is_array( $propertyValues ) || $propertyValues === array() ) {
219 11
			return $linkReferences;
220
		}
221
222 9
		foreach ( $propertyValues as $containerSubject ) {
223
224 9
			$values = $this->store->getPropertyValues(
225 9
				$containerSubject,
226 9
				new DIProperty( PropertyRegistry::SIL_ILL_REF )
227 9
			);
228
229 9
			$linkReferences = array_merge( $linkReferences, $values );
230 9
		}
231
232 9
		return $linkReferences;
233
	}
234
235
	/**
236
	 * @return QueryResult
237
	 */
238 10
	private function getQueryResultForInterlanguageLink( InterlanguageLink $interlanguageLink ) {
239
240 10
		$description = new Conjunction();
241
242 10
		$languageDataValue = $interlanguageLink->newLanguageDataValue();
243
244 10
		$linkReferenceDataValue = $interlanguageLink->newLinkReferenceDataValue();
245
246 10
		$description->addDescription(
247 10
			new SomeProperty(
248 10
				$linkReferenceDataValue->getProperty(),
0 ignored issues
show
Bug introduced by
It seems like $linkReferenceDataValue->getProperty() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
249 10
				new ValueDescription( $linkReferenceDataValue->getDataItem(), null, SMW_CMP_EQ )
250 10
			)
251 10
		);
252
253 10
		$propertyValue = DataValueFactory::getInstance()->newDataValueByType( '__pro' );
254 10
		$propertyValue->setDataItem( $languageDataValue->getProperty() );
255
256 10
		$description->addPrintRequest(
257 10
			new PrintRequest( PrintRequest::PRINT_PROP, null, $propertyValue )
258 10
		);
259
260 10
		$query = new Query(
261
			$description
262 10
		);
263
264 10
		if ( defined( 'SMWQuery::PROC_CONTEXT' ) ) {
265 10
			$query->setOption( Query::PROC_CONTEXT, 'SIL.InterlanguageLinksLookup' );
266 10
		}
267
268 10
		if ( defined( 'SMWQuery::NO_CACHE' ) ) {
269 10
			$query->setOption( Query::NO_CACHE, true );
270 10
		}
271
272
		//	$query->sort = true;
273
		//	$query->sortkey = array( $languageDataValue->getProperty()->getLabel() => 'asc' );
274
275
		// set query limit to certain threshold
276
277 10
		return $this->store->getQueryResult( $query );
278
	}
279
280 10
	private function iterateQueryResultToFindLanguageTargetLinks( $queryResult, array &$languageTargetLinks ) {
281
282 10
		while ( $resultArray = $queryResult->getNext() ) {
283 7
			foreach ( $resultArray as $row ) {
284
285 7
				$dataValue = $row->getNextDataValue();
286
287 7
				if ( $dataValue === false ) {
288 1
					continue;
289
				}
290
291 7
				$languageTargetLinks[ $dataValue->getWikiValue() ] = $row->getResultSubject()->getTitle();
292 7
			}
293 7
		}
294 10
	}
295
296 12
	private function lookupLastPageLanguageForTarget( Title $title ) {
297
298
		try{
299 12
			$property = new DIProperty( PropertyRegistry::SIL_CONTAINER );
300 12
		} catch ( \Exception $e ) {
301
			return self::NO_LANG;
302
		}
303
304 12
		$propertyValues = $this->store->getPropertyValues(
305 12
			DIWikiPage::newFromTitle( $title ),
306
			$property
307 12
		);
308
309 12
		if ( !is_array( $propertyValues ) || $propertyValues === array() ) {
310 10
			return self::NO_LANG;
311
		}
312
313 3
		$containerSubject = end( $propertyValues );
314
315 3
		$propertyValues = $this->store->getPropertyValues(
316 3
			$containerSubject,
317 3
			new DIProperty( PropertyRegistry::SIL_ILL_LANG )
318 3
		);
319
320 3
		$languageCodeValue = end( $propertyValues );
321
322 3
		if ( $languageCodeValue instanceOf DIBlob ) {
323 2
			return $languageCodeValue->getString();
324
		}
325
326 1
		return self::NO_LANG;
327
	}
328
329
}
330