MediaWiki REL1_40
DatabaseDomain.php
Go to the documentation of this file.
1<?php
20namespace Wikimedia\Rdbms;
21
22use InvalidArgumentException;
23
43 private $database;
45 private $schema;
47 private $prefix;
48
50 private $equivalentString;
51
57 public function __construct( $database, $schema, $prefix ) {
58 if ( $database !== null && ( !is_string( $database ) || $database === '' ) ) {
59 throw new InvalidArgumentException( 'Database must be null or a non-empty string.' );
60 }
61 $this->database = $database;
62 if ( $schema !== null && ( !is_string( $schema ) || $schema === '' ) ) {
63 throw new InvalidArgumentException( 'Schema must be null or a non-empty string.' );
64 } elseif ( $database === null && $schema !== null ) {
65 throw new InvalidArgumentException( 'Schema must be null if database is null.' );
66 }
67 $this->schema = $schema;
68 if ( !is_string( $prefix ) ) {
69 throw new InvalidArgumentException( "Prefix must be a string." );
70 }
71 $this->prefix = $prefix;
72 }
73
78 public static function newFromId( $domain ): self {
79 if ( $domain instanceof self ) {
80 return $domain;
81 }
82
83 if ( !is_string( $domain ) ) {
84 throw new InvalidArgumentException( "Domain must be a string or " . __CLASS__ );
85 }
86
87 $parts = array_map( [ __CLASS__, 'decode' ], explode( '-', $domain ) );
88
89 $schema = null;
90 $prefix = '';
91
92 if ( count( $parts ) == 1 ) {
93 $database = $parts[0];
94 } elseif ( count( $parts ) == 2 ) {
95 [ $database, $prefix ] = $parts;
96 } elseif ( count( $parts ) == 3 ) {
97 [ $database, $schema, $prefix ] = $parts;
98 } else {
99 throw new InvalidArgumentException( "Domain '$domain' has too few or too many parts." );
100 }
101
102 if ( $database === '' ) {
103 $database = null;
104 }
105
106 if ( $schema === '' ) {
107 $schema = null;
108 }
109
110 $instance = new self( $database, $schema, $prefix );
111 $instance->equivalentString = $domain;
112
113 return $instance;
114 }
115
119 public static function newUnspecified() {
120 return new self( null, null, '' );
121 }
122
127 public function equals( $other ) {
128 if ( $other instanceof self ) {
129 return (
130 $this->database === $other->database &&
131 $this->schema === $other->schema &&
132 $this->prefix === $other->prefix
133 );
134 }
135
136 return ( $this->getId() === $other );
137 }
138
153 public function isCompatible( $other ) {
154 if ( $this->isUnspecified() ) {
155 return true; // even the prefix doesn't matter
156 }
157
158 $other = self::newFromId( $other );
159
160 return (
161 ( $this->database === $other->database || $this->database === null ) &&
162 ( $this->schema === $other->schema || $this->schema === null ) &&
163 $this->prefix === $other->prefix
164 );
165 }
166
171 public function isUnspecified() {
172 return (
173 $this->database === null && $this->schema === null && $this->prefix === ''
174 );
175 }
176
180 public function getDatabase() {
181 return $this->database;
182 }
183
187 public function getSchema() {
188 return $this->schema;
189 }
190
194 public function getTablePrefix() {
195 return $this->prefix;
196 }
197
201 public function getId(): string {
202 $this->equivalentString ??= $this->convertToString();
203
204 return $this->equivalentString;
205 }
206
210 private function convertToString(): string {
211 $parts = [ (string)$this->database ];
212 if ( $this->schema !== null ) {
213 $parts[] = $this->schema;
214 }
215 if ( $this->prefix != '' || $this->schema !== null ) {
216 // If there is a schema, then we need the prefix to disambiguate.
217 // For engines like Postgres that use schemas, this awkwardness is hopefully
218 // avoided since it is easy to have one DB per server (to avoid having many users)
219 // and use schema/prefix to have wiki farms. For example, a domain schemes could be
220 // wiki-<project>-<language>, e.g. "wiki-fitness-es"/"wiki-sports-fr"/"wiki-news-en".
221 $parts[] = $this->prefix;
222 }
223
224 return implode( '-', array_map( [ __CLASS__, 'encode' ], $parts ) );
225 }
226
227 private static function encode( $decoded ) {
228 $encoded = '';
229
230 $length = strlen( $decoded );
231 for ( $i = 0; $i < $length; ++$i ) {
232 $char = $decoded[$i];
233 if ( $char === '-' ) {
234 $encoded .= '?h';
235 } elseif ( $char === '?' ) {
236 $encoded .= '??';
237 } else {
238 $encoded .= $char;
239 }
240 }
241
242 return $encoded;
243 }
244
245 private static function decode( $encoded ) {
246 $decoded = '';
247
248 $length = strlen( $encoded );
249 for ( $i = 0; $i < $length; ++$i ) {
250 $char = $encoded[$i];
251 if ( $char === '?' ) {
252 $nextChar = $encoded[$i + 1] ?? null;
253 if ( $nextChar === 'h' ) {
254 $decoded .= '-';
255 ++$i;
256 } elseif ( $nextChar === '?' ) {
257 $decoded .= '?';
258 ++$i;
259 } else {
260 $decoded .= $char;
261 }
262 } else {
263 $decoded .= $char;
264 }
265 }
266
267 return $decoded;
268 }
269
273 public function __toString() {
274 return $this->getId();
275 }
276}
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:88
Class to handle database/schema/prefix specifications for IDatabase.
isCompatible( $other)
Check whether the domain $other meets the specifications of this domain.
__construct( $database, $schema, $prefix)