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
21 namespace MediaWiki\Linker
;
23 use InvalidArgumentException
;
24 use MediaWiki\Config\Config
;
25 use MediaWiki\MainConfigNames
;
28 * Service for compat reading of links tables
32 class LinksMigration
{
37 /** @var LinkTargetLookup */
38 private $linkTargetLookup;
41 public static $mapping = [
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' => [],
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
66 public static $prefixToTableMapping = [
67 'tl' => 'templatelinks',
72 * @param Config $config
73 * @param LinkTargetLookup $linktargetLookup
75 public function __construct( Config
$config, LinkTargetLookup
$linktargetLookup ) {
76 $this->config
= $config;
77 $this->linkTargetLookup
= $linktargetLookup;
81 * Return the conditions to be used in querying backlinks to a page
83 * @param string $table
84 * @param LinkTarget $linkTarget
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
96 self
::$mapping[$table]['target_id'] => $targetId,
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
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' ];
121 $tables = [ 'linktarget', $table ];
130 'joins' => [ $joinTable => [
132 [ "$targetId=lt_id" ]
138 self
::$mapping[$table]['ns'],
139 self
::$mapping[$table]['title']
141 'tables' => [ $table ],
147 public function getTitleFields( $table ) {
148 $this->assertMapping( $table );
150 if ( $this->isMigrationReadNew( $table ) ) {
151 return [ 'lt_namespace', 'lt_title' ];
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"