MediaWiki  master
LinksMigration.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Linker;
22 
23 use Config;
24 use 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  ];
50 
51  public static $prefixToTableMapping = [
52  'tl' => 'templatelinks'
53  ];
54 
59  public function __construct( Config $config, LinkTargetLookup $linktargetLookup ) {
60  $this->config = $config;
61  $this->linkTargetLookup = $linktargetLookup;
62  }
63 
71  public function getLinksConditions( string $table, LinkTarget $linkTarget ): array {
72  $this->assertMapping( $table );
73  if ( $this->config->get( self::$mapping[$table]['config'] ) & SCHEMA_COMPAT_READ_NEW ) {
74  $targetId = $this->linkTargetLookup->getLinkTargetId( $linkTarget );
75  return [
76  self::$mapping[$table]['target_id'] => $targetId,
77  ];
78  } else {
79  return [
80  self::$mapping[$table]['ns'] => $linkTarget->getNamespace(),
81  self::$mapping[$table]['title'] => $linkTarget->getDBkey(),
82  ];
83  }
84  }
85 
94  public function getQueryInfo( string $table, string $joinTable = 'linktarget', string $joinType = 'JOIN' ) {
95  $this->assertMapping( $table );
96  if ( $this->config->get( self::$mapping[$table]['config'] ) & SCHEMA_COMPAT_READ_NEW ) {
97  $targetId = self::$mapping[$table]['target_id'];
98  if ( $joinTable === 'linktarget' ) {
99  $tables = [ $table, 'linktarget' ];
100  } else {
101  $tables = [ 'linktarget', $table ];
102  }
103  return [
104  'tables' => $tables,
105  'fields' => [
106  $targetId,
107  'lt_namespace',
108  'lt_title'
109  ],
110  'joins' => [ $joinTable => [
111  $joinType,
112  [ "$targetId=lt_id" ]
113  ] ],
114  ];
115  } else {
116  return [
117  'fields' => [
118  self::$mapping[$table]['ns'],
119  self::$mapping[$table]['title']
120  ],
121  'tables' => [ $table ],
122  'joins' => [],
123  ];
124  }
125  }
126 
127  public function getTitleFields( $table ) {
128  $this->assertMapping( $table );
129 
130  if ( $this->config->get( self::$mapping[$table]['config'] ) & SCHEMA_COMPAT_READ_NEW ) {
131  return [ 'lt_namespace', 'lt_title' ];
132  } else {
133  return [ self::$mapping[$table]['ns'], self::$mapping[$table]['title'] ];
134  }
135  }
136 
137  private function assertMapping( string $table ) {
138  if ( !isset( self::$mapping[$table] ) ) {
139  throw new InvalidArgumentException(
140  "LinksMigration doesn't support the $table table yet"
141  );
142  }
143 
144  $config = $this->config->get( self::$mapping[$table]['config'] );
145  if ( in_array( $config, self::$mapping[$table]['deprecated_configs'] ) ) {
146  throw new InvalidArgumentException(
147  "LinksMigration config $config on $table table is not supported anymore"
148  );
149  }
150  }
151 }
const SCHEMA_COMPAT_OLD
Definition: Defines.php:277
const SCHEMA_COMPAT_READ_NEW
Definition: Defines.php:270
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 TemplateLinksSchemaMigrationStage
Name constant for the TemplateLinksSchemaMigrationStage setting, for use with Config::get()
Interface for configuration instances.
Definition: Config.php:30
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
getNamespace()
Get the namespace index.
getDBkey()
Get the main part of the link target, in canonical database form.