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