Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / linker / LinksMigration.php
blob6d6ea16b438b91f189467f3a7e5f278170795540
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Linker;
23 use InvalidArgumentException;
24 use MediaWiki\Config\Config;
25 use MediaWiki\MainConfigNames;
27 /**
28 * Service for compat reading of links tables
30 * @since 1.39
32 class LinksMigration {
34 /** @var Config */
35 private $config;
37 /** @var LinkTargetLookup */
38 private $linkTargetLookup;
40 /** @var array[] */
41 public static $mapping = [
42 'templatelinks' => [
43 'config' => -1,
44 'page_id' => 'tl_from',
45 // Used by the updater only
46 'ns' => 'tl_namespace',
47 // Used by the updater only
48 'title' => 'tl_title',
49 'target_id' => 'tl_target_id',
50 'deprecated_configs' => [],
52 'pagelinks' => [
53 'config' => MainConfigNames::PageLinksSchemaMigrationStage,
54 'page_id' => 'pl_from',
55 'ns' => 'pl_namespace',
56 'title' => 'pl_title',
57 'target_id' => 'pl_target_id',
58 'deprecated_configs' => [
59 SCHEMA_COMPAT_WRITE_OLD,
60 SCHEMA_COMPAT_READ_OLD
65 /** @var string[] */
66 public static $prefixToTableMapping = [
67 'tl' => 'templatelinks',
68 'pl' => 'pagelinks',
71 /**
72 * @param Config $config
73 * @param LinkTargetLookup $linktargetLookup
75 public function __construct( Config $config, LinkTargetLookup $linktargetLookup ) {
76 $this->config = $config;
77 $this->linkTargetLookup = $linktargetLookup;
80 /**
81 * Return the conditions to be used in querying backlinks to a page
83 * @param string $table
84 * @param LinkTarget $linkTarget
85 * @return array
87 public function getLinksConditions( string $table, LinkTarget $linkTarget ): array {
88 $this->assertMapping( $table );
89 if ( $this->isMigrationReadNew( $table ) ) {
90 $targetId = $this->linkTargetLookup->getLinkTargetId( $linkTarget );
91 // Not found, it shouldn't pick anything
92 if ( !$targetId ) {
93 return [ '1=0' ];
95 return [
96 self::$mapping[$table]['target_id'] => $targetId,
98 } else {
99 return [
100 self::$mapping[$table]['ns'] => $linkTarget->getNamespace(),
101 self::$mapping[$table]['title'] => $linkTarget->getDBkey(),
107 * Return the query to be used when you want to or from a group of pages
109 * @param string $table
110 * @param string $joinTable table to end the join chain. Most of the time it's linktarget
111 * @param string $joinType
112 * @return array
114 public function getQueryInfo( string $table, string $joinTable = 'linktarget', string $joinType = 'JOIN' ) {
115 $this->assertMapping( $table );
116 if ( $this->isMigrationReadNew( $table ) ) {
117 $targetId = self::$mapping[$table]['target_id'];
118 if ( $joinTable === 'linktarget' ) {
119 $tables = [ $table, 'linktarget' ];
120 } else {
121 $tables = [ 'linktarget', $table ];
123 return [
124 'tables' => $tables,
125 'fields' => [
126 $targetId,
127 'lt_namespace',
128 'lt_title'
130 'joins' => [ $joinTable => [
131 $joinType,
132 [ "$targetId=lt_id" ]
133 ] ],
135 } else {
136 return [
137 'fields' => [
138 self::$mapping[$table]['ns'],
139 self::$mapping[$table]['title']
141 'tables' => [ $table ],
142 'joins' => [],
147 public function getTitleFields( $table ) {
148 $this->assertMapping( $table );
150 if ( $this->isMigrationReadNew( $table ) ) {
151 return [ 'lt_namespace', 'lt_title' ];
152 } else {
153 return [ self::$mapping[$table]['ns'], self::$mapping[$table]['title'] ];
157 private function isMigrationReadNew( string $table ): bool {
158 return self::$mapping[$table]['config'] === -1 ||
159 $this->config->get( self::$mapping[$table]['config'] ) & SCHEMA_COMPAT_READ_NEW;
162 private function assertMapping( string $table ) {
163 if ( !isset( self::$mapping[$table] ) ) {
164 throw new InvalidArgumentException(
165 "LinksMigration doesn't support the $table table yet"
169 if ( self::$mapping[$table]['config'] !== -1 && self::$mapping[$table]['deprecated_configs'] ) {
170 $config = $this->config->get( self::$mapping[$table]['config'] );
171 foreach ( self::$mapping[$table]['deprecated_configs'] as $deprecatedConfig ) {
172 if ( $config & $deprecatedConfig ) {
173 throw new InvalidArgumentException(
174 "LinksMigration config $config on $table table is not supported anymore"