MediaWiki REL1_33
DatabaseDomain.php
Go to the documentation of this file.
1<?php
21namespace Wikimedia\Rdbms;
22
23use InvalidArgumentException;
24
30 private $database;
32 private $schema;
34 private $prefix;
35
38
44 public function __construct( $database, $schema, $prefix ) {
45 if ( $database !== null && ( !is_string( $database ) || $database === '' ) ) {
46 throw new InvalidArgumentException( 'Database must be null or a non-empty string.' );
47 }
48 $this->database = $database;
49 if ( $schema !== null && ( !is_string( $schema ) || $schema === '' ) ) {
50 throw new InvalidArgumentException( 'Schema must be null or a non-empty string.' );
51 } elseif ( $database === null && $schema !== null ) {
52 throw new InvalidArgumentException( 'Schema must be null if database is null.' );
53 }
54 $this->schema = $schema;
55 if ( !is_string( $prefix ) ) {
56 throw new InvalidArgumentException( "Prefix must be a string." );
57 }
58 $this->prefix = $prefix;
59 }
60
65 public static function newFromId( $domain ) {
66 if ( $domain instanceof self ) {
67 return $domain;
68 }
69
70 $parts = array_map( [ __CLASS__, 'decode' ], explode( '-', $domain ) );
71
72 $schema = null;
73 $prefix = '';
74
75 if ( count( $parts ) == 1 ) {
76 $database = $parts[0];
77 } elseif ( count( $parts ) == 2 ) {
78 list( $database, $prefix ) = $parts;
79 } elseif ( count( $parts ) == 3 ) {
80 list( $database, $schema, $prefix ) = $parts;
81 } else {
82 throw new InvalidArgumentException( "Domain '$domain' has too few or too many parts." );
83 }
84
85 if ( $database === '' ) {
86 $database = null;
87 }
88
89 if ( $schema === '' ) {
90 $schema = null;
91 }
92
93 return new self( $database, $schema, $prefix );
94 }
95
99 public static function newUnspecified() {
100 return new self( null, null, '' );
101 }
102
107 public function equals( $other ) {
108 if ( $other instanceof self ) {
109 return (
110 $this->database === $other->database &&
111 $this->schema === $other->schema &&
112 $this->prefix === $other->prefix
113 );
114 }
115
116 return ( $this->getId() === $other );
117 }
118
133 public function isCompatible( $other ) {
134 if ( $this->isUnspecified() ) {
135 return true; // even the prefix doesn't matter
136 }
137
138 $other = self::newFromId( $other );
139
140 return (
141 ( $this->database === $other->database || $this->database === null ) &&
142 ( $this->schema === $other->schema || $this->schema === null ) &&
143 $this->prefix === $other->prefix
144 );
145 }
146
151 public function isUnspecified() {
152 return (
153 $this->database === null && $this->schema === null && $this->prefix === ''
154 );
155 }
156
160 public function getDatabase() {
161 return $this->database;
162 }
163
167 public function getSchema() {
168 return $this->schema;
169 }
170
174 public function getTablePrefix() {
175 return $this->prefix;
176 }
177
181 public function getId() {
182 if ( $this->equivalentString === null ) {
183 $this->equivalentString = $this->convertToString();
184 }
185
187 }
188
192 private function convertToString() {
193 $parts = [ (string)$this->database ];
194 if ( $this->schema !== null ) {
195 $parts[] = $this->schema;
196 }
197 if ( $this->prefix != '' || $this->schema !== null ) {
198 // If there is a schema, then we need the prefix to disambiguate.
199 // For engines like Postgres that use schemas, this awkwardness is hopefully
200 // avoided since it is easy to have one DB per server (to avoid having many users)
201 // and use schema/prefix to have wiki farms. For example, a domain schemes could be
202 // wiki-<project>-<language>, e.g. "wiki-fitness-es"/"wiki-sports-fr"/"wiki-news-en".
203 $parts[] = $this->prefix;
204 }
205
206 return implode( '-', array_map( [ __CLASS__, 'encode' ], $parts ) );
207 }
208
209 private static function encode( $decoded ) {
210 $encoded = '';
211
212 $length = strlen( $decoded );
213 for ( $i = 0; $i < $length; ++$i ) {
214 $char = $decoded[$i];
215 if ( $char === '-' ) {
216 $encoded .= '?h';
217 } elseif ( $char === '?' ) {
218 $encoded .= '??';
219 } else {
220 $encoded .= $char;
221 }
222 }
223
224 return $encoded;
225 }
226
227 private static function decode( $encoded ) {
228 $decoded = '';
229
230 $length = strlen( $encoded );
231 for ( $i = 0; $i < $length; ++$i ) {
232 $char = $encoded[$i];
233 if ( $char === '?' ) {
234 $nextChar = $encoded[$i + 1] ?? null;
235 if ( $nextChar === 'h' ) {
236 $decoded .= '-';
237 ++$i;
238 } elseif ( $nextChar === '?' ) {
239 $decoded .= '?';
240 ++$i;
241 } else {
242 $decoded .= $char;
243 }
244 } else {
245 $decoded .= $char;
246 }
247 }
248
249 return $decoded;
250 }
251
255 function __toString() {
256 return $this->getId();
257 }
258}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
Class to handle database/prefix specification for IDatabase domains.
string $equivalentString
Cache of convertToString()
isCompatible( $other)
Check whether the domain $other meets the specifications of this domain.
__construct( $database, $schema, $prefix)
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition hooks.txt:181