MediaWiki REL1_33
DatabaseMysqlBase.php
Go to the documentation of this file.
1<?php
23namespace Wikimedia\Rdbms;
24
25use DateTime;
26use DateTimeZone;
27use Wikimedia;
28use InvalidArgumentException;
29use Exception;
30use RuntimeException;
31use stdClass;
32
41abstract class DatabaseMysqlBase extends Database {
47 protected $lagDetectionOptions = [];
49 protected $useGTIDs = false;
51 protected $sslKeyPath;
53 protected $sslCertPath;
55 protected $sslCAFile;
57 protected $sslCAPath;
59 protected $sslCiphers;
61 protected $sqlMode;
63 protected $utf8Mode;
65 protected $defaultBigSelects = null;
66
68 private $serverVersion = null;
70 private $insertSelectIsSafe = null;
72 private $replicationInfoRow = null;
73
74 // Cache getServerId() for 24 hours
75 const SERVER_ID_CACHE_TTL = 86400;
76
78 const LAG_STALE_WARN_THRESHOLD = 0.100;
79
100 $this->lagDetectionMethod = $params['lagDetectionMethod'] ?? 'Seconds_Behind_Master';
101 $this->lagDetectionOptions = $params['lagDetectionOptions'] ?? [];
102 $this->useGTIDs = !empty( $params['useGTIDs' ] );
103 foreach ( [ 'KeyPath', 'CertPath', 'CAFile', 'CAPath', 'Ciphers' ] as $name ) {
104 $var = "ssl{$name}";
105 if ( isset( $params[$var] ) ) {
106 $this->$var = $params[$var];
107 }
108 }
109 $this->sqlMode = $params['sqlMode'] ?? null;
110 $this->utf8Mode = !empty( $params['utf8Mode'] );
111 $this->insertSelectIsSafe = isset( $params['insertSelectIsSafe'] )
112 ? (bool)$params['insertSelectIsSafe'] : null;
113
114 parent::__construct( $params );
115 }
116
120 public function getType() {
121 return 'mysql';
122 }
123
124 protected function open( $server, $user, $password, $dbName, $schema, $tablePrefix ) {
125 # Close/unset connection handle
126 $this->close();
127
128 $this->server = $server;
129 $this->user = $user;
130 $this->password = $password;
131
132 $this->installErrorHandler();
133 try {
134 $this->conn = $this->mysqlConnect( $this->server, $dbName );
135 } catch ( Exception $ex ) {
136 $this->restoreErrorHandler();
137 throw $ex;
138 }
139 $error = $this->restoreErrorHandler();
140
141 # Always log connection errors
142 if ( !$this->conn ) {
143 $error = $error ?: $this->lastError();
144 $this->connLogger->error(
145 "Error connecting to {db_server}: {error}",
146 $this->getLogContext( [
147 'method' => __METHOD__,
148 'error' => $error,
149 ] )
150 );
151 $this->connLogger->debug( "DB connection error\n" .
152 "Server: $server, User: $user, Password: " .
153 substr( $password, 0, 3 ) . "..., error: " . $error . "\n" );
154
155 throw new DBConnectionError( $this, $error );
156 }
157
158 if ( strlen( $dbName ) ) {
159 $this->selectDomain( new DatabaseDomain( $dbName, null, $tablePrefix ) );
160 } else {
161 $this->currentDomain = new DatabaseDomain( null, null, $tablePrefix );
162 }
163
164 // Tell the server what we're communicating with
165 if ( !$this->connectInitCharset() ) {
166 $error = $this->lastError();
167 $this->queryLogger->error(
168 "Error setting character set: {error}",
169 $this->getLogContext( [
170 'method' => __METHOD__,
171 'error' => $this->lastError(),
172 ] )
173 );
174 throw new DBConnectionError( $this, "Error setting character set: $error" );
175 }
176
177 // Abstract over any insane MySQL defaults
178 $set = [ 'group_concat_max_len = 262144' ];
179 // Set SQL mode, default is turning them all off, can be overridden or skipped with null
180 if ( is_string( $this->sqlMode ) ) {
181 $set[] = 'sql_mode = ' . $this->addQuotes( $this->sqlMode );
182 }
183 // Set any custom settings defined by site config
184 // (e.g. https://dev.mysql.com/doc/refman/4.1/en/innodb-parameters.html)
185 foreach ( $this->sessionVars as $var => $val ) {
186 // Escape strings but not numbers to avoid MySQL complaining
187 if ( !is_int( $val ) && !is_float( $val ) ) {
188 $val = $this->addQuotes( $val );
189 }
190 $set[] = $this->addIdentifierQuotes( $var ) . ' = ' . $val;
191 }
192
193 if ( $set ) {
194 // Use doQuery() to avoid opening implicit transactions (DBO_TRX)
195 $success = $this->doQuery( 'SET ' . implode( ', ', $set ) );
196 if ( !$success ) {
197 $error = $this->lastError();
198 $this->queryLogger->error(
199 'Error setting MySQL variables on server {db_server}: {error}',
200 $this->getLogContext( [
201 'method' => __METHOD__,
202 'error' => $error,
203 ] )
204 );
205 throw new DBConnectionError( $this, "Error setting MySQL variables: $error" );
206 }
207 }
208
209 $this->opened = true;
210
211 return true;
212 }
213
218 protected function connectInitCharset() {
219 if ( $this->utf8Mode ) {
220 // Tell the server we're communicating with it in UTF-8.
221 // This may engage various charset conversions.
222 return $this->mysqlSetCharset( 'utf8' );
223 } else {
224 return $this->mysqlSetCharset( 'binary' );
225 }
226 }
227
228 protected function doSelectDomain( DatabaseDomain $domain ) {
229 if ( $domain->getSchema() !== null ) {
230 throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
231 }
232
233 $database = $domain->getDatabase();
234 // A null database means "don't care" so leave it as is and update the table prefix
235 if ( $database === null ) {
236 $this->currentDomain = new DatabaseDomain(
237 $this->currentDomain->getDatabase(),
238 null,
239 $domain->getTablePrefix()
240 );
241
242 return true;
243 }
244
245 if ( $database !== $this->getDBname() ) {
246 $sql = 'USE ' . $this->addIdentifierQuotes( $database );
247 $ret = $this->doQuery( $sql );
248 if ( $ret === false ) {
249 $error = $this->lastError();
250 $errno = $this->lastErrno();
251 $this->reportQueryError( $error, $errno, $sql, __METHOD__ );
252 }
253 }
254
255 // Update that domain fields on success (no exception thrown)
256 $this->currentDomain = $domain;
257
258 return true;
259 }
260
269 abstract protected function mysqlConnect( $realServer, $dbName );
270
277 abstract protected function mysqlSetCharset( $charset );
278
283 public function freeResult( $res ) {
284 if ( $res instanceof ResultWrapper ) {
285 $res = $res->result;
286 }
288 $ok = $this->mysqlFreeResult( $res );
290 if ( !$ok ) {
291 throw new DBUnexpectedError( $this, "Unable to free MySQL result" );
292 }
293 }
294
301 abstract protected function mysqlFreeResult( $res );
302
308 public function fetchObject( $res ) {
309 if ( $res instanceof ResultWrapper ) {
310 $res = $res->result;
311 }
313 $row = $this->mysqlFetchObject( $res );
315
316 $errno = $this->lastErrno();
317 // Unfortunately, mysql_fetch_object does not reset the last errno.
318 // Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as
319 // these are the only errors mysql_fetch_object can cause.
320 // See https://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html.
321 if ( $errno == 2000 || $errno == 2013 ) {
322 throw new DBUnexpectedError(
323 $this,
324 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() )
325 );
326 }
327
328 return $row;
329 }
330
337 abstract protected function mysqlFetchObject( $res );
338
344 public function fetchRow( $res ) {
345 if ( $res instanceof ResultWrapper ) {
346 $res = $res->result;
347 }
349 $row = $this->mysqlFetchArray( $res );
351
352 $errno = $this->lastErrno();
353 // Unfortunately, mysql_fetch_array does not reset the last errno.
354 // Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as
355 // these are the only errors mysql_fetch_array can cause.
356 // See https://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html.
357 if ( $errno == 2000 || $errno == 2013 ) {
358 throw new DBUnexpectedError(
359 $this,
360 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() )
361 );
362 }
363
364 return $row;
365 }
366
373 abstract protected function mysqlFetchArray( $res );
374
380 function numRows( $res ) {
381 if ( $res instanceof ResultWrapper ) {
382 $res = $res->result;
383 }
385 $n = !is_bool( $res ) ? $this->mysqlNumRows( $res ) : 0;
387
388 // Unfortunately, mysql_num_rows does not reset the last errno.
389 // We are not checking for any errors here, since
390 // there are no errors mysql_num_rows can cause.
391 // See https://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html.
392 // See https://phabricator.wikimedia.org/T44430
393 return $n;
394 }
395
402 abstract protected function mysqlNumRows( $res );
403
408 public function numFields( $res ) {
409 if ( $res instanceof ResultWrapper ) {
410 $res = $res->result;
411 }
412
413 return $this->mysqlNumFields( $res );
414 }
415
422 abstract protected function mysqlNumFields( $res );
423
429 public function fieldName( $res, $n ) {
430 if ( $res instanceof ResultWrapper ) {
431 $res = $res->result;
432 }
433
434 return $this->mysqlFieldName( $res, $n );
435 }
436
444 abstract protected function mysqlFieldName( $res, $n );
445
452 public function fieldType( $res, $n ) {
453 if ( $res instanceof ResultWrapper ) {
454 $res = $res->result;
455 }
456
457 return $this->mysqlFieldType( $res, $n );
458 }
459
467 abstract protected function mysqlFieldType( $res, $n );
468
474 public function dataSeek( $res, $row ) {
475 if ( $res instanceof ResultWrapper ) {
476 $res = $res->result;
477 }
478
479 return $this->mysqlDataSeek( $res, $row );
480 }
481
489 abstract protected function mysqlDataSeek( $res, $row );
490
494 public function lastError() {
495 if ( $this->conn ) {
496 # Even if it's non-zero, it can still be invalid
498 $error = $this->mysqlError( $this->conn );
499 if ( !$error ) {
500 $error = $this->mysqlError();
501 }
503 } else {
504 $error = $this->mysqlError();
505 }
506 if ( $error ) {
507 $error .= ' (' . $this->server . ')';
508 }
509
510 return $error;
511 }
512
519 abstract protected function mysqlError( $conn = null );
520
521 protected function wasQueryTimeout( $error, $errno ) {
522 // https://dev.mysql.com/doc/refman/8.0/en/client-error-reference.html
523 // https://phabricator.wikimedia.org/T170638
524 return in_array( $errno, [ 2062, 3024 ] );
525 }
526
527 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
528 $this->nativeReplace( $table, $rows, $fname );
529 }
530
531 protected function isInsertSelectSafe( array $insertOptions, array $selectOptions ) {
532 $row = $this->getReplicationSafetyInfo();
533 // For row-based-replication, the resulting changes will be relayed, not the query
534 if ( $row->binlog_format === 'ROW' ) {
535 return true;
536 }
537 // LIMIT requires ORDER BY on a unique key or it is non-deterministic
538 if ( isset( $selectOptions['LIMIT'] ) ) {
539 return false;
540 }
541 // In MySQL, an INSERT SELECT is only replication safe with row-based
542 // replication or if innodb_autoinc_lock_mode is 0. When those
543 // conditions aren't met, use non-native mode.
544 // While we could try to determine if the insert is safe anyway by
545 // checking if the target table has an auto-increment column that
546 // isn't set in $varMap, that seems unlikely to be worth the extra
547 // complexity.
548 return (
549 in_array( 'NO_AUTO_COLUMNS', $insertOptions ) ||
550 (int)$row->innodb_autoinc_lock_mode === 0
551 );
552 }
553
557 protected function getReplicationSafetyInfo() {
558 if ( $this->replicationInfoRow === null ) {
559 $this->replicationInfoRow = $this->selectRow(
560 false,
561 [
562 'innodb_autoinc_lock_mode' => '@@innodb_autoinc_lock_mode',
563 'binlog_format' => '@@binlog_format',
564 ],
565 [],
566 __METHOD__
567 );
568 }
569
571 }
572
586 public function estimateRowCount( $table, $var = '*', $conds = '',
587 $fname = __METHOD__, $options = [], $join_conds = []
588 ) {
589 $conds = $this->normalizeConditions( $conds, $fname );
590 $column = $this->extractSingleFieldFromList( $var );
591 if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) {
592 $conds[] = "$column IS NOT NULL";
593 }
594
595 $options['EXPLAIN'] = true;
596 $res = $this->select( $table, $var, $conds, $fname, $options, $join_conds );
597 if ( $res === false ) {
598 return false;
599 }
600 if ( !$this->numRows( $res ) ) {
601 return 0;
602 }
603
604 $rows = 1;
605 foreach ( $res as $plan ) {
606 $rows *= $plan->rows > 0 ? $plan->rows : 1; // avoid resetting to zero
607 }
608
609 return (int)$rows;
610 }
611
612 public function tableExists( $table, $fname = __METHOD__ ) {
613 // Split database and table into proper variables as Database::tableName() returns
614 // shared tables prefixed with their database, which do not work in SHOW TABLES statements
615 list( $database, , $prefix, $table ) = $this->qualifiedTableComponents( $table );
616 $tableName = "{$prefix}{$table}";
617
618 if ( isset( $this->sessionTempTables[$tableName] ) ) {
619 return true; // already known to exist and won't show in SHOW TABLES anyway
620 }
621
622 // We can't use buildLike() here, because it specifies an escape character
623 // other than the backslash, which is the only one supported by SHOW TABLES
624 $encLike = $this->escapeLikeInternal( $tableName, '\\' );
625
626 // If the database has been specified (such as for shared tables), use "FROM"
627 if ( $database !== '' ) {
628 $encDatabase = $this->addIdentifierQuotes( $database );
629 $query = "SHOW TABLES FROM $encDatabase LIKE '$encLike'";
630 } else {
631 $query = "SHOW TABLES LIKE '$encLike'";
632 }
633
634 return $this->query( $query, $fname )->numRows() > 0;
635 }
636
642 public function fieldInfo( $table, $field ) {
643 $table = $this->tableName( $table );
644 $res = $this->query( "SELECT * FROM $table LIMIT 1", __METHOD__, true );
645 if ( !$res ) {
646 return false;
647 }
648 $n = $this->mysqlNumFields( $res->result );
649 for ( $i = 0; $i < $n; $i++ ) {
650 $meta = $this->mysqlFetchField( $res->result, $i );
651 if ( $field == $meta->name ) {
652 return new MySQLField( $meta );
653 }
654 }
655
656 return false;
657 }
658
666 abstract protected function mysqlFetchField( $res, $n );
667
677 public function indexInfo( $table, $index, $fname = __METHOD__ ) {
678 # SHOW INDEX works in MySQL 3.23.58, but SHOW INDEXES does not.
679 # SHOW INDEX should work for 3.x and up:
680 # https://dev.mysql.com/doc/mysql/en/SHOW_INDEX.html
681 $table = $this->tableName( $table );
682 $index = $this->indexName( $index );
683
684 $sql = 'SHOW INDEX FROM ' . $table;
685 $res = $this->query( $sql, $fname );
686
687 if ( !$res ) {
688 return null;
689 }
690
691 $result = [];
692
693 foreach ( $res as $row ) {
694 if ( $row->Key_name == $index ) {
695 $result[] = $row;
696 }
697 }
698
699 return $result ?: false;
700 }
701
706 public function strencode( $s ) {
707 return $this->mysqlRealEscapeString( $s );
708 }
709
714 abstract protected function mysqlRealEscapeString( $s );
715
716 public function addQuotes( $s ) {
717 if ( is_bool( $s ) ) {
718 // Parent would transform to int, which does not play nice with MySQL type juggling.
719 // When searching for an int in a string column, the strings are cast to int, which
720 // means false would match any string not starting with a number.
721 $s = (string)(int)$s;
722 }
723 return parent::addQuotes( $s );
724 }
725
732 public function addIdentifierQuotes( $s ) {
733 // Characters in the range \u0001-\uFFFF are valid in a quoted identifier
734 // Remove NUL bytes and escape backticks by doubling
735 return '`' . str_replace( [ "\0", '`' ], [ '', '``' ], $s ) . '`';
736 }
737
742 public function isQuotedIdentifier( $name ) {
743 return strlen( $name ) && $name[0] == '`' && substr( $name, -1, 1 ) == '`';
744 }
745
746 public function getLag() {
747 if ( $this->getLagDetectionMethod() === 'pt-heartbeat' ) {
748 return $this->getLagFromPtHeartbeat();
749 } else {
750 return $this->getLagFromSlaveStatus();
751 }
752 }
753
757 protected function getLagDetectionMethod() {
759 }
760
764 protected function getLagFromSlaveStatus() {
765 $res = $this->query( 'SHOW SLAVE STATUS', __METHOD__ );
766 $row = $res ? $res->fetchObject() : false;
767 // If the server is not replicating, there will be no row
768 if ( $row && strval( $row->Seconds_Behind_Master ) !== '' ) {
769 return intval( $row->Seconds_Behind_Master );
770 }
771
772 return false;
773 }
774
778 protected function getLagFromPtHeartbeat() {
780
781 $currentTrxInfo = $this->getRecordedTransactionLagStatus();
782 if ( $currentTrxInfo ) {
783 // There is an active transaction and the initial lag was already queried
784 $staleness = microtime( true ) - $currentTrxInfo['since'];
785 if ( $staleness > self::LAG_STALE_WARN_THRESHOLD ) {
786 // Avoid returning higher and higher lag value due to snapshot age
787 // given that the isolation level will typically be REPEATABLE-READ
788 $this->queryLogger->warning(
789 "Using cached lag value for {db_server} due to active transaction",
790 $this->getLogContext( [
791 'method' => __METHOD__,
792 'age' => $staleness,
793 'exception' => new RuntimeException()
794 ] )
795 );
796 }
797
798 return $currentTrxInfo['lag'];
799 }
800
801 if ( isset( $options['conds'] ) ) {
802 // Best method for multi-DC setups: use logical channel names
803 $data = $this->getHeartbeatData( $options['conds'] );
804 } else {
805 // Standard method: use master server ID (works with stock pt-heartbeat)
806 $masterInfo = $this->getMasterServerInfo();
807 if ( !$masterInfo ) {
808 $this->queryLogger->error(
809 "Unable to query master of {db_server} for server ID",
810 $this->getLogContext( [
811 'method' => __METHOD__
812 ] )
813 );
814
815 return false; // could not get master server ID
816 }
817
818 $conds = [ 'server_id' => intval( $masterInfo['serverId'] ) ];
819 $data = $this->getHeartbeatData( $conds );
820 }
821
822 list( $time, $nowUnix ) = $data;
823 if ( $time !== null ) {
824 // @time is in ISO format like "2015-09-25T16:48:10.000510"
825 $dateTime = new DateTime( $time, new DateTimeZone( 'UTC' ) );
826 $timeUnix = (int)$dateTime->format( 'U' ) + $dateTime->format( 'u' ) / 1e6;
827
828 return max( $nowUnix - $timeUnix, 0.0 );
829 }
830
831 $this->queryLogger->error(
832 "Unable to find pt-heartbeat row for {db_server}",
833 $this->getLogContext( [
834 'method' => __METHOD__
835 ] )
836 );
837
838 return false;
839 }
840
841 protected function getMasterServerInfo() {
843 $key = $cache->makeGlobalKey(
844 'mysql',
845 'master-info',
846 // Using one key for all cluster replica DBs is preferable
847 $this->getLBInfo( 'clusterMasterHost' ) ?: $this->getServer()
848 );
850
851 return $cache->getWithSetCallback(
852 $key,
853 $cache::TTL_INDEFINITE,
854 function () use ( $cache, $key, $fname ) {
855 // Get and leave a lock key in place for a short period
856 if ( !$cache->lock( $key, 0, 10 ) ) {
857 return false; // avoid master connection spike slams
858 }
859
860 $conn = $this->getLazyMasterHandle();
861 if ( !$conn ) {
862 return false; // something is misconfigured
863 }
864
865 // Connect to and query the master; catch errors to avoid outages
866 try {
867 $res = $conn->query( 'SELECT @@server_id AS id', $fname );
868 $row = $res ? $res->fetchObject() : false;
869 $id = $row ? (int)$row->id : 0;
870 } catch ( DBError $e ) {
871 $id = 0;
872 }
873
874 // Cache the ID if it was retrieved
875 return $id ? [ 'serverId' => $id, 'asOf' => time() ] : false;
876 }
877 );
878 }
879
885 protected function getHeartbeatData( array $conds ) {
886 // Query time and trip time are not counted
887 $nowUnix = microtime( true );
888 // Do not bother starting implicit transactions here
889 $this->clearFlag( self::DBO_TRX, self::REMEMBER_PRIOR );
890 try {
891 $whereSQL = $this->makeList( $conds, self::LIST_AND );
892 // Use ORDER BY for channel based queries since that field might not be UNIQUE.
893 // Note: this would use "TIMESTAMPDIFF(MICROSECOND,ts,UTC_TIMESTAMP(6))" but the
894 // percision field is not supported in MySQL <= 5.5.
895 $res = $this->query(
896 "SELECT ts FROM heartbeat.heartbeat WHERE $whereSQL ORDER BY ts DESC LIMIT 1",
897 __METHOD__
898 );
899 $row = $res ? $res->fetchObject() : false;
900 } finally {
901 $this->restoreFlags();
902 }
903
904 return [ $row ? $row->ts : null, $nowUnix ];
905 }
906
907 protected function getApproximateLagStatus() {
908 if ( $this->getLagDetectionMethod() === 'pt-heartbeat' ) {
909 // Disable caching since this is fast enough and we don't wan't
910 // to be *too* pessimistic by having both the cache TTL and the
911 // pt-heartbeat interval count as lag in getSessionLagStatus()
912 return parent::getApproximateLagStatus();
913 }
914
915 $key = $this->srvCache->makeGlobalKey( 'mysql-lag', $this->getServer() );
916 $approxLag = $this->srvCache->get( $key );
917 if ( !$approxLag ) {
918 $approxLag = parent::getApproximateLagStatus();
919 $this->srvCache->set( $key, $approxLag, 1 );
920 }
921
922 return $approxLag;
923 }
924
925 public function masterPosWait( DBMasterPos $pos, $timeout ) {
926 if ( !( $pos instanceof MySQLMasterPos ) ) {
927 throw new InvalidArgumentException( "Position not an instance of MySQLMasterPos" );
928 }
929
930 if ( $this->getLBInfo( 'is static' ) === true ) {
931 return 0; // this is a copy of a read-only dataset with no master DB
932 } elseif ( $this->lastKnownReplicaPos && $this->lastKnownReplicaPos->hasReached( $pos ) ) {
933 return 0; // already reached this point for sure
934 }
935
936 // Call doQuery() directly, to avoid opening a transaction if DBO_TRX is set
937 if ( $pos->getGTIDs() ) {
938 // Ignore GTIDs from domains exclusive to the master DB (presumably inactive)
939 $rpos = $this->getReplicaPos();
940 $gtidsWait = $rpos ? MySQLMasterPos::getCommonDomainGTIDs( $pos, $rpos ) : [];
941 if ( !$gtidsWait ) {
942 $this->queryLogger->error(
943 "No GTIDs with the same domain between master ($pos) and replica ($rpos)",
944 $this->getLogContext( [
945 'method' => __METHOD__,
946 ] )
947 );
948
949 return -1; // $pos is from the wrong cluster?
950 }
951 // Wait on the GTID set (MariaDB only)
952 $gtidArg = $this->addQuotes( implode( ',', $gtidsWait ) );
953 if ( strpos( $gtidArg, ':' ) !== false ) {
954 // MySQL GTIDs, e.g "source_id:transaction_id"
955 $res = $this->doQuery( "SELECT WAIT_FOR_EXECUTED_GTID_SET($gtidArg, $timeout)" );
956 } else {
957 // MariaDB GTIDs, e.g."domain:server:sequence"
958 $res = $this->doQuery( "SELECT MASTER_GTID_WAIT($gtidArg, $timeout)" );
959 }
960 } else {
961 // Wait on the binlog coordinates
962 $encFile = $this->addQuotes( $pos->getLogFile() );
963 $encPos = intval( $pos->getLogPosition()[$pos::CORD_EVENT] );
964 $res = $this->doQuery( "SELECT MASTER_POS_WAIT($encFile, $encPos, $timeout)" );
965 }
966
967 $row = $res ? $this->fetchRow( $res ) : false;
968 if ( !$row ) {
969 throw new DBExpectedError( $this, "Replication wait failed: {$this->lastError()}" );
970 }
971
972 // Result can be NULL (error), -1 (timeout), or 0+ per the MySQL manual
973 $status = ( $row[0] !== null ) ? intval( $row[0] ) : null;
974 if ( $status === null ) {
975 if ( !$pos->getGTIDs() ) {
976 // T126436: jobs programmed to wait on master positions might be referencing
977 // binlogs with an old master hostname; this makes MASTER_POS_WAIT() return null.
978 // Try to detect this case and treat the replica DB as having reached the given
979 // position (any master switchover already requires that the new master be caught
980 // up before the switch).
981 $replicationPos = $this->getReplicaPos();
982 if ( $replicationPos && !$replicationPos->channelsMatch( $pos ) ) {
983 $this->lastKnownReplicaPos = $replicationPos;
984 $status = 0;
985 }
986 }
987 } elseif ( $status >= 0 ) {
988 // Remember that this position was reached to save queries next time
989 $this->lastKnownReplicaPos = $pos;
990 }
991
992 return $status;
993 }
994
1000 public function getReplicaPos() {
1001 $now = microtime( true ); // as-of-time *before* fetching GTID variables
1002
1003 if ( $this->useGTIDs() ) {
1004 // Try to use GTIDs, fallbacking to binlog positions if not possible
1005 $data = $this->getServerGTIDs( __METHOD__ );
1006 // Use gtid_slave_pos for MariaDB and gtid_executed for MySQL
1007 foreach ( [ 'gtid_slave_pos', 'gtid_executed' ] as $name ) {
1008 if ( isset( $data[$name] ) && strlen( $data[$name] ) ) {
1009 return new MySQLMasterPos( $data[$name], $now );
1010 }
1011 }
1012 }
1013
1014 $data = $this->getServerRoleStatus( 'SLAVE', __METHOD__ );
1015 if ( $data && strlen( $data['Relay_Master_Log_File'] ) ) {
1016 return new MySQLMasterPos(
1017 "{$data['Relay_Master_Log_File']}/{$data['Exec_Master_Log_Pos']}",
1018 $now
1019 );
1020 }
1021
1022 return false;
1023 }
1024
1030 public function getMasterPos() {
1031 $now = microtime( true ); // as-of-time *before* fetching GTID variables
1032
1033 $pos = false;
1034 if ( $this->useGTIDs() ) {
1035 // Try to use GTIDs, fallbacking to binlog positions if not possible
1036 $data = $this->getServerGTIDs( __METHOD__ );
1037 // Use gtid_binlog_pos for MariaDB and gtid_executed for MySQL
1038 foreach ( [ 'gtid_binlog_pos', 'gtid_executed' ] as $name ) {
1039 if ( isset( $data[$name] ) && strlen( $data[$name] ) ) {
1040 $pos = new MySQLMasterPos( $data[$name], $now );
1041 break;
1042 }
1043 }
1044 // Filter domains that are inactive or not relevant to the session
1045 if ( $pos ) {
1046 $pos->setActiveOriginServerId( $this->getServerId() );
1047 $pos->setActiveOriginServerUUID( $this->getServerUUID() );
1048 if ( isset( $data['gtid_domain_id'] ) ) {
1049 $pos->setActiveDomain( $data['gtid_domain_id'] );
1050 }
1051 }
1052 }
1053
1054 if ( !$pos ) {
1055 $data = $this->getServerRoleStatus( 'MASTER', __METHOD__ );
1056 if ( $data && strlen( $data['File'] ) ) {
1057 $pos = new MySQLMasterPos( "{$data['File']}/{$data['Position']}", $now );
1058 }
1059 }
1060
1061 return $pos;
1062 }
1063
1068 protected function getServerId() {
1070 return $this->srvCache->getWithSetCallback(
1071 $this->srvCache->makeGlobalKey( 'mysql-server-id', $this->getServer() ),
1072 self::SERVER_ID_CACHE_TTL,
1073 function () use ( $fname ) {
1074 $res = $this->query( "SELECT @@server_id AS id", $fname );
1075 return intval( $this->fetchObject( $res )->id );
1076 }
1077 );
1078 }
1079
1083 protected function getServerUUID() {
1084 return $this->srvCache->getWithSetCallback(
1085 $this->srvCache->makeGlobalKey( 'mysql-server-uuid', $this->getServer() ),
1086 self::SERVER_ID_CACHE_TTL,
1087 function () {
1088 $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'server_uuid'" );
1089 $row = $this->fetchObject( $res );
1090
1091 return $row ? $row->Value : null;
1092 }
1093 );
1094 }
1095
1100 protected function getServerGTIDs( $fname = __METHOD__ ) {
1101 $map = [];
1102 // Get global-only variables like gtid_executed
1103 $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'gtid_%'", $fname );
1104 foreach ( $res as $row ) {
1105 $map[$row->Variable_name] = $row->Value;
1106 }
1107 // Get session-specific (e.g. gtid_domain_id since that is were writes will log)
1108 $res = $this->query( "SHOW SESSION VARIABLES LIKE 'gtid_%'", $fname );
1109 foreach ( $res as $row ) {
1110 $map[$row->Variable_name] = $row->Value;
1111 }
1112
1113 return $map;
1114 }
1115
1121 protected function getServerRoleStatus( $role, $fname = __METHOD__ ) {
1122 return $this->query( "SHOW $role STATUS", $fname )->fetchRow() ?: [];
1123 }
1124
1125 public function serverIsReadOnly() {
1126 $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'read_only'", __METHOD__ );
1127 $row = $this->fetchObject( $res );
1128
1129 return $row ? ( strtolower( $row->Value ) === 'on' ) : false;
1130 }
1131
1136 function useIndexClause( $index ) {
1137 return "FORCE INDEX (" . $this->indexName( $index ) . ")";
1138 }
1139
1144 function ignoreIndexClause( $index ) {
1145 return "IGNORE INDEX (" . $this->indexName( $index ) . ")";
1146 }
1147
1152 return 'LOW_PRIORITY';
1153 }
1154
1158 public function getSoftwareLink() {
1159 // MariaDB includes its name in its version string; this is how MariaDB's version of
1160 // the mysql command-line client identifies MariaDB servers (see mariadb_connection()
1161 // in libmysql/libmysql.c).
1162 $version = $this->getServerVersion();
1163 if ( strpos( $version, 'MariaDB' ) !== false || strpos( $version, '-maria-' ) !== false ) {
1164 return '[{{int:version-db-mariadb-url}} MariaDB]';
1165 }
1166
1167 // Percona Server's version suffix is not very distinctive, and @@version_comment
1168 // doesn't give the necessary info for source builds, so assume the server is MySQL.
1169 // (Even Percona's version of mysql doesn't try to make the distinction.)
1170 return '[{{int:version-db-mysql-url}} MySQL]';
1171 }
1172
1176 public function getServerVersion() {
1177 // Not using mysql_get_server_info() or similar for consistency: in the handshake,
1178 // MariaDB 10 adds the prefix "5.5.5-", and only some newer client libraries strip
1179 // it off (see RPL_VERSION_HACK in include/mysql_com.h).
1180 if ( $this->serverVersion === null ) {
1181 $this->serverVersion = $this->selectField( '', 'VERSION()', '', __METHOD__ );
1182 }
1183 return $this->serverVersion;
1184 }
1185
1189 public function setSessionOptions( array $options ) {
1190 if ( isset( $options['connTimeout'] ) ) {
1191 $timeout = (int)$options['connTimeout'];
1192 $this->query( "SET net_read_timeout=$timeout" );
1193 $this->query( "SET net_write_timeout=$timeout" );
1194 }
1195 }
1196
1202 public function streamStatementEnd( &$sql, &$newLine ) {
1203 if ( strtoupper( substr( $newLine, 0, 9 ) ) == 'DELIMITER' ) {
1204 preg_match( '/^DELIMITER\s+(\S+)/', $newLine, $m );
1205 $this->delimiter = $m[1];
1206 $newLine = '';
1207 }
1208
1209 return parent::streamStatementEnd( $sql, $newLine );
1210 }
1211
1220 public function lockIsFree( $lockName, $method ) {
1221 if ( !parent::lockIsFree( $lockName, $method ) ) {
1222 return false; // already held
1223 }
1224
1225 $encName = $this->addQuotes( $this->makeLockName( $lockName ) );
1226 $result = $this->query( "SELECT IS_FREE_LOCK($encName) AS lockstatus", $method );
1227 $row = $this->fetchObject( $result );
1228
1229 return ( $row->lockstatus == 1 );
1230 }
1231
1238 public function lock( $lockName, $method, $timeout = 5 ) {
1239 $encName = $this->addQuotes( $this->makeLockName( $lockName ) );
1240 $result = $this->query( "SELECT GET_LOCK($encName, $timeout) AS lockstatus", $method );
1241 $row = $this->fetchObject( $result );
1242
1243 if ( $row->lockstatus == 1 ) {
1244 parent::lock( $lockName, $method, $timeout ); // record
1245 return true;
1246 }
1247
1248 $this->queryLogger->info( __METHOD__ . " failed to acquire lock '{lockname}'",
1249 [ 'lockname' => $lockName ] );
1250
1251 return false;
1252 }
1253
1261 public function unlock( $lockName, $method ) {
1262 $encName = $this->addQuotes( $this->makeLockName( $lockName ) );
1263 $result = $this->query( "SELECT RELEASE_LOCK($encName) as lockstatus", $method );
1264 $row = $this->fetchObject( $result );
1265
1266 if ( $row->lockstatus == 1 ) {
1267 parent::unlock( $lockName, $method ); // record
1268 return true;
1269 }
1270
1271 $this->queryLogger->warning( __METHOD__ . " failed to release lock '$lockName'\n" );
1272
1273 return false;
1274 }
1275
1276 private function makeLockName( $lockName ) {
1277 // https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock
1278 // Newer version enforce a 64 char length limit.
1279 return ( strlen( $lockName ) > 64 ) ? sha1( $lockName ) : $lockName;
1280 }
1281
1282 public function namedLocksEnqueue() {
1283 return true;
1284 }
1285
1287 return false; // tied to TCP connection
1288 }
1289
1290 protected function doLockTables( array $read, array $write, $method ) {
1291 $items = [];
1292 foreach ( $write as $table ) {
1293 $items[] = $this->tableName( $table ) . ' WRITE';
1294 }
1295 foreach ( $read as $table ) {
1296 $items[] = $this->tableName( $table ) . ' READ';
1297 }
1298
1299 $sql = "LOCK TABLES " . implode( ',', $items );
1300 $this->query( $sql, $method );
1301
1302 return true;
1303 }
1304
1305 protected function doUnlockTables( $method ) {
1306 $this->query( "UNLOCK TABLES", $method );
1307
1308 return true;
1309 }
1310
1314 public function setBigSelects( $value = true ) {
1315 if ( $value === 'default' ) {
1316 if ( $this->defaultBigSelects === null ) {
1317 # Function hasn't been called before so it must already be set to the default
1318 return;
1319 } else {
1321 }
1322 } elseif ( $this->defaultBigSelects === null ) {
1323 $this->defaultBigSelects =
1324 (bool)$this->selectField( false, '@@sql_big_selects', '', __METHOD__ );
1325 }
1326 $encValue = $value ? '1' : '0';
1327 $this->query( "SET sql_big_selects=$encValue", __METHOD__ );
1328 }
1329
1340 public function deleteJoin(
1341 $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
1342 ) {
1343 if ( !$conds ) {
1344 throw new DBUnexpectedError( $this, __METHOD__ . ' called with empty $conds' );
1345 }
1346
1347 $delTable = $this->tableName( $delTable );
1348 $joinTable = $this->tableName( $joinTable );
1349 $sql = "DELETE $delTable FROM $delTable, $joinTable WHERE $delVar=$joinVar ";
1350
1351 if ( $conds != '*' ) {
1352 $sql .= ' AND ' . $this->makeList( $conds, self::LIST_AND );
1353 }
1354
1355 $this->query( $sql, $fname );
1356 }
1357
1358 public function upsert(
1359 $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__
1360 ) {
1361 if ( $rows === [] ) {
1362 return true; // nothing to do
1363 }
1364
1365 if ( !is_array( reset( $rows ) ) ) {
1366 $rows = [ $rows ];
1367 }
1368
1369 $table = $this->tableName( $table );
1370 $columns = array_keys( $rows[0] );
1371
1372 $sql = "INSERT INTO $table (" . implode( ',', $columns ) . ') VALUES ';
1373 $rowTuples = [];
1374 foreach ( $rows as $row ) {
1375 $rowTuples[] = '(' . $this->makeList( $row ) . ')';
1376 }
1377 $sql .= implode( ',', $rowTuples );
1378 $sql .= " ON DUPLICATE KEY UPDATE " . $this->makeList( $set, self::LIST_SET );
1379
1380 $this->query( $sql, $fname );
1381
1382 return true;
1383 }
1384
1390 public function getServerUptime() {
1391 $vars = $this->getMysqlStatus( 'Uptime' );
1392
1393 return (int)$vars['Uptime'];
1394 }
1395
1401 public function wasDeadlock() {
1402 return $this->lastErrno() == 1213;
1403 }
1404
1410 public function wasLockTimeout() {
1411 return $this->lastErrno() == 1205;
1412 }
1413
1419 public function wasReadOnlyError() {
1420 return $this->lastErrno() == 1223 ||
1421 ( $this->lastErrno() == 1290 && strpos( $this->lastError(), '--read-only' ) !== false );
1422 }
1423
1424 public function wasConnectionError( $errno ) {
1425 return $errno == 2013 || $errno == 2006;
1426 }
1427
1428 protected function wasKnownStatementRollbackError() {
1429 $errno = $this->lastErrno();
1430
1431 if ( $errno === 1205 ) { // lock wait timeout
1432 // Note that this is uncached to avoid stale values of SET is used
1433 $row = $this->selectRow(
1434 false,
1435 [ 'innodb_rollback_on_timeout' => '@@innodb_rollback_on_timeout' ],
1436 [],
1437 __METHOD__
1438 );
1439 // https://dev.mysql.com/doc/refman/5.7/en/innodb-error-handling.html
1440 // https://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.html
1441 return $row->innodb_rollback_on_timeout ? false : true;
1442 }
1443
1444 // See https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html
1445 return in_array( $errno, [ 1022, 1062, 1216, 1217, 1137, 1146, 1051, 1054 ], true );
1446 }
1447
1456 $oldName, $newName, $temporary = false, $fname = __METHOD__
1457 ) {
1458 $tmp = $temporary ? 'TEMPORARY ' : '';
1459 $newName = $this->addIdentifierQuotes( $newName );
1460 $oldName = $this->addIdentifierQuotes( $oldName );
1461 $query = "CREATE $tmp TABLE $newName (LIKE $oldName)";
1462
1463 return $this->query( $query, $fname, $this::QUERY_PSEUDO_PERMANENT );
1464 }
1465
1473 public function listTables( $prefix = null, $fname = __METHOD__ ) {
1474 $result = $this->query( "SHOW TABLES", $fname );
1475
1476 $endArray = [];
1477
1478 foreach ( $result as $table ) {
1479 $vars = get_object_vars( $table );
1480 $table = array_pop( $vars );
1481
1482 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
1483 $endArray[] = $table;
1484 }
1485 }
1486
1487 return $endArray;
1488 }
1489
1495 public function dropTable( $tableName, $fName = __METHOD__ ) {
1496 if ( !$this->tableExists( $tableName, $fName ) ) {
1497 return false;
1498 }
1499
1500 return $this->query( "DROP TABLE IF EXISTS " . $this->tableName( $tableName ), $fName );
1501 }
1502
1509 private function getMysqlStatus( $which = "%" ) {
1510 $res = $this->query( "SHOW STATUS LIKE '{$which}'" );
1511 $status = [];
1512
1513 foreach ( $res as $row ) {
1514 $status[$row->Variable_name] = $row->Value;
1515 }
1516
1517 return $status;
1518 }
1519
1529 public function listViews( $prefix = null, $fname = __METHOD__ ) {
1530 // The name of the column containing the name of the VIEW
1531 $propertyName = 'Tables_in_' . $this->getDBname();
1532
1533 // Query for the VIEWS
1534 $res = $this->query( 'SHOW FULL TABLES WHERE TABLE_TYPE = "VIEW"' );
1535 $allViews = [];
1536 foreach ( $res as $row ) {
1537 array_push( $allViews, $row->$propertyName );
1538 }
1539
1540 if ( is_null( $prefix ) || $prefix === '' ) {
1541 return $allViews;
1542 }
1543
1544 $filteredViews = [];
1545 foreach ( $allViews as $viewName ) {
1546 // Does the name of this VIEW start with the table-prefix?
1547 if ( strpos( $viewName, $prefix ) === 0 ) {
1548 array_push( $filteredViews, $viewName );
1549 }
1550 }
1551
1552 return $filteredViews;
1553 }
1554
1563 public function isView( $name, $prefix = null ) {
1564 return in_array( $name, $this->listViews( $prefix ) );
1565 }
1566
1567 protected function isTransactableQuery( $sql ) {
1568 return parent::isTransactableQuery( $sql ) &&
1569 !preg_match( '/^SELECT\s+(GET|RELEASE|IS_FREE)_LOCK\‍(/', $sql );
1570 }
1571
1572 public function buildStringCast( $field ) {
1573 return "CAST( $field AS BINARY )";
1574 }
1575
1580 public function buildIntegerCast( $field ) {
1581 return 'CAST( ' . $field . ' AS SIGNED )';
1582 }
1583
1584 /*
1585 * @return bool Whether GTID support is used (mockable for testing)
1586 */
1587 protected function useGTIDs() {
1588 return $this->useGTIDs;
1589 }
1590}
1591
1595class_alias( DatabaseMysqlBase::class, 'DatabaseMysqlBase' );
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition Setup.php:123
makeGlobalKey( $class, $component=null)
Make a global cache key.
Database error base class.
Definition DBError.php:30
Base class for the more common types of database errors.
Class to handle database/prefix specification for IDatabase domains.
Database abstraction object for MySQL.
doSelectDomain(DatabaseDomain $domain)
__construct(array $params)
Additional $params include:
duplicateTableStructure( $oldName, $newName, $temporary=false, $fname=__METHOD__)
getMysqlStatus( $which="%")
Get status information from SHOW STATUS in an associative array.
addQuotes( $s)
Adds quotes and backslashes.
namedLocksEnqueue()
Check to see if a named lock used by lock() use blocking queues.
mysqlFetchArray( $res)
Fetch a result row as an associative and numeric array.
array $lagDetectionOptions
Method to detect replica DB lag.
isInsertSelectSafe(array $insertOptions, array $selectOptions)
dropTable( $tableName, $fName=__METHOD__)
wasReadOnlyError()
Determines if the last failure was due to the database being read-only.
fieldType( $res, $n)
mysql_field_type() wrapper
getMasterPos()
Get the position of the master from SHOW MASTER STATUS.
mysqlFetchField( $res, $n)
Get column information from a result.
string $lagDetectionMethod
Method to detect replica DB lag.
getApproximateLagStatus()
Get a replica DB lag estimate for this server.
mysqlFieldType( $res, $n)
Get the type of the specified field in a result.
doLockTables(array $read, array $write, $method)
Helper function for lockTables() that handles the actual table locking.
getLag()
Get the amount of replication lag for this database server.
upsert( $table, array $rows, $uniqueIndexes, array $set, $fname=__METHOD__)
INSERT ON DUPLICATE KEY UPDATE wrapper, upserts an array into a table.
mysqlConnect( $realServer, $dbName)
Open a connection to a MySQL server.
mysqlSetCharset( $charset)
Set the character set of the MySQL link.
mysqlFreeResult( $res)
Free result memory.
lockIsFree( $lockName, $method)
Check to see if a named lock is available.
masterPosWait(DBMasterPos $pos, $timeout)
Wait for the replica DB to catch up to a given master position.
indexInfo( $table, $index, $fname=__METHOD__)
Get information about an index into an object Returns false if the index does not exist.
estimateRowCount( $table, $var=' *', $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Estimate rows in dataset Returns estimated count, based on EXPLAIN output Takes same arguments as Dat...
mysqlDataSeek( $res, $row)
Move internal result pointer.
mysqlError( $conn=null)
Returns the text of the error message from previous MySQL operation.
addIdentifierQuotes( $s)
MySQL uses backticks for identifier quoting instead of the sql standard "double quotes".
mysqlFetchObject( $res)
Fetch a result row as an object.
mysqlFieldName( $res, $n)
Get the name of the specified field in a result.
open( $server, $user, $password, $dbName, $schema, $tablePrefix)
Open a new connection to the database (closing any existing one)
isTransactableQuery( $sql)
Determine whether a SQL statement is sensitive to isolation level.
lock( $lockName, $method, $timeout=5)
wasQueryTimeout( $error, $errno)
Checks whether the cause of the error is detected to be a timeout.
unlock( $lockName, $method)
FROM MYSQL DOCS: https://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_releas...
bool $utf8Mode
Use experimental UTF-8 transmission encoding.
listTables( $prefix=null, $fname=__METHOD__)
List all tables on the database.
bool $useGTIDs
bool Whether to use GTID methods
getServerRoleStatus( $role, $fname=__METHOD__)
wasLockTimeout()
Determines if the last failure was due to a lock timeout.
string $sqlMode
sql_mode value to send on connection
connectInitCharset()
Set the character set information right after connection.
mysqlNumRows( $res)
Get number of rows in result.
tableLocksHaveTransactionScope()
Checks if table locks acquired by lockTables() are transaction-bound in their scope.
getServerUptime()
Determines how long the server has been up.
doUnlockTables( $method)
Helper function for unlockTables() that handles the actual table unlocking.
getReplicaPos()
Get the position of the master from SHOW SLAVE STATUS.
listViews( $prefix=null, $fname=__METHOD__)
Lists VIEWs in the database.
isView( $name, $prefix=null)
Differentiates between a TABLE and a VIEW.
tableExists( $table, $fname=__METHOD__)
Query whether a given table exists.
wasDeadlock()
Determines if the last failure was due to a deadlock.
wasConnectionError( $errno)
Do not use this method outside of Database/DBError classes.
deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname=__METHOD__)
DELETE where the condition is a join.
mysqlNumFields( $res)
Get number of fields in result.
replace( $table, $uniqueIndexes, $rows, $fname=__METHOD__)
REPLACE query wrapper.
Relational database abstraction object.
Definition Database.php:49
selectRow( $table, $vars, $conds, $fname=__METHOD__, $options=[], $join_conds=[])
Single row SELECT wrapper.
restoreErrorHandler()
Restore the previous error handler and return the last PHP error for this DB.
Definition Database.php:892
object resource null $conn
Database connection.
Definition Database.php:109
indexName( $index)
Allows for index remapping in queries where this is not consistent across DBMS.
string $user
User that this instance is currently connected under the name of.
Definition Database.php:84
getRecordedTransactionLagStatus()
Get the replica DB lag when the current transaction started.
reportQueryError( $error, $errno, $sql, $fname, $ignoreErrors=false)
Report a query error.
restoreFlags( $state=self::RESTORE_PRIOR)
Restore the flags to their prior state before the last setFlag/clearFlag call.
Definition Database.php:827
installErrorHandler()
Set a custom error handler for logging errors during database connection.
Definition Database.php:881
selectDomain( $domain)
Set the current domain (database, schema, and table prefix)
makeList( $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
clearFlag( $flag, $remember=self::REMEMBER_NOTHING)
Clear a flag for this connection.
Definition Database.php:816
qualifiedTableComponents( $name)
Get the table components needed for a query given the currently selected database.
getLBInfo( $name=null)
Get properties passed down from the server info array of the load balancer.
Definition Database.php:645
getLogContext(array $extras=[])
Create a log context to pass to PSR-3 logger functions.
Definition Database.php:932
nativeReplace( $table, $rows, $fname)
REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE statement.
getServer()
Get the server hostname or IP address.
string $password
Password used to establish the current connection.
Definition Database.php:86
escapeLikeInternal( $s, $escapeChar='`')
string $server
Server that this instance is currently connected to.
Definition Database.php:82
normalizeConditions( $conds, $fname)
BagOStuff $srvCache
APC cache.
Definition Database.php:98
doQuery( $sql)
Run a query and return a DBMS-dependent wrapper or boolean.
getDBname()
Get the current DB name.
close()
Close the database connection.
Definition Database.php:943
selectField( $table, $var, $cond='', $fname=__METHOD__, $options=[], $join_conds=[])
A SELECT wrapper which returns a single field from a single result row.
DBMasterPos class for MySQL/MariaDB.
static getCommonDomainGTIDs(MySQLMasterPos $pos, MySQLMasterPos $refPos)
Result wrapper for grabbing data queried from an IDatabase object.
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 select() and insert() are usually more convenient. They take care of things like table prefixes and escaping for you. If you really need to make your own SQL
$res
Definition database.txt:21
For a write query
Definition database.txt:26
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
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition hooks.txt:1802
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
Definition hooks.txt:2228
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:2818
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition hooks.txt:181
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 & $options
Definition hooks.txt:1999
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition hooks.txt:783
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:2003
Status::newGood()` to allow deletion, and then `return false` from the hook function. Ensure you consume the 'ChangeTagAfterDelete' hook to carry out custom deletion actions. $tag:name of the tag $user:user initiating the action & $status:Status object. See above. 'ChangeTagsListActive':Allows you to nominate which of the tags your extension uses are in active use. & $tags:list of all active tags. Append to this array. 'ChangeTagsAfterUpdateTags':Called after tags have been updated with the ChangeTags::updateTags function. Params:$addedTags:tags effectively added in the update $removedTags:tags effectively removed in the update $prevTags:tags that were present prior to the update $rc_id:recentchanges table id $rev_id:revision table id $log_id:logging table id $params:tag params $rc:RecentChange being tagged when the tagging accompanies the action, or null $user:User who performed the tagging when the tagging is subsequent to the action, or null 'ChangeTagsAllowedAdd':Called when checking if a user can add tags to a change. & $allowedTags:List of all the tags the user is allowed to add. Any tags the user wants to add( $addTags) that are not in this array will cause it to fail. You may add or remove tags to this array as required. $addTags:List of tags user intends to add. $user:User who is adding the tags. 'ChangeUserGroups':Called before user groups are changed. $performer:The User who will perform the change $user:The User whose groups will be changed & $add:The groups that will be added & $remove:The groups that will be removed 'Collation::factory':Called if $wgCategoryCollation is an unknown collation. $collationName:Name of the collation in question & $collationObject:Null. Replace with a subclass of the Collation class that implements the collation given in $collationName. 'ConfirmEmailComplete':Called after a user 's email has been confirmed successfully. $user:user(object) whose email is being confirmed 'ContentAlterParserOutput':Modify parser output for a given content object. Called by Content::getParserOutput after parsing has finished. Can be used for changes that depend on the result of the parsing but have to be done before LinksUpdate is called(such as adding tracking categories based on the rendered HTML). $content:The Content to render $title:Title of the page, as context $parserOutput:ParserOutput to manipulate 'ContentGetParserOutput':Customize parser output for a given content object, called by AbstractContent::getParserOutput. May be used to override the normal model-specific rendering of page content. $content:The Content to render $title:Title of the page, as context $revId:The revision ID, as context $options:ParserOptions for rendering. To avoid confusing the parser cache, the output can only depend on parameters provided to this hook function, not on global state. $generateHtml:boolean, indicating whether full HTML should be generated. If false, generation of HTML may be skipped, but other information should still be present in the ParserOutput object. & $output:ParserOutput, to manipulate or replace 'ContentHandlerDefaultModelFor':Called when the default content model is determined for a given title. May be used to assign a different model for that title. $title:the Title in question & $model:the model name. Use with CONTENT_MODEL_XXX constants. 'ContentHandlerForModelID':Called when a ContentHandler is requested for a given content model name, but no entry for that model exists in $wgContentHandlers. Note:if your extension implements additional models via this hook, please use GetContentModels hook to make them known to core. $modeName:the requested content model name & $handler:set this to a ContentHandler object, if desired. 'ContentModelCanBeUsedOn':Called to determine whether that content model can be used on a given page. This is especially useful to prevent some content models to be used in some special location. $contentModel:ID of the content model in question $title:the Title in question. & $ok:Output parameter, whether it is OK to use $contentModel on $title. Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok. 'ContribsPager::getQueryInfo':Before the contributions query is about to run & $pager:Pager object for contributions & $queryInfo:The query for the contribs Pager 'ContribsPager::reallyDoQuery':Called before really executing the query for My Contributions & $data:an array of results of all contribs queries $pager:The ContribsPager object hooked into $offset:Index offset, inclusive $limit:Exact query limit $descending:Query direction, false for ascending, true for descending 'ContributionsLineEnding':Called before a contributions HTML line is finished $page:SpecialPage object for contributions & $ret:the HTML line $row:the DB row for this line & $classes:the classes to add to the surrounding< li > & $attribs:associative array of other HTML attributes for the< li > element. Currently only data attributes reserved to MediaWiki are allowed(see Sanitizer::isReservedDataAttribute). 'ContributionsToolLinks':Change tool links above Special:Contributions $id:User identifier $title:User page title & $tools:Array of tool links $specialPage:SpecialPage instance for context and services. Can be either SpecialContributions or DeletedContributionsPage. Extensions should type hint against a generic SpecialPage though. 'ConvertContent':Called by AbstractContent::convert when a conversion to another content model is requested. Handler functions that modify $result should generally return false to disable further attempts at conversion. $content:The Content object to be converted. $toModel:The ID of the content model to convert to. $lossy: boolean indicating whether lossy conversion is allowed. & $result:Output parameter, in case the handler function wants to provide a converted Content object. Note that $result->getContentModel() must return $toModel. 'ContentSecurityPolicyDefaultSource':Modify the allowed CSP load sources. This affects all directives except for the script directive. If you want to add a script source, see ContentSecurityPolicyScriptSource hook. & $defaultSrc:Array of Content-Security-Policy allowed sources $policyConfig:Current configuration for the Content-Security-Policy header $mode:ContentSecurityPolicy::REPORT_ONLY_MODE or ContentSecurityPolicy::FULL_MODE depending on type of header 'ContentSecurityPolicyDirectives':Modify the content security policy directives. Use this only if ContentSecurityPolicyDefaultSource and ContentSecurityPolicyScriptSource do not meet your needs. & $directives:Array of CSP directives $policyConfig:Current configuration for the CSP header $mode:ContentSecurityPolicy::REPORT_ONLY_MODE or ContentSecurityPolicy::FULL_MODE depending on type of header 'ContentSecurityPolicyScriptSource':Modify the allowed CSP script sources. Note that you also have to use ContentSecurityPolicyDefaultSource if you want non-script sources to be loaded from whatever you add. & $scriptSrc:Array of CSP directives $policyConfig:Current configuration for the CSP header $mode:ContentSecurityPolicy::REPORT_ONLY_MODE or ContentSecurityPolicy::FULL_MODE depending on type of header 'CustomEditor':When invoking the page editor Return true to allow the normal editor to be used, or false if implementing a custom editor, e.g. for a special namespace, etc. $article:Article being edited $user:User performing the edit 'DatabaseOraclePostInit':Called after initialising an Oracle database $db:the DatabaseOracle object 'DeletedContribsPager::reallyDoQuery':Called before really executing the query for Special:DeletedContributions Similar to ContribsPager::reallyDoQuery & $data:an array of results of all contribs queries $pager:The DeletedContribsPager object hooked into $offset:Index offset, inclusive $limit:Exact query limit $descending:Query direction, false for ascending, true for descending 'DeletedContributionsLineEnding':Called before a DeletedContributions HTML line is finished. Similar to ContributionsLineEnding $page:SpecialPage object for DeletedContributions & $ret:the HTML line $row:the DB row for this line & $classes:the classes to add to the surrounding< li > & $attribs:associative array of other HTML attributes for the< li > element. Currently only data attributes reserved to MediaWiki are allowed(see Sanitizer::isReservedDataAttribute). 'DeleteUnknownPreferences':Called by the cleanupPreferences.php maintenance script to build a WHERE clause with which to delete preferences that are not known about. This hook is used by extensions that have dynamically-named preferences that should not be deleted in the usual cleanup process. For example, the Gadgets extension creates preferences prefixed with 'gadget-', and so anything with that prefix is excluded from the deletion. &where:An array that will be passed as the $cond parameter to IDatabase::select() to determine what will be deleted from the user_properties table. $db:The IDatabase object, useful for accessing $db->buildLike() etc. 'DifferenceEngineAfterLoadNewText':called in DifferenceEngine::loadNewText() after the new revision 's content has been loaded into the class member variable $differenceEngine->mNewContent but before returning true from this function. $differenceEngine:DifferenceEngine object 'DifferenceEngineLoadTextAfterNewContentIsLoaded':called in DifferenceEngine::loadText() after the new revision 's content has been loaded into the class member variable $differenceEngine->mNewContent but before checking if the variable 's value is null. This hook can be used to inject content into said class member variable. $differenceEngine:DifferenceEngine object 'DifferenceEngineMarkPatrolledLink':Allows extensions to change the "mark as patrolled" link which is shown both on the diff header as well as on the bottom of a page, usually wrapped in a span element which has class="patrollink". $differenceEngine:DifferenceEngine object & $markAsPatrolledLink:The "mark as patrolled" link HTML(string) $rcid:Recent change ID(rc_id) for this change(int) 'DifferenceEngineMarkPatrolledRCID':Allows extensions to possibly change the rcid parameter. For example the rcid might be set to zero due to the user being the same as the performer of the change but an extension might still want to show it under certain conditions. & $rcid:rc_id(int) of the change or 0 $differenceEngine:DifferenceEngine object $change:RecentChange object $user:User object representing the current user 'DifferenceEngineNewHeader':Allows extensions to change the $newHeader variable, which contains information about the new revision, such as the revision 's author, whether the revision was marked as a minor edit or not, etc. $differenceEngine:DifferenceEngine object & $newHeader:The string containing the various #mw-diff-otitle[1-5] divs, which include things like revision author info, revision comment, RevisionDelete link and more $formattedRevisionTools:Array containing revision tools, some of which may have been injected with the DiffRevisionTools hook $nextlink:String containing the link to the next revision(if any) $status
Definition hooks.txt:1032
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1617
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
returning false will NOT prevent logging $e
Definition hooks.txt:2175
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
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
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 function
Definition injection.txt:30
An object representing a master or replica DB position in a replicated setup.
lastErrno()
Get the last error number.
$cache
Definition mcc.php:33
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
$params