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  // Not found, it shouldn't pick anything
76  if ( !$targetId ) {
77  return [ '1=0' ];
78  }
79  return [
80  self::$mapping[$table]['target_id'] => $targetId,
81  ];
82  } else {
83  return [
84  self::$mapping[$table]['ns'] => $linkTarget->getNamespace(),
85  self::$mapping[$table]['title'] => $linkTarget->getDBkey(),
86  ];
87  }
88  }
89 
98  public function getQueryInfo( string $table, string $joinTable = 'linktarget', string $joinType = 'JOIN' ) {
99  $this->assertMapping( $table );
100  if ( $this->config->get( self::$mapping[$table]['config'] ) & SCHEMA_COMPAT_READ_NEW ) {
101  $targetId = self::$mapping[$table]['target_id'];
102  if ( $joinTable === 'linktarget' ) {
103  $tables = [ $table, 'linktarget' ];
104  } else {
105  $tables = [ 'linktarget', $table ];
106  }
107  return [
108  'tables' => $tables,
109  'fields' => [
110  $targetId,
111  'lt_namespace',
112  'lt_title'
113  ],
114  'joins' => [ $joinTable => [
115  $joinType,
116  [ "$targetId=lt_id" ]
117  ] ],
118  ];
119  } else {
120  return [
121  'fields' => [
122  self::$mapping[$table]['ns'],
123  self::$mapping[$table]['title']
124  ],
125  'tables' => [ $table ],
126  'joins' => [],
127  ];
128  }
129  }
130 
131  public function getTitleFields( $table ) {
132  $this->assertMapping( $table );
133 
134  if ( $this->config->get( self::$mapping[$table]['config'] ) & SCHEMA_COMPAT_READ_NEW ) {
135  return [ 'lt_namespace', 'lt_title' ];
136  } else {
137  return [ self::$mapping[$table]['ns'], self::$mapping[$table]['title'] ];
138  }
139  }
140 
141  private function assertMapping( string $table ) {
142  if ( !isset( self::$mapping[$table] ) ) {
143  throw new InvalidArgumentException(
144  "LinksMigration doesn't support the $table table yet"
145  );
146  }
147 
148  $config = $this->config->get( self::$mapping[$table]['config'] );
149  if ( in_array( $config, self::$mapping[$table]['deprecated_configs'] ) ) {
150  throw new InvalidArgumentException(
151  "LinksMigration config $config on $table table is not supported anymore"
152  );
153  }
154  }
155 }
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.