| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Linker; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use MediaWiki\Config\Config; |
| 25 |
use MediaWiki\MainConfigNames; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class LinksMigration { |
| 33 |
|
| 34 |
|
| 35 |
private $config; |
| 36 |
|
| 37 |
|
| 38 |
private $linkTargetLookup; |
| 39 |
|
| 40 |
|
| 41 |
public static $mapping = [ |
| 42 |
'templatelinks' => [ |
| 43 |
'config' => -1, |
| 44 |
'page_id' => 'tl_from', |
| 45 |
|
| 46 |
'ns' => 'tl_namespace', |
| 47 |
|
| 48 |
'title' => 'tl_title', |
| 49 |
'target_id' => 'tl_target_id', |
| 50 |
'deprecated_configs' => [], |
| 51 |
], |
| 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 |
| 61 |
], |
| 62 |
], |
| 63 |
]; |
| 64 |
|
| 65 |
|
| 66 |
public static $prefixToTableMapping = [ |
| 67 |
'tl' => 'templatelinks', |
| 68 |
'pl' => 'pagelinks', |
| 69 |
]; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function __construct( Config $config, LinkTargetLookup $linktargetLookup ) { |
| 76 |
$this->config = $config; |
| 77 |
$this->linkTargetLookup = $linktargetLookup; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 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 |
|
| 92 |
if ( !$targetId ) { |
| 93 |
return [ '1=0' ]; |
| 94 |
} |
| 95 |
return [ |
| 96 |
self::$mapping[$table]['target_id'] => $targetId, |
| 97 |
]; |
| 98 |
} else { |
| 99 |
return [ |
| 100 |
self::$mapping[$table]['ns'] => $linkTarget->getNamespace(), |
| 101 |
self::$mapping[$table]['title'] => $linkTarget->getDBkey(), |
| 102 |
]; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 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 ]; |
| 122 |
} |
| 123 |
return [ |
| 124 |
'tables' => $tables, |
| 125 |
'fields' => [ |
| 126 |
$targetId, |
| 127 |
'lt_namespace', |
| 128 |
'lt_title' |
| 129 |
], |
| 130 |
'joins' => [ $joinTable => [ |
| 131 |
$joinType, |
| 132 |
[ "$targetId=lt_id" ] |
| 133 |
] ], |
| 134 |
]; |
| 135 |
} else { |
| 136 |
return [ |
| 137 |
'fields' => [ |
| 138 |
self::$mapping[$table]['ns'], |
| 139 |
self::$mapping[$table]['title'] |
| 140 |
], |
| 141 |
'tables' => [ $table ], |
| 142 |
'joins' => [], |
| 143 |
]; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
public function getTitleFields( $table ) { |
| 148 |
$this->assertMapping( $table ); |
| 149 |
|
| 150 |
if ( $this->isMigrationReadNew( $table ) ) { |
| 151 |
return [ 'lt_namespace', 'lt_title' ]; |
| 152 |
} else { |
| 153 |
return [ self::$mapping[$table]['ns'], self::$mapping[$table]['title'] ]; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 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; |
| 160 |
} |
| 161 |
|
| 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" |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 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" |
| 175 |
); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
} |
| 181 |
} |