MediaWiki  1.30.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 ) || !strlen( $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 ) || !strlen( $schema ) ) ) {
50  throw new InvalidArgumentException( "Schema must be null or a non-empty string." );
51  }
52  $this->schema = $schema;
53  if ( !is_string( $prefix ) ) {
54  throw new InvalidArgumentException( "Prefix must be a string." );
55  }
56  $this->prefix = $prefix;
57  }
58 
63  public static function newFromId( $domain ) {
64  if ( $domain instanceof self ) {
65  return $domain;
66  }
67 
68  $parts = array_map( [ __CLASS__, 'decode' ], explode( '-', $domain ) );
69 
70  $schema = null;
71  $prefix = '';
72 
73  if ( count( $parts ) == 1 ) {
74  $database = $parts[0];
75  } elseif ( count( $parts ) == 2 ) {
76  list( $database, $prefix ) = $parts;
77  } elseif ( count( $parts ) == 3 ) {
78  list( $database, $schema, $prefix ) = $parts;
79  } else {
80  throw new InvalidArgumentException( "Domain has too few or too many parts." );
81  }
82 
83  if ( $database === '' ) {
84  $database = null;
85  }
86 
87  return new self( $database, $schema, $prefix );
88  }
89 
93  public static function newUnspecified() {
94  return new self( null, null, '' );
95  }
96 
101  public function equals( $other ) {
102  if ( $other instanceof DatabaseDomain ) {
103  return (
104  $this->database === $other->database &&
105  $this->schema === $other->schema &&
106  $this->prefix === $other->prefix
107  );
108  }
109 
110  return ( $this->getId() === $other );
111  }
112 
116  public function getDatabase() {
117  return $this->database;
118  }
119 
123  public function getSchema() {
124  return $this->schema;
125  }
126 
130  public function getTablePrefix() {
131  return $this->prefix;
132  }
133 
137  public function getId() {
138  if ( $this->equivalentString === null ) {
139  $this->equivalentString = $this->convertToString();
140  }
141 
143  }
144 
148  private function convertToString() {
149  $parts = [ $this->database ];
150  if ( $this->schema !== null ) {
151  $parts[] = $this->schema;
152  }
153  if ( $this->prefix != '' ) {
154  $parts[] = $this->prefix;
155  }
156 
157  return implode( '-', array_map( [ __CLASS__, 'encode' ], $parts ) );
158  }
159 
160  private static function encode( $decoded ) {
161  $encoded = '';
162 
163  $length = strlen( $decoded );
164  for ( $i = 0; $i < $length; ++$i ) {
165  $char = $decoded[$i];
166  if ( $char === '-' ) {
167  $encoded .= '?h';
168  } elseif ( $char === '?' ) {
169  $encoded .= '??';
170  } else {
171  $encoded .= $char;
172  }
173  }
174 
175  return $encoded;
176  }
177 
178  private static function decode( $encoded ) {
179  $decoded = '';
180 
181  $length = strlen( $encoded );
182  for ( $i = 0; $i < $length; ++$i ) {
183  $char = $encoded[$i];
184  if ( $char === '?' ) {
185  $nextChar = isset( $encoded[$i + 1] ) ? $encoded[$i + 1] : null;
186  if ( $nextChar === 'h' ) {
187  $decoded .= '-';
188  ++$i;
189  } elseif ( $nextChar === '?' ) {
190  $decoded .= '?';
191  ++$i;
192  } else {
193  $decoded .= $char;
194  }
195  } else {
196  $decoded .= $char;
197  }
198  }
199 
200  return $decoded;
201  }
202 
206  function __toString() {
207  return $this->getId();
208  }
209 }
Wikimedia\Rdbms\DatabaseDomain\getId
getId()
Definition: DatabaseDomain.php:137
Wikimedia\Rdbms\DatabaseDomain\newFromId
static newFromId( $domain)
Definition: DatabaseDomain.php:63
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:130
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\$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:116
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:93
Wikimedia\Rdbms\DatabaseDomain\encode
static encode( $decoded)
Definition: DatabaseDomain.php:160
Wikimedia\Rdbms\DatabaseDomain\__toString
__toString()
Definition: DatabaseDomain.php:206
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
database
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the database
Definition: design.txt:12
Wikimedia\Rdbms\DatabaseDomain\$database
string null $database
Definition: DatabaseDomain.php:30
Wikimedia\Rdbms\DatabaseDomain\getSchema
getSchema()
Definition: DatabaseDomain.php:123
Wikimedia\Rdbms\DatabaseDomain\$schema
string null $schema
Definition: DatabaseDomain.php:32
Wikimedia\Rdbms\DatabaseDomain\decode
static decode( $encoded)
Definition: DatabaseDomain.php:178
Wikimedia\Rdbms\DatabaseDomain\convertToString
convertToString()
Definition: DatabaseDomain.php:148
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/prefix specification for IDatabase domains.
Definition: DatabaseDomain.php:28
Wikimedia\Rdbms\DatabaseDomain\equals
equals( $other)
Definition: DatabaseDomain.php:101