MediaWiki  master
DatabaseDomain.php
Go to the documentation of this file.
1 <?php
21 namespace Wikimedia\Rdbms;
22 
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  $instance = new self( $database, $schema, $prefix );
94  $instance->equivalentString = (string)$domain;
95 
96  return $instance;
97  }
98 
102  public static function newUnspecified() {
103  return new self( null, null, '' );
104  }
105 
110  public function equals( $other ) {
111  if ( $other instanceof self ) {
112  return (
113  $this->database === $other->database &&
114  $this->schema === $other->schema &&
115  $this->prefix === $other->prefix
116  );
117  }
118 
119  return ( $this->getId() === $other );
120  }
121 
136  public function isCompatible( $other ) {
137  if ( $this->isUnspecified() ) {
138  return true; // even the prefix doesn't matter
139  }
140 
141  $other = self::newFromId( $other );
142 
143  return (
144  ( $this->database === $other->database || $this->database === null ) &&
145  ( $this->schema === $other->schema || $this->schema === null ) &&
146  $this->prefix === $other->prefix
147  );
148  }
149 
154  public function isUnspecified() {
155  return (
156  $this->database === null && $this->schema === null && $this->prefix === ''
157  );
158  }
159 
163  public function getDatabase() {
164  return $this->database;
165  }
166 
170  public function getSchema() {
171  return $this->schema;
172  }
173 
177  public function getTablePrefix() {
178  return $this->prefix;
179  }
180 
184  public function getId() {
185  if ( $this->equivalentString === null ) {
186  $this->equivalentString = $this->convertToString();
187  }
188 
190  }
191 
195  private function convertToString() {
196  $parts = [ (string)$this->database ];
197  if ( $this->schema !== null ) {
198  $parts[] = $this->schema;
199  }
200  if ( $this->prefix != '' || $this->schema !== null ) {
201  // If there is a schema, then we need the prefix to disambiguate.
202  // For engines like Postgres that use schemas, this awkwardness is hopefully
203  // avoided since it is easy to have one DB per server (to avoid having many users)
204  // and use schema/prefix to have wiki farms. For example, a domain schemes could be
205  // wiki-<project>-<language>, e.g. "wiki-fitness-es"/"wiki-sports-fr"/"wiki-news-en".
206  $parts[] = $this->prefix;
207  }
208 
209  return implode( '-', array_map( [ __CLASS__, 'encode' ], $parts ) );
210  }
211 
212  private static function encode( $decoded ) {
213  $encoded = '';
214 
215  $length = strlen( $decoded );
216  for ( $i = 0; $i < $length; ++$i ) {
217  $char = $decoded[$i];
218  if ( $char === '-' ) {
219  $encoded .= '?h';
220  } elseif ( $char === '?' ) {
221  $encoded .= '??';
222  } else {
223  $encoded .= $char;
224  }
225  }
226 
227  return $encoded;
228  }
229 
230  private static function decode( $encoded ) {
231  $decoded = '';
232 
233  $length = strlen( $encoded );
234  for ( $i = 0; $i < $length; ++$i ) {
235  $char = $encoded[$i];
236  if ( $char === '?' ) {
237  $nextChar = $encoded[$i + 1] ?? null;
238  if ( $nextChar === 'h' ) {
239  $decoded .= '-';
240  ++$i;
241  } elseif ( $nextChar === '?' ) {
242  $decoded .= '?';
243  ++$i;
244  } else {
245  $decoded .= $char;
246  }
247  } else {
248  $decoded .= $char;
249  }
250  }
251 
252  return $decoded;
253  }
254 
258  function __toString() {
259  return $this->getId();
260  }
261 }
string $equivalentString
Cache of convertToString()
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
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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
isCompatible( $other)
Check whether the domain $other meets the specifications of this domain.
__construct( $database, $schema, $prefix)
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
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
Class to handle database/prefix specification for IDatabase domains.