MediaWiki  1.27.1
DatabaseMysqlBaseTest.php
Go to the documentation of this file.
1 <?php
32  // From DatabaseBase
33  function __construct() {
34  }
35 
36  protected function closeConnection() {
37  }
38 
39  protected function doQuery( $sql ) {
40  }
41 
42  // From DatabaseMysql
43  protected function mysqlConnect( $realServer ) {
44  }
45 
46  protected function mysqlSetCharset( $charset ) {
47  }
48 
49  protected function mysqlFreeResult( $res ) {
50  }
51 
52  protected function mysqlFetchObject( $res ) {
53  }
54 
55  protected function mysqlFetchArray( $res ) {
56  }
57 
58  protected function mysqlNumRows( $res ) {
59  }
60 
61  protected function mysqlNumFields( $res ) {
62  }
63 
64  protected function mysqlFieldName( $res, $n ) {
65  }
66 
67  protected function mysqlFieldType( $res, $n ) {
68  }
69 
70  protected function mysqlDataSeek( $res, $row ) {
71  }
72 
73  protected function mysqlError( $conn = null ) {
74  }
75 
76  protected function mysqlFetchField( $res, $n ) {
77  }
78 
79  protected function mysqlPing() {
80  }
81 
82  protected function mysqlRealEscapeString( $s ) {
83 
84  }
85 
86  // From interface DatabaseType
87  function insertId() {
88  }
89 
90  function lastErrno() {
91  }
92 
93  function affectedRows() {
94  }
95 
96  function getServerVersion() {
97  }
98 }
99 
105  public function testAddIdentifierQuotes( $expected, $in ) {
106  $db = new FakeDatabaseMysqlBase();
107  $quoted = $db->addIdentifierQuotes( $in );
108  $this->assertEquals( $expected, $quoted );
109  }
110 
116  function provideDiapers() {
117  return [
118  // Format: expected, input
119  [ '``', '' ],
120 
121  // Yeah I really hate loosely typed PHP idiocies nowadays
122  [ '``', null ],
123 
124  // Dear codereviewer, guess what addIdentifierQuotes()
125  // will return with thoses:
126  [ '``', false ],
127  [ '`1`', true ],
128 
129  // We never know what could happen
130  [ '`0`', 0 ],
131  [ '`1`', 1 ],
132 
133  // Whatchout! Should probably use something more meaningful
134  [ "`'`", "'" ], # single quote
135  [ '`"`', '"' ], # double quote
136  [ '````', '`' ], # backtick
137  [ '`’`', '’' ], # apostrophe (look at your encyclopedia)
138 
139  // sneaky NUL bytes are lurking everywhere
140  [ '``', "\0" ],
141  [ '`xyzzy`', "\0x\0y\0z\0z\0y\0" ],
142 
143  // unicode chars
144  [
145  self::createUnicodeString( '`\u0001a\uFFFFb`' ),
146  self::createUnicodeString( '\u0001a\uFFFFb' )
147  ],
148  [
149  self::createUnicodeString( '`\u0001\uFFFF`' ),
150  self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
151  ],
152  [ '`☃`', '☃' ],
153  [ '`メインページ`', 'メインページ' ],
154  [ '`Басты_бет`', 'Басты_бет' ],
155 
156  // Real world:
157  [ '`Alix`', 'Alix' ], # while( ! $recovered ) { sleep(); }
158  [ '`Backtick: ```', 'Backtick: `' ],
159  [ '`This is a test`', 'This is a test' ],
160  ];
161  }
162 
163  private static function createUnicodeString( $str ) {
164  return json_decode( '"' . $str . '"' );
165  }
166 
167  function getMockForViews() {
168  $db = $this->getMockBuilder( 'DatabaseMysql' )
169  ->disableOriginalConstructor()
170  ->setMethods( [ 'fetchRow', 'query' ] )
171  ->getMock();
172 
173  $db->expects( $this->any() )
174  ->method( 'query' )
175  ->with( $this->anything() )
176  ->will(
177  $this->returnValue( null )
178  );
179 
180  $db->expects( $this->any() )
181  ->method( 'fetchRow' )
182  ->with( $this->anything() )
183  ->will( $this->onConsecutiveCalls(
184  [ 'Tables_in_' => 'view1' ],
185  [ 'Tables_in_' => 'view2' ],
186  [ 'Tables_in_' => 'myview' ],
187  false # no more rows
188  ) );
189  return $db;
190  }
194  function testListviews() {
195  $db = $this->getMockForViews();
196 
197  // The first call populate an internal cache of views
198  $this->assertEquals( [ 'view1', 'view2', 'myview' ],
199  $db->listViews() );
200  $this->assertEquals( [ 'view1', 'view2', 'myview' ],
201  $db->listViews() );
202 
203  // Prefix filtering
204  $this->assertEquals( [ 'view1', 'view2' ],
205  $db->listViews( 'view' ) );
206  $this->assertEquals( [ 'myview' ],
207  $db->listViews( 'my' ) );
208  $this->assertEquals( [],
209  $db->listViews( 'UNUSED_PREFIX' ) );
210  $this->assertEquals( [ 'view1', 'view2', 'myview' ],
211  $db->listViews( '' ) );
212  }
213 
218  function testIsView( $isView, $viewName ) {
219  $db = $this->getMockForViews();
220 
221  switch ( $isView ) {
222  case true:
223  $this->assertTrue( $db->isView( $viewName ),
224  "$viewName should be considered a view" );
225  break;
226 
227  case false:
228  $this->assertFalse( $db->isView( $viewName ),
229  "$viewName has not been defined as a view" );
230  break;
231  }
232 
233  }
234 
236  return [
237  // format: whether it is a view, view name
238  [ true, 'view1' ],
239  [ true, 'view2' ],
240  [ true, 'myview' ],
241 
242  [ false, 'user' ],
243 
244  [ false, 'view10' ],
245  [ false, 'my' ],
246  [ false, 'OH_MY_GOD' ], # they killed kenny!
247  ];
248  }
249 
253  function testHasReached( MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos ) {
254  $this->assertTrue( $higherPos->hasReached( $lowerPos ) );
255  $this->assertTrue( $higherPos->hasReached( $higherPos ) );
256  $this->assertTrue( $lowerPos->hasReached( $lowerPos ) );
257  $this->assertFalse( $lowerPos->hasReached( $higherPos ) );
258  }
259 
261  return [
262  [
263  new MySQLMasterPos( 'db1034-bin.000976', '843431247' ),
264  new MySQLMasterPos( 'db1034-bin.000976', '843431248' )
265  ],
266  [
267  new MySQLMasterPos( 'db1034-bin.000976', '999' ),
268  new MySQLMasterPos( 'db1034-bin.000976', '1000' )
269  ],
270  [
271  new MySQLMasterPos( 'db1034-bin.000976', '999' ),
272  new MySQLMasterPos( 'db1035-bin.000976', '1000' )
273  ],
274  ];
275  }
276 
281  $this->assertEquals( $matches, $pos1->channelsMatch( $pos2 ) );
282  $this->assertEquals( $matches, $pos2->channelsMatch( $pos1 ) );
283  }
284 
286  return [
287  [
288  new MySQLMasterPos( 'db1034-bin.000876', '44' ),
289  new MySQLMasterPos( 'db1034-bin.000976', '74' ),
290  true
291  ],
292  [
293  new MySQLMasterPos( 'db1052-bin.000976', '999' ),
294  new MySQLMasterPos( 'db1052-bin.000976', '1000' ),
295  true
296  ],
297  [
298  new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
299  new MySQLMasterPos( 'db1035-bin.000976', '10000' ),
300  false
301  ],
302  [
303  new MySQLMasterPos( 'db1066-bin.000976', '9999' ),
304  new MySQLMasterPos( 'trump2016.000976', '10000' ),
305  false
306  ],
307  ];
308  }
309 
313  function testPtHeartbeat( $lag ) {
314  $db = $this->getMockBuilder( 'DatabaseMysql' )
315  ->disableOriginalConstructor()
316  ->setMethods( [
317  'getLagDetectionMethod', 'getHeartbeatData', 'getMasterServerInfo' ] )
318  ->getMock();
319 
320  $db->expects( $this->any() )
321  ->method( 'getLagDetectionMethod' )
322  ->will( $this->returnValue( 'pt-heartbeat' ) );
323 
324  $db->expects( $this->any() )
325  ->method( 'getMasterServerInfo' )
326  ->will( $this->returnValue( [ 'serverId' => 172, 'asOf' => time() ] ) );
327 
328  // Fake the current time.
329  list( $nowSecFrac, $nowSec ) = explode( ' ', microtime() );
330  $now = (float)$nowSec + (float)$nowSecFrac;
331  // Fake the heartbeat time.
332  // Work arounds for weak DataTime microseconds support.
333  $ptTime = $now - $lag;
334  $ptSec = (int)$ptTime;
335  $ptSecFrac = ( $ptTime - $ptSec );
336  $ptDateTime = new DateTime( "@$ptSec" );
337  $ptTimeISO = $ptDateTime->format( 'Y-m-d\TH:i:s' );
338  $ptTimeISO .= ltrim( number_format( $ptSecFrac, 6 ), '0' );
339 
340  $db->expects( $this->any() )
341  ->method( 'getHeartbeatData' )
342  ->with( [ 'server_id' => 172 ] )
343  ->will( $this->returnValue( [ $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.
setLBInfo($name, $value=null)
Set the LB info array, or a member of it.
Definition: Database.php:264
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
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
listViews($prefix=null, $fname=__METHOD__)
Lists all the VIEWs in the database.
Definition: Database.php:2793
testAddIdentifierQuotes($expected, $in)
provideDiapers DatabaseMysqlBase::addIdentifierQuotes
testPtHeartbeat($lag)
provideLagAmounts
testHasReached(MySQLMasterPos $lowerPos, MySQLMasterPos $higherPos)
provideComparePositions
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)
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:1798
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().
addIdentifierQuotes($s)
Quotes an identifier using backticks or "double quotes" depending on the database type...
Definition: Database.php:1979
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.
testIsView($isView, $viewName)
DatabaseMysqlBase::isView provideViewExistanceChecks.
DatabaseBase $db
Primary database.
isView($name)
Differentiates between a TABLE and a VIEW.
Definition: Database.php:2805
affectedRows()
Get the number of rows affected by the last write query.
$matches
getLag()
Get slave lag.
Definition: Database.php:2921