Merge "Revert "Use display name in category page subheadings if provided""
[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 }
55
56 /**
57 * @param DatabaseDomain|string $domain Result of DatabaseDomain::toString()
58 * @return DatabaseDomain
59 */
60 public static function newFromId( $domain ) {
61 if ( $domain instanceof self ) {
62 return $domain;
63 }
64
65 $parts = array_map( [ __CLASS__, 'decode' ], explode( '-', $domain ) );
66
67 $schema = null;
68 $prefix = '';
69
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." );
78 }
79
80 if ( $database === '' ) {
81 $database = null;
82 }
83
84 return new self( $database, $schema, $prefix );
85 }
86
87 /**
88 * @return DatabaseDomain
89 */
90 public static function newUnspecified() {
91 return new self( null, null, '' );
92 }
93
94 /**
95 * @param DatabaseDomain|string $other
96 * @return bool
97 */
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
104 );
105 }
106
107 return ( $this->getId() === $other );
108 }
109
110 /**
111 * @return string|null Database name
112 */
113 public function getDatabase() {
114 return $this->database;
115 }
116
117 /**
118 * @return string|null Database schema
119 */
120 public function getSchema() {
121 return $this->schema;
122 }
123
124 /**
125 * @return string Table prefix
126 */
127 public function getTablePrefix() {
128 return $this->prefix;
129 }
130
131 /**
132 * @return string
133 */
134 public function getId() {
135 if ( $this->equivalentString === null ) {
136 $this->equivalentString = $this->convertToString();
137 }
138
139 return $this->equivalentString;
140 }
141
142 /**
143 * @return string
144 */
145 private function convertToString() {
146 $parts = [ $this->database ];
147 if ( $this->schema !== null ) {
148 $parts[] = $this->schema;
149 }
150 if ( $this->prefix != '' ) {
151 $parts[] = $this->prefix;
152 }
153
154 return implode( '-', array_map( [ __CLASS__, 'encode' ], $parts ) );
155 }
156
157 private static function encode( $decoded ) {
158 $encoded = '';
159
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;
169 }
170 }
171
172 return $encoded;
173 }
174
175 private static function decode( $encoded ) {
176 $decoded = '';
177
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;
191 }
192 } else {
193 $decoded .= $char;
194 }
195 }
196
197 return $decoded;
198 }
199
200 /**
201 * @return string
202 */
203 function __toString() {
204 return $this->getId();
205 }
206 }