MediaWiki REL1_28
DatabaseTestHelper.php
Go to the documentation of this file.
1<?php
2
8
13 protected $testName = [];
14
20 protected $lastSqls = [];
21
23 protected $nextResult = [];
24
29 protected $tablesExists;
30
31 public function __construct( $testName, array $opts = [] ) {
32 $this->testName = $testName;
33
34 $this->profiler = new ProfilerStub( [] );
35 $this->trxProfiler = new TransactionProfiler();
36 $this->cliMode = isset( $opts['cliMode'] ) ? $opts['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
49 public function getLastSqls() {
50 $lastSqls = implode( '; ', $this->lastSqls );
51 $this->lastSqls = [];
52
53 return $lastSqls;
54 }
55
56 public function setExistingTables( $tablesExists ) {
57 $this->tablesExists = (array)$tablesExists;
58 }
59
63 public function forceNextResult( $res ) {
64 $this->nextResult = $res;
65 }
66
67 protected function addSql( $sql ) {
68 // clean up spaces before and after some words and the whole string
69 $this->lastSqls[] = trim( preg_replace(
70 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
71 ' ', $sql
72 ) );
73 }
74
75 protected function checkFunctionName( $fname ) {
76 if ( substr( $fname, 0, strlen( $this->testName ) ) !== $this->testName ) {
77 throw new MWException( 'function name does not start with test class. ' .
78 $fname . ' vs. ' . $this->testName . '. ' .
79 'Please provide __METHOD__ to database methods.' );
80 }
81 }
82
83 function strencode( $s ) {
84 // Choose apos to avoid handling of escaping double quotes in quoted text
85 return str_replace( "'", "\'", $s );
86 }
87
88 public function addIdentifierQuotes( $s ) {
89 // no escaping to avoid handling of double quotes in quoted text
90 return $s;
91 }
92
93 public function query( $sql, $fname = '', $tempIgnore = false ) {
94 $this->checkFunctionName( $fname );
95 $this->addSql( $sql );
96
97 return parent::query( $sql, $fname, $tempIgnore );
98 }
99
100 public function tableExists( $table, $fname = __METHOD__ ) {
101 $tableRaw = $this->tableName( $table, 'raw' );
102 if ( isset( $this->mSessionTempTables[$tableRaw] ) ) {
103 return true; // already known to exist
104 }
105
106 $this->checkFunctionName( $fname );
107
108 return in_array( $table, (array)$this->tablesExists );
109 }
110
111 // Redeclare parent method to make it public
112 public function nativeReplace( $table, $rows, $fname ) {
113 return parent::nativeReplace( $table, $rows, $fname );
114 }
115
116 function getType() {
117 return 'test';
118 }
119
120 function open( $server, $user, $password, $dbName ) {
121 return false;
122 }
123
124 function fetchObject( $res ) {
125 return false;
126 }
127
128 function fetchRow( $res ) {
129 return false;
130 }
131
132 function numRows( $res ) {
133 return -1;
134 }
135
136 function numFields( $res ) {
137 return -1;
138 }
139
140 function fieldName( $res, $n ) {
141 return 'test';
142 }
143
144 function insertId() {
145 return -1;
146 }
147
148 function dataSeek( $res, $row ) {
149 /* nop */
150 }
151
152 function lastErrno() {
153 return -1;
154 }
155
156 function lastError() {
157 return 'test';
158 }
159
160 function fieldInfo( $table, $field ) {
161 return false;
162 }
163
164 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
165 return false;
166 }
167
168 function affectedRows() {
169 return -1;
170 }
171
172 function getSoftwareLink() {
173 return 'test';
174 }
175
176 function getServerVersion() {
177 return 'test';
178 }
179
180 function getServerInfo() {
181 return 'test';
182 }
183
184 function isOpen() {
185 return true;
186 }
187
188 function ping( &$rtt = null ) {
189 $rtt = 0.0;
190 return true;
191 }
192
193 protected function closeConnection() {
194 return false;
195 }
196
197 protected function doQuery( $sql ) {
199 $this->nextResult = [];
200
201 return new FakeResultWrapper( $res );
202 }
203}
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition Setup.php:36
static newUnspecified()
Helper for testing the methods from the Database class.
numFields( $res)
Get the number of fields in a result object.
array $nextResult
List of row arrays.
getSoftwareLink()
Returns a wikitext link to the DB's website, e.g., return "[http://www.mysql.com/ MySQL]"; Should at ...
addIdentifierQuotes( $s)
Quotes an identifier using backticks or "double quotes" depending on the database type.
strencode( $s)
Wrapper for addslashes()
ping(&$rtt=null)
Ping the server and try to reconnect if it there is no connection.
doQuery( $sql)
The DBMS-dependent part of query()
indexInfo( $table, $index, $fname='Database::indexInfo')
Get information about an index into an object.
nativeReplace( $table, $rows, $fname)
REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE statement.
numRows( $res)
Get the number of rows in a result object.
getServerVersion()
A string describing the current software version, like from mysql_get_server_info().
setExistingTables( $tablesExists)
query( $sql, $fname='', $tempIgnore=false)
Run an SQL query and return the result.
closeConnection()
Closes underlying database connection.
fetchRow( $res)
Fetch the next row from the given result object, in associative array form.
affectedRows()
Get the number of rows affected by the last write query.
__construct( $testName, array $opts=[])
getType()
Get the type of the DBMS, as it appears in $wgDBtype.
fieldName( $res, $n)
Get a field name in a result object.
fetchObject( $res)
Fetch the next row from the given result object, in object form.
getLastSqls()
Returns SQL queries grouped by '; ' Clear the list of queries that have been done so far.
lastError()
Get a description of the last error.
getServerInfo()
A string describing the current software version, and possibly other details in a user-friendly way.
dataSeek( $res, $row)
Change the position of the cursor in a result object.
tableExists( $table, $fname=__METHOD__)
Query whether a given table exists.
$testName
CLASS of the test suite, used to determine, if the function name is passed every time to query()
open( $server, $user, $password, $dbName)
Open a connection to the database.
$tablesExists
Array of tables to be considered as existing by tableExist() Use setExistingTables() to alter.
fieldInfo( $table, $field)
mysql_fetch_field() wrapper Returns false if the field doesn't exist
lastErrno()
Get the last error number.
insertId()
Get the inserted value of an auto-increment row.
isOpen()
Is a connection to the database open?
$lastSqls
Array of lastSqls passed to query(), This is an array since some methods in Database can do more than...
Relational database abstraction object.
Definition Database.php:36
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
MediaWiki exception.
Stub profiler that does nothing.
Helper class that detects high-contention DB queries via profiling calls.
$res
Definition database.txt:21
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
the array() calling protocol came about after MediaWiki 1.4rc1.
returning false will NOT prevent logging $e
Definition hooks.txt:2110
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:37