| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace Wikimedia\Rdbms; |
| 21 |
|
| 22 |
use InvalidArgumentException; |
| 23 |
use Stringable; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class DatabaseDomain implements Stringable { |
| 43 |
|
| 44 |
private $database; |
| 45 |
|
| 46 |
private $schema; |
| 47 |
|
| 48 |
private $prefix; |
| 49 |
|
| 50 |
|
| 51 |
private $equivalentString; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function __construct( $database, $schema, $prefix ) { |
| 59 |
if ( $database !== null && ( !is_string( $database ) || $database === '' ) ) { |
| 60 |
throw new InvalidArgumentException( 'Database must be null or a non-empty string.' ); |
| 61 |
} |
| 62 |
$this->database = $database; |
| 63 |
if ( $schema !== null && ( !is_string( $schema ) || $schema === '' ) ) { |
| 64 |
throw new InvalidArgumentException( 'Schema must be null or a non-empty string.' ); |
| 65 |
} elseif ( $database === null && $schema !== null ) { |
| 66 |
throw new InvalidArgumentException( 'Schema must be null if database is null.' ); |
| 67 |
} |
| 68 |
$this->schema = $schema; |
| 69 |
if ( !is_string( $prefix ) ) { |
| 70 |
throw new InvalidArgumentException( "Prefix must be a string." ); |
| 71 |
} |
| 72 |
$this->prefix = $prefix; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public static function newFromId( $domain ): self { |
| 80 |
if ( $domain instanceof self ) { |
| 81 |
return $domain; |
| 82 |
} |
| 83 |
|
| 84 |
if ( !is_string( $domain ) ) { |
| 85 |
throw new InvalidArgumentException( "Domain must be a string or " . __CLASS__ ); |
| 86 |
} |
| 87 |
|
| 88 |
$parts = explode( '-', $domain ); |
| 89 |
foreach ( $parts as &$part ) { |
| 90 |
$part = strtr( $part, [ '?h' => '-', '??' => '?' ] ); |
| 91 |
} |
| 92 |
|
| 93 |
$schema = null; |
| 94 |
$prefix = ''; |
| 95 |
|
| 96 |
if ( count( $parts ) == 1 ) { |
| 97 |
$database = $parts[0]; |
| 98 |
} elseif ( count( $parts ) == 2 ) { |
| 99 |
[ $database, $prefix ] = $parts; |
| 100 |
} elseif ( count( $parts ) == 3 ) { |
| 101 |
[ $database, $schema, $prefix ] = $parts; |
| 102 |
} else { |
| 103 |
throw new InvalidArgumentException( "Domain '$domain' has too few or too many parts." ); |
| 104 |
} |
| 105 |
|
| 106 |
if ( $database === '' ) { |
| 107 |
$database = null; |
| 108 |
} |
| 109 |
|
| 110 |
if ( $schema === '' ) { |
| 111 |
$schema = null; |
| 112 |
} |
| 113 |
|
| 114 |
$instance = new self( $database, $schema, $prefix ); |
| 115 |
$instance->equivalentString = $domain; |
| 116 |
|
| 117 |
return $instance; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public static function newUnspecified() { |
| 124 |
return new self( null, null, '' ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function equals( $other ) { |
| 132 |
if ( $other instanceof self ) { |
| 133 |
return ( |
| 134 |
$this->database === $other->database && |
| 135 |
$this->schema === $other->schema && |
| 136 |
$this->prefix === $other->prefix |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
return ( $this->getId() === $other ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function isCompatible( $other ) { |
| 158 |
if ( $this->isUnspecified() ) { |
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
$other = self::newFromId( $other ); |
| 163 |
|
| 164 |
return ( |
| 165 |
( $this->database === $other->database || $this->database === null ) && |
| 166 |
( $this->schema === $other->schema || $this->schema === null ) && |
| 167 |
$this->prefix === $other->prefix |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function isUnspecified() { |
| 176 |
return ( |
| 177 |
$this->database === null && $this->schema === null && $this->prefix === '' |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function getDatabase() { |
| 185 |
return $this->database; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function getSchema() { |
| 192 |
return $this->schema; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public function getTablePrefix() { |
| 199 |
return $this->prefix; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function getId(): string { |
| 206 |
$this->equivalentString ??= $this->convertToString(); |
| 207 |
|
| 208 |
return $this->equivalentString; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
private function convertToString(): string { |
| 215 |
$parts = [ (string)$this->database ]; |
| 216 |
if ( $this->schema !== null ) { |
| 217 |
$parts[] = $this->schema; |
| 218 |
} |
| 219 |
if ( $this->prefix != '' || $this->schema !== null ) { |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
$parts[] = $this->prefix; |
| 226 |
} |
| 227 |
|
| 228 |
foreach ( $parts as &$part ) { |
| 229 |
$part = strtr( $part, [ '-' => '?h', '?' => '??' ] ); |
| 230 |
} |
| 231 |
return implode( '-', $parts ); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public function __toString() { |
| 238 |
return $this->getId(); |
| 239 |
} |
| 240 |
} |