MediaWiki master
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 = explode( '-', $domain );
88 foreach ( $parts as &$part ) {
89 $part = strtr( $part, [ '?h' => '-', '??' => '?' ] );
90 }
91
92 $schema = null;
93 $prefix = '';
94
95 if ( count( $parts ) == 1 ) {
96 $database = $parts[0];
97 } elseif ( count( $parts ) == 2 ) {
98 [ $database, $prefix ] = $parts;
99 } elseif ( count( $parts ) == 3 ) {
100 [ $database, $schema, $prefix ] = $parts;
101 } else {
102 throw new InvalidArgumentException( "Domain '$domain' has too few or too many parts." );
103 }
104
105 if ( $database === '' ) {
106 $database = null;
107 }
108
109 if ( $schema === '' ) {
110 $schema = null;
111 }
112
113 $instance = new self( $database, $schema, $prefix );
114 $instance->equivalentString = $domain;
115
116 return $instance;
117 }
118
122 public static function newUnspecified() {
123 return new self( null, null, '' );
124 }
125
130 public function equals( $other ) {
131 if ( $other instanceof self ) {
132 return (
133 $this->database === $other->database &&
134 $this->schema === $other->schema &&
135 $this->prefix === $other->prefix
136 );
137 }
138
139 return ( $this->getId() === $other );
140 }
141
156 public function isCompatible( $other ) {
157 if ( $this->isUnspecified() ) {
158 return true; // even the prefix doesn't matter
159 }
160
161 $other = self::newFromId( $other );
162
163 return (
164 ( $this->database === $other->database || $this->database === null ) &&
165 ( $this->schema === $other->schema || $this->schema === null ) &&
166 $this->prefix === $other->prefix
167 );
168 }
169
174 public function isUnspecified() {
175 return (
176 $this->database === null && $this->schema === null && $this->prefix === ''
177 );
178 }
179
183 public function getDatabase() {
184 return $this->database;
185 }
186
190 public function getSchema() {
191 return $this->schema;
192 }
193
197 public function getTablePrefix() {
198 return $this->prefix;
199 }
200
204 public function getId(): string {
205 $this->equivalentString ??= $this->convertToString();
206
207 return $this->equivalentString;
208 }
209
213 private function convertToString(): string {
214 $parts = [ (string)$this->database ];
215 if ( $this->schema !== null ) {
216 $parts[] = $this->schema;
217 }
218 if ( $this->prefix != '' || $this->schema !== null ) {
219 // If there is a schema, then we need the prefix to disambiguate.
220 // For engines like Postgres that use schemas, this awkwardness is hopefully
221 // avoided since it is easy to have one DB per server (to avoid having many users)
222 // and use schema/prefix to have wiki farms. For example, a domain schemes could be
223 // wiki-<project>-<language>, e.g. "wiki-fitness-es"/"wiki-sports-fr"/"wiki-news-en".
224 $parts[] = $this->prefix;
225 }
226
227 foreach ( $parts as &$part ) {
228 $part = strtr( $part, [ '-' => '?h', '?' => '??' ] );
229 }
230 return implode( '-', $parts );
231 }
232
236 public function __toString() {
237 return $this->getId();
238 }
239}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
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)