MediaWiki master
LinksTableGroup.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
14use Wikimedia\ObjectFactory\ObjectFactory;
16
31 private const CORE_LIST = [
32 'categorylinks' => [
33 'class' => CategoryLinksTable::class,
34 'services' => [
35 'LanguageConverterFactory',
36 'NamespaceInfo',
37 'WikiPageFactory',
38 'DBLoadBalancer',
39 'MainWANObjectCache',
40 'MainConfig',
41 'JobQueueGroup',
42 'HookContainer',
43 ],
44 'needCollation' => true,
45 ],
46 'externallinks' => [
47 'class' => ExternalLinksTable::class,
48 ],
49 'existencelinks' => [
50 'class' => ExistenceLinksTable::class,
51 ],
52 'imagelinks' => [
53 'class' => ImageLinksTable::class,
54 ],
55 'iwlinks' => [
56 'class' => InterwikiLinksTable::class
57 ],
58 'langlinks' => [
59 'class' => LangLinksTable::class
60 ],
61 'pagelinks' => [
62 'class' => PageLinksTable::class,
63 ],
64 'page_props' => [
65 'class' => PagePropsTable::class,
66 'services' => [
67 'JobQueueGroup'
68 ],
69 'serviceOptions' => PagePropsTable::CONSTRUCTOR_OPTIONS
70 ],
71 'templatelinks' => [
72 'class' => TemplateLinksTable::class,
73 ]
74 ];
75
77 private $objectFactory;
78
80 private $lbFactory;
81
83 private $collationFactory;
84
86 private $page;
87
89 private $movedPage;
90
92 private $parserOutput;
93
95 private $linkTargetLookup;
96
98 private $batchSize;
99
101 private $ticket;
102
104 private $revision;
105
107 private $tables = [];
108
110 private $tempCollations;
111
121 public function __construct(
122 ObjectFactory $objectFactory,
123 LBFactory $lbFactory,
124 CollationFactory $collationFactory,
125 PageIdentity $page,
126 LinkTargetLookup $linkTargetLookup,
127 $batchSize,
128 array $tempCollations
129 ) {
130 $this->objectFactory = $objectFactory;
131 $this->lbFactory = $lbFactory;
132 $this->collationFactory = $collationFactory;
133 $this->page = $page;
134 $this->batchSize = $batchSize;
135 $this->linkTargetLookup = $linkTargetLookup;
136 $this->tempCollations = [];
137 foreach ( $tempCollations as $info ) {
138 $this->tempCollations[$info['table']] = $info;
139 }
140 }
141
145 public function setParserOutput( ParserOutput $parserOutput ) {
146 $this->parserOutput = $parserOutput;
147 foreach ( $this->tables as $table ) {
148 $table->setParserOutput( $parserOutput );
149 }
150 }
151
155 public function setMoveDetails( PageReference $oldPage ) {
156 $this->movedPage = $oldPage;
157 foreach ( $this->tables as $table ) {
158 $table->setMoveDetails( $oldPage );
159 }
160 }
161
167 public function setTransactionTicket( $ticket ) {
168 $this->ticket = $ticket;
169 foreach ( $this->tables as $table ) {
170 $table->setTransactionTicket( $ticket );
171 }
172 }
173
177 public function setRevision( RevisionRecord $revision ) {
178 $this->revision = $revision;
179 foreach ( $this->tables as $table ) {
180 $table->setRevision( $revision );
181 }
182 }
183
189 public function setStrictTestMode( $mode = true ) {
190 foreach ( $this->getAll() as $table ) {
191 $table->setStrictTestMode( $mode );
192 }
193 }
194
201 private function getSpec( $tableName ) {
202 if ( isset( self::CORE_LIST[$tableName] ) ) {
203 $spec = self::CORE_LIST[$tableName];
204 return $this->addCollationArgs( $spec, $tableName, false );
205 }
206 if ( isset( $this->tempCollations[$tableName] ) ) {
207 $info = $this->tempCollations[$tableName];
208 $spec = self::CORE_LIST['categorylinks'];
209 return $this->addCollationArgs( $spec, $tableName, true, $info );
210 }
211 throw new InvalidArgumentException(
212 __CLASS__ . ": unknown table name \"$tableName\"" );
213 }
214
224 private function addCollationArgs( $spec, $tableName, $isTempTable, $info = [] ) {
225 if ( isset( $spec['needCollation'] ) ) {
226 if ( isset( $info['collation'] ) ) {
227 $collation = $this->collationFactory->makeCollation( $info['collation'] );
228 $collationName = $info['fakeCollation'] ?? $info['collation'];
229 } else {
230 $collation = $this->collationFactory->getCategoryCollation();
231 $collationName = $this->collationFactory->getDefaultCollationName();
232 }
233 $spec['args'] = [
234 $collation,
235 $info['fakeCollation'] ?? $collationName,
236 $tableName,
237 $isTempTable
238 ];
239 unset( $spec['needCollation'] );
240 }
241 return $spec;
242 }
243
250 public function get( $tableName ) {
251 if ( !isset( $this->tables[$tableName] ) ) {
252 $spec = $this->getSpec( $tableName );
253 if ( isset( $spec['serviceOptions'] ) ) {
254 $config = MediaWikiServices::getInstance()->getMainConfig();
255 $extraArgs = [ new ServiceOptions( $spec['serviceOptions'], $config ) ];
256 unset( $spec['serviceOptions'] );
257 } else {
258 $extraArgs = [];
259 }
261 $table = $this->objectFactory->createObject(
262 $spec,
263 [ 'extraArgs' => $extraArgs, 'assertClass' => LinksTable::class ]
264 );
265 $table->injectBaseDependencies(
266 $this->lbFactory,
267 $this->linkTargetLookup,
268 $this->page,
269 $this->batchSize
270 );
271 if ( $this->parserOutput ) {
272 $table->setParserOutput( $this->parserOutput );
273 }
274 if ( $this->movedPage ) {
275 $table->setMoveDetails( $this->movedPage );
276 }
277 if ( $this->ticket ) {
278 $table->setTransactionTicket( $this->ticket );
279 }
280 if ( $this->revision ) {
281 $table->setRevision( $this->revision );
282 }
283 $this->tables[$tableName] = $table;
284 }
285 return $this->tables[$tableName];
286 }
287
292 public function getAll() {
293 foreach ( self::CORE_LIST as $tableName => $spec ) {
294 yield $this->get( $tableName );
295 }
296 foreach ( $this->tempCollations as $tableName => $collation ) {
297 yield $this->get( $tableName );
298 }
299 }
300}
Common factory to construct collation classes.
A class for passing options to services.
setParserOutput(ParserOutput $parserOutput)
Set the ParserOutput object to be used in new and existing objects.
setTransactionTicket( $ticket)
Set the transaction ticket to be used in new and existing objects.
getAll()
Get LinksTable objects for all known links tables.
__construct(ObjectFactory $objectFactory, LBFactory $lbFactory, CollationFactory $collationFactory, PageIdentity $page, LinkTargetLookup $linkTargetLookup, $batchSize, array $tempCollations)
setRevision(RevisionRecord $revision)
Set the revision to be used in new and existing objects.
setStrictTestMode( $mode=true)
Set the strict test mode.
setMoveDetails(PageReference $oldPage)
Set the original title in the case of a page move.
The base class for classes which update a single link table.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
ParserOutput is a rendering of a Content object or a message.
Page revision base class.
Interface for objects (potentially) representing an editable wiki page.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
if(!isset( $specs[$class])) $spec