MediaWiki  1.30.2
DatabaseTestHelper.php
Go to the documentation of this file.
1 <?php
2 
5 
10 class DatabaseTestHelper extends Database {
11 
16  protected $testName = [];
17 
23  protected $lastSqls = [];
24 
26  protected $nextResult = [];
27 
32  protected $tablesExists;
33 
37  protected $unionSupportsOrderAndLimit = true;
38 
39  public function __construct( $testName, array $opts = [] ) {
40  $this->testName = $testName;
41 
42  $this->profiler = new ProfilerStub( [] );
43  $this->trxProfiler = new TransactionProfiler();
44  $this->cliMode = isset( $opts['cliMode'] ) ? $opts['cliMode'] : true;
45  $this->connLogger = new \Psr\Log\NullLogger();
46  $this->queryLogger = new \Psr\Log\NullLogger();
47  $this->errorLogger = function ( Exception $e ) {
48  wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
49  };
50  $this->currentDomain = DatabaseDomain::newUnspecified();
51  }
52 
58  public function getLastSqls() {
59  $lastSqls = implode( '; ', $this->lastSqls );
60  $this->lastSqls = [];
61 
62  return $lastSqls;
63  }
64 
65  public function setExistingTables( $tablesExists ) {
66  $this->tablesExists = (array)$tablesExists;
67  }
68 
72  public function forceNextResult( $res ) {
73  $this->nextResult = $res;
74  }
75 
76  protected function addSql( $sql ) {
77  // clean up spaces before and after some words and the whole string
78  $this->lastSqls[] = trim( preg_replace(
79  '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
80  ' ', $sql
81  ) );
82  }
83 
84  protected function checkFunctionName( $fname ) {
85  if ( substr( $fname, 0, strlen( $this->testName ) ) !== $this->testName ) {
86  throw new MWException( 'function name does not start with test class. ' .
87  $fname . ' vs. ' . $this->testName . '. ' .
88  'Please provide __METHOD__ to database methods.' );
89  }
90  }
91 
92  function strencode( $s ) {
93  // Choose apos to avoid handling of escaping double quotes in quoted text
94  return str_replace( "'", "\'", $s );
95  }
96 
97  public function addIdentifierQuotes( $s ) {
98  // no escaping to avoid handling of double quotes in quoted text
99  return $s;
100  }
101 
102  public function query( $sql, $fname = '', $tempIgnore = false ) {
103  $this->checkFunctionName( $fname );
104  $this->addSql( $sql );
105 
106  return parent::query( $sql, $fname, $tempIgnore );
107  }
108 
109  public function tableExists( $table, $fname = __METHOD__ ) {
110  $tableRaw = $this->tableName( $table, 'raw' );
111  if ( isset( $this->mSessionTempTables[$tableRaw] ) ) {
112  return true; // already known to exist
113  }
114 
115  $this->checkFunctionName( $fname );
116 
117  return in_array( $table, (array)$this->tablesExists );
118  }
119 
120  // Redeclare parent method to make it public
121  public function nativeReplace( $table, $rows, $fname ) {
122  return parent::nativeReplace( $table, $rows, $fname );
123  }
124 
125  function getType() {
126  return 'test';
127  }
128 
129  function open( $server, $user, $password, $dbName ) {
130  return false;
131  }
132 
133  function fetchObject( $res ) {
134  return false;
135  }
136 
137  function fetchRow( $res ) {
138  return false;
139  }
140 
141  function numRows( $res ) {
142  return -1;
143  }
144 
145  function numFields( $res ) {
146  return -1;
147  }
148 
149  function fieldName( $res, $n ) {
150  return 'test';
151  }
152 
153  function insertId() {
154  return -1;
155  }
156 
157  function dataSeek( $res, $row ) {
158  /* nop */
159  }
160 
161  function lastErrno() {
162  return -1;
163  }
164 
165  function lastError() {
166  return 'test';
167  }
168 
169  function fieldInfo( $table, $field ) {
170  return false;
171  }
172 
173  function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
174  return false;
175  }
176 
177  function affectedRows() {
178  return -1;
179  }
180 
181  function getSoftwareLink() {
182  return 'test';
183  }
184 
185  function getServerVersion() {
186  return 'test';
187  }
188 
189  function getServerInfo() {
190  return 'test';
191  }
192 
193  function isOpen() {
194  return true;
195  }
196 
197  function ping( &$rtt = null ) {
198  $rtt = 0.0;
199  return true;
200  }
201 
202  protected function closeConnection() {
203  return false;
204  }
205 
206  protected function doQuery( $sql ) {
208  $this->nextResult = [];
209 
210  return new FakeResultWrapper( $res );
211  }
212 
213  public function unionSupportsOrderAndLimit() {
215  }
216 
217  public function setUnionSupportsOrderAndLimit( $v ) {
218  $this->unionSupportsOrderAndLimit = (bool)$v;
219  }
220 }
DatabaseTestHelper\$testName
$testName
CLASS of the test suite, used to determine, if the function name is passed every time to query()
Definition: DatabaseTestHelper.php:16
DatabaseTestHelper\getType
getType()
Definition: DatabaseTestHelper.php:125
DatabaseTestHelper\setUnionSupportsOrderAndLimit
setUnionSupportsOrderAndLimit( $v)
Definition: DatabaseTestHelper.php:217
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
query
For a write query
Definition: database.txt:26
DatabaseTestHelper\isOpen
isOpen()
Definition: DatabaseTestHelper.php:193
DatabaseTestHelper\numFields
numFields( $res)
Definition: DatabaseTestHelper.php:145
DatabaseTestHelper\fieldInfo
fieldInfo( $table, $field)
Definition: DatabaseTestHelper.php:169
DatabaseTestHelper\getLastSqls
getLastSqls()
Returns SQL queries grouped by '; ' Clear the list of queries that have been done so far.
Definition: DatabaseTestHelper.php:58
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
ProfilerStub
Stub profiler that does nothing.
Definition: ProfilerStub.php:29
$fname
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition: Setup.php:36
$s
$s
Definition: mergeMessageFileList.php:188
$res
$res
Definition: database.txt:21
DatabaseTestHelper\doQuery
doQuery( $sql)
Definition: DatabaseTestHelper.php:206
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
DatabaseTestHelper\nativeReplace
nativeReplace( $table, $rows, $fname)
Definition: DatabaseTestHelper.php:121
DatabaseTestHelper\$nextResult
array $nextResult
List of row arrays.
Definition: DatabaseTestHelper.php:26
DatabaseTestHelper\fetchRow
fetchRow( $res)
Definition: DatabaseTestHelper.php:137
DatabaseTestHelper\tableExists
tableExists( $table, $fname=__METHOD__)
Definition: DatabaseTestHelper.php:109
DatabaseTestHelper\getSoftwareLink
getSoftwareLink()
Definition: DatabaseTestHelper.php:181
MWException
MediaWiki exception.
Definition: MWException.php:26
DatabaseTestHelper\strencode
strencode( $s)
Definition: DatabaseTestHelper.php:92
DatabaseTestHelper\unionSupportsOrderAndLimit
unionSupportsOrderAndLimit()
Definition: DatabaseTestHelper.php:213
tableName
We use the convention $dbr for read and $dbw for write to help you keep track of whether the database object is a the world will explode Or to be a subsequent write query which succeeded on the master may fail when replicated to the slave due to a unique key collision Replication on the slave will stop and it may take hours to repair the database and get it back online Setting read_only in my cnf on the slave will avoid this but given the dire we prefer to have as many checks as possible We provide a but the wrapper functions like please read the documentation for tableName() and addQuotes(). You will need both of them. ------------------------------------------------------------------------ Basic query optimisation ------------------------------------------------------------------------ MediaWiki developers who need to write DB queries should have some understanding of databases and the performance issues associated with them. Patches containing unacceptably slow features will not be accepted. Unindexed queries are generally not welcome in MediaWiki
DatabaseTestHelper\lastError
lastError()
Definition: DatabaseTestHelper.php:165
DatabaseTestHelper\$tablesExists
$tablesExists
Array of tables to be considered as existing by tableExist() Use setExistingTables() to alter.
Definition: DatabaseTestHelper.php:32
DatabaseTestHelper\setExistingTables
setExistingTables( $tablesExists)
Definition: DatabaseTestHelper.php:65
DatabaseTestHelper\query
query( $sql, $fname='', $tempIgnore=false)
Definition: DatabaseTestHelper.php:102
DatabaseTestHelper\ping
ping(&$rtt=null)
Definition: DatabaseTestHelper.php:197
DatabaseTestHelper\numRows
numRows( $res)
Definition: DatabaseTestHelper.php:141
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2141
DatabaseTestHelper\getServerInfo
getServerInfo()
Definition: DatabaseTestHelper.php:189
DatabaseTestHelper\getServerVersion
getServerVersion()
Definition: DatabaseTestHelper.php:185
DatabaseTestHelper\addIdentifierQuotes
addIdentifierQuotes( $s)
Definition: DatabaseTestHelper.php:97
DatabaseTestHelper\insertId
insertId()
Definition: DatabaseTestHelper.php:153
DatabaseTestHelper\checkFunctionName
checkFunctionName( $fname)
Definition: DatabaseTestHelper.php:84
DatabaseTestHelper\dataSeek
dataSeek( $res, $row)
Definition: DatabaseTestHelper.php:157
DatabaseTestHelper
Helper for testing the methods from the Database class.
Definition: DatabaseTestHelper.php:10
DatabaseTestHelper\fieldName
fieldName( $res, $n)
Definition: DatabaseTestHelper.php:149
DatabaseTestHelper\addSql
addSql( $sql)
Definition: DatabaseTestHelper.php:76
$rows
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition: hooks.txt:2581
DatabaseTestHelper\indexInfo
indexInfo( $table, $index, $fname='Database::indexInfo')
Definition: DatabaseTestHelper.php:173
DatabaseTestHelper\$lastSqls
$lastSqls
Array of lastSqls passed to query(), This is an array since some methods in Database can do more than...
Definition: DatabaseTestHelper.php:23
DatabaseTestHelper\forceNextResult
forceNextResult( $res)
Definition: DatabaseTestHelper.php:72
DatabaseTestHelper\open
open( $server, $user, $password, $dbName)
Definition: DatabaseTestHelper.php:129
DatabaseTestHelper\closeConnection
closeConnection()
Definition: DatabaseTestHelper.php:202
DatabaseTestHelper\$unionSupportsOrderAndLimit
$unionSupportsOrderAndLimit
Value to return from unionSupportsOrderAndLimit()
Definition: DatabaseTestHelper.php:37
wfWarn
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Definition: GlobalFunctions.php:1190
DatabaseTestHelper\affectedRows
affectedRows()
Definition: DatabaseTestHelper.php:177
DatabaseTestHelper\fetchObject
fetchObject( $res)
Definition: DatabaseTestHelper.php:133
DatabaseTestHelper\__construct
__construct( $testName, array $opts=[])
Definition: DatabaseTestHelper.php:39
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/prefix specification for IDatabase domains.
Definition: DatabaseDomain.php:28
Wikimedia\Rdbms\TransactionProfiler
Helper class that detects high-contention DB queries via profiling calls.
Definition: TransactionProfiler.php:38
DatabaseTestHelper\lastErrno
lastErrno()
Definition: DatabaseTestHelper.php:161
array
the array() calling protocol came about after MediaWiki 1.4rc1.