MediaWiki  1.29.2
DatabaseMysql.php
Go to the documentation of this file.
1 <?php
23 namespace Wikimedia\Rdbms;
24 
36  protected function doQuery( $sql ) {
37  $conn = $this->getBindingHandle();
38 
39  if ( $this->bufferResults() ) {
40  $ret = mysql_query( $sql, $conn );
41  } else {
42  $ret = mysql_unbuffered_query( $sql, $conn );
43  }
44 
45  return $ret;
46  }
47 
53  protected function mysqlConnect( $realServer ) {
54  # Avoid a suppressed fatal error, which is very hard to track down
55  if ( !extension_loaded( 'mysql' ) ) {
56  throw new DBConnectionError(
57  $this,
58  "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n"
59  );
60  }
61 
62  $connFlags = 0;
63  if ( $this->mFlags & self::DBO_SSL ) {
64  $connFlags |= MYSQL_CLIENT_SSL;
65  }
66  if ( $this->mFlags & self::DBO_COMPRESS ) {
67  $connFlags |= MYSQL_CLIENT_COMPRESS;
68  }
69 
70  if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
71  $numAttempts = 2;
72  } else {
73  $numAttempts = 1;
74  }
75 
76  $conn = false;
77 
78  # The kernel's default SYN retransmission period is far too slow for us,
79  # so we use a short timeout plus a manual retry. Retrying means that a small
80  # but finite rate of SYN packet loss won't cause user-visible errors.
81  for ( $i = 0; $i < $numAttempts && !$conn; $i++ ) {
82  if ( $i > 1 ) {
83  usleep( 1000 );
84  }
85  if ( $this->mFlags & self::DBO_PERSISTENT ) {
86  $conn = mysql_pconnect( $realServer, $this->mUser, $this->mPassword, $connFlags );
87  } else {
88  # Create a new connection...
89  $conn = mysql_connect( $realServer, $this->mUser, $this->mPassword, true, $connFlags );
90  }
91  }
92 
93  return $conn;
94  }
95 
100  protected function mysqlSetCharset( $charset ) {
101  $conn = $this->getBindingHandle();
102 
103  if ( function_exists( 'mysql_set_charset' ) ) {
104  return mysql_set_charset( $charset, $conn );
105  } else {
106  return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
107  }
108  }
109 
113  protected function closeConnection() {
114  $conn = $this->getBindingHandle();
115 
116  return mysql_close( $conn );
117  }
118 
122  function insertId() {
123  $conn = $this->getBindingHandle();
124 
125  return mysql_insert_id( $conn );
126  }
127 
131  function lastErrno() {
132  if ( $this->mConn ) {
133  return mysql_errno( $this->mConn );
134  } else {
135  return mysql_errno();
136  }
137  }
138 
142  function affectedRows() {
143  $conn = $this->getBindingHandle();
144 
145  return mysql_affected_rows( $conn );
146  }
147 
152  function selectDB( $db ) {
153  $conn = $this->getBindingHandle();
154 
155  $this->mDBname = $db;
156 
157  return mysql_select_db( $db, $conn );
158  }
159 
160  protected function mysqlFreeResult( $res ) {
161  return mysql_free_result( $res );
162  }
163 
164  protected function mysqlFetchObject( $res ) {
165  return mysql_fetch_object( $res );
166  }
167 
168  protected function mysqlFetchArray( $res ) {
169  return mysql_fetch_array( $res );
170  }
171 
172  protected function mysqlNumRows( $res ) {
173  return mysql_num_rows( $res );
174  }
175 
176  protected function mysqlNumFields( $res ) {
177  return mysql_num_fields( $res );
178  }
179 
180  protected function mysqlFetchField( $res, $n ) {
181  return mysql_fetch_field( $res, $n );
182  }
183 
184  protected function mysqlFieldName( $res, $n ) {
185  return mysql_field_name( $res, $n );
186  }
187 
188  protected function mysqlFieldType( $res, $n ) {
189  return mysql_field_type( $res, $n );
190  }
191 
192  protected function mysqlDataSeek( $res, $row ) {
193  return mysql_data_seek( $res, $row );
194  }
195 
196  protected function mysqlError( $conn = null ) {
197  return ( $conn !== null ) ? mysql_error( $conn ) : mysql_error(); // avoid warning
198  }
199 
200  protected function mysqlRealEscapeString( $s ) {
201  $conn = $this->getBindingHandle();
202 
203  return mysql_real_escape_string( (string)$s, $conn );
204  }
205 }
206 
207 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:3426
Wikimedia\Rdbms\DatabaseMysql\mysqlError
mysqlError( $conn=null)
Returns the text of the error message from previous MySQL operation.
Definition: DatabaseMysql.php:196
Wikimedia\Rdbms\DatabaseMysql\mysqlFieldType
mysqlFieldType( $res, $n)
Get the type of the specified field in a result.
Definition: DatabaseMysql.php:188
Wikimedia\Rdbms\DatabaseMysql\mysqlDataSeek
mysqlDataSeek( $res, $row)
Move internal result pointer.
Definition: DatabaseMysql.php:192
Wikimedia\Rdbms\DatabaseMysql\doQuery
doQuery( $sql)
Definition: DatabaseMysql.php:36
Wikimedia\Rdbms\DatabaseMysql\mysqlFieldName
mysqlFieldName( $res, $n)
Get the name of the specified field in a result.
Definition: DatabaseMysql.php:184
Wikimedia\Rdbms\DatabaseMysql
Database abstraction object for PHP extension mysql.
Definition: DatabaseMysql.php:31
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:160
$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:122
Wikimedia\Rdbms\DatabaseMysql\selectDB
selectDB( $db)
Definition: DatabaseMysql.php:152
Wikimedia\Rdbms\DatabaseMysql\mysqlNumFields
mysqlNumFields( $res)
Get number of fields in result.
Definition: DatabaseMysql.php:176
Wikimedia\Rdbms\DatabaseMysql\mysqlFetchField
mysqlFetchField( $res, $n)
Get column information from a result.
Definition: DatabaseMysql.php:180
Wikimedia\Rdbms\DatabaseMysql\affectedRows
affectedRows()
Definition: DatabaseMysql.php:142
DBO_COMPRESS
const DBO_COMPRESS
Definition: defines.php:18
Wikimedia\Rdbms\DatabaseMysql\closeConnection
closeConnection()
Definition: DatabaseMysql.php:113
Wikimedia\Rdbms\DatabaseMysql\mysqlConnect
mysqlConnect( $realServer)
Definition: DatabaseMysql.php:53
Wikimedia\Rdbms\DatabaseMysql\mysqlRealEscapeString
mysqlRealEscapeString( $s)
Definition: DatabaseMysql.php:200
$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:1956
Wikimedia\Rdbms\Database\query
query( $sql, $fname=__METHOD__, $tempIgnore=false)
Run an SQL query and return the result.
Definition: Database.php:850
Wikimedia\Rdbms\Database\bufferResults
bufferResults( $buffer=null)
Turns buffering of SQL result sets on (true) or off (false).
Definition: Database.php:427
Wikimedia\Rdbms\DBConnectionError
Definition: DBConnectionError.php:26
Wikimedia\Rdbms\DatabaseMysql\mysqlFetchObject
mysqlFetchObject( $res)
Fetch a result row as an object.
Definition: DatabaseMysql.php:164
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:172
Wikimedia\Rdbms\DatabaseMysql\lastErrno
lastErrno()
Definition: DatabaseMysql.php:131
Wikimedia\Rdbms\DatabaseMysql\mysqlSetCharset
mysqlSetCharset( $charset)
Definition: DatabaseMysql.php:100
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:168