File: LinksTableGroup.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (298 lines) | stat: -rw-r--r-- 7,576 bytes parent folder | download
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
<?php

namespace MediaWiki\Deferred\LinksUpdate;

use InvalidArgumentException;
use MediaWiki\Collation\CollationFactory;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Linker\LinkTargetLookup;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Page\PageReference;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Revision\RevisionRecord;
use Wikimedia\ObjectFactory\ObjectFactory;
use Wikimedia\Rdbms\LBFactory;

/**
 * @since 1.38
 */
class LinksTableGroup {
	/**
	 * ObjectFactory specifications for the subclasses. The following
	 * additional keys are defined:
	 *
	 *   - serviceOptions: An array of configuration variable names. If this is
	 *     set, the specified configuration will be sent to the subclass
	 *     constructor as a ServiceOptions object.
	 *   - needCollation: If true, the following additional args will be added:
	 *     Collation, collation name and table name.
	 */
	private const CORE_LIST = [
		'categorylinks' => [
			'class' => CategoryLinksTable::class,
			'services' => [
				'LanguageConverterFactory',
				'NamespaceInfo',
				'WikiPageFactory'
			],
			'needCollation' => true,
		],
		'externallinks' => [
			'class' => ExternalLinksTable::class,
		],
		'imagelinks' => [
			'class' => ImageLinksTable::class
		],
		'iwlinks' => [
			'class' => InterwikiLinksTable::class
		],
		'langlinks' => [
			'class' => LangLinksTable::class
		],
		'pagelinks' => [
			'class' => PageLinksTable::class,
			'services' => [
				'MainConfig'
			],
		],
		'page_props' => [
			'class' => PagePropsTable::class,
			'services' => [
				'JobQueueGroup'
			],
			'serviceOptions' => PagePropsTable::CONSTRUCTOR_OPTIONS
		],
		'templatelinks' => [
			'class' => TemplateLinksTable::class,
		]
	];

	/** @var ObjectFactory */
	private $objectFactory;

	/** @var LBFactory */
	private $lbFactory;

	/** @var CollationFactory */
	private $collationFactory;

	/** @var PageIdentity */
	private $page;

	/** @var PageReference|null */
	private $movedPage;

	/** @var ParserOutput|null */
	private $parserOutput;

	/** @var LinkTargetLookup */
	private $linkTargetLookup;

	/** @var int */
	private $batchSize;

	/** @var mixed */
	private $ticket;

	/** @var RevisionRecord|null */
	private $revision;

	/** @var LinksTable[] */
	private $tables = [];

	/** @var array */
	private $tempCollations;

	/**
	 * @param ObjectFactory $objectFactory
	 * @param LBFactory $lbFactory
	 * @param CollationFactory $collationFactory
	 * @param PageIdentity $page
	 * @param LinkTargetLookup $linkTargetLookup
	 * @param int $batchSize
	 * @param array $tempCollations
	 */
	public function __construct(
		ObjectFactory $objectFactory,
		LBFactory $lbFactory,
		CollationFactory $collationFactory,
		PageIdentity $page,
		LinkTargetLookup $linkTargetLookup,
		$batchSize,
		array $tempCollations
	) {
		$this->objectFactory = $objectFactory;
		$this->lbFactory = $lbFactory;
		$this->collationFactory = $collationFactory;
		$this->page = $page;
		$this->batchSize = $batchSize;
		$this->linkTargetLookup = $linkTargetLookup;
		$this->tempCollations = [];
		foreach ( $tempCollations as $info ) {
			$this->tempCollations[$info['table']] = $info;
		}
	}

	/**
	 * Set the ParserOutput object to be used in new and existing objects.
	 *
	 * @param ParserOutput $parserOutput
	 */
	public function setParserOutput( ParserOutput $parserOutput ) {
		$this->parserOutput = $parserOutput;
		foreach ( $this->tables as $table ) {
			$table->setParserOutput( $parserOutput );
		}
	}

	/**
	 * Set the original title in the case of a page move.
	 *
	 * @param PageReference $oldPage
	 */
	public function setMoveDetails( PageReference $oldPage ) {
		$this->movedPage = $oldPage;
		foreach ( $this->tables as $table ) {
			$table->setMoveDetails( $oldPage );
		}
	}

	/**
	 * Set the transaction ticket to be used in new and existing objects.
	 *
	 * @param mixed $ticket
	 */
	public function setTransactionTicket( $ticket ) {
		$this->ticket = $ticket;
		foreach ( $this->tables as $table ) {
			$table->setTransactionTicket( $ticket );
		}
	}

	/**
	 * Set the revision to be used in new and existing objects.
	 *
	 * @param RevisionRecord $revision
	 */
	public function setRevision( RevisionRecord $revision ) {
		$this->revision = $revision;
		foreach ( $this->tables as $table ) {
			$table->setRevision( $revision );
		}
	}

	/**
	 * Set the strict test mode
	 *
	 * @param bool $mode
	 */
	public function setStrictTestMode( $mode = true ) {
		foreach ( $this->getAll() as $table ) {
			$table->setStrictTestMode( $mode );
		}
	}

	/**
	 * Get the spec array for a given table.
	 *
	 * @param string $tableName
	 * @return array
	 */
	private function getSpec( $tableName ) {
		if ( isset( self::CORE_LIST[$tableName] ) ) {
			$spec = self::CORE_LIST[$tableName];
			return $this->addCollationArgs( $spec, $tableName, false );
		}
		if ( isset( $this->tempCollations[$tableName] ) ) {
			$info = $this->tempCollations[$tableName];
			$spec = self::CORE_LIST['categorylinks'];
			return $this->addCollationArgs( $spec, $tableName, true, $info );
		}
		throw new InvalidArgumentException(
			__CLASS__ . ": unknown table name \"$tableName\"" );
	}

	/**
	 * Add extra args to the spec of a table that needs collation information
	 *
	 * @param array $spec
	 * @param string $tableName
	 * @param bool $isTempTable
	 * @param array $info Temporary collation info
	 * @return array ObjectFactory spec
	 */
	private function addCollationArgs( $spec, $tableName, $isTempTable, $info = [] ) {
		if ( isset( $spec['needCollation'] ) ) {
			if ( isset( $info['collation'] ) ) {
				$collation = $this->collationFactory->makeCollation( $info['collation'] );
				$collationName = $info['fakeCollation'] ?? $info['collation'];
			} else {
				$collation = $this->collationFactory->getCategoryCollation();
				$collationName = $this->collationFactory->getDefaultCollationName();
			}
			$spec['args'] = [
				$collation,
				$info['fakeCollation'] ?? $collationName,
				$tableName,
				$isTempTable
			];
			unset( $spec['needCollation'] );
		}
		return $spec;
	}

	/**
	 * Get a LinksTable for a given table.
	 *
	 * @param string $tableName
	 * @return LinksTable
	 */
	public function get( $tableName ) {
		if ( !isset( $this->tables[$tableName] ) ) {
			$spec = $this->getSpec( $tableName );
			if ( isset( $spec['serviceOptions'] ) ) {
				$config = MediaWikiServices::getInstance()->getMainConfig();
				$extraArgs = [ new ServiceOptions( $spec['serviceOptions'], $config ) ];
				unset( $spec['serviceOptions'] );
			} else {
				$extraArgs = [];
			}
			/** @var LinksTable $table */
			$table = $this->objectFactory->createObject( $spec, [ 'extraArgs' => $extraArgs ] );
			$table->injectBaseDependencies(
				$this->lbFactory,
				$this->linkTargetLookup,
				$this->page,
				$this->batchSize
			);
			if ( $this->parserOutput ) {
				$table->setParserOutput( $this->parserOutput );
			}
			if ( $this->movedPage ) {
				$table->setMoveDetails( $this->movedPage );
			}
			if ( $this->ticket ) {
				$table->setTransactionTicket( $this->ticket );
			}
			if ( $this->revision ) {
				$table->setRevision( $this->revision );
			}
			$this->tables[$tableName] = $table;
		}
		return $this->tables[$tableName];
	}

	/**
	 * Get LinksTable objects for all known links tables.
	 * @return iterable<LinksTable>
	 */
	public function getAll() {
		foreach ( self::CORE_LIST as $tableName => $spec ) {
			yield $this->get( $tableName );
		}
		foreach ( $this->tempCollations as $tableName => $collation ) {
			yield $this->get( $tableName );
		}
	}
}