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 
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  ],
48  ];
49 
50  public static $prefixToTableMapping = [
51  'tl' => 'templatelinks'
52  ];
53 
58  public function __construct( Config $config, LinkTargetLookup $linktargetLookup ) {
59  $this->config = $config;
60  $this->linkTargetLookup = $linktargetLookup;
61  }
62 
70  public function getLinksConditions( string $table, LinkTarget $linkTarget ): array {
71  $this->assertMapping( $table );
72  if ( $this->config->get( self::$mapping[$table]['config'] ) & SCHEMA_COMPAT_READ_NEW ) {
73  $targetId = $this->linkTargetLookup->getLinkTargetId( $linkTarget );
74  return [
75  self::$mapping[$table]['target_id'] => $targetId,
76  ];
77  } else {
78  return [
79  self::$mapping[$table]['ns'] => $linkTarget->getNamespace(),
80  self::$mapping[$table]['title'] => $linkTarget->getDBkey(),
81  ];
82  }
83  }
84 
93  public function getQueryInfo( string $table, string $joinTable = 'linktarget', string $joinType = 'JOIN' ) {
94  $this->assertMapping( $table );
95  if ( $this->config->get( self::$mapping[$table]['config'] ) & SCHEMA_COMPAT_READ_NEW ) {
96  $targetId = self::$mapping[$table]['target_id'];
97  if ( $joinTable === 'linktarget' ) {
98  $tables = [ $table, 'linktarget' ];
99  } else {
100  $tables = [ 'linktarget', $table ];
101  }
102  return [
103  'tables' => $tables,
104  'fields' => [
105  $targetId,
106  'lt_namespace',
107  'lt_title'
108  ],
109  'joins' => [ $joinTable => [
110  $joinType,
111  [ "$targetId=lt_id" ]
112  ] ],
113  ];
114  } else {
115  return [
116  'fields' => [
117  self::$mapping[$table]['ns'],
118  self::$mapping[$table]['title']
119  ],
120  'tables' => [ $table ],
121  'joins' => [],
122  ];
123  }
124  }
125 
126  public function getTitleFields( $table ) {
127  $this->assertMapping( $table );
128 
129  if ( $this->config->get( self::$mapping[$table]['config'] ) & SCHEMA_COMPAT_READ_NEW ) {
130  return [ 'lt_namespace', 'lt_title' ];
131  } else {
132  return [ self::$mapping[$table]['ns'], self::$mapping[$table]['title'] ];
133  }
134  }
135 
136  private function assertMapping( string $table ) {
137  if ( !isset( self::$mapping[$table] ) ) {
138  throw new InvalidArgumentException(
139  "LinksMigration doesn't support the $table table yet"
140  );
141  }
142  }
143 }
const SCHEMA_COMPAT_READ_NEW
Definition: Defines.php:269
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
getNamespace()
Get the namespace index.
getDBkey()
Get the main part of the link target, in canonical database form.