DatabaseMysqlBase visibility cleanups
[mediawiki.git] / includes / libs / rdbms / database / DatabaseDomain.php
bloba3ae6f10a143593a936eb136bdcb8f1ac0acdd78
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.
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.
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
18 * @file
19 * @ingroup Database
22 /**
23 * Class to handle database/prefix specification for IDatabase domains
25 class DatabaseDomain {
26 /** @var string|null */
27 private $database;
28 /** @var string|null */
29 private $schema;
30 /** @var string */
31 private $prefix;
33 /** @var string Cache of convertToString() */
34 private $equivalentString;
36 /**
37 * @param string|null $database Database name
38 * @param string|null $schema Schema name
39 * @param string $prefix Table prefix
41 public function __construct( $database, $schema, $prefix ) {
42 if ( $database !== null && ( !is_string( $database ) || !strlen( $database ) ) ) {
43 throw new InvalidArgumentException( "Database must be null or a non-empty string." );
45 $this->database = $database;
46 if ( $schema !== null && ( !is_string( $schema ) || !strlen( $schema ) ) ) {
47 throw new InvalidArgumentException( "Schema must be null or a non-empty string." );
49 $this->schema = $schema;
50 if ( !is_string( $prefix ) ) {
51 throw new InvalidArgumentException( "Prefix must be a string." );
53 $this->prefix = $prefix;
56 /**
57 * @param DatabaseDomain|string $domain Result of DatabaseDomain::toString()
58 * @return DatabaseDomain
60 public static function newFromId( $domain ) {
61 if ( $domain instanceof self ) {
62 return $domain;
65 $parts = array_map( [ __CLASS__, 'decode' ], explode( '-', $domain ) );
67 $schema = null;
68 $prefix = '';
70 if ( count( $parts ) == 1 ) {
71 $database = $parts[0];
72 } elseif ( count( $parts ) == 2 ) {
73 list( $database, $prefix ) = $parts;
74 } elseif ( count( $parts ) == 3 ) {
75 list( $database, $schema, $prefix ) = $parts;
76 } else {
77 throw new InvalidArgumentException( "Domain has too few or too many parts." );
80 if ( $database === '' ) {
81 $database = null;
84 return new self( $database, $schema, $prefix );
87 /**
88 * @return DatabaseDomain
90 public static function newUnspecified() {
91 return new self( null, null, '' );
94 /**
95 * @param DatabaseDomain|string $other
96 * @return bool
98 public function equals( $other ) {
99 if ( $other instanceof DatabaseDomain ) {
100 return (
101 $this->database === $other->database &&
102 $this->schema === $other->schema &&
103 $this->prefix === $other->prefix
107 return ( $this->getId() === $other );
111 * @return string|null Database name
113 public function getDatabase() {
114 return $this->database;
118 * @return string|null Database schema
120 public function getSchema() {
121 return $this->schema;
125 * @return string Table prefix
127 public function getTablePrefix() {
128 return $this->prefix;
132 * @return string
134 public function getId() {
135 if ( $this->equivalentString === null ) {
136 $this->equivalentString = $this->convertToString();
139 return $this->equivalentString;
143 * @return string
145 private function convertToString() {
146 $parts = [ $this->database ];
147 if ( $this->schema !== null ) {
148 $parts[] = $this->schema;
150 if ( $this->prefix != '' ) {
151 $parts[] = $this->prefix;
154 return implode( '-', array_map( [ __CLASS__, 'encode' ], $parts ) );
157 private static function encode( $decoded ) {
158 $encoded = '';
160 $length = strlen( $decoded );
161 for ( $i = 0; $i < $length; ++$i ) {
162 $char = $decoded[$i];
163 if ( $char === '-' ) {
164 $encoded .= '?h';
165 } elseif ( $char === '?' ) {
166 $encoded .= '??';
167 } else {
168 $encoded .= $char;
172 return $encoded;
175 private static function decode( $encoded ) {
176 $decoded = '';
178 $length = strlen( $encoded );
179 for ( $i = 0; $i < $length; ++$i ) {
180 $char = $encoded[$i];
181 if ( $char === '?' ) {
182 $nextChar = isset( $encoded[$i + 1] ) ? $encoded[$i + 1] : null;
183 if ( $nextChar === 'h' ) {
184 $decoded .= '-';
185 ++$i;
186 } elseif ( $nextChar === '?' ) {
187 $decoded .= '?';
188 ++$i;
189 } else {
190 $decoded .= $char;
192 } else {
193 $decoded .= $char;
197 return $decoded;
201 * @return string
203 function __toString() {
204 return $this->getId();