rdbms: re-add DB domain sanity checks to LoadBalancer
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabaseDomain.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Database
20 */
21 namespace Wikimedia\Rdbms;
22
23 use InvalidArgumentException;
24
25 /**
26 * Class to handle database/prefix specification for IDatabase domains
27 */
28 class DatabaseDomain {
29 /** @var string|null */
30 private $database;
31 /** @var string|null */
32 private $schema;
33 /** @var string */
34 private $prefix;
35
36 /** @var string Cache of convertToString() */
37 private $equivalentString;
38
39 /**
40 * @param string|null $database Database name
41 * @param string|null $schema Schema name
42 * @param string $prefix Table prefix
43 */
44 public function __construct( $database, $schema, $prefix ) {
45 if ( $database !== null && ( !is_string( $database ) || !strlen( $database ) ) ) {
46 throw new InvalidArgumentException( "Database must be null or a non-empty string." );
47 }
48 $this->database = $database;
49 if ( $schema !== null && ( !is_string( $schema ) || !strlen( $schema ) ) ) {
50 throw new InvalidArgumentException( "Schema must be null or a non-empty string." );
51 }
52 $this->schema = $schema;
53 if ( !is_string( $prefix ) ) {
54 throw new InvalidArgumentException( "Prefix must be a string." );
55 }
56 $this->prefix = $prefix;
57 }
58
59 /**
60 * @param DatabaseDomain|string $domain Result of DatabaseDomain::toString()
61 * @return DatabaseDomain
62 */
63 public static function newFromId( $domain ) {
64 if ( $domain instanceof self ) {
65 return $domain;
66 }
67
68 $parts = array_map( [ __CLASS__, 'decode' ], explode( '-', $domain ) );
69
70 $schema = null;
71 $prefix = '';
72
73 if ( count( $parts ) == 1 ) {
74 $database = $parts[0];
75 } elseif ( count( $parts ) == 2 ) {
76 list( $database, $prefix ) = $parts;
77 } elseif ( count( $parts ) == 3 ) {
78 list( $database, $schema, $prefix ) = $parts;
79 } else {
80 throw new InvalidArgumentException( "Domain has too few or too many parts." );
81 }
82
83 if ( $database === '' ) {
84 $database = null;
85 }
86
87 if ( $schema === '' ) {
88 $schema = null;
89 }
90
91 return new self( $database, $schema, $prefix );
92 }
93
94 /**
95 * @return DatabaseDomain
96 */
97 public static function newUnspecified() {
98 return new self( null, null, '' );
99 }
100
101 /**
102 * @param DatabaseDomain|string $other
103 * @return bool Whether the domain instances are the same by value
104 */
105 public function equals( $other ) {
106 if ( $other instanceof self ) {
107 return (
108 $this->database === $other->database &&
109 $this->schema === $other->schema &&
110 $this->prefix === $other->prefix
111 );
112 }
113
114 return ( $this->getId() === $other );
115 }
116
117 /**
118 * Check whether the domain $other meets the specifications of this domain
119 *
120 * If this instance has a null database specifier, then $other can have any database
121 * specified, including the null, and likewise if the schema specifier is null. This
122 * is not transitive like equals() since a domain that explicitly wants a certain
123 * database or schema cannot be satisfied by one of another (nor null). If the prefix
124 * is empty and the DB and schema are both null, then the entire domain is considered
125 * unspecified, and any prefix of $other is considered compatible.
126 *
127 * @param DatabaseDomain|string $other
128 * @return bool
129 * @since 1.32
130 */
131 public function isCompatible( $other ) {
132 if ( $this->isUnspecified() ) {
133 return true; // even the prefix doesn't matter
134 }
135
136 $other = ( $other instanceof self ) ? $other : self::newFromId( $other );
137
138 return (
139 ( $this->database === $other->database || $this->database === null ) &&
140 ( $this->schema === $other->schema || $this->schema === null ) &&
141 $this->prefix === $other->prefix
142 );
143 }
144
145 /**
146 * @return bool
147 * @since 1.32
148 */
149 public function isUnspecified() {
150 return (
151 $this->database === null && $this->schema === null && $this->prefix === ''
152 );
153 }
154
155 /**
156 * @return string|null Database name
157 */
158 public function getDatabase() {
159 return $this->database;
160 }
161
162 /**
163 * @return string|null Database schema
164 */
165 public function getSchema() {
166 return $this->schema;
167 }
168
169 /**
170 * @return string Table prefix
171 */
172 public function getTablePrefix() {
173 return $this->prefix;
174 }
175
176 /**
177 * @return string
178 */
179 public function getId() {
180 if ( $this->equivalentString === null ) {
181 $this->equivalentString = $this->convertToString();
182 }
183
184 return $this->equivalentString;
185 }
186
187 /**
188 * @return string
189 */
190 private function convertToString() {
191 $parts = [ $this->database ];
192 if ( $this->schema !== null ) {
193 $parts[] = $this->schema;
194 }
195 if ( $this->prefix != '' || $this->schema !== null ) {
196 // If there is a schema, then we need the prefix to disambiguate.
197 // For engines like Postgres that use schemas, this awkwardness is hopefully
198 // avoided since it is easy to have one DB per server (to avoid having many users)
199 // and use schema/prefix to have wiki farms. For example, a domain schemes could be
200 // wiki-<project>-<language>, e.g. "wiki-fitness-es"/"wiki-sports-fr"/"wiki-news-en".
201 $parts[] = $this->prefix;
202 }
203
204 return implode( '-', array_map( [ __CLASS__, 'encode' ], $parts ) );
205 }
206
207 private static function encode( $decoded ) {
208 $encoded = '';
209
210 $length = strlen( $decoded );
211 for ( $i = 0; $i < $length; ++$i ) {
212 $char = $decoded[$i];
213 if ( $char === '-' ) {
214 $encoded .= '?h';
215 } elseif ( $char === '?' ) {
216 $encoded .= '??';
217 } else {
218 $encoded .= $char;
219 }
220 }
221
222 return $encoded;
223 }
224
225 private static function decode( $encoded ) {
226 $decoded = '';
227
228 $length = strlen( $encoded );
229 for ( $i = 0; $i < $length; ++$i ) {
230 $char = $encoded[$i];
231 if ( $char === '?' ) {
232 $nextChar = isset( $encoded[$i + 1] ) ? $encoded[$i + 1] : null;
233 if ( $nextChar === 'h' ) {
234 $decoded .= '-';
235 ++$i;
236 } elseif ( $nextChar === '?' ) {
237 $decoded .= '?';
238 ++$i;
239 } else {
240 $decoded .= $char;
241 }
242 } else {
243 $decoded .= $char;
244 }
245 }
246
247 return $decoded;
248 }
249
250 /**
251 * @return string
252 */
253 function __toString() {
254 return $this->getId();
255 }
256 }