|
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
|
15 |
|
public function setStore( Store $store ) { |
|
63
|
15 |
|
$this->store = $store; |
|
64
|
15 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @since 1.2 |
|
69
|
|
|
* |
|
70
|
|
|
* @param Title $title |
|
71
|
|
|
* |
|
72
|
|
|
* @return Title |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public function getRedirectTargetFor( Title $title ) { |
|
75
|
3 |
|
return $this->store->getRedirectTarget( DIWikiPage::newFromTitle( $title ) )->getTitle(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @since 1.0 |
|
80
|
|
|
* |
|
81
|
|
|
* @param Title $title |
|
82
|
|
|
*/ |
|
83
|
3 |
|
public function resetLookupCacheBy( Title $title ) { |
|
84
|
|
|
|
|
85
|
3 |
|
$this->languageTargetLinksCache->deleteLanguageTargetLinksFromCache( |
|
86
|
3 |
|
$this->findFullListOfReferenceTargetLinks( $title ) |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
3 |
|
$this->languageTargetLinksCache->deletePageLanguageForTargetFromCache( |
|
90
|
3 |
|
$title |
|
91
|
|
|
); |
|
92
|
3 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @since 1.0 |
|
96
|
|
|
* |
|
97
|
|
|
* @param Title|null $title |
|
98
|
|
|
* @param string $languageCode |
|
99
|
|
|
*/ |
|
100
|
6 |
|
public function pushPageLanguageToLookupCache( Title $title = null, $languageCode ) { |
|
101
|
|
|
|
|
102
|
6 |
|
if ( $title !== null && $this->languageTargetLinksCache->getPageLanguageFromCache( $title ) === $languageCode ) { |
|
103
|
2 |
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
6 |
|
$this->languageTargetLinksCache->pushPageLanguageToCache( |
|
107
|
6 |
|
$title, |
|
108
|
6 |
|
$languageCode |
|
109
|
|
|
); |
|
110
|
6 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @since 1.0 |
|
114
|
|
|
* |
|
115
|
|
|
* @param InterlanguageLink $interlanguageLink |
|
116
|
|
|
* @param Title|null $target |
|
117
|
|
|
* |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
6 |
|
public function queryLanguageTargetLinks( InterlanguageLink $interlanguageLink, Title $target = null ) { |
|
121
|
|
|
|
|
122
|
6 |
|
$languageTargetLinks = $this->languageTargetLinksCache->getLanguageTargetLinksFromCache( |
|
123
|
6 |
|
$interlanguageLink |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
6 |
|
if ( is_array( $languageTargetLinks ) && $languageTargetLinks !== [] ) { |
|
127
|
1 |
|
return $languageTargetLinks; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
5 |
|
$languageTargetLinks = []; |
|
131
|
|
|
|
|
132
|
5 |
|
if ( $target !== null && $interlanguageLink->getLanguageCode() !== '' ) { |
|
133
|
3 |
|
$languageTargetLinks[ $interlanguageLink->getLanguageCode() ] = $target; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
5 |
|
$queryResult = $this->getQueryResultForInterlanguageLink( $interlanguageLink ); |
|
137
|
|
|
|
|
138
|
5 |
|
$this->iterateQueryResultToFindLanguageTargetLinks( |
|
139
|
5 |
|
$queryResult, |
|
140
|
5 |
|
$languageTargetLinks |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
5 |
|
$this->languageTargetLinksCache->saveLanguageTargetLinksToCache( |
|
144
|
5 |
|
$interlanguageLink, |
|
145
|
5 |
|
$languageTargetLinks |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
5 |
|
return $languageTargetLinks; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @since 1.0 |
|
153
|
|
|
* |
|
154
|
|
|
* @param Title $title |
|
155
|
|
|
* |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
8 |
|
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
|
8 |
|
$lookupLanguageCode = $this->languageTargetLinksCache->getPageLanguageFromCache( $title ); |
|
164
|
|
|
|
|
165
|
8 |
|
if ( $lookupLanguageCode !== null && $lookupLanguageCode !== false ) { |
|
166
|
3 |
|
return $lookupLanguageCode; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
6 |
|
$lookupLanguageCode = $this->lookupLastPageLanguageForTarget( $title ); |
|
170
|
|
|
|
|
171
|
6 |
|
$this->pushPageLanguageToLookupCache( |
|
172
|
6 |
|
$title, |
|
173
|
6 |
|
$lookupLanguageCode |
|
174
|
|
|
); |
|
175
|
|
|
|
|
176
|
6 |
|
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
|
|
|
); |
|
192
|
|
|
|
|
193
|
2 |
|
return $propertyValues !== []; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @since 1.0 |
|
198
|
|
|
* |
|
199
|
|
|
* @param Title $title |
|
200
|
|
|
* |
|
201
|
|
|
* @return DIWikiPage[]|[] |
|
202
|
|
|
*/ |
|
203
|
6 |
|
public function findFullListOfReferenceTargetLinks( Title $title ) { |
|
204
|
|
|
|
|
205
|
6 |
|
$linkReferences = []; |
|
206
|
|
|
|
|
207
|
|
|
try{ |
|
208
|
6 |
|
$property = new DIProperty( PropertyRegistry::SIL_CONTAINER ); |
|
209
|
|
|
} catch ( \Exception $e ) { |
|
210
|
|
|
return $linkReferences; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
6 |
|
$propertyValues = $this->store->getPropertyValues( |
|
214
|
6 |
|
DIWikiPage::newFromTitle( $title ), |
|
215
|
6 |
|
$property |
|
216
|
|
|
); |
|
217
|
|
|
|
|
218
|
6 |
|
if ( !is_array( $propertyValues ) || $propertyValues === [] ) { |
|
219
|
5 |
|
return $linkReferences; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
3 |
|
foreach ( $propertyValues as $containerSubject ) { |
|
223
|
|
|
|
|
224
|
3 |
|
$values = $this->store->getPropertyValues( |
|
225
|
3 |
|
$containerSubject, |
|
226
|
3 |
|
new DIProperty( PropertyRegistry::SIL_ILL_REF ) |
|
227
|
|
|
); |
|
228
|
|
|
|
|
229
|
3 |
|
$linkReferences = array_merge( $linkReferences, $values ); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
3 |
|
return $linkReferences; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @return QueryResult |
|
237
|
|
|
*/ |
|
238
|
5 |
|
private function getQueryResultForInterlanguageLink( InterlanguageLink $interlanguageLink ) { |
|
239
|
|
|
|
|
240
|
5 |
|
$description = new Conjunction(); |
|
241
|
|
|
|
|
242
|
5 |
|
$languageDataValue = $interlanguageLink->newLanguageDataValue(); |
|
243
|
|
|
|
|
244
|
5 |
|
$linkReferenceDataValue = $interlanguageLink->newLinkReferenceDataValue(); |
|
245
|
|
|
|
|
246
|
5 |
|
$description->addDescription( |
|
247
|
5 |
|
new SomeProperty( |
|
248
|
5 |
|
$linkReferenceDataValue->getProperty(), |
|
|
|
|
|
|
249
|
5 |
|
new ValueDescription( $linkReferenceDataValue->getDataItem(), null, SMW_CMP_EQ ) |
|
250
|
|
|
) |
|
251
|
|
|
); |
|
252
|
|
|
|
|
253
|
5 |
|
$propertyValue = DataValueFactory::getInstance()->newDataValueByType( '__pro' ); |
|
254
|
5 |
|
$propertyValue->setDataItem( $languageDataValue->getProperty() ); |
|
255
|
|
|
|
|
256
|
5 |
|
$description->addPrintRequest( |
|
257
|
5 |
|
new PrintRequest( PrintRequest::PRINT_PROP, null, $propertyValue ) |
|
258
|
|
|
); |
|
259
|
|
|
|
|
260
|
5 |
|
$query = new Query( |
|
261
|
5 |
|
$description |
|
262
|
|
|
); |
|
263
|
|
|
|
|
264
|
5 |
|
if ( defined( 'SMWQuery::PROC_CONTEXT' ) ) { |
|
265
|
5 |
|
$query->setOption( Query::PROC_CONTEXT, 'SIL.InterlanguageLinksLookup' ); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
5 |
|
if ( defined( 'SMWQuery::NO_CACHE' ) ) { |
|
269
|
5 |
|
$query->setOption( Query::NO_CACHE, true ); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
// $query->sort = true; |
|
273
|
|
|
// $query->sortkey = array( $languageDataValue->getProperty()->getLabel() => 'asc' ); |
|
274
|
|
|
|
|
275
|
|
|
// set query limit to certain threshold |
|
276
|
|
|
|
|
277
|
5 |
|
return $this->store->getQueryResult( $query ); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
5 |
|
private function iterateQueryResultToFindLanguageTargetLinks( $queryResult, array &$languageTargetLinks ) { |
|
281
|
|
|
|
|
282
|
5 |
|
while ( $resultArray = $queryResult->getNext() ) { |
|
283
|
3 |
|
foreach ( $resultArray as $row ) { |
|
284
|
|
|
|
|
285
|
3 |
|
$dataValue = $row->getNextDataValue(); |
|
286
|
|
|
|
|
287
|
3 |
|
if ( $dataValue === false ) { |
|
288
|
1 |
|
continue; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
3 |
|
$languageTargetLinks[ $dataValue->getWikiValue() ] = $row->getResultSubject()->getTitle(); |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
5 |
|
} |
|
295
|
|
|
|
|
296
|
6 |
|
private function lookupLastPageLanguageForTarget( Title $title ) { |
|
297
|
|
|
|
|
298
|
|
|
try{ |
|
299
|
6 |
|
$property = new DIProperty( PropertyRegistry::SIL_CONTAINER ); |
|
300
|
|
|
} catch ( \Exception $e ) { |
|
301
|
|
|
return self::NO_LANG; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
6 |
|
$propertyValues = $this->store->getPropertyValues( |
|
305
|
6 |
|
DIWikiPage::newFromTitle( $title ), |
|
306
|
6 |
|
$property |
|
307
|
|
|
); |
|
308
|
|
|
|
|
309
|
6 |
|
if ( !is_array( $propertyValues ) || $propertyValues === [] ) { |
|
310
|
4 |
|
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
|
|
|
); |
|
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
|
|
|
|
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: