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 ],
42 'needCollation' => true,
43 ],
44 'externallinks' => [
45 'class' => ExternalLinksTable::class,
46 ],
47 'imagelinks' => [
48 'class' => ImageLinksTable::class
49 ],
50 'iwlinks' => [
51 'class' => InterwikiLinksTable::class
52 ],
53 'langlinks' => [
54 'class' => LangLinksTable::class
55 ],
56 'pagelinks' => [
57 'class' => PageLinksTable::class,
58 'services' => [
59 'MainConfig'
60 ],
61 ],
62 'page_props' => [
63 'class' => PagePropsTable::class,
64 'services' => [
65 'JobQueueGroup'
66 ],
67 'serviceOptions' => PagePropsTable::CONSTRUCTOR_OPTIONS
68 ],
69 'templatelinks' => [
70 'class' => TemplateLinksTable::class,
71 ]
72 ];
73
75 private $objectFactory;
76
78 private $lbFactory;
79
81 private $collationFactory;
82
84 private $page;
85
87 private $movedPage;
88
90 private $parserOutput;
91
93 private $linkTargetLookup;
94
96 private $batchSize;
97
99 private $ticket;
100
102 private $revision;
103
105 private $tables = [];
106
108 private $tempCollations;
109
119 public function __construct(
120 ObjectFactory $objectFactory,
121 LBFactory $lbFactory,
122 CollationFactory $collationFactory,
123 PageIdentity $page,
124 LinkTargetLookup $linkTargetLookup,
125 $batchSize,
126 array $tempCollations
127 ) {
128 $this->objectFactory = $objectFactory;
129 $this->lbFactory = $lbFactory;
130 $this->collationFactory = $collationFactory;
131 $this->page = $page;
132 $this->batchSize = $batchSize;
133 $this->linkTargetLookup = $linkTargetLookup;
134 $this->tempCollations = [];
135 foreach ( $tempCollations as $info ) {
136 $this->tempCollations[$info['table']] = $info;
137 }
138 }
139
143 public function setParserOutput( ParserOutput $parserOutput ) {
144 $this->parserOutput = $parserOutput;
145 foreach ( $this->tables as $table ) {
146 $table->setParserOutput( $parserOutput );
147 }
148 }
149
153 public function setMoveDetails( PageReference $oldPage ) {
154 $this->movedPage = $oldPage;
155 foreach ( $this->tables as $table ) {
156 $table->setMoveDetails( $oldPage );
157 }
158 }
159
165 public function setTransactionTicket( $ticket ) {
166 $this->ticket = $ticket;
167 foreach ( $this->tables as $table ) {
168 $table->setTransactionTicket( $ticket );
169 }
170 }
171
175 public function setRevision( RevisionRecord $revision ) {
176 $this->revision = $revision;
177 foreach ( $this->tables as $table ) {
178 $table->setRevision( $revision );
179 }
180 }
181
187 public function setStrictTestMode( $mode = true ) {
188 foreach ( $this->getAll() as $table ) {
189 $table->setStrictTestMode( $mode );
190 }
191 }
192
199 private function getSpec( $tableName ) {
200 if ( isset( self::CORE_LIST[$tableName] ) ) {
201 $spec = self::CORE_LIST[$tableName];
202 return $this->addCollationArgs( $spec, $tableName, false );
203 }
204 if ( isset( $this->tempCollations[$tableName] ) ) {
205 $info = $this->tempCollations[$tableName];
206 $spec = self::CORE_LIST['categorylinks'];
207 return $this->addCollationArgs( $spec, $tableName, true, $info );
208 }
209 throw new InvalidArgumentException(
210 __CLASS__ . ": unknown table name \"$tableName\"" );
211 }
212
222 private function addCollationArgs( $spec, $tableName, $isTempTable, $info = [] ) {
223 if ( isset( $spec['needCollation'] ) ) {
224 if ( isset( $info['collation'] ) ) {
225 $collation = $this->collationFactory->makeCollation( $info['collation'] );
226 $collationName = $info['fakeCollation'] ?? $info['collation'];
227 } else {
228 $collation = $this->collationFactory->getCategoryCollation();
229 $collationName = $this->collationFactory->getDefaultCollationName();
230 }
231 $spec['args'] = [
232 $collation,
233 $info['fakeCollation'] ?? $collationName,
234 $tableName,
235 $isTempTable
236 ];
237 unset( $spec['needCollation'] );
238 }
239 return $spec;
240 }
241
248 public function get( $tableName ) {
249 if ( !isset( $this->tables[$tableName] ) ) {
250 $spec = $this->getSpec( $tableName );
251 if ( isset( $spec['serviceOptions'] ) ) {
252 $config = MediaWikiServices::getInstance()->getMainConfig();
253 $extraArgs = [ new ServiceOptions( $spec['serviceOptions'], $config ) ];
254 unset( $spec['serviceOptions'] );
255 } else {
256 $extraArgs = [];
257 }
259 $table = $this->objectFactory->createObject( $spec, [ 'extraArgs' => $extraArgs ] );
260 $table->injectBaseDependencies(
261 $this->lbFactory,
262 $this->linkTargetLookup,
263 $this->page,
264 $this->batchSize
265 );
266 if ( $this->parserOutput ) {
267 $table->setParserOutput( $this->parserOutput );
268 }
269 if ( $this->movedPage ) {
270 $table->setMoveDetails( $this->movedPage );
271 }
272 if ( $this->ticket ) {
273 $table->setTransactionTicket( $this->ticket );
274 }
275 if ( $this->revision ) {
276 $table->setRevision( $this->revision );
277 }
278 $this->tables[$tableName] = $table;
279 }
280 return $this->tables[$tableName];
281 }
282
287 public function getAll() {
288 foreach ( self::CORE_LIST as $tableName => $spec ) {
289 yield $this->get( $tableName );
290 }
291 foreach ( $this->tempCollations as $tableName => $collation ) {
292 yield $this->get( $tableName );
293 }
294 }
295}
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.