MediaWiki  1.33.0
DatabaseDomain.php
Go to the documentation of this file.
1 <?php
21 namespace Wikimedia\Rdbms;
22 
23 use 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  } elseif ( $prefix !== '' && substr( $prefix, -1, 1 ) !== '_' ) {
58  throw new InvalidArgumentException( 'A non-empty prefix must end with "_".' );
59  }
60  $this->prefix = $prefix;
61  }
62 
67  public static function newFromId( $domain ) {
68  if ( $domain instanceof self ) {
69  return $domain;
70  }
71 
72  $parts = array_map( [ __CLASS__, 'decode' ], explode( '-', $domain ) );
73 
74  $schema = null;
75  $prefix = '';
76 
77  if ( count( $parts ) == 1 ) {
78  $database = $parts[0];
79  } elseif ( count( $parts ) == 2 ) {
80  list( $database, $prefix ) = $parts;
81  } elseif ( count( $parts ) == 3 ) {
82  list( $database, $schema, $prefix ) = $parts;
83  } else {
84  throw new InvalidArgumentException( "Domain '$domain' has too few or too many parts." );
85  }
86 
87  if ( $database === '' ) {
88  $database = null;
89  }
90 
91  if ( $schema === '' ) {
92  $schema = null;
93  }
94 
95  return new self( $database, $schema, $prefix );
96  }
97 
101  public static function newUnspecified() {
102  return new self( null, null, '' );
103  }
104 
109  public function equals( $other ) {
110  if ( $other instanceof self ) {
111  return (
112  $this->database === $other->database &&
113  $this->schema === $other->schema &&
114  $this->prefix === $other->prefix
115  );
116  }
117 
118  return ( $this->getId() === $other );
119  }
120 
135  public function isCompatible( $other ) {
136  if ( $this->isUnspecified() ) {
137  return true; // even the prefix doesn't matter
138  }
139 
140  $other = self::newFromId( $other );
141 
142  return (
143  ( $this->database === $other->database || $this->database === null ) &&
144  ( $this->schema === $other->schema || $this->schema === null ) &&
145  $this->prefix === $other->prefix
146  );
147  }
148 
153  public function isUnspecified() {
154  return (
155  $this->database === null && $this->schema === null && $this->prefix === ''
156  );
157  }
158 
162  public function getDatabase() {
163  return $this->database;
164  }
165 
169  public function getSchema() {
170  return $this->schema;
171  }
172 
176  public function getTablePrefix() {
177  return $this->prefix;
178  }
179 
183  public function getId() {
184  if ( $this->equivalentString === null ) {
185  $this->equivalentString = $this->convertToString();
186  }
187 
189  }
190 
194  private function convertToString() {
195  $parts = [ (string)$this->database ];
196  if ( $this->schema !== null ) {
197  $parts[] = $this->schema;
198  }
199  if ( $this->prefix != '' || $this->schema !== null ) {
200  // If there is a schema, then we need the prefix to disambiguate.
201  // For engines like Postgres that use schemas, this awkwardness is hopefully
202  // avoided since it is easy to have one DB per server (to avoid having many users)
203  // and use schema/prefix to have wiki farms. For example, a domain schemes could be
204  // wiki-<project>-<language>, e.g. "wiki-fitness-es"/"wiki-sports-fr"/"wiki-news-en".
205  $parts[] = $this->prefix;
206  }
207 
208  return implode( '-', array_map( [ __CLASS__, 'encode' ], $parts ) );
209  }
210 
211  private static function encode( $decoded ) {
212  $encoded = '';
213 
214  $length = strlen( $decoded );
215  for ( $i = 0; $i < $length; ++$i ) {
216  $char = $decoded[$i];
217  if ( $char === '-' ) {
218  $encoded .= '?h';
219  } elseif ( $char === '?' ) {
220  $encoded .= '??';
221  } else {
222  $encoded .= $char;
223  }
224  }
225 
226  return $encoded;
227  }
228 
229  private static function decode( $encoded ) {
230  $decoded = '';
231 
232  $length = strlen( $encoded );
233  for ( $i = 0; $i < $length; ++$i ) {
234  $char = $encoded[$i];
235  if ( $char === '?' ) {
236  $nextChar = $encoded[$i + 1] ?? null;
237  if ( $nextChar === 'h' ) {
238  $decoded .= '-';
239  ++$i;
240  } elseif ( $nextChar === '?' ) {
241  $decoded .= '?';
242  ++$i;
243  } else {
244  $decoded .= $char;
245  }
246  } else {
247  $decoded .= $char;
248  }
249  }
250 
251  return $decoded;
252  }
253 
257  function __toString() {
258  return $this->getId();
259  }
260 }
Wikimedia\Rdbms\DatabaseDomain\getId
getId()
Definition: DatabaseDomain.php:183
Wikimedia\Rdbms\DatabaseDomain\newFromId
static newFromId( $domain)
Definition: DatabaseDomain.php:67
Wikimedia\Rdbms\DatabaseDomain\__construct
__construct( $database, $schema, $prefix)
Definition: DatabaseDomain.php:44
captcha-old.count
count
Definition: captcha-old.py:249
Wikimedia\Rdbms\DatabaseDomain\getTablePrefix
getTablePrefix()
Definition: DatabaseDomain.php:176
Wikimedia\Rdbms\DatabaseDomain\$equivalentString
string $equivalentString
Cache of convertToString()
Definition: DatabaseDomain.php:37
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\DatabaseDomain\$prefix
string $prefix
Definition: DatabaseDomain.php:34
Wikimedia\Rdbms\DatabaseDomain\getDatabase
getDatabase()
Definition: DatabaseDomain.php:162
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\DatabaseDomain\newUnspecified
static newUnspecified()
Definition: DatabaseDomain.php:101
Wikimedia\Rdbms\DatabaseDomain\encode
static encode( $decoded)
Definition: DatabaseDomain.php:211
Wikimedia\Rdbms\DatabaseDomain\__toString
__toString()
Definition: DatabaseDomain.php:257
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
Wikimedia\Rdbms\DatabaseDomain\isUnspecified
isUnspecified()
Definition: DatabaseDomain.php:153
string
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:175
list
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
Wikimedia\Rdbms\DatabaseDomain\$database
string null $database
Definition: DatabaseDomain.php:30
Wikimedia\Rdbms\DatabaseDomain\getSchema
getSchema()
Definition: DatabaseDomain.php:169
Wikimedia\Rdbms\DatabaseDomain\$schema
string null $schema
Definition: DatabaseDomain.php:32
Wikimedia\Rdbms\DatabaseDomain\decode
static decode( $encoded)
Definition: DatabaseDomain.php:229
Wikimedia\Rdbms\DatabaseDomain\convertToString
convertToString()
Definition: DatabaseDomain.php:194
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/prefix specification for IDatabase domains.
Definition: DatabaseDomain.php:28
Wikimedia\Rdbms\DatabaseDomain\isCompatible
isCompatible( $other)
Check whether the domain $other meets the specifications of this domain.
Definition: DatabaseDomain.php:135
Wikimedia\Rdbms\DatabaseDomain\equals
equals( $other)
Definition: DatabaseDomain.php:109