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
23 * Class to handle database/prefix specification for IDatabase domains
25 class DatabaseDomain
{
26 /** @var string|null */
28 /** @var string|null */
33 /** @var string Cache of convertToString() */
34 private $equivalentString;
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;
57 * @param DatabaseDomain|string $domain Result of DatabaseDomain::toString()
58 * @return DatabaseDomain
60 public static function newFromId( $domain ) {
61 if ( $domain instanceof self
) {
65 $parts = array_map( [ __CLASS__
, 'decode' ], explode( '-', $domain ) );
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;
77 throw new InvalidArgumentException( "Domain has too few or too many parts." );
80 if ( $database === '' ) {
84 return new self( $database, $schema, $prefix );
88 * @return DatabaseDomain
90 public static function newUnspecified() {
91 return new self( null, null, '' );
95 * @param DatabaseDomain|string $other
98 public function equals( $other ) {
99 if ( $other instanceof DatabaseDomain
) {
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
;
134 public function getId() {
135 if ( $this->equivalentString
=== null ) {
136 $this->equivalentString
= $this->convertToString();
139 return $this->equivalentString
;
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 ) {
160 $length = strlen( $decoded );
161 for ( $i = 0; $i < $length; ++
$i ) {
162 $char = $decoded[$i];
163 if ( $char === '-' ) {
165 } elseif ( $char === '?' ) {
175 private static function decode( $encoded ) {
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' ) {
186 } elseif ( $nextChar === '?' ) {
203 function __toString() {
204 return $this->getId();