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
|
<?php
/**
* Batch query to determine page existence.
*
* 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
* @ingroup Cache
*/
namespace MediaWiki\Cache;
use InvalidArgumentException;
use MediaWiki\Language\Language;
use MediaWiki\Linker\LinksMigration;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Page\PageIdentityValue;
use MediaWiki\Page\PageReference;
use MediaWiki\Page\ProperPageIdentity;
use MediaWiki\Title\TitleFormatter;
use MediaWiki\Title\TitleValue;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Wikimedia\Assert\Assert;
use Wikimedia\Rdbms\IConnectionProvider;
use Wikimedia\Rdbms\IResultWrapper;
use Wikimedia\Rdbms\Platform\ISQLPlatform;
/**
* Class representing a list of titles
* The execute() method checks them all for existence and adds them to a LinkCache object
*
* @ingroup Cache
*/
class LinkBatch {
/**
* @var array<int,array<string,mixed>> 2-d array, first index namespace, second index dbkey, value arbitrary
*/
public $data = [];
/**
* @var ProperPageIdentity[]|null page identity objects corresponding to the links in the batch
*/
private $pageIdentities = null;
/**
* @var string|null For debugging which method is using this class.
*/
protected $caller;
/**
* @var LinkCache
*/
private $linkCache;
/**
* @var TitleFormatter
*/
private $titleFormatter;
/**
* @var Language
*/
private $contentLanguage;
/**
* @var GenderCache
*/
private $genderCache;
/**
* @var IConnectionProvider
*/
private $dbProvider;
/** @var LinksMigration */
private $linksMigration;
/** @var LoggerInterface */
private $logger;
/**
* @see \MediaWiki\Cache\LinkBatchFactory
*
* @internal
* @param iterable<LinkTarget>|iterable<PageReference> $arr Initial items to be added to the batch
* @param LinkCache $linkCache
* @param TitleFormatter $titleFormatter
* @param Language $contentLanguage
* @param GenderCache $genderCache
* @param IConnectionProvider $dbProvider
* @param LinksMigration $linksMigration
* @param LoggerInterface $logger
*/
public function __construct(
iterable $arr,
LinkCache $linkCache,
TitleFormatter $titleFormatter,
Language $contentLanguage,
GenderCache $genderCache,
IConnectionProvider $dbProvider,
LinksMigration $linksMigration,
LoggerInterface $logger
) {
$this->linkCache = $linkCache;
$this->titleFormatter = $titleFormatter;
$this->contentLanguage = $contentLanguage;
$this->genderCache = $genderCache;
$this->dbProvider = $dbProvider;
$this->linksMigration = $linksMigration;
$this->logger = $logger;
foreach ( $arr as $item ) {
$this->addObj( $item );
}
}
/**
* Use ->setCaller( __METHOD__ ) to indicate which code is using this
* class. Only used in debugging output.
* @since 1.17
*
* @param string $caller
* @return self (since 1.32)
*/
public function setCaller( $caller ) {
$this->caller = $caller;
return $this;
}
/**
* @param LinkTarget|PageReference $link
*/
public function addObj( $link ) {
if ( !$link ) {
// Don't die if we got null, just skip. There is nothing to do anyway.
// For now, let's avoid things like T282180. We should be more strict in the future.
$this->logger->warning(
'Skipping null link, probably due to a bad title.',
[ 'exception' => new RuntimeException() ]
);
return;
}
if ( $link instanceof LinkTarget && $link->isExternal() ) {
$this->logger->warning(
'Skipping interwiki link',
[ 'exception' => new RuntimeException() ]
);
return;
}
Assert::parameterType( [ LinkTarget::class, PageReference::class ], $link, '$link' );
$this->add( $link->getNamespace(), $link->getDBkey() );
}
/**
* @param int $ns
* @param string $dbkey
*/
public function add( $ns, $dbkey ) {
if ( $ns < 0 || $dbkey === '' ) {
// T137083
return;
}
$this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1;
}
/**
* Set the link list to a given 2-d array
* First key is the namespace, second is the DB key, value arbitrary
*
* @param array<int,array<string,mixed>> $array
*/
public function setArray( $array ) {
$this->data = $array;
}
/**
* Returns true if no pages have been added, false otherwise.
*
* @return bool
*/
public function isEmpty() {
return $this->getSize() == 0;
}
/**
* Returns the size of the batch.
*
* @return int
*/
public function getSize() {
return count( $this->data );
}
/**
* Do the query and add the results to the LinkCache object
*
* @return int[] Mapping PDBK to ID
*/
public function execute() {
return $this->executeInto( $this->linkCache );
}
/**
* Do the query, add the results to the LinkCache object,
* and return ProperPageIdentity instances corresponding to the pages in the batch.
*
* @since 1.37
* @return ProperPageIdentity[] A list of ProperPageIdentities
*/
public function getPageIdentities(): array {
if ( $this->pageIdentities === null ) {
$this->execute();
}
return $this->pageIdentities;
}
/**
* Do the query and add the results to a given LinkCache object
* Return an array mapping PDBK to ID
*
* @param LinkCache $cache
* @return int[] Remaining IDs
*/
protected function executeInto( $cache ) {
$res = $this->doQuery();
$this->doGenderQuery();
return $this->addResultToCache( $cache, $res );
}
/**
* Add a result wrapper containing IDs and titles to a LinkCache object.
* As normal, titles will go into the static Title cache field.
* This function *also* stores extra fields of the title used for link
* parsing to avoid extra DB queries.
*
* @param LinkCache $cache
* @param IResultWrapper $res
* @return int[] Array of remaining titles
*/
public function addResultToCache( $cache, $res ) {
if ( !$res ) {
return [];
}
// For each returned entry, add it to the list of good links, and remove it from $remaining
$this->pageIdentities ??= [];
$ids = [];
$remaining = $this->data;
foreach ( $res as $row ) {
try {
$title = new TitleValue( (int)$row->page_namespace, $row->page_title );
$cache->addGoodLinkObjFromRow( $title, $row );
$pdbk = $this->titleFormatter->getPrefixedDBkey( $title );
$ids[$pdbk] = $row->page_id;
$pageIdentity = new PageIdentityValue(
(int)$row->page_id,
(int)$row->page_namespace,
$row->page_title,
ProperPageIdentity::LOCAL
);
$key = CacheKeyHelper::getKeyForPage( $pageIdentity );
$this->pageIdentities[$key] = $pageIdentity;
} catch ( InvalidArgumentException $ex ) {
$this->logger->warning(
'Encountered invalid title',
[ 'title_namespace' => $row->page_namespace, 'title_dbkey' => $row->page_title ]
);
}
unset( $remaining[$row->page_namespace][$row->page_title] );
}
// The remaining links in $data are bad links, register them as such
foreach ( $remaining as $ns => $dbkeys ) {
foreach ( $dbkeys as $dbkey => $unused ) {
try {
$title = new TitleValue( (int)$ns, (string)$dbkey );
$cache->addBadLinkObj( $title );
$pdbk = $this->titleFormatter->getPrefixedDBkey( $title );
$ids[$pdbk] = 0;
$pageIdentity = new PageIdentityValue( 0, (int)$ns, $dbkey, ProperPageIdentity::LOCAL );
$key = CacheKeyHelper::getKeyForPage( $pageIdentity );
$this->pageIdentities[$key] = $pageIdentity;
} catch ( InvalidArgumentException $ex ) {
$this->logger->warning(
'Encountered invalid title',
[ 'title_namespace' => $ns, 'title_dbkey' => $dbkey ]
);
}
}
}
return $ids;
}
/**
* Perform the existence test query, return a result wrapper with page_id fields
* @return IResultWrapper|false
*/
public function doQuery() {
if ( $this->isEmpty() ) {
return false;
}
// This is similar to LinkHolderArray::replaceInternal
$dbr = $this->dbProvider->getReplicaDatabase();
$queryBuilder = $dbr->newSelectQueryBuilder()
->select( LinkCache::getSelectFields() )
->from( 'page' )
->where( $this->constructSet( 'page', $dbr ) );
$caller = __METHOD__;
if ( strval( $this->caller ) !== '' ) {
$caller .= " (for {$this->caller})";
}
return $queryBuilder->caller( $caller )->fetchResultSet();
}
/**
* Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
*
* @return bool Whether the query was successful
*/
public function doGenderQuery() {
if ( $this->isEmpty() || !$this->contentLanguage->needsGenderDistinction() ) {
return false;
}
$this->genderCache->doLinkBatch( $this->data, $this->caller );
return true;
}
/**
* Construct a WHERE clause which will match all the given titles.
*
* It is the caller's responsibility to only call this if the LinkBatch is
* not empty, because there is no safe way to represent a SQL conditional
* for the empty set.
*
* @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
* @param ISQLPlatform $db DB object to use
* @return string String with SQL where clause fragment
*/
public function constructSet( $prefix, $db ) {
if ( isset( $this->linksMigration::$prefixToTableMapping[$prefix] ) ) {
[ $blNamespace, $blTitle ] = $this->linksMigration->getTitleFields(
$this->linksMigration::$prefixToTableMapping[$prefix]
);
} else {
$blNamespace = "{$prefix}_namespace";
$blTitle = "{$prefix}_title";
}
return $db->makeWhereFrom2d( $this->data, $blNamespace, $blTitle );
}
}
/** @deprecated class alias since 1.42 */
class_alias( LinkBatch::class, 'LinkBatch' );
|