MediaWiki  1.30.0
DatabaseMysql.php
Go to the documentation of this file.
1 <?php
23 namespace Wikimedia\Rdbms;
24 
39  protected function doQuery( $sql ) {
40  $conn = $this->getBindingHandle();
41 
42  if ( $this->bufferResults() ) {
43  $ret = mysql_query( $sql, $conn );
44  } else {
45  $ret = mysql_unbuffered_query( $sql, $conn );
46  }
47 
48  return $ret;
49  }
50 
56  protected function mysqlConnect( $realServer ) {
57  # Avoid a suppressed fatal error, which is very hard to track down
58  if ( !extension_loaded( 'mysql' ) ) {
59  throw new DBConnectionError(
60  $this,
61  "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n"
62  );
63  }
64 
65  $connFlags = 0;
66  if ( $this->mFlags & self::DBO_SSL ) {
67  $connFlags |= MYSQL_CLIENT_SSL;
68  }
69  if ( $this->mFlags & self::DBO_COMPRESS ) {
70  $connFlags |= MYSQL_CLIENT_COMPRESS;
71  }
72 
73  if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
74  $numAttempts = 2;
75  } else {
76  $numAttempts = 1;
77  }
78 
79  $conn = false;
80 
81  # The kernel's default SYN retransmission period is far too slow for us,
82  # so we use a short timeout plus a manual retry. Retrying means that a small
83  # but finite rate of SYN packet loss won't cause user-visible errors.
84  for ( $i = 0; $i < $numAttempts && !$conn; $i++ ) {
85  if ( $i > 1 ) {
86  usleep( 1000 );
87  }
88  if ( $this->mFlags & self::DBO_PERSISTENT ) {
89  $conn = mysql_pconnect( $realServer, $this->mUser, $this->mPassword, $connFlags );
90  } else {
91  # Create a new connection...
92  $conn = mysql_connect( $realServer, $this->mUser, $this->mPassword, true, $connFlags );
93  }
94  }
95 
96  return $conn;
97  }
98 
103  protected function mysqlSetCharset( $charset ) {
104  $conn = $this->getBindingHandle();
105 
106  if ( function_exists( 'mysql_set_charset' ) ) {
107  return mysql_set_charset( $charset, $conn );
108  } else {
109  return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
110  }
111  }
112 
116  protected function closeConnection() {
117  $conn = $this->getBindingHandle();
118 
119  return mysql_close( $conn );
120  }
121 
125  function insertId() {
126  $conn = $this->getBindingHandle();
127 
128  return mysql_insert_id( $conn );
129  }
130 
134  function lastErrno() {
135  if ( $this->mConn ) {
136  return mysql_errno( $this->mConn );
137  } else {
138  return mysql_errno();
139  }
140  }
141 
145  function affectedRows() {
146  $conn = $this->getBindingHandle();
147 
148  return mysql_affected_rows( $conn );
149  }
150 
155  function selectDB( $db ) {
156  $conn = $this->getBindingHandle();
157 
158  $this->mDBname = $db;
159 
160  return mysql_select_db( $db, $conn );
161  }
162 
163  protected function mysqlFreeResult( $res ) {
164  return mysql_free_result( $res );
165  }
166 
167  protected function mysqlFetchObject( $res ) {
168  return mysql_fetch_object( $res );
169  }
170 
171  protected function mysqlFetchArray( $res ) {
172  return mysql_fetch_array( $res );
173  }
174 
175  protected function mysqlNumRows( $res ) {
176  return mysql_num_rows( $res );
177  }
178 
179  protected function mysqlNumFields( $res ) {
180  return mysql_num_fields( $res );
181  }
182 
183  protected function mysqlFetchField( $res, $n ) {
184  return mysql_fetch_field( $res, $n );
185  }
186 
187  protected function mysqlFieldName( $res, $n ) {
188  return mysql_field_name( $res, $n );
189  }
190 
191  protected function mysqlFieldType( $res, $n ) {
192  return mysql_field_type( $res, $n );
193  }
194 
195  protected function mysqlDataSeek( $res, $row ) {
196  return mysql_data_seek( $res, $row );
197  }
198 
199  protected function mysqlError( $conn = null ) {
200  return ( $conn !== null ) ? mysql_error( $conn ) : mysql_error(); // avoid warning
201  }
202 
203  protected function mysqlRealEscapeString( $s ) {
204  $conn = $this->getBindingHandle();
205 
206  return mysql_real_escape_string( (string)$s, $conn );
207  }
208 }
209 
210 class_alias( DatabaseMysql::class, 'DatabaseMysql' );
DBO_PERSISTENT
const DBO_PERSISTENT
Definition: defines.php:14
Wikimedia\Rdbms\Database\getBindingHandle
getBindingHandle()
Get the underlying binding handle, mConn.
Definition: Database.php:3619
Wikimedia\Rdbms\DatabaseMysql\mysqlError
mysqlError( $conn=null)
Returns the text of the error message from previous MySQL operation.
Definition: DatabaseMysql.php:199
Wikimedia\Rdbms\DatabaseMysql\mysqlFieldType
mysqlFieldType( $res, $n)
Get the type of the specified field in a result.
Definition: DatabaseMysql.php:191
Wikimedia\Rdbms\DatabaseMysql\mysqlDataSeek
mysqlDataSeek( $res, $row)
Move internal result pointer.
Definition: DatabaseMysql.php:195
Wikimedia\Rdbms\DatabaseMysql\doQuery
doQuery( $sql)
Definition: DatabaseMysql.php:39
Wikimedia\Rdbms\DatabaseMysql\mysqlFieldName
mysqlFieldName( $res, $n)
Get the name of the specified field in a result.
Definition: DatabaseMysql.php:187
Wikimedia\Rdbms\DatabaseMysql
Database abstraction object for PHP extension mysql.
Definition: DatabaseMysql.php:34
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
$s
$s
Definition: mergeMessageFileList.php:188
DBO_SSL
const DBO_SSL
Definition: defines.php:17
Wikimedia\Rdbms\DatabaseMysql\mysqlFreeResult
mysqlFreeResult( $res)
Free result memory.
Definition: DatabaseMysql.php:163
$res
$res
Definition: database.txt:21
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\DatabaseMysql\insertId
insertId()
Definition: DatabaseMysql.php:125
Wikimedia\Rdbms\DatabaseMysql\selectDB
selectDB( $db)
Definition: DatabaseMysql.php:155
Wikimedia\Rdbms\DatabaseMysql\mysqlNumFields
mysqlNumFields( $res)
Get number of fields in result.
Definition: DatabaseMysql.php:179
Wikimedia\Rdbms\DatabaseMysql\mysqlFetchField
mysqlFetchField( $res, $n)
Get column information from a result.
Definition: DatabaseMysql.php:183
Wikimedia\Rdbms\DatabaseMysql\affectedRows
affectedRows()
Definition: DatabaseMysql.php:145
DBO_COMPRESS
const DBO_COMPRESS
Definition: defines.php:18
Wikimedia\Rdbms\DatabaseMysql\closeConnection
closeConnection()
Definition: DatabaseMysql.php:116
Wikimedia\Rdbms\DatabaseMysql\mysqlConnect
mysqlConnect( $realServer)
Definition: DatabaseMysql.php:56
Wikimedia\Rdbms\DatabaseMysql\mysqlRealEscapeString
mysqlRealEscapeString( $s)
Definition: DatabaseMysql.php:203
$ret
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 & $ret
Definition: hooks.txt:1965
Wikimedia\Rdbms\Database\query
query( $sql, $fname=__METHOD__, $tempIgnore=false)
Run an SQL query and return the result.
Definition: Database.php:888
Wikimedia\Rdbms\Database\bufferResults
bufferResults( $buffer=null)
Turns buffering of SQL result sets on (true) or off (false).
Definition: Database.php:438
Wikimedia\Rdbms\DBConnectionError
Definition: DBConnectionError.php:26
Wikimedia\Rdbms\DatabaseMysql\mysqlFetchObject
mysqlFetchObject( $res)
Fetch a result row as an object.
Definition: DatabaseMysql.php:167
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Wikimedia\Rdbms\DatabaseMysql\mysqlNumRows
mysqlNumRows( $res)
Get number of rows in result.
Definition: DatabaseMysql.php:175
Wikimedia\Rdbms\DatabaseMysql\lastErrno
lastErrno()
Definition: DatabaseMysql.php:134
Wikimedia\Rdbms\DatabaseMysql\mysqlSetCharset
mysqlSetCharset( $charset)
Definition: DatabaseMysql.php:103
Wikimedia\Rdbms\DatabaseMysqlBase
Database abstraction object for MySQL.
Definition: DatabaseMysqlBase.php:40
Wikimedia\Rdbms\DatabaseMysql\mysqlFetchArray
mysqlFetchArray( $res)
Fetch a result row as an associative and numeric array.
Definition: DatabaseMysql.php:171