MediaWiki  1.28.3
DatabaseMysqlBaseTest.php
Go to the documentation of this file.
1 <?php
32  // From Database
33  function __construct() {
34  $this->profiler = new ProfilerStub( [] );
35  $this->trxProfiler = new TransactionProfiler();
36  $this->cliMode = true;
37  $this->connLogger = new \Psr\Log\NullLogger();
38  $this->queryLogger = new \Psr\Log\NullLogger();
39  $this->errorLogger = function ( Exception $e ) {
40  wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
41  };
42  $this->currentDomain = DatabaseDomain::newUnspecified();
43  }
44 
45  protected function closeConnection() {
46  }
47 
48  protected function doQuery( $sql ) {
49  }
50 
51  // From DatabaseMysql
52  protected function mysqlConnect( $realServer ) {
53  }
54 
55  protected function mysqlSetCharset( $charset ) {
56  }
57 
58  protected function mysqlFreeResult( $res ) {
59  }
60 
61  protected function mysqlFetchObject( $res ) {
62  }
63 
64  protected function mysqlFetchArray( $res ) {
65  }
66 
67  protected function mysqlNumRows( $res ) {
68  }
69 
70  protected function mysqlNumFields( $res ) {
71  }
72 
73  protected function mysqlFieldName( $res, $n ) {
74  }
75 
76  protected function mysqlFieldType( $res, $n ) {
77  }
78 
79  protected function mysqlDataSeek( $res, $row ) {
80  }
81 
82  protected function mysqlError( $conn = null ) {
83  }
84 
85  protected function mysqlFetchField( $res, $n ) {
86  }
87 
88  protected function mysqlRealEscapeString( $s ) {
89 
90  }
91 
92  function insertId() {
93  }
94 
95  function lastErrno() {
96  }
97 
98  function affectedRows() {
99  }
100 
101  function getServerVersion() {
102  }
103 }
104 
110  public function testAddIdentifierQuotes( $expected, $in ) {
111  $db = new FakeDatabaseMysqlBase();
112  $quoted = $db->addIdentifierQuotes( $in );
113  $this->assertEquals( $expected, $quoted );
114  }
115 
121  function provideDiapers() {
122  return [
123  // Format: expected, input
124  [ '``', '' ],
125 
126  // Yeah I really hate loosely typed PHP idiocies nowadays
127  [ '``', null ],
128 
129  // Dear codereviewer, guess what addIdentifierQuotes()
130  // will return with thoses:
131  [ '``', false ],
132  [ '`1`', true ],
133 
134  // We never know what could happen
135  [ '`0`', 0 ],
136  [ '`1`', 1 ],
137 
138  // Whatchout! Should probably use something more meaningful
139  [ "`'`", "'" ], # single quote
140  [ '`"`', '"' ], # double quote
141  [ '````', '`' ], # backtick
142  [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
143 
144  // sneaky NUL bytes are lurking everywhere
145  [ '``', "\0" ],
146  [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
147 
148  // unicode chars
149  [
150  self::createUnicodeString( '`\u0001a\uFFFFb`' ),
151  self::createUnicodeString( '\u0001a\uFFFFb' )
152  ],
153  [
154  self::createUnicodeString( '`\u0001\uFFFF`' ),
155  self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
156  ],
157  [ '`☃`', '☃' ],
158  [ '`メインページ`', 'メインページ' ],
159  [ '`Басты_бет`', 'Басты_бет' ],
160 
161  // Real world:
162  [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
163  [ '`Backtick: ```', 'Backtick: `' ],
164  [ '`This is a test`', 'This is a test' ],
165  ];
166  }
167 
168  private static function createUnicodeString( $str ) {
169  return json_decode( '"' . $str . '"' );
170  }
171 
172  function getMockForViews() {
173  $db = $this->getMockBuilder( 'DatabaseMysql' )
174  ->disableOriginalConstructor()
175  ->setMethods( [ 'fetchRow', 'query' ] )
176  ->getMock();
177 
178  $db->method( 'query' )
179  ->with( $this->anything() )
180  ->willReturn( new FakeResultWrapper( [
181  (object)[ 'Tables_in_' => 'view1' ],
182  (object)[ 'Tables_in_' => 'view2' ],
183  (object)[ 'Tables_in_' => 'myview' ]
184  ] ) );
185 
186  return $db;
187  }
191  function testListviews() {
192  $db = $this->getMockForViews();
193 
194  $this->assertEquals( [ 'view1', 'view2', 'myview' ],
195  $db->listViews() );
196 
197  // Prefix filtering
198  $this->assertEquals( [ 'view1', 'view2' ],
199  $db->listViews( 'view' ) );
200  $this->assertEquals( [ 'myview' ],
201  $db->listViews( 'my' ) );
202  $this->assertEquals( [],
203  $db->listViews( 'UNUSED_PREFIX' ) );
204  $this->assertEquals( [ 'view1', 'view2', 'myview' ],
205  $db->listViews( '' ) );
206  }
207 
211  function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match ) {
212  if ( $match ) {
213  $this->assertTrue( $lowerPos->channelsMatch( $higherPos ) );
214 
215  $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
216  $this->assertTrue( $higherPos->hasReached( $higherPos ) );
217  $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
218  $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
219  } else { // channels don't match
220  $this->assertFalse( $lowerPos->channelsMatch( $higherPos ) );
221 
222  $this->assertFalse( $higherPos->hasReached( $lowerPos ) );
223  $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
224  }
225  }
226 
228  return [
229  // Binlog style
230  [
231  new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
232  new MySQLMasterPos( 'db1034-bin.000976', '843431248' ),
233  true
234  ],
235  [
236  new MySQLMasterPos( 'db1034-bin.000976', '999' ),
237  new MySQLMasterPos( 'db1034-bin.000976', '1000' ),
238  true
239  ],
240  [
241  new MySQLMasterPos( 'db1034-bin.000976', '999' ),
242  new MySQLMasterPos( 'db1035-bin.000976', '1000' ),
243  false
244  ],
245  // MySQL GTID style
246  [
247  new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:23' ),
248  new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:24' ),
249  true
250  ],
251  [
252  new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
253  new MySQLMasterPos( 'db1-bin.2', '2', '3E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
254  true
255  ],
256  [
257  new MySQLMasterPos( 'db1-bin.2', '1', '3E11FA47-71CA-11E1-9E33-C80AA9429562:99' ),
258  new MySQLMasterPos( 'db1-bin.2', '2', '1E11FA47-71CA-11E1-9E33-C80AA9429562:100' ),
259  false
260  ],
261  // MariaDB GTID style
262  [
263  new MySQLMasterPos( 'db1-bin.2', '1', '255-11-23' ),
264  new MySQLMasterPos( 'db1-bin.2', '2', '255-11-24' ),
265  true
266  ],
267  [
268  new MySQLMasterPos( 'db1-bin.2', '1', '255-11-99' ),
269  new MySQLMasterPos( 'db1-bin.2', '2', '255-11-100' ),
270  true
271  ],
272  [
273  new MySQLMasterPos( 'db1-bin.2', '1', '255-11-999' ),
274  new MySQLMasterPos( 'db1-bin.2', '2', '254-11-1000' ),
275  false
276  ],
277  ];
278  }
279 
284  $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
285  $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
286  }
287 
289  return [
290  [
291  new MySQLMasterPos( 'db1034-bin.000876', '44' ),
292  new MySQLMasterPos( 'db1034-bin.000976', '74' ),
293  true
294  ],
295  [
296  new MySQLMasterPos( 'db1052-bin.000976', '999' ),
297  new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
298  true
299  ],
300  [
301  new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
302  new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
303  false
304  ],
305  [
306  new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
307  new MySQLMasterPos( 'trump2016.000976', '10000' ),
308  false
309  ],
310  ];
311  }
312 
316  function testPtHeartbeat( $lag ) {
317  $db = $this->getMockBuilder( 'DatabaseMysql' )
318  ->disableOriginalConstructor()
319  ->setMethods( [
320  'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
321  ->getMock();
322 
323  $db->method( 'getLagDetectionMethod' )
324  ->willReturn( 'pt-heartbeat' );
325 
326  $db->method( 'getMasterServerInfo' )
327  ->willReturn( [ 'serverId' => 172, 'asOf' => time() ] );
328 
329  // Fake the current time.
330  list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
331  $now = (float)$nowSec + (float)$nowSecFrac;
332  // Fake the heartbeat time.
333  // Work arounds for weak DataTime microseconds support.
334  $ptTime = $now - $lag;
335  $ptSec = (int)$ptTime;
336  $ptSecFrac = ( $ptTime - $ptSec );
337  $ptDateTime = new DateTime( "@$ptSec" );
338  $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
339  $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
340 
341  $db->method( 'getHeartbeatData' )
342  ->with( [ 'server_id' => 172 ] )
343  ->willReturn( [ $ptTimeISO, $now ] );
344 
345  $db->setLBInfo( 'clusterMasterHost', 'db1052' );
346  $lagEst = $db->getLag();
347 
348  $this->assertGreaterThan( $lag - .010, $lagEst, "Correct heatbeat lag" );
349  $this->assertLessThan( $lag + .010, $lagEst, "Correct heatbeat lag" );
350  }
351 
352  function provideLagAmounts() {
353  return [
354  [ 0 ],
355  [ 0.3 ],
356  [ 6.5 ],
357  [ 10.1 ],
358  [ 200.2 ],
359  [ 400.7 ],
360  [ 600.22 ],
361  [ 1000.77 ],
362  ];
363  }
364 }
testListviews()
DatabaseMysqlBase::listViews.
lastErrno()
Get the last error number.
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
addIdentifierQuotes($s)
Quotes an identifier using backticks or "double quotes" depending on the database type...
Definition: Database.php:1998
static newUnspecified()
setLBInfo($name, $value=null)
Set the LB info array, or a member of it.
Definition: Database.php:481
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2106
testHasReached(MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos, $match)
provideComparePositions
testAddIdentifierQuotes($expected, $in)
provideDiapers DatabaseMysqlBase::addIdentifierQuotes
testPtHeartbeat($lag)
provideLagAmounts
Stub profiler that does nothing.
testChannelsMatch(MySQLMasterPos $pos1, MySQLMasterPos $pos2, $matches)
provideChannelPositions
channelsMatch(DBMasterPos $pos)
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same so they can t rely on Unix and must forbid reads to even standard directories like tmp lest users read each others files We cannot assume that the user has the ability to install or run any programs not written as web accessible PHP scripts Since anything that works on cheap shared hosting will work if you have shell or root access MediaWiki s design is based around catering to the lowest common denominator Although we support higher end setups as the way many things work by default is tailored toward shared hosting These defaults are unconventional from the point of view of and they certainly aren t ideal for someone who s installing MediaWiki as MediaWiki does not conform to normal Unix filesystem layout Hopefully we ll offer direct support for standard layouts in the but for now *any change to the location of files is unsupported *Moving things and leaving symlinks will *probably *not break anything
hasReached(DBMasterPos $pos)
DBMasterPos class for MySQL/MariaDB.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1940
wfWarn($msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Helper class that detects high-contention DB queries via profiling calls.
provideDiapers()
Feeds testAddIdentifierQuotes.
$res
Definition: database.txt:21
insertId()
Get the inserted value of an auto-increment row.
Fake class around abstract class so we can call concrete methods.
getServerVersion()
A string describing the current software version, like from mysql_get_server_info().
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
Database abstraction object for MySQL.
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
listViews($prefix=null, $fname=__METHOD__)
Lists all the VIEWs in the database.
Definition: Database.php:2904
Database $db
Primary database.
affectedRows()
Get the number of rows affected by the last write query.
getLag()
Get replica DB lag.
Definition: Database.php:3056
$matches