Make database classes handle hyphens in $wgDBname
[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
22 /**
23 * Class to handle database/prefix specification for IDatabase domains
24 */
25 class DatabaseDomain {
26 /** @var string|null */
27 private $database;
28 /** @var string|null */
29 private $schema;
30 /** @var string */
31 private $prefix;
32
33 /** @var string Cache of convertToString() */
34 private $equivalentString;
35
36 /**
37 * @param string|null $database Database name
38 * @param string|null $schema Schema name
39 * @param string $prefix Table prefix
40 */
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." );
44 }
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." );
48 }
49 $this->schema = $schema;
50 if ( !is_string( $prefix ) ) {
51 throw new InvalidArgumentException( "Prefix must be a string." );
52 }
53 $this->prefix = $prefix;
54 $this->equivalentString = $this->convertToString();
55 }
56
57 /**
58 * @param DatabaseDomain|string $domain Result of DatabaseDomain::toString()
59 * @return DatabaseDomain
60 */
61 public static function newFromId( $domain ) {
62 if ( $domain instanceof self ) {
63 return $domain;
64 }
65
66 $parts = array_map( [ __CLASS__, 'decode' ], explode( '-', $domain ) );
67
68 $schema = null;
69 $prefix = '';
70
71 if ( count( $parts ) == 1 ) {
72 $database = $parts[0];
73 } elseif ( count( $parts ) == 2 ) {
74 list( $database, $prefix ) = $parts;
75 } elseif ( count( $parts ) == 3 ) {
76 list( $database, $schema, $prefix ) = $parts;
77 } else {
78 throw new InvalidArgumentException( "Domain has too few or too many parts." );
79 }
80
81 if ( $database === '' ) {
82 $database = null;
83 }
84
85 return new self( $database, $schema, $prefix );
86 }
87
88 /**
89 * @return DatabaseDomain
90 */
91 public static function newUnspecified() {
92 return new self( null, null, '' );
93 }
94
95 /**
96 * @param DatabaseDomain|string $other
97 * @return bool
98 */
99 public function equals( $other ) {
100 if ( $other instanceof DatabaseDomain ) {
101 return (
102 $this->database === $other->database &&
103 $this->schema === $other->schema &&
104 $this->prefix === $other->prefix
105 );
106 }
107
108 return ( $this->equivalentString === $other );
109 }
110
111 /**
112 * @return string|null Database name
113 */
114 public function getDatabase() {
115 return $this->database;
116 }
117
118 /**
119 * @return string|null Database schema
120 */
121 public function getSchema() {
122 return $this->schema;
123 }
124
125 /**
126 * @return string Table prefix
127 */
128 public function getTablePrefix() {
129 return $this->prefix;
130 }
131
132 /**
133 * @return string
134 */
135 public function getId() {
136 return $this->equivalentString;
137 }
138
139 /**
140 * @return string
141 */
142 private function convertToString() {
143 $parts = [ $this->database ];
144 if ( $this->schema !== null ) {
145 $parts[] = $this->schema;
146 }
147 if ( $this->prefix != '' ) {
148 $parts[] = $this->prefix;
149 }
150
151 return implode( '-', array_map( [ __CLASS__, 'encode' ], $parts ) );
152 }
153
154 private static function encode( $decoded ) {
155 $encoded = '';
156
157 $length = strlen( $decoded );
158 for ( $i = 0; $i < $length; ++$i ) {
159 $char = $decoded[$i];
160 if ( $char === '-' ) {
161 $encoded .= '?h';
162 } elseif ( $char === '?' ) {
163 $encoded .= '??';
164 } else {
165 $encoded .= $char;
166 }
167 }
168
169 return $encoded;
170 }
171
172 private static function decode( $encoded ) {
173 $decoded = '';
174
175 $length = strlen( $encoded );
176 for ( $i = 0; $i < $length; ++$i ) {
177 $char = $encoded[$i];
178 if ( $char === '?' ) {
179 $nextChar = isset( $encoded[$i + 1] ) ? $encoded[$i + 1] : null;
180 if ( $nextChar === 'h' ) {
181 $decoded .= '-';
182 ++$i;
183 } elseif ( $nextChar === '?' ) {
184 $decoded .= '?';
185 ++$i;
186 } else {
187 $decoded .= $char;
188 }
189 } else {
190 $decoded .= $char;
191 }
192 }
193
194 return $decoded;
195 }
196
197 /**
198 * @return string
199 */
200 function __toString() {
201 return $this->getId();
202 }
203 }