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