MediaWiki master
LinksMigration.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Linker;
8
9use InvalidArgumentException;
11
18
20 private $config;
21
23 private $linkTargetLookup;
24
26 public static $mapping = [
27 'templatelinks' => [
28 'page_id' => 'tl_from',
29 // Used by the updater only
30 'ns' => 'tl_namespace',
31 // Used by the updater only
32 'title' => 'tl_title',
33 'target_id' => 'tl_target_id',
34 ],
35 'pagelinks' => [
36 'page_id' => 'pl_from',
37 // Used by the updater only
38 'ns' => 'pl_namespace',
39 // Used by the updater only
40 'title' => 'pl_title',
41 'target_id' => 'pl_target_id',
42 ],
43 'categorylinks' => [
44 'page_id' => 'cl_from',
45 // Used by the updater only
46 'ns' => NS_CATEGORY,
47 // Used by the updater only
48 'title' => 'cl_to',
49 'target_id' => 'cl_target_id',
50 ],
51 'existencelinks' => [
52 'page_id' => 'exl_from',
53 'target_id' => 'exl_target_id',
54 ],
55 'imagelinks' => [
56 'page_id' => 'il_from',
57 // Used by the updater only
58 'ns' => NS_FILE,
59 // Used by the updater only
60 'title' => 'il_to',
61 'target_id' => 'il_target_id',
62 ],
63 ];
64
66 public static $prefixToTableMapping = [
67 'tl' => 'templatelinks',
68 'pl' => 'pagelinks',
69 'cl' => 'categorylinks',
70 'exl' => 'existencelinks',
71 'il' => 'imagelinks',
72 ];
73
74 public function __construct( Config $config, LinkTargetLookup $linktargetLookup ) {
75 $this->config = $config;
76 $this->linkTargetLookup = $linktargetLookup;
77 }
78
86 public function getLinksConditions( string $table, LinkTarget $linkTarget ): array {
87 $this->assertMapping( $table );
88 $targetId = $this->linkTargetLookup->getLinkTargetId( $linkTarget );
89 // Not found, it shouldn't pick anything
90 if ( !$targetId ) {
91 return [ '1=0' ];
92 }
93 return [ self::$mapping[$table]['target_id'] => $targetId ];
94 }
95
104 public function getQueryInfo( string $table, string $joinTable = 'linktarget', string $joinType = 'JOIN' ) {
105 $this->assertMapping( $table );
106 $targetId = self::$mapping[$table]['target_id'];
107 if ( $joinTable === 'linktarget' ) {
108 $tables = [ $table, 'linktarget' ];
109 } else {
110 $tables = [ 'linktarget', $table ];
111 }
112 return [
113 'tables' => $tables,
114 'fields' => [ $targetId, 'lt_namespace', 'lt_title' ],
115 'joins' => [ $joinTable => [ $joinType, [ "$targetId = lt_id" ] ] ],
116 ];
117 }
118
119 public function getTitleFields( string $table ): array {
120 $this->assertMapping( $table );
121
122 return [ 'lt_namespace', 'lt_title' ];
123 }
124
125 private function assertMapping( string $table ) {
126 if ( !isset( self::$mapping[$table] ) ) {
127 throw new InvalidArgumentException( "LinksMigration doesn't support the $table table yet" );
128 }
129 }
130}
const NS_FILE
Definition Defines.php:57
const NS_CATEGORY
Definition Defines.php:65
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.
Interface for configuration instances.
Definition Config.php:18
Represents the target of a wiki link.