MediaWiki master
LinksMigration.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Linker;
22
23use InvalidArgumentException;
26
33
35 private $config;
36
38 private $linkTargetLookup;
39
40 public static $mapping = [
41 'templatelinks' => [
43 'page_id' => 'tl_from',
44 'ns' => 'tl_namespace',
45 'title' => 'tl_title',
46 'target_id' => 'tl_target_id',
47 'deprecated_configs' => [ SCHEMA_COMPAT_OLD ],
48 ],
49 'pagelinks' => [
51 'page_id' => 'pl_from',
52 'ns' => 'pl_namespace',
53 'title' => 'pl_title',
54 'target_id' => 'pl_target_id',
55 'deprecated_configs' => [],
56 ],
57 ];
58
59 public static $prefixToTableMapping = [
60 'tl' => 'templatelinks',
61 'pl' => 'pagelinks',
62 ];
63
68 public function __construct( Config $config, LinkTargetLookup $linktargetLookup ) {
69 $this->config = $config;
70 $this->linkTargetLookup = $linktargetLookup;
71 }
72
80 public function getLinksConditions( string $table, LinkTarget $linkTarget ): array {
81 $this->assertMapping( $table );
82 if ( $this->config->get( self::$mapping[$table]['config'] ) & SCHEMA_COMPAT_READ_NEW ) {
83 $targetId = $this->linkTargetLookup->getLinkTargetId( $linkTarget );
84 // Not found, it shouldn't pick anything
85 if ( !$targetId ) {
86 return [ '1=0' ];
87 }
88 return [
89 self::$mapping[$table]['target_id'] => $targetId,
90 ];
91 } else {
92 return [
93 self::$mapping[$table]['ns'] => $linkTarget->getNamespace(),
94 self::$mapping[$table]['title'] => $linkTarget->getDBkey(),
95 ];
96 }
97 }
98
107 public function getQueryInfo( string $table, string $joinTable = 'linktarget', string $joinType = 'JOIN' ) {
108 $this->assertMapping( $table );
109 if ( $this->config->get( self::$mapping[$table]['config'] ) & SCHEMA_COMPAT_READ_NEW ) {
110 $targetId = self::$mapping[$table]['target_id'];
111 if ( $joinTable === 'linktarget' ) {
112 $tables = [ $table, 'linktarget' ];
113 } else {
114 $tables = [ 'linktarget', $table ];
115 }
116 return [
117 'tables' => $tables,
118 'fields' => [
119 $targetId,
120 'lt_namespace',
121 'lt_title'
122 ],
123 'joins' => [ $joinTable => [
124 $joinType,
125 [ "$targetId=lt_id" ]
126 ] ],
127 ];
128 } else {
129 return [
130 'fields' => [
131 self::$mapping[$table]['ns'],
132 self::$mapping[$table]['title']
133 ],
134 'tables' => [ $table ],
135 'joins' => [],
136 ];
137 }
138 }
139
140 public function getTitleFields( $table ) {
141 $this->assertMapping( $table );
142
143 if ( $this->config->get( self::$mapping[$table]['config'] ) & SCHEMA_COMPAT_READ_NEW ) {
144 return [ 'lt_namespace', 'lt_title' ];
145 } else {
146 return [ self::$mapping[$table]['ns'], self::$mapping[$table]['title'] ];
147 }
148 }
149
150 private function assertMapping( string $table ) {
151 if ( !isset( self::$mapping[$table] ) ) {
152 throw new InvalidArgumentException(
153 "LinksMigration doesn't support the $table table yet"
154 );
155 }
156
157 $config = $this->config->get( self::$mapping[$table]['config'] );
158 if ( in_array( $config, self::$mapping[$table]['deprecated_configs'] ) ) {
159 throw new InvalidArgumentException(
160 "LinksMigration config $config on $table table is not supported anymore"
161 );
162 }
163 }
164}
const SCHEMA_COMPAT_OLD
Definition Defines.php:286
const SCHEMA_COMPAT_READ_NEW
Definition Defines.php:279
Service for compat reading of links tables.
__construct(Config $config, LinkTargetLookup $linktargetLookup)
getLinksConditions(string $table, LinkTarget $linkTarget)
Return the conditions to be used in querying backlinks to a page.
getQueryInfo(string $table, string $joinTable='linktarget', string $joinType='JOIN')
Return the query to be used when you want to or from a group of pages.
A class containing constants representing the names of configuration variables.
const PageLinksSchemaMigrationStage
Name constant for the PageLinksSchemaMigrationStage setting, for use with Config::get()
const TemplateLinksSchemaMigrationStage
Name constant for the TemplateLinksSchemaMigrationStage setting, for use with Config::get()
Interface for configuration instances.
Definition Config.php:32
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
Represents the target of a wiki link.
getNamespace()
Get the namespace index.
getDBkey()
Get the main part of the link target, in canonical database form.