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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Interwiki;
use Interwiki;
use MapCacheLRU;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\HookContainer\HookRunner;
use MediaWiki\Language\Language;
use MediaWiki\MainConfigNames;
use MediaWiki\WikiMap\WikiMap;
use Wikimedia\ObjectCache\WANObjectCache;
use Wikimedia\Rdbms\IConnectionProvider;
/**
* InterwikiLookup backed by the `interwiki` database table or $wgInterwikiCache.
*
* By default this uses the SQL backend (`interwiki` database table) and includes
* two levels of caching. When parsing a wiki page, many interwiki lookups may
* be required and thus there is in-class caching for repeat lookups. To reduce
* database pressure, there is also WANObjectCache for each prefix.
*
* Optionally, a pregenerated dataset can be statically set via $wgInterwikiCache,
* in which case there are no calls to either database or WANObjectCache.
*
* @since 1.28
*/
class ClassicInterwikiLookup implements InterwikiLookup {
/**
* @internal For use by ServiceWiring
*/
public const CONSTRUCTOR_OPTIONS = [
MainConfigNames::InterwikiExpiry,
MainConfigNames::InterwikiCache,
MainConfigNames::InterwikiScopes,
MainConfigNames::InterwikiFallbackSite,
'wikiId',
];
private ServiceOptions $options;
/** @var Language */
private $contLang;
/** @var WANObjectCache */
private $wanCache;
/** @var HookRunner */
private $hookRunner;
/** @var IConnectionProvider */
private $dbProvider;
/** @var MapCacheLRU<Interwiki|false> */
private $instances;
/**
* Specify number of domains to check for messages:
* - 1: Just local wiki level
* - 2: wiki and global levels
* - 3: site level as well as wiki and global levels
* @var int
*/
private $interwikiScopes;
/** @var array|null Complete pregenerated data if available */
private $data;
/** @var string */
private $wikiId;
/** @var string|null */
private $thisSite = null;
/**
* @param ServiceOptions $options
* @param Language $contLang Language object used to convert prefixes to lower case
* @param WANObjectCache $wanCache Cache for interwiki info retrieved from the database
* @param HookContainer $hookContainer
* @param IConnectionProvider $dbProvider
*/
public function __construct(
ServiceOptions $options,
Language $contLang,
WANObjectCache $wanCache,
HookContainer $hookContainer,
IConnectionProvider $dbProvider
) {
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->options = $options;
$this->contLang = $contLang;
$this->wanCache = $wanCache;
$this->hookRunner = new HookRunner( $hookContainer );
$this->dbProvider = $dbProvider;
$this->instances = new MapCacheLRU( 1000 );
$this->interwikiScopes = $options->get( MainConfigNames::InterwikiScopes );
$interwikiData = $options->get( MainConfigNames::InterwikiCache );
$this->data = is_array( $interwikiData ) ? $interwikiData : null;
$this->wikiId = $options->get( 'wikiId' );
}
/**
* @inheritDoc
* @param string $prefix
* @return bool
*/
public function isValidInterwiki( $prefix ) {
$iw = $this->fetch( $prefix );
return (bool)$iw;
}
/**
* @inheritDoc
* @param string|null $prefix
* @return Interwiki|null|false
*/
public function fetch( $prefix ) {
if ( $prefix === null || $prefix === '' ) {
return null;
}
$prefix = $this->contLang->lc( $prefix );
return $this->instances->getWithSetCallback(
$prefix,
function () use ( $prefix ) {
return $this->load( $prefix );
}
);
}
/**
* Purge the instance cache and memcached for an interwiki prefix
*
* Note that memcached is not used when $wgInterwikiCache
* is enabled, as the pregenerated data will be used statically
* without need for memcached.
*
* @param string $prefix
*/
public function invalidateCache( $prefix ) {
$this->instances->clear( $prefix );
$key = $this->wanCache->makeKey( 'interwiki', $prefix );
$this->wanCache->delete( $key );
}
/**
* Get value from pregenerated data
*
* @param string $prefix
* @return string|false The pregen value or false if prefix is not known
*/
private function getPregenValue( string $prefix ) {
// Lazily resolve site name
if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
$this->thisSite = $this->data['__sites:' . $this->wikiId]
?? $this->options->get( MainConfigNames::InterwikiFallbackSite );
}
$value = $this->data[$this->wikiId . ':' . $prefix] ?? false;
// Site level
if ( $value === false && $this->interwikiScopes >= 3 ) {
$value = $this->data["_{$this->thisSite}:{$prefix}"] ?? false;
}
// Global level
if ( $value === false && $this->interwikiScopes >= 2 ) {
$value = $this->data["__global:{$prefix}"] ?? false;
}
return $value;
}
/**
* Fetch interwiki data and create an Interwiki object.
*
* Use pregenerated data if enabled. Otherwise try memcached first
* and fallback to a DB query.
*
* @param string $prefix The interwiki prefix
* @return Interwiki|false False is prefix is invalid
*/
private function load( $prefix ) {
if ( $this->data !== null ) {
$value = $this->getPregenValue( $prefix );
return $value ? $this->makeFromPregen( $prefix, $value ) : false;
}
$iwData = [];
$abort = !$this->hookRunner->onInterwikiLoadPrefix( $prefix, $iwData );
if ( isset( $iwData['iw_url'] ) ) {
// Hook provided data
return $this->makeFromRow( $iwData );
}
if ( $abort ) {
// Hook indicated no other source may be considered
return false;
}
$fname = __METHOD__;
$iwData = $this->wanCache->getWithSetCallback(
$this->wanCache->makeKey( 'interwiki', $prefix ),
$this->options->get( MainConfigNames::InterwikiExpiry ),
function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix, $fname ) {
$dbr = $this->dbProvider->getReplicaDatabase();
$row = $dbr->newSelectQueryBuilder()
->select( self::selectFields() )
->from( 'interwiki' )
->where( [ 'iw_prefix' => $prefix ] )
->caller( $fname )->fetchRow();
return $row ? (array)$row : '!NONEXISTENT';
}
);
// Handle non-existent case
return is_array( $iwData ) ? $this->makeFromRow( $iwData ) : false;
}
/**
* @param array $row Row from the interwiki table, possibly via memcached
* @return Interwiki
*/
private function makeFromRow( array $row ) {
$url = $row['iw_url'];
$local = $row['iw_local'] ?? 0;
$trans = $row['iw_trans'] ?? 0;
$api = $row['iw_api'] ?? '';
$wikiId = $row['iw_wikiid'] ?? '';
return new Interwiki( null, $url, $api, $wikiId, $local, $trans );
}
/**
* @param string $prefix
* @param string $value
* @return Interwiki
*/
private function makeFromPregen( string $prefix, string $value ) {
// Split values
[ $local, $url ] = explode( ' ', $value, 2 );
return new Interwiki( $prefix, $url, '', '', (int)$local );
}
/**
* Fetch all interwiki prefixes from pregenerated data
*
* @param null|string $local
* @return array Database-like rows
*/
private function getAllPrefixesPregenerated( $local ) {
// Lazily resolve site name
if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
$this->thisSite = $this->data['__sites:' . $this->wikiId]
?? $this->options->get( MainConfigNames::InterwikiFallbackSite );
}
// List of interwiki sources
$sources = [];
// Global level
if ( $this->interwikiScopes >= 2 ) {
$sources[] = '__global';
}
// Site level
if ( $this->interwikiScopes >= 3 ) {
$sources[] = '_' . $this->thisSite;
}
$sources[] = $this->wikiId;
$data = [];
foreach ( $sources as $source ) {
$list = $this->data['__list:' . $source] ?? '';
foreach ( explode( ' ', $list ) as $iw_prefix ) {
$row = $this->data["{$source}:{$iw_prefix}"] ?? null;
if ( !$row ) {
continue;
}
[ $iw_local, $iw_url ] = explode( ' ', $row );
if ( $local !== null && $local != $iw_local ) {
continue;
}
$data[$iw_prefix] = [
'iw_prefix' => $iw_prefix,
'iw_url' => $iw_url,
'iw_local' => $iw_local,
];
}
}
return array_values( $data );
}
/**
* Build an array in the format accepted by $wgInterwikiCache.
*
* Given the array returned by getAllPrefixes(), build a PHP array which
* can be given to self::__construct() as $interwikiData, i.e. as the
* value of $wgInterwikiCache. This is used to construct mock
* interwiki lookup services for testing (in particular, parsertests).
*
* @param array $allPrefixes An array of interwiki information such as
* would be returned by ::getAllPrefixes()
* @param int $scope The scope at which to insert interwiki prefixes.
* See the $interwikiScopes parameter to ::__construct().
* @param ?string $thisSite The value of $thisSite, if $scope is 3.
* @return array
*/
public static function buildCdbHash(
array $allPrefixes, int $scope = 1, ?string $thisSite = null
): array {
$result = [];
$wikiId = WikiMap::getCurrentWikiId();
$keyPrefix = ( $scope >= 2 ) ? '__global' : $wikiId;
if ( $scope >= 3 && $thisSite ) {
$result[ "__sites:$wikiId" ] = $thisSite;
$keyPrefix = "_$thisSite";
}
$list = [];
foreach ( $allPrefixes as $iwInfo ) {
$prefix = $iwInfo['iw_prefix'];
$result["$keyPrefix:$prefix"] = implode( ' ', [
$iwInfo['iw_local'] ?? 0, $iwInfo['iw_url']
] );
$list[] = $prefix;
}
$result["__list:$keyPrefix"] = implode( ' ', $list );
$result["__list:__sites"] = $wikiId;
return $result;
}
/**
* Fetch all interwiki prefixes from DB
*
* @param bool|null $local
* @return array[] Database rows
*/
private function getAllPrefixesDB( $local ) {
$where = [];
if ( $local !== null ) {
$where['iw_local'] = (int)$local;
}
$dbr = $this->dbProvider->getReplicaDatabase();
$res = $dbr->newSelectQueryBuilder()
->select( self::selectFields() )
->from( 'interwiki' )
->where( $where )
->orderBy( 'iw_prefix' )
->caller( __METHOD__ )->fetchResultSet();
$retval = [];
foreach ( $res as $row ) {
$retval[] = (array)$row;
}
return $retval;
}
/**
* Fetch all interwiki data
*
* @param string|null $local If set, limit returned data to local or non-local interwikis
* @return array[] Database-like interwiki rows
*/
public function getAllPrefixes( $local = null ) {
if ( $this->data !== null ) {
return $this->getAllPrefixesPregenerated( $local );
} else {
return $this->getAllPrefixesDB( $local );
}
}
/**
* List of interwiki table fields to select.
*
* @return string[]
*/
private static function selectFields() {
return [
'iw_prefix',
'iw_url',
'iw_api',
'iw_wikiid',
'iw_local',
'iw_trans'
];
}
}
|