fix bug #29102: Upgrade fails "Unknown character set: 'mysql4'"
[lhc/web/wiklou.git] / includes / db / DatabaseMysql.php
1 <?php
2 /**
3 * This is the MySQL database abstraction layer.
4 *
5 * @file
6 * @ingroup Database
7 */
8
9 /**
10 * Database abstraction object for mySQL
11 * Inherit all methods and properties of Database::Database()
12 *
13 * @ingroup Database
14 * @see Database
15 */
16 class DatabaseMysql extends DatabaseBase {
17
18 /**
19 * @return string
20 */
21 function getType() {
22 return 'mysql';
23 }
24
25 protected function doQuery( $sql ) {
26 if( $this->bufferResults() ) {
27 $ret = mysql_query( $sql, $this->mConn );
28 } else {
29 $ret = mysql_unbuffered_query( $sql, $this->mConn );
30 }
31 return $ret;
32 }
33
34 function open( $server, $user, $password, $dbName ) {
35 global $wgAllDBsAreLocalhost;
36 wfProfileIn( __METHOD__ );
37
38 # Load mysql.so if we don't have it
39 wfDl( 'mysql' );
40
41 # Fail now
42 # Otherwise we get a suppressed fatal error, which is very hard to track down
43 if ( !function_exists( 'mysql_connect' ) ) {
44 throw new DBConnectionError( $this, "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n" );
45 }
46
47 # Debugging hack -- fake cluster
48 if ( $wgAllDBsAreLocalhost ) {
49 $realServer = 'localhost';
50 } else {
51 $realServer = $server;
52 }
53 $this->close();
54 $this->mServer = $server;
55 $this->mUser = $user;
56 $this->mPassword = $password;
57 $this->mDBname = $dbName;
58
59 wfProfileIn("dbconnect-$server");
60
61 # The kernel's default SYN retransmission period is far too slow for us,
62 # so we use a short timeout plus a manual retry. Retrying means that a small
63 # but finite rate of SYN packet loss won't cause user-visible errors.
64 $this->mConn = false;
65 if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
66 $numAttempts = 2;
67 } else {
68 $numAttempts = 1;
69 }
70 $this->installErrorHandler();
71 for ( $i = 0; $i < $numAttempts && !$this->mConn; $i++ ) {
72 if ( $i > 1 ) {
73 usleep( 1000 );
74 }
75 if ( $this->mFlags & DBO_PERSISTENT ) {
76 $this->mConn = mysql_pconnect( $realServer, $user, $password );
77 } else {
78 # Create a new connection...
79 $this->mConn = mysql_connect( $realServer, $user, $password, true );
80 }
81 #if ( $this->mConn === false ) {
82 #$iplus = $i + 1;
83 #wfLogDBError("Connect loop error $iplus of $max ($server): " . mysql_errno() . " - " . mysql_error()."\n");
84 #}
85 }
86 $phpError = $this->restoreErrorHandler();
87 # Always log connection errors
88 if ( !$this->mConn ) {
89 $error = $this->lastError();
90 if ( !$error ) {
91 $error = $phpError;
92 }
93 wfLogDBError( "Error connecting to {$this->mServer}: $error\n" );
94 wfDebug( "DB connection error\n" );
95 wfDebug( "Server: $server, User: $user, Password: " .
96 substr( $password, 0, 3 ) . "..., error: " . mysql_error() . "\n" );
97 }
98
99 wfProfileOut("dbconnect-$server");
100
101 if ( $dbName != '' && $this->mConn !== false ) {
102 wfSuppressWarnings();
103 $success = mysql_select_db( $dbName, $this->mConn );
104 wfRestoreWarnings();
105 if ( !$success ) {
106 $error = "Error selecting database $dbName on server {$this->mServer} " .
107 "from client host " . wfHostname() . "\n";
108 wfLogDBError(" Error selecting database $dbName on server {$this->mServer} \n");
109 wfDebug( $error );
110 }
111 } else {
112 # Delay USE query
113 $success = (bool)$this->mConn;
114 }
115
116 if ( $success ) {
117 $version = $this->getServerVersion();
118 if ( version_compare( $version, '4.1' ) >= 0 ) {
119 // Tell the server we're communicating with it in UTF-8.
120 // This may engage various charset conversions.
121 global $wgDBmysql5;
122 if( $wgDBmysql5 ) {
123 $this->query( 'SET NAMES utf8', __METHOD__ );
124 } else {
125 $this->query( 'SET NAMES binary', __METHOD__ );
126 }
127 // Set SQL mode, default is turning them all off, can be overridden or skipped with null
128 global $wgSQLMode;
129 if ( is_string( $wgSQLMode ) ) {
130 $mode = $this->addQuotes( $wgSQLMode );
131 $this->query( "SET sql_mode = $mode", __METHOD__ );
132 }
133 }
134
135 // Turn off strict mode if it is on
136 } else {
137 $this->reportConnectionError( $phpError );
138 }
139
140 $this->mOpened = $success;
141 wfProfileOut( __METHOD__ );
142 return $success;
143 }
144
145 /**
146 * @return bool
147 */
148 function close() {
149 $this->mOpened = false;
150 if ( $this->mConn ) {
151 if ( $this->trxLevel() ) {
152 $this->commit();
153 }
154 return mysql_close( $this->mConn );
155 } else {
156 return true;
157 }
158 }
159
160 function freeResult( $res ) {
161 if ( $res instanceof ResultWrapper ) {
162 $res = $res->result;
163 }
164 wfSuppressWarnings();
165 $ok = mysql_free_result( $res );
166 wfRestoreWarnings();
167 if ( !$ok ) {
168 throw new DBUnexpectedError( $this, "Unable to free MySQL result" );
169 }
170 }
171
172 function fetchObject( $res ) {
173 if ( $res instanceof ResultWrapper ) {
174 $res = $res->result;
175 }
176 wfSuppressWarnings();
177 $row = mysql_fetch_object( $res );
178 wfRestoreWarnings();
179 if( $this->lastErrno() ) {
180 throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() ) );
181 }
182 return $row;
183 }
184
185 function fetchRow( $res ) {
186 if ( $res instanceof ResultWrapper ) {
187 $res = $res->result;
188 }
189 wfSuppressWarnings();
190 $row = mysql_fetch_array( $res );
191 wfRestoreWarnings();
192 if ( $this->lastErrno() ) {
193 throw new DBUnexpectedError( $this, 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() ) );
194 }
195 return $row;
196 }
197
198 /**
199 * @throws DBUnexpectedError
200 * @param $res
201 * @return int
202 */
203 function numRows( $res ) {
204 if ( $res instanceof ResultWrapper ) {
205 $res = $res->result;
206 }
207 wfSuppressWarnings();
208 $n = mysql_num_rows( $res );
209 wfRestoreWarnings();
210 if( $this->lastErrno() ) {
211 throw new DBUnexpectedError( $this, 'Error in numRows(): ' . htmlspecialchars( $this->lastError() ) );
212 }
213 return $n;
214 }
215
216 /**
217 * @param $res
218 * @return int
219 */
220 function numFields( $res ) {
221 if ( $res instanceof ResultWrapper ) {
222 $res = $res->result;
223 }
224 return mysql_num_fields( $res );
225 }
226
227 /**
228 * @param $res
229 * @param $n
230 * @return string
231 */
232 function fieldName( $res, $n ) {
233 if ( $res instanceof ResultWrapper ) {
234 $res = $res->result;
235 }
236 return mysql_field_name( $res, $n );
237 }
238
239 /**
240 * @return int
241 */
242 function insertId() {
243 return mysql_insert_id( $this->mConn );
244 }
245
246 function dataSeek( $res, $row ) {
247 if ( $res instanceof ResultWrapper ) {
248 $res = $res->result;
249 }
250 return mysql_data_seek( $res, $row );
251 }
252
253 function lastErrno() {
254 if ( $this->mConn ) {
255 return mysql_errno( $this->mConn );
256 } else {
257 return mysql_errno();
258 }
259 }
260
261 function lastError() {
262 if ( $this->mConn ) {
263 # Even if it's non-zero, it can still be invalid
264 wfSuppressWarnings();
265 $error = mysql_error( $this->mConn );
266 if ( !$error ) {
267 $error = mysql_error();
268 }
269 wfRestoreWarnings();
270 } else {
271 $error = mysql_error();
272 }
273 if( $error ) {
274 $error .= ' (' . $this->mServer . ')';
275 }
276 return $error;
277 }
278
279 /**
280 * @return int
281 */
282 function affectedRows() {
283 return mysql_affected_rows( $this->mConn );
284 }
285
286 function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabaseMysql::replace' ) {
287 return $this->nativeReplace( $table, $rows, $fname );
288 }
289
290 /**
291 * Estimate rows in dataset
292 * Returns estimated count, based on EXPLAIN output
293 * Takes same arguments as Database::select()
294 *
295 * @return int
296 */
297 public function estimateRowCount( $table, $vars='*', $conds='', $fname = 'DatabaseMysql::estimateRowCount', $options = array() ) {
298 $options['EXPLAIN'] = true;
299 $res = $this->select( $table, $vars, $conds, $fname, $options );
300 if ( $res === false ) {
301 return false;
302 }
303 if ( !$this->numRows( $res ) ) {
304 return 0;
305 }
306
307 $rows = 1;
308 foreach ( $res as $plan ) {
309 $rows *= $plan->rows > 0 ? $plan->rows : 1; // avoid resetting to zero
310 }
311 return $rows;
312 }
313
314 /**
315 * @param $table string
316 * @param $field string
317 * @return bool|MySQLField
318 */
319 function fieldInfo( $table, $field ) {
320 $table = $this->tableName( $table );
321 $res = $this->query( "SELECT * FROM $table LIMIT 1", __METHOD__, true );
322 if ( !$res ) {
323 return false;
324 }
325 $n = mysql_num_fields( $res->result );
326 for( $i = 0; $i < $n; $i++ ) {
327 $meta = mysql_fetch_field( $res->result, $i );
328 if( $field == $meta->name ) {
329 return new MySQLField($meta);
330 }
331 }
332 return false;
333 }
334
335 /**
336 * Get information about an index into an object
337 * Returns false if the index does not exist
338 *
339 * @return false|array
340 */
341 function indexInfo( $table, $index, $fname = 'DatabaseMysql::indexInfo' ) {
342 # SHOW INDEX works in MySQL 3.23.58, but SHOW INDEXES does not.
343 # SHOW INDEX should work for 3.x and up:
344 # http://dev.mysql.com/doc/mysql/en/SHOW_INDEX.html
345 $table = $this->tableName( $table );
346 $index = $this->indexName( $index );
347 $sql = 'SHOW INDEX FROM ' . $table;
348 $res = $this->query( $sql, $fname );
349
350 if ( !$res ) {
351 return null;
352 }
353
354 $result = array();
355
356 foreach ( $res as $row ) {
357 if ( $row->Key_name == $index ) {
358 $result[] = $row;
359 }
360 }
361
362 return empty( $result ) ? false : $result;
363 }
364
365 /**
366 * @param $db
367 * @return bool
368 */
369 function selectDB( $db ) {
370 $this->mDBname = $db;
371 return mysql_select_db( $db, $this->mConn );
372 }
373
374 /**
375 * @param $s string
376 *
377 * @return string
378 */
379 function strencode( $s ) {
380 $sQuoted = mysql_real_escape_string( $s, $this->mConn );
381
382 if($sQuoted === false) {
383 $this->ping();
384 $sQuoted = mysql_real_escape_string( $s, $this->mConn );
385 }
386 return $sQuoted;
387 }
388
389 /**
390 * MySQL uses `backticks` for identifier quoting instead of the sql standard "double quotes".
391 *
392 * @param $s string
393 *
394 * @return string
395 */
396 public function addIdentifierQuotes( $s ) {
397 return "`" . $this->strencode( $s ) . "`";
398 }
399
400 /**
401 * @param $name string
402 * @return bool
403 */
404 public function isQuotedIdentifier( $name ) {
405 return strlen( $name ) && $name[0] == '`' && substr( $name, -1, 1 ) == '`';
406 }
407
408 /**
409 * @return bool
410 */
411 function ping() {
412 $ping = mysql_ping( $this->mConn );
413 if ( $ping ) {
414 return true;
415 }
416
417 mysql_close( $this->mConn );
418 $this->mOpened = false;
419 $this->mConn = false;
420 $this->open( $this->mServer, $this->mUser, $this->mPassword, $this->mDBname );
421 return true;
422 }
423
424 /**
425 * Returns slave lag.
426 *
427 * On MySQL 4.1.9 and later, this will do a SHOW SLAVE STATUS. On earlier
428 * versions of MySQL, it uses SHOW PROCESSLIST, which requires the PROCESS
429 * privilege.
430 *
431 * @return int
432 */
433 function getLag() {
434 if ( !is_null( $this->mFakeSlaveLag ) ) {
435 wfDebug( "getLag: fake slave lagged {$this->mFakeSlaveLag} seconds\n" );
436 return $this->mFakeSlaveLag;
437 }
438
439 if ( version_compare( $this->getServerVersion(), '4.1.9', '>=' ) ) {
440 return $this->getLagFromSlaveStatus();
441 } else {
442 return $this->getLagFromProcesslist();
443 }
444 }
445
446 /**
447 * @return bool|int
448 */
449 function getLagFromSlaveStatus() {
450 $res = $this->query( 'SHOW SLAVE STATUS', __METHOD__ );
451 if ( !$res ) {
452 return false;
453 }
454 $row = $res->fetchObject();
455 if ( !$row ) {
456 return false;
457 }
458 if ( strval( $row->Seconds_Behind_Master ) === '' ) {
459 return false;
460 } else {
461 return intval( $row->Seconds_Behind_Master );
462 }
463 }
464
465 /**
466 * @return bool|int
467 */
468 function getLagFromProcesslist() {
469 $res = $this->query( 'SHOW PROCESSLIST', __METHOD__ );
470 if( !$res ) {
471 return false;
472 }
473 # Find slave SQL thread
474 foreach( $res as $row ) {
475 /* This should work for most situations - when default db
476 * for thread is not specified, it had no events executed,
477 * and therefore it doesn't know yet how lagged it is.
478 *
479 * Relay log I/O thread does not select databases.
480 */
481 if ( $row->User == 'system user' &&
482 $row->State != 'Waiting for master to send event' &&
483 $row->State != 'Connecting to master' &&
484 $row->State != 'Queueing master event to the relay log' &&
485 $row->State != 'Waiting for master update' &&
486 $row->State != 'Requesting binlog dump' &&
487 $row->State != 'Waiting to reconnect after a failed master event read' &&
488 $row->State != 'Reconnecting after a failed master event read' &&
489 $row->State != 'Registering slave on master'
490 ) {
491 # This is it, return the time (except -ve)
492 if ( $row->Time > 0x7fffffff ) {
493 return false;
494 } else {
495 return $row->Time;
496 }
497 }
498 }
499 return false;
500 }
501
502 /**
503 * Wait for the slave to catch up to a given master position.
504 *
505 * @param $pos DBMasterPos object
506 * @param $timeout Integer: the maximum number of seconds to wait for synchronisation
507 */
508 function masterPosWait( DBMasterPos $pos, $timeout ) {
509 $fname = 'DatabaseBase::masterPosWait';
510 wfProfileIn( $fname );
511
512 # Commit any open transactions
513 if ( $this->mTrxLevel ) {
514 $this->commit();
515 }
516
517 if ( !is_null( $this->mFakeSlaveLag ) ) {
518 $status = parent::masterPosWait( $pos, $timeout );
519 wfProfileOut( $fname );
520 return $status;
521 }
522
523 # Call doQuery() directly, to avoid opening a transaction if DBO_TRX is set
524 $encFile = $this->addQuotes( $pos->file );
525 $encPos = intval( $pos->pos );
526 $sql = "SELECT MASTER_POS_WAIT($encFile, $encPos, $timeout)";
527 $res = $this->doQuery( $sql );
528
529 if ( $res && $row = $this->fetchRow( $res ) ) {
530 wfProfileOut( $fname );
531 return $row[0];
532 } else {
533 wfProfileOut( $fname );
534 return false;
535 }
536 }
537
538 /**
539 * Get the position of the master from SHOW SLAVE STATUS
540 *
541 * @return MySQLMasterPos|false
542 */
543 function getSlavePos() {
544 if ( !is_null( $this->mFakeSlaveLag ) ) {
545 return parent::getSlavePos();
546 }
547
548 $res = $this->query( 'SHOW SLAVE STATUS', 'DatabaseBase::getSlavePos' );
549 $row = $this->fetchObject( $res );
550
551 if ( $row ) {
552 $pos = isset( $row->Exec_master_log_pos ) ? $row->Exec_master_log_pos : $row->Exec_Master_Log_Pos;
553 return new MySQLMasterPos( $row->Relay_Master_Log_File, $pos );
554 } else {
555 return false;
556 }
557 }
558
559 /**
560 * Get the position of the master from SHOW MASTER STATUS
561 *
562 * @return MySQLMasterPos|false
563 */
564 function getMasterPos() {
565 if ( $this->mFakeMaster ) {
566 return parent::getMasterPos();
567 }
568
569 $res = $this->query( 'SHOW MASTER STATUS', 'DatabaseBase::getMasterPos' );
570 $row = $this->fetchObject( $res );
571
572 if ( $row ) {
573 return new MySQLMasterPos( $row->File, $row->Position );
574 } else {
575 return false;
576 }
577 }
578
579 /**
580 * @return string
581 */
582 function getServerVersion() {
583 return mysql_get_server_info( $this->mConn );
584 }
585
586 /**
587 * @param $index
588 * @return string
589 */
590 function useIndexClause( $index ) {
591 return "FORCE INDEX (" . $this->indexName( $index ) . ")";
592 }
593
594 /**
595 * @return string
596 */
597 function lowPriorityOption() {
598 return 'LOW_PRIORITY';
599 }
600
601 /**
602 * @return string
603 */
604 public static function getSoftwareLink() {
605 return '[http://www.mysql.com/ MySQL]';
606 }
607
608 function standardSelectDistinct() {
609 return false;
610 }
611
612 public function setTimeout( $timeout ) {
613 $this->query( "SET net_read_timeout=$timeout" );
614 $this->query( "SET net_write_timeout=$timeout" );
615 }
616
617 public function lock( $lockName, $method, $timeout = 5 ) {
618 $lockName = $this->addQuotes( $lockName );
619 $result = $this->query( "SELECT GET_LOCK($lockName, $timeout) AS lockstatus", $method );
620 $row = $this->fetchObject( $result );
621
622 if( $row->lockstatus == 1 ) {
623 return true;
624 } else {
625 wfDebug( __METHOD__." failed to acquire lock\n" );
626 return false;
627 }
628 }
629
630 /**
631 * FROM MYSQL DOCS: http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock
632 */
633 public function unlock( $lockName, $method ) {
634 $lockName = $this->addQuotes( $lockName );
635 $result = $this->query( "SELECT RELEASE_LOCK($lockName) as lockstatus", $method );
636 $row = $this->fetchObject( $result );
637 return $row->lockstatus;
638 }
639
640 public function lockTables( $read, $write, $method, $lowPriority = true ) {
641 $items = array();
642
643 foreach( $write as $table ) {
644 $tbl = $this->tableName( $table ) .
645 ( $lowPriority ? ' LOW_PRIORITY' : '' ) .
646 ' WRITE';
647 $items[] = $tbl;
648 }
649 foreach( $read as $table ) {
650 $items[] = $this->tableName( $table ) . ' READ';
651 }
652 $sql = "LOCK TABLES " . implode( ',', $items );
653 $this->query( $sql, $method );
654 }
655
656 public function unlockTables( $method ) {
657 $this->query( "UNLOCK TABLES", $method );
658 }
659
660 /**
661 * Get search engine class. All subclasses of this
662 * need to implement this if they wish to use searching.
663 *
664 * @return String
665 */
666 public function getSearchEngine() {
667 return 'SearchMySQL';
668 }
669
670 public function setBigSelects( $value = true ) {
671 if ( $value === 'default' ) {
672 if ( $this->mDefaultBigSelects === null ) {
673 # Function hasn't been called before so it must already be set to the default
674 return;
675 } else {
676 $value = $this->mDefaultBigSelects;
677 }
678 } elseif ( $this->mDefaultBigSelects === null ) {
679 $this->mDefaultBigSelects = (bool)$this->selectField( false, '@@sql_big_selects' );
680 }
681 $encValue = $value ? '1' : '0';
682 $this->query( "SET sql_big_selects=$encValue", __METHOD__ );
683 }
684
685 /**
686 * DELETE where the condition is a join. MySql uses multi-table deletes.
687 */
688 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = 'DatabaseBase::deleteJoin' ) {
689 if ( !$conds ) {
690 throw new DBUnexpectedError( $this, 'DatabaseBase::deleteJoin() called with empty $conds' );
691 }
692
693 $delTable = $this->tableName( $delTable );
694 $joinTable = $this->tableName( $joinTable );
695 $sql = "DELETE $delTable FROM $delTable, $joinTable WHERE $delVar=$joinVar ";
696
697 if ( $conds != '*' ) {
698 $sql .= ' AND ' . $this->makeList( $conds, LIST_AND );
699 }
700
701 return $this->query( $sql, $fname );
702 }
703
704 /**
705 * Determines if the last failure was due to a deadlock
706 *
707 * @return bool
708 */
709 function wasDeadlock() {
710 return $this->lastErrno() == 1213;
711 }
712
713 /**
714 * Determines if the last query error was something that should be dealt
715 * with by pinging the connection and reissuing the query
716 *
717 * @return bool
718 */
719 function wasErrorReissuable() {
720 return $this->lastErrno() == 2013 || $this->lastErrno() == 2006;
721 }
722
723 /**
724 * Determines if the last failure was due to the database being read-only.
725 *
726 * @return bool
727 */
728 function wasReadOnlyError() {
729 return $this->lastErrno() == 1223 ||
730 ( $this->lastErrno() == 1290 && strpos( $this->lastError(), '--read-only' ) !== false );
731 }
732
733 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseMysql::duplicateTableStructure' ) {
734 $tmp = $temporary ? 'TEMPORARY ' : '';
735 if ( strcmp( $this->getServerVersion(), '4.1' ) < 0 ) {
736 # Hack for MySQL versions < 4.1, which don't support
737 # "CREATE TABLE ... LIKE". Note that
738 # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0"
739 # would not create the indexes we need....
740 #
741 # Note that we don't bother changing around the prefixes here be-
742 # cause we know we're using MySQL anyway.
743
744 $res = $this->query( 'SHOW CREATE TABLE ' . $this->addIdentifierQuotes( $oldName ) );
745 $row = $this->fetchRow( $res );
746 $oldQuery = $row[1];
747 $query = preg_replace( '/CREATE TABLE `(.*?)`/',
748 "CREATE $tmp TABLE " . $this->addIdentifierQuotes( $newName ), $oldQuery );
749 if ($oldQuery === $query) {
750 # Couldn't do replacement
751 throw new MWException( "could not create temporary table $newName" );
752 }
753 } else {
754 $newName = $this->addIdentifierQuotes( $newName );
755 $oldName = $this->addIdentifierQuotes( $oldName );
756 $query = "CREATE $tmp TABLE $newName (LIKE $oldName)";
757 }
758 $this->query( $query, $fname );
759 }
760
761 /**
762 * List all tables on the database
763 *
764 * @param $prefix Only show tables with this prefix, e.g. mw_
765 * @param $fname String: calling function name
766 */
767 function listTables( $prefix = null, $fname = 'DatabaseMysql::listTables' ) {
768 $result = $this->query( "SHOW TABLES", $fname);
769
770 $endArray = array();
771
772 foreach( $result as $table ) {
773 $vars = get_object_vars($table);
774 $table = array_pop( $vars );
775
776 if( !$prefix || strpos( $table, $prefix ) === 0 ) {
777 $endArray[] = $table;
778 }
779 }
780
781 return $endArray;
782 }
783
784 /**
785 * @param $tableName
786 * @param $fName string
787 * @return bool|ResultWrapper
788 */
789 public function dropTable( $tableName, $fName = 'DatabaseMysql::dropTable' ) {
790 if( !$this->tableExists( $tableName ) ) {
791 return false;
792 }
793 return $this->query( "DROP TABLE IF EXISTS " . $this->tableName( $tableName ), $fName );
794 }
795
796 /**
797 * @return array
798 */
799 protected function getDefaultSchemaVars() {
800 $vars = parent::getDefaultSchemaVars();
801 $vars['wgDBTableOptions'] = str_replace( 'TYPE', 'ENGINE', $GLOBALS['wgDBTableOptions'] );
802 $vars['wgDBTableOptions'] = str_replace( 'CHARSET=mysql4', 'CHARSET=binary', $GLOBALS['wgDBTableOptions'] );
803 return $vars;
804 }
805
806 /**
807 * Get status information from SHOW STATUS in an associative array
808 *
809 * @return array
810 */
811 function getMysqlStatus( $which = "%" ) {
812 $res = $this->query( "SHOW STATUS LIKE '{$which}'" );
813 $status = array();
814
815 foreach ( $res as $row ) {
816 $status[$row->Variable_name] = $row->Value;
817 }
818
819 return $status;
820 }
821
822 }
823
824 /**
825 * Legacy support: Database == DatabaseMysql
826 *
827 * @deprecated in 1.16
828 */
829 class Database extends DatabaseMysql {}
830
831 /**
832 * Utility class.
833 * @ingroup Database
834 */
835 class MySQLField implements Field {
836 private $name, $tablename, $default, $max_length, $nullable,
837 $is_pk, $is_unique, $is_multiple, $is_key, $type;
838
839 function __construct ( $info ) {
840 $this->name = $info->name;
841 $this->tablename = $info->table;
842 $this->default = $info->def;
843 $this->max_length = $info->max_length;
844 $this->nullable = !$info->not_null;
845 $this->is_pk = $info->primary_key;
846 $this->is_unique = $info->unique_key;
847 $this->is_multiple = $info->multiple_key;
848 $this->is_key = ( $this->is_pk || $this->is_unique || $this->is_multiple );
849 $this->type = $info->type;
850 }
851
852 /**
853 * @return string
854 */
855 function name() {
856 return $this->name;
857 }
858
859 /**
860 * @return string
861 */
862 function tableName() {
863 return $this->tableName;
864 }
865
866 function type() {
867 return $this->type;
868 }
869
870 /**
871 * @return bool
872 */
873 function isNullable() {
874 return $this->nullable;
875 }
876
877 function defaultValue() {
878 return $this->default;
879 }
880
881 /**
882 * @return bool
883 */
884 function isKey() {
885 return $this->is_key;
886 }
887
888 /**
889 * @return bool
890 */
891 function isMultipleKey() {
892 return $this->is_multiple;
893 }
894 }
895
896 class MySQLMasterPos implements DBMasterPos {
897 var $file, $pos;
898
899 function __construct( $file, $pos ) {
900 $this->file = $file;
901 $this->pos = $pos;
902 }
903
904 function __toString() {
905 return "{$this->file}/{$this->pos}";
906 }
907 }