Don't fallback from uk to ru
[lhc/web/wiklou.git] / includes / db / DatabaseMysqlBase.php
1 <?php
2 /**
3 * This is the MySQL database abstraction layer.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * Database abstraction object for MySQL.
26 * Defines methods independent on used MySQL extension.
27 *
28 * @ingroup Database
29 * @since 1.22
30 * @see Database
31 */
32 abstract class DatabaseMysqlBase extends Database {
33 /** @var MysqlMasterPos */
34 protected $lastKnownSlavePos;
35 /** @var string Method to detect slave lag */
36 protected $lagDetectionMethod;
37 /** @var array Method to detect slave lag */
38 protected $lagDetectionOptions = [];
39 /** @var bool bool Whether to use GTID methods */
40 protected $useGTIDs = false;
41 /** @var string|null */
42 protected $sslKeyPath;
43 /** @var string|null */
44 protected $sslCertPath;
45 /** @var string|null */
46 protected $sslCAPath;
47 /** @var string[]|null */
48 protected $sslCiphers;
49 /** @var string|null */
50 private $serverVersion = null;
51
52 /**
53 * Additional $params include:
54 * - lagDetectionMethod : set to one of (Seconds_Behind_Master,pt-heartbeat).
55 * pt-heartbeat assumes the table is at heartbeat.heartbeat
56 * and uses UTC timestamps in the heartbeat.ts column.
57 * (https://www.percona.com/doc/percona-toolkit/2.2/pt-heartbeat.html)
58 * - lagDetectionOptions : if using pt-heartbeat, this can be set to an array map to change
59 * the default behavior. Normally, the heartbeat row with the server
60 * ID of this server's master will be used. Set the "conds" field to
61 * override the query conditions, e.g. ['shard' => 's1'].
62 * - useGTIDs : use GTID methods like MASTER_GTID_WAIT() when possible.
63 * - sslKeyPath : path to key file [default: null]
64 * - sslCertPath : path to certificate file [default: null]
65 * - sslCAPath : parth to certificate authority PEM files [default: null]
66 * - sslCiphers : array list of allowable ciphers [default: null]
67 * @param array $params
68 */
69 function __construct( array $params ) {
70 parent::__construct( $params );
71
72 $this->lagDetectionMethod = isset( $params['lagDetectionMethod'] )
73 ? $params['lagDetectionMethod']
74 : 'Seconds_Behind_Master';
75 $this->lagDetectionOptions = isset( $params['lagDetectionOptions'] )
76 ? $params['lagDetectionOptions']
77 : [];
78 $this->useGTIDs = !empty( $params['useGTIDs' ] );
79 foreach ( [ 'KeyPath', 'CertPath', 'CAPath', 'Ciphers' ] as $name ) {
80 $var = "ssl{$name}";
81 if ( isset( $params[$var] ) ) {
82 $this->$var = $params[$var];
83 }
84 }
85 }
86
87 /**
88 * @return string
89 */
90 function getType() {
91 return 'mysql';
92 }
93
94 /**
95 * @param string $server
96 * @param string $user
97 * @param string $password
98 * @param string $dbName
99 * @throws Exception|DBConnectionError
100 * @return bool
101 */
102 function open( $server, $user, $password, $dbName ) {
103 global $wgAllDBsAreLocalhost, $wgSQLMode;
104
105 # Close/unset connection handle
106 $this->close();
107
108 # Debugging hack -- fake cluster
109 $realServer = $wgAllDBsAreLocalhost ? 'localhost' : $server;
110 $this->mServer = $server;
111 $this->mUser = $user;
112 $this->mPassword = $password;
113 $this->mDBname = $dbName;
114
115 $this->installErrorHandler();
116 try {
117 $this->mConn = $this->mysqlConnect( $realServer );
118 } catch ( Exception $ex ) {
119 $this->restoreErrorHandler();
120 throw $ex;
121 }
122 $error = $this->restoreErrorHandler();
123
124 # Always log connection errors
125 if ( !$this->mConn ) {
126 if ( !$error ) {
127 $error = $this->lastError();
128 }
129 wfLogDBError(
130 "Error connecting to {db_server}: {error}",
131 $this->getLogContext( [
132 'method' => __METHOD__,
133 'error' => $error,
134 ] )
135 );
136 wfDebug( "DB connection error\n" .
137 "Server: $server, User: $user, Password: " .
138 substr( $password, 0, 3 ) . "..., error: " . $error . "\n" );
139
140 $this->reportConnectionError( $error );
141 }
142
143 if ( $dbName != '' ) {
144 MediaWiki\suppressWarnings();
145 $success = $this->selectDB( $dbName );
146 MediaWiki\restoreWarnings();
147 if ( !$success ) {
148 wfLogDBError(
149 "Error selecting database {db_name} on server {db_server}",
150 $this->getLogContext( [
151 'method' => __METHOD__,
152 ] )
153 );
154 wfDebug( "Error selecting database $dbName on server {$this->mServer} " .
155 "from client host " . wfHostname() . "\n" );
156
157 $this->reportConnectionError( "Error selecting database $dbName" );
158 }
159 }
160
161 // Tell the server what we're communicating with
162 if ( !$this->connectInitCharset() ) {
163 $this->reportConnectionError( "Error setting character set" );
164 }
165
166 // Abstract over any insane MySQL defaults
167 $set = [ 'group_concat_max_len = 262144' ];
168 // Set SQL mode, default is turning them all off, can be overridden or skipped with null
169 if ( is_string( $wgSQLMode ) ) {
170 $set[] = 'sql_mode = ' . $this->addQuotes( $wgSQLMode );
171 }
172 // Set any custom settings defined by site config
173 // (e.g. https://dev.mysql.com/doc/refman/4.1/en/innodb-parameters.html)
174 foreach ( $this->mSessionVars as $var => $val ) {
175 // Escape strings but not numbers to avoid MySQL complaining
176 if ( !is_int( $val ) && !is_float( $val ) ) {
177 $val = $this->addQuotes( $val );
178 }
179 $set[] = $this->addIdentifierQuotes( $var ) . ' = ' . $val;
180 }
181
182 if ( $set ) {
183 // Use doQuery() to avoid opening implicit transactions (DBO_TRX)
184 $success = $this->doQuery( 'SET ' . implode( ', ', $set ) );
185 if ( !$success ) {
186 wfLogDBError(
187 'Error setting MySQL variables on server {db_server} (check $wgSQLMode)',
188 $this->getLogContext( [
189 'method' => __METHOD__,
190 ] )
191 );
192 $this->reportConnectionError(
193 'Error setting MySQL variables on server {db_server} (check $wgSQLMode)' );
194 }
195 }
196
197 $this->mOpened = true;
198
199 return true;
200 }
201
202 /**
203 * Set the character set information right after connection
204 * @return bool
205 */
206 protected function connectInitCharset() {
207 global $wgDBmysql5;
208
209 if ( $wgDBmysql5 ) {
210 // Tell the server we're communicating with it in UTF-8.
211 // This may engage various charset conversions.
212 return $this->mysqlSetCharset( 'utf8' );
213 } else {
214 return $this->mysqlSetCharset( 'binary' );
215 }
216 }
217
218 /**
219 * Open a connection to a MySQL server
220 *
221 * @param string $realServer
222 * @return mixed Raw connection
223 * @throws DBConnectionError
224 */
225 abstract protected function mysqlConnect( $realServer );
226
227 /**
228 * Set the character set of the MySQL link
229 *
230 * @param string $charset
231 * @return bool
232 */
233 abstract protected function mysqlSetCharset( $charset );
234
235 /**
236 * @param ResultWrapper|resource $res
237 * @throws DBUnexpectedError
238 */
239 function freeResult( $res ) {
240 if ( $res instanceof ResultWrapper ) {
241 $res = $res->result;
242 }
243 MediaWiki\suppressWarnings();
244 $ok = $this->mysqlFreeResult( $res );
245 MediaWiki\restoreWarnings();
246 if ( !$ok ) {
247 throw new DBUnexpectedError( $this, "Unable to free MySQL result" );
248 }
249 }
250
251 /**
252 * Free result memory
253 *
254 * @param resource $res Raw result
255 * @return bool
256 */
257 abstract protected function mysqlFreeResult( $res );
258
259 /**
260 * @param ResultWrapper|resource $res
261 * @return stdClass|bool
262 * @throws DBUnexpectedError
263 */
264 function fetchObject( $res ) {
265 if ( $res instanceof ResultWrapper ) {
266 $res = $res->result;
267 }
268 MediaWiki\suppressWarnings();
269 $row = $this->mysqlFetchObject( $res );
270 MediaWiki\restoreWarnings();
271
272 $errno = $this->lastErrno();
273 // Unfortunately, mysql_fetch_object does not reset the last errno.
274 // Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as
275 // these are the only errors mysql_fetch_object can cause.
276 // See http://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html.
277 if ( $errno == 2000 || $errno == 2013 ) {
278 throw new DBUnexpectedError(
279 $this,
280 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() )
281 );
282 }
283
284 return $row;
285 }
286
287 /**
288 * Fetch a result row as an object
289 *
290 * @param resource $res Raw result
291 * @return stdClass
292 */
293 abstract protected function mysqlFetchObject( $res );
294
295 /**
296 * @param ResultWrapper|resource $res
297 * @return array|bool
298 * @throws DBUnexpectedError
299 */
300 function fetchRow( $res ) {
301 if ( $res instanceof ResultWrapper ) {
302 $res = $res->result;
303 }
304 MediaWiki\suppressWarnings();
305 $row = $this->mysqlFetchArray( $res );
306 MediaWiki\restoreWarnings();
307
308 $errno = $this->lastErrno();
309 // Unfortunately, mysql_fetch_array does not reset the last errno.
310 // Only check for CR_SERVER_LOST and CR_UNKNOWN_ERROR, as
311 // these are the only errors mysql_fetch_array can cause.
312 // See http://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html.
313 if ( $errno == 2000 || $errno == 2013 ) {
314 throw new DBUnexpectedError(
315 $this,
316 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() )
317 );
318 }
319
320 return $row;
321 }
322
323 /**
324 * Fetch a result row as an associative and numeric array
325 *
326 * @param resource $res Raw result
327 * @return array
328 */
329 abstract protected function mysqlFetchArray( $res );
330
331 /**
332 * @throws DBUnexpectedError
333 * @param ResultWrapper|resource $res
334 * @return int
335 */
336 function numRows( $res ) {
337 if ( $res instanceof ResultWrapper ) {
338 $res = $res->result;
339 }
340 MediaWiki\suppressWarnings();
341 $n = $this->mysqlNumRows( $res );
342 MediaWiki\restoreWarnings();
343
344 // Unfortunately, mysql_num_rows does not reset the last errno.
345 // We are not checking for any errors here, since
346 // these are no errors mysql_num_rows can cause.
347 // See http://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html.
348 // See https://phabricator.wikimedia.org/T44430
349 return $n;
350 }
351
352 /**
353 * Get number of rows in result
354 *
355 * @param resource $res Raw result
356 * @return int
357 */
358 abstract protected function mysqlNumRows( $res );
359
360 /**
361 * @param ResultWrapper|resource $res
362 * @return int
363 */
364 function numFields( $res ) {
365 if ( $res instanceof ResultWrapper ) {
366 $res = $res->result;
367 }
368
369 return $this->mysqlNumFields( $res );
370 }
371
372 /**
373 * Get number of fields in result
374 *
375 * @param resource $res Raw result
376 * @return int
377 */
378 abstract protected function mysqlNumFields( $res );
379
380 /**
381 * @param ResultWrapper|resource $res
382 * @param int $n
383 * @return string
384 */
385 function fieldName( $res, $n ) {
386 if ( $res instanceof ResultWrapper ) {
387 $res = $res->result;
388 }
389
390 return $this->mysqlFieldName( $res, $n );
391 }
392
393 /**
394 * Get the name of the specified field in a result
395 *
396 * @param ResultWrapper|resource $res
397 * @param int $n
398 * @return string
399 */
400 abstract protected function mysqlFieldName( $res, $n );
401
402 /**
403 * mysql_field_type() wrapper
404 * @param ResultWrapper|resource $res
405 * @param int $n
406 * @return string
407 */
408 public function fieldType( $res, $n ) {
409 if ( $res instanceof ResultWrapper ) {
410 $res = $res->result;
411 }
412
413 return $this->mysqlFieldType( $res, $n );
414 }
415
416 /**
417 * Get the type of the specified field in a result
418 *
419 * @param ResultWrapper|resource $res
420 * @param int $n
421 * @return string
422 */
423 abstract protected function mysqlFieldType( $res, $n );
424
425 /**
426 * @param ResultWrapper|resource $res
427 * @param int $row
428 * @return bool
429 */
430 function dataSeek( $res, $row ) {
431 if ( $res instanceof ResultWrapper ) {
432 $res = $res->result;
433 }
434
435 return $this->mysqlDataSeek( $res, $row );
436 }
437
438 /**
439 * Move internal result pointer
440 *
441 * @param ResultWrapper|resource $res
442 * @param int $row
443 * @return bool
444 */
445 abstract protected function mysqlDataSeek( $res, $row );
446
447 /**
448 * @return string
449 */
450 function lastError() {
451 if ( $this->mConn ) {
452 # Even if it's non-zero, it can still be invalid
453 MediaWiki\suppressWarnings();
454 $error = $this->mysqlError( $this->mConn );
455 if ( !$error ) {
456 $error = $this->mysqlError();
457 }
458 MediaWiki\restoreWarnings();
459 } else {
460 $error = $this->mysqlError();
461 }
462 if ( $error ) {
463 $error .= ' (' . $this->mServer . ')';
464 }
465
466 return $error;
467 }
468
469 /**
470 * Returns the text of the error message from previous MySQL operation
471 *
472 * @param resource $conn Raw connection
473 * @return string
474 */
475 abstract protected function mysqlError( $conn = null );
476
477 /**
478 * @param string $table
479 * @param array $uniqueIndexes
480 * @param array $rows
481 * @param string $fname
482 * @return ResultWrapper
483 */
484 function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
485 return $this->nativeReplace( $table, $rows, $fname );
486 }
487
488 /**
489 * Estimate rows in dataset
490 * Returns estimated count, based on EXPLAIN output
491 * Takes same arguments as Database::select()
492 *
493 * @param string|array $table
494 * @param string|array $vars
495 * @param string|array $conds
496 * @param string $fname
497 * @param string|array $options
498 * @return bool|int
499 */
500 public function estimateRowCount( $table, $vars = '*', $conds = '',
501 $fname = __METHOD__, $options = []
502 ) {
503 $options['EXPLAIN'] = true;
504 $res = $this->select( $table, $vars, $conds, $fname, $options );
505 if ( $res === false ) {
506 return false;
507 }
508 if ( !$this->numRows( $res ) ) {
509 return 0;
510 }
511
512 $rows = 1;
513 foreach ( $res as $plan ) {
514 $rows *= $plan->rows > 0 ? $plan->rows : 1; // avoid resetting to zero
515 }
516
517 return (int)$rows;
518 }
519
520 /**
521 * @param string $table
522 * @param string $field
523 * @return bool|MySQLField
524 */
525 function fieldInfo( $table, $field ) {
526 $table = $this->tableName( $table );
527 $res = $this->query( "SELECT * FROM $table LIMIT 1", __METHOD__, true );
528 if ( !$res ) {
529 return false;
530 }
531 $n = $this->mysqlNumFields( $res->result );
532 for ( $i = 0; $i < $n; $i++ ) {
533 $meta = $this->mysqlFetchField( $res->result, $i );
534 if ( $field == $meta->name ) {
535 return new MySQLField( $meta );
536 }
537 }
538
539 return false;
540 }
541
542 /**
543 * Get column information from a result
544 *
545 * @param resource $res Raw result
546 * @param int $n
547 * @return stdClass
548 */
549 abstract protected function mysqlFetchField( $res, $n );
550
551 /**
552 * Get information about an index into an object
553 * Returns false if the index does not exist
554 *
555 * @param string $table
556 * @param string $index
557 * @param string $fname
558 * @return bool|array|null False or null on failure
559 */
560 function indexInfo( $table, $index, $fname = __METHOD__ ) {
561 # SHOW INDEX works in MySQL 3.23.58, but SHOW INDEXES does not.
562 # SHOW INDEX should work for 3.x and up:
563 # http://dev.mysql.com/doc/mysql/en/SHOW_INDEX.html
564 $table = $this->tableName( $table );
565 $index = $this->indexName( $index );
566
567 $sql = 'SHOW INDEX FROM ' . $table;
568 $res = $this->query( $sql, $fname );
569
570 if ( !$res ) {
571 return null;
572 }
573
574 $result = [];
575
576 foreach ( $res as $row ) {
577 if ( $row->Key_name == $index ) {
578 $result[] = $row;
579 }
580 }
581
582 return empty( $result ) ? false : $result;
583 }
584
585 /**
586 * @param string $s
587 * @return string
588 */
589 function strencode( $s ) {
590 return $this->mysqlRealEscapeString( $s );
591 }
592
593 /**
594 * @param string $s
595 * @return mixed
596 */
597 abstract protected function mysqlRealEscapeString( $s );
598
599 /**
600 * MySQL uses `backticks` for identifier quoting instead of the sql standard "double quotes".
601 *
602 * @param string $s
603 * @return string
604 */
605 public function addIdentifierQuotes( $s ) {
606 // Characters in the range \u0001-\uFFFF are valid in a quoted identifier
607 // Remove NUL bytes and escape backticks by doubling
608 return '`' . str_replace( [ "\0", '`' ], [ '', '``' ], $s ) . '`';
609 }
610
611 /**
612 * @param string $name
613 * @return bool
614 */
615 public function isQuotedIdentifier( $name ) {
616 return strlen( $name ) && $name[0] == '`' && substr( $name, -1, 1 ) == '`';
617 }
618
619 function getLag() {
620 if ( $this->getLagDetectionMethod() === 'pt-heartbeat' ) {
621 return $this->getLagFromPtHeartbeat();
622 } else {
623 return $this->getLagFromSlaveStatus();
624 }
625 }
626
627 /**
628 * @return string
629 */
630 protected function getLagDetectionMethod() {
631 return $this->lagDetectionMethod;
632 }
633
634 /**
635 * @return bool|int
636 */
637 protected function getLagFromSlaveStatus() {
638 $res = $this->query( 'SHOW SLAVE STATUS', __METHOD__ );
639 $row = $res ? $res->fetchObject() : false;
640 if ( $row && strval( $row->Seconds_Behind_Master ) !== '' ) {
641 return intval( $row->Seconds_Behind_Master );
642 }
643
644 return false;
645 }
646
647 /**
648 * @return bool|float
649 */
650 protected function getLagFromPtHeartbeat() {
651 $options = $this->lagDetectionOptions;
652
653 if ( isset( $options['conds'] ) ) {
654 // Best method for multi-DC setups: use logical channel names
655 $data = $this->getHeartbeatData( $options['conds'] );
656 } else {
657 // Standard method: use master server ID (works with stock pt-heartbeat)
658 $masterInfo = $this->getMasterServerInfo();
659 if ( !$masterInfo ) {
660 wfLogDBError(
661 "Unable to query master of {db_server} for server ID",
662 $this->getLogContext( [
663 'method' => __METHOD__
664 ] )
665 );
666
667 return false; // could not get master server ID
668 }
669
670 $conds = [ 'server_id' => intval( $masterInfo['serverId'] ) ];
671 $data = $this->getHeartbeatData( $conds );
672 }
673
674 list( $time, $nowUnix ) = $data;
675 if ( $time !== null ) {
676 // @time is in ISO format like "2015-09-25T16:48:10.000510"
677 $dateTime = new DateTime( $time, new DateTimeZone( 'UTC' ) );
678 $timeUnix = (int)$dateTime->format( 'U' ) + $dateTime->format( 'u' ) / 1e6;
679
680 return max( $nowUnix - $timeUnix, 0.0 );
681 }
682
683 wfLogDBError(
684 "Unable to find pt-heartbeat row for {db_server}",
685 $this->getLogContext( [
686 'method' => __METHOD__
687 ] )
688 );
689
690 return false;
691 }
692
693 protected function getMasterServerInfo() {
694 $cache = $this->srvCache;
695 $key = $cache->makeGlobalKey(
696 'mysql',
697 'master-info',
698 // Using one key for all cluster slaves is preferable
699 $this->getLBInfo( 'clusterMasterHost' ) ?: $this->getServer()
700 );
701
702 return $cache->getWithSetCallback(
703 $key,
704 $cache::TTL_INDEFINITE,
705 function () use ( $cache, $key ) {
706 // Get and leave a lock key in place for a short period
707 if ( !$cache->lock( $key, 0, 10 ) ) {
708 return false; // avoid master connection spike slams
709 }
710
711 $conn = $this->getLazyMasterHandle();
712 if ( !$conn ) {
713 return false; // something is misconfigured
714 }
715
716 // Connect to and query the master; catch errors to avoid outages
717 try {
718 $res = $conn->query( 'SELECT @@server_id AS id', __METHOD__ );
719 $row = $res ? $res->fetchObject() : false;
720 $id = $row ? (int)$row->id : 0;
721 } catch ( DBError $e ) {
722 $id = 0;
723 }
724
725 // Cache the ID if it was retrieved
726 return $id ? [ 'serverId' => $id, 'asOf' => time() ] : false;
727 }
728 );
729 }
730
731 /**
732 * @param array $conds WHERE clause conditions to find a row
733 * @return array (heartbeat `ts` column value or null, UNIX timestamp) for the newest beat
734 * @see https://www.percona.com/doc/percona-toolkit/2.1/pt-heartbeat.html
735 */
736 protected function getHeartbeatData( array $conds ) {
737 $whereSQL = $this->makeList( $conds, LIST_AND );
738 // Use ORDER BY for channel based queries since that field might not be UNIQUE.
739 // Note: this would use "TIMESTAMPDIFF(MICROSECOND,ts,UTC_TIMESTAMP(6))" but the
740 // percision field is not supported in MySQL <= 5.5.
741 $res = $this->query(
742 "SELECT ts FROM heartbeat.heartbeat WHERE $whereSQL ORDER BY ts DESC LIMIT 1"
743 );
744 $row = $res ? $res->fetchObject() : false;
745
746 return [ $row ? $row->ts : null, microtime( true ) ];
747 }
748
749 public function getApproximateLagStatus() {
750 if ( $this->getLagDetectionMethod() === 'pt-heartbeat' ) {
751 // Disable caching since this is fast enough and we don't wan't
752 // to be *too* pessimistic by having both the cache TTL and the
753 // pt-heartbeat interval count as lag in getSessionLagStatus()
754 return parent::getApproximateLagStatus();
755 }
756
757 $key = $this->srvCache->makeGlobalKey( 'mysql-lag', $this->getServer() );
758 $approxLag = $this->srvCache->get( $key );
759 if ( !$approxLag ) {
760 $approxLag = parent::getApproximateLagStatus();
761 $this->srvCache->set( $key, $approxLag, 1 );
762 }
763
764 return $approxLag;
765 }
766
767 function masterPosWait( DBMasterPos $pos, $timeout ) {
768 if ( !( $pos instanceof MySQLMasterPos ) ) {
769 throw new InvalidArgumentException( "Position not an instance of MySQLMasterPos" );
770 }
771
772 if ( $this->getLBInfo( 'is static' ) === true ) {
773 return 0; // this is a copy of a read-only dataset with no master DB
774 } elseif ( $this->lastKnownSlavePos && $this->lastKnownSlavePos->hasReached( $pos ) ) {
775 return 0; // already reached this point for sure
776 }
777
778 // Call doQuery() directly, to avoid opening a transaction if DBO_TRX is set
779 if ( $this->useGTIDs && $pos->gtids ) {
780 // Wait on the GTID set (MariaDB only)
781 $gtidArg = $this->addQuotes( implode( ',', $pos->gtids ) );
782 $res = $this->doQuery( "SELECT MASTER_GTID_WAIT($gtidArg, $timeout)" );
783 } else {
784 // Wait on the binlog coordinates
785 $encFile = $this->addQuotes( $pos->file );
786 $encPos = intval( $pos->pos );
787 $res = $this->doQuery( "SELECT MASTER_POS_WAIT($encFile, $encPos, $timeout)" );
788 }
789
790 $row = $res ? $this->fetchRow( $res ) : false;
791 if ( !$row ) {
792 throw new DBExpectedError( $this, "Failed to query MASTER_POS_WAIT()" );
793 }
794
795 // Result can be NULL (error), -1 (timeout), or 0+ per the MySQL manual
796 $status = ( $row[0] !== null ) ? intval( $row[0] ) : null;
797 if ( $status === null ) {
798 // T126436: jobs programmed to wait on master positions might be referencing binlogs
799 // with an old master hostname. Such calls make MASTER_POS_WAIT() return null. Try
800 // to detect this and treat the slave as having reached the position; a proper master
801 // switchover already requires that the new master be caught up before the switch.
802 $slavePos = $this->getSlavePos();
803 if ( $slavePos && !$slavePos->channelsMatch( $pos ) ) {
804 $this->lastKnownSlavePos = $slavePos;
805 $status = 0;
806 }
807 } elseif ( $status >= 0 ) {
808 // Remember that this position was reached to save queries next time
809 $this->lastKnownSlavePos = $pos;
810 }
811
812 return $status;
813 }
814
815 /**
816 * Get the position of the master from SHOW SLAVE STATUS
817 *
818 * @return MySQLMasterPos|bool
819 */
820 function getSlavePos() {
821 $res = $this->query( 'SHOW SLAVE STATUS', __METHOD__ );
822 $row = $this->fetchObject( $res );
823
824 if ( $row ) {
825 $pos = isset( $row->Exec_master_log_pos )
826 ? $row->Exec_master_log_pos
827 : $row->Exec_Master_Log_Pos;
828 // Also fetch the last-applied GTID set (MariaDB)
829 if ( $this->useGTIDs ) {
830 $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'gtid_slave_pos'", __METHOD__ );
831 $gtidRow = $this->fetchObject( $res );
832 $gtidSet = $gtidRow ? $gtidRow->Value : '';
833 } else {
834 $gtidSet = '';
835 }
836
837 return new MySQLMasterPos( $row->Relay_Master_Log_File, $pos, $gtidSet );
838 } else {
839 return false;
840 }
841 }
842
843 /**
844 * Get the position of the master from SHOW MASTER STATUS
845 *
846 * @return MySQLMasterPos|bool
847 */
848 function getMasterPos() {
849 $res = $this->query( 'SHOW MASTER STATUS', __METHOD__ );
850 $row = $this->fetchObject( $res );
851
852 if ( $row ) {
853 // Also fetch the last-written GTID set (MariaDB)
854 if ( $this->useGTIDs ) {
855 $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'gtid_binlog_pos'", __METHOD__ );
856 $gtidRow = $this->fetchObject( $res );
857 $gtidSet = $gtidRow ? $gtidRow->Value : '';
858 } else {
859 $gtidSet = '';
860 }
861
862 return new MySQLMasterPos( $row->File, $row->Position, $gtidSet );
863 } else {
864 return false;
865 }
866 }
867
868 public function serverIsReadOnly() {
869 $res = $this->query( "SHOW GLOBAL VARIABLES LIKE 'read_only'", __METHOD__ );
870 $row = $this->fetchObject( $res );
871
872 return $row ? ( strtolower( $row->Value ) === 'on' ) : false;
873 }
874
875 /**
876 * @param string $index
877 * @return string
878 */
879 function useIndexClause( $index ) {
880 return "FORCE INDEX (" . $this->indexName( $index ) . ")";
881 }
882
883 /**
884 * @return string
885 */
886 function lowPriorityOption() {
887 return 'LOW_PRIORITY';
888 }
889
890 /**
891 * @return string
892 */
893 public function getSoftwareLink() {
894 // MariaDB includes its name in its version string; this is how MariaDB's version of
895 // the mysql command-line client identifies MariaDB servers (see mariadb_connection()
896 // in libmysql/libmysql.c).
897 $version = $this->getServerVersion();
898 if ( strpos( $version, 'MariaDB' ) !== false || strpos( $version, '-maria-' ) !== false ) {
899 return '[{{int:version-db-mariadb-url}} MariaDB]';
900 }
901
902 // Percona Server's version suffix is not very distinctive, and @@version_comment
903 // doesn't give the necessary info for source builds, so assume the server is MySQL.
904 // (Even Percona's version of mysql doesn't try to make the distinction.)
905 return '[{{int:version-db-mysql-url}} MySQL]';
906 }
907
908 /**
909 * @return string
910 */
911 public function getServerVersion() {
912 // Not using mysql_get_server_info() or similar for consistency: in the handshake,
913 // MariaDB 10 adds the prefix "5.5.5-", and only some newer client libraries strip
914 // it off (see RPL_VERSION_HACK in include/mysql_com.h).
915 if ( $this->serverVersion === null ) {
916 $this->serverVersion = $this->selectField( '', 'VERSION()', '', __METHOD__ );
917 }
918 return $this->serverVersion;
919 }
920
921 /**
922 * @param array $options
923 */
924 public function setSessionOptions( array $options ) {
925 if ( isset( $options['connTimeout'] ) ) {
926 $timeout = (int)$options['connTimeout'];
927 $this->query( "SET net_read_timeout=$timeout" );
928 $this->query( "SET net_write_timeout=$timeout" );
929 }
930 }
931
932 /**
933 * @param string $sql
934 * @param string $newLine
935 * @return bool
936 */
937 public function streamStatementEnd( &$sql, &$newLine ) {
938 if ( strtoupper( substr( $newLine, 0, 9 ) ) == 'DELIMITER' ) {
939 preg_match( '/^DELIMITER\s+(\S+)/', $newLine, $m );
940 $this->delimiter = $m[1];
941 $newLine = '';
942 }
943
944 return parent::streamStatementEnd( $sql, $newLine );
945 }
946
947 /**
948 * Check to see if a named lock is available. This is non-blocking.
949 *
950 * @param string $lockName Name of lock to poll
951 * @param string $method Name of method calling us
952 * @return bool
953 * @since 1.20
954 */
955 public function lockIsFree( $lockName, $method ) {
956 $lockName = $this->addQuotes( $this->makeLockName( $lockName ) );
957 $result = $this->query( "SELECT IS_FREE_LOCK($lockName) AS lockstatus", $method );
958 $row = $this->fetchObject( $result );
959
960 return ( $row->lockstatus == 1 );
961 }
962
963 /**
964 * @param string $lockName
965 * @param string $method
966 * @param int $timeout
967 * @return bool
968 */
969 public function lock( $lockName, $method, $timeout = 5 ) {
970 $lockName = $this->addQuotes( $this->makeLockName( $lockName ) );
971 $result = $this->query( "SELECT GET_LOCK($lockName, $timeout) AS lockstatus", $method );
972 $row = $this->fetchObject( $result );
973
974 if ( $row->lockstatus == 1 ) {
975 parent::lock( $lockName, $method, $timeout ); // record
976 return true;
977 }
978
979 wfDebug( __METHOD__ . " failed to acquire lock\n" );
980
981 return false;
982 }
983
984 /**
985 * FROM MYSQL DOCS:
986 * http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock
987 * @param string $lockName
988 * @param string $method
989 * @return bool
990 */
991 public function unlock( $lockName, $method ) {
992 $lockName = $this->addQuotes( $this->makeLockName( $lockName ) );
993 $result = $this->query( "SELECT RELEASE_LOCK($lockName) as lockstatus", $method );
994 $row = $this->fetchObject( $result );
995
996 if ( $row->lockstatus == 1 ) {
997 parent::unlock( $lockName, $method ); // record
998 return true;
999 }
1000
1001 wfDebug( __METHOD__ . " failed to release lock\n" );
1002
1003 return false;
1004 }
1005
1006 private function makeLockName( $lockName ) {
1007 // http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock
1008 // Newer version enforce a 64 char length limit.
1009 return ( strlen( $lockName ) > 64 ) ? sha1( $lockName ) : $lockName;
1010 }
1011
1012 public function namedLocksEnqueue() {
1013 return true;
1014 }
1015
1016 /**
1017 * @param array $read
1018 * @param array $write
1019 * @param string $method
1020 * @param bool $lowPriority
1021 * @return bool
1022 */
1023 public function lockTables( $read, $write, $method, $lowPriority = true ) {
1024 $items = [];
1025
1026 foreach ( $write as $table ) {
1027 $tbl = $this->tableName( $table ) .
1028 ( $lowPriority ? ' LOW_PRIORITY' : '' ) .
1029 ' WRITE';
1030 $items[] = $tbl;
1031 }
1032 foreach ( $read as $table ) {
1033 $items[] = $this->tableName( $table ) . ' READ';
1034 }
1035 $sql = "LOCK TABLES " . implode( ',', $items );
1036 $this->query( $sql, $method );
1037
1038 return true;
1039 }
1040
1041 /**
1042 * @param string $method
1043 * @return bool
1044 */
1045 public function unlockTables( $method ) {
1046 $this->query( "UNLOCK TABLES", $method );
1047
1048 return true;
1049 }
1050
1051 /**
1052 * Get search engine class. All subclasses of this
1053 * need to implement this if they wish to use searching.
1054 *
1055 * @return string
1056 */
1057 public function getSearchEngine() {
1058 return 'SearchMySQL';
1059 }
1060
1061 /**
1062 * @param bool $value
1063 */
1064 public function setBigSelects( $value = true ) {
1065 if ( $value === 'default' ) {
1066 if ( $this->mDefaultBigSelects === null ) {
1067 # Function hasn't been called before so it must already be set to the default
1068 return;
1069 } else {
1070 $value = $this->mDefaultBigSelects;
1071 }
1072 } elseif ( $this->mDefaultBigSelects === null ) {
1073 $this->mDefaultBigSelects =
1074 (bool)$this->selectField( false, '@@sql_big_selects', '', __METHOD__ );
1075 }
1076 $encValue = $value ? '1' : '0';
1077 $this->query( "SET sql_big_selects=$encValue", __METHOD__ );
1078 }
1079
1080 /**
1081 * DELETE where the condition is a join. MySql uses multi-table deletes.
1082 * @param string $delTable
1083 * @param string $joinTable
1084 * @param string $delVar
1085 * @param string $joinVar
1086 * @param array|string $conds
1087 * @param bool|string $fname
1088 * @throws DBUnexpectedError
1089 * @return bool|ResultWrapper
1090 */
1091 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__ ) {
1092 if ( !$conds ) {
1093 throw new DBUnexpectedError( $this, 'DatabaseBase::deleteJoin() called with empty $conds' );
1094 }
1095
1096 $delTable = $this->tableName( $delTable );
1097 $joinTable = $this->tableName( $joinTable );
1098 $sql = "DELETE $delTable FROM $delTable, $joinTable WHERE $delVar=$joinVar ";
1099
1100 if ( $conds != '*' ) {
1101 $sql .= ' AND ' . $this->makeList( $conds, LIST_AND );
1102 }
1103
1104 return $this->query( $sql, $fname );
1105 }
1106
1107 /**
1108 * @param string $table
1109 * @param array $rows
1110 * @param array $uniqueIndexes
1111 * @param array $set
1112 * @param string $fname
1113 * @return bool
1114 */
1115 public function upsert( $table, array $rows, array $uniqueIndexes,
1116 array $set, $fname = __METHOD__
1117 ) {
1118 if ( !count( $rows ) ) {
1119 return true; // nothing to do
1120 }
1121
1122 if ( !is_array( reset( $rows ) ) ) {
1123 $rows = [ $rows ];
1124 }
1125
1126 $table = $this->tableName( $table );
1127 $columns = array_keys( $rows[0] );
1128
1129 $sql = "INSERT INTO $table (" . implode( ',', $columns ) . ') VALUES ';
1130 $rowTuples = [];
1131 foreach ( $rows as $row ) {
1132 $rowTuples[] = '(' . $this->makeList( $row ) . ')';
1133 }
1134 $sql .= implode( ',', $rowTuples );
1135 $sql .= " ON DUPLICATE KEY UPDATE " . $this->makeList( $set, LIST_SET );
1136
1137 return (bool)$this->query( $sql, $fname );
1138 }
1139
1140 /**
1141 * Determines how long the server has been up
1142 *
1143 * @return int
1144 */
1145 function getServerUptime() {
1146 $vars = $this->getMysqlStatus( 'Uptime' );
1147
1148 return (int)$vars['Uptime'];
1149 }
1150
1151 /**
1152 * Determines if the last failure was due to a deadlock
1153 *
1154 * @return bool
1155 */
1156 function wasDeadlock() {
1157 return $this->lastErrno() == 1213;
1158 }
1159
1160 /**
1161 * Determines if the last failure was due to a lock timeout
1162 *
1163 * @return bool
1164 */
1165 function wasLockTimeout() {
1166 return $this->lastErrno() == 1205;
1167 }
1168
1169 function wasErrorReissuable() {
1170 return $this->lastErrno() == 2013 || $this->lastErrno() == 2006;
1171 }
1172
1173 /**
1174 * Determines if the last failure was due to the database being read-only.
1175 *
1176 * @return bool
1177 */
1178 function wasReadOnlyError() {
1179 return $this->lastErrno() == 1223 ||
1180 ( $this->lastErrno() == 1290 && strpos( $this->lastError(), '--read-only' ) !== false );
1181 }
1182
1183 function wasConnectionError( $errno ) {
1184 return $errno == 2013 || $errno == 2006;
1185 }
1186
1187 /**
1188 * Get the underlying binding handle, mConn
1189 *
1190 * Makes sure that mConn is set (disconnects and ping() failure can unset it).
1191 * This catches broken callers than catch and ignore disconnection exceptions.
1192 * Unlike checking isOpen(), this is safe to call inside of open().
1193 *
1194 * @return resource|object
1195 * @throws DBUnexpectedError
1196 * @since 1.26
1197 */
1198 protected function getBindingHandle() {
1199 if ( !$this->mConn ) {
1200 throw new DBUnexpectedError(
1201 $this,
1202 'DB connection was already closed or the connection dropped.'
1203 );
1204 }
1205
1206 return $this->mConn;
1207 }
1208
1209 /**
1210 * @param string $oldName
1211 * @param string $newName
1212 * @param bool $temporary
1213 * @param string $fname
1214 * @return bool
1215 */
1216 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__ ) {
1217 $tmp = $temporary ? 'TEMPORARY ' : '';
1218 $newName = $this->addIdentifierQuotes( $newName );
1219 $oldName = $this->addIdentifierQuotes( $oldName );
1220 $query = "CREATE $tmp TABLE $newName (LIKE $oldName)";
1221
1222 return $this->query( $query, $fname );
1223 }
1224
1225 /**
1226 * List all tables on the database
1227 *
1228 * @param string $prefix Only show tables with this prefix, e.g. mw_
1229 * @param string $fname Calling function name
1230 * @return array
1231 */
1232 function listTables( $prefix = null, $fname = __METHOD__ ) {
1233 $result = $this->query( "SHOW TABLES", $fname );
1234
1235 $endArray = [];
1236
1237 foreach ( $result as $table ) {
1238 $vars = get_object_vars( $table );
1239 $table = array_pop( $vars );
1240
1241 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
1242 $endArray[] = $table;
1243 }
1244 }
1245
1246 return $endArray;
1247 }
1248
1249 /**
1250 * @param string $tableName
1251 * @param string $fName
1252 * @return bool|ResultWrapper
1253 */
1254 public function dropTable( $tableName, $fName = __METHOD__ ) {
1255 if ( !$this->tableExists( $tableName, $fName ) ) {
1256 return false;
1257 }
1258
1259 return $this->query( "DROP TABLE IF EXISTS " . $this->tableName( $tableName ), $fName );
1260 }
1261
1262 /**
1263 * @return array
1264 */
1265 protected function getDefaultSchemaVars() {
1266 $vars = parent::getDefaultSchemaVars();
1267 $vars['wgDBTableOptions'] = str_replace( 'TYPE', 'ENGINE', $GLOBALS['wgDBTableOptions'] );
1268 $vars['wgDBTableOptions'] = str_replace(
1269 'CHARSET=mysql4',
1270 'CHARSET=binary',
1271 $vars['wgDBTableOptions']
1272 );
1273
1274 return $vars;
1275 }
1276
1277 /**
1278 * Get status information from SHOW STATUS in an associative array
1279 *
1280 * @param string $which
1281 * @return array
1282 */
1283 function getMysqlStatus( $which = "%" ) {
1284 $res = $this->query( "SHOW STATUS LIKE '{$which}'" );
1285 $status = [];
1286
1287 foreach ( $res as $row ) {
1288 $status[$row->Variable_name] = $row->Value;
1289 }
1290
1291 return $status;
1292 }
1293
1294 /**
1295 * Lists VIEWs in the database
1296 *
1297 * @param string $prefix Only show VIEWs with this prefix, eg.
1298 * unit_test_, or $wgDBprefix. Default: null, would return all views.
1299 * @param string $fname Name of calling function
1300 * @return array
1301 * @since 1.22
1302 */
1303 public function listViews( $prefix = null, $fname = __METHOD__ ) {
1304
1305 if ( !isset( $this->allViews ) ) {
1306
1307 // The name of the column containing the name of the VIEW
1308 $propertyName = 'Tables_in_' . $this->mDBname;
1309
1310 // Query for the VIEWS
1311 $result = $this->query( 'SHOW FULL TABLES WHERE TABLE_TYPE = "VIEW"' );
1312 $this->allViews = [];
1313 while ( ( $row = $this->fetchRow( $result ) ) !== false ) {
1314 array_push( $this->allViews, $row[$propertyName] );
1315 }
1316 }
1317
1318 if ( is_null( $prefix ) || $prefix === '' ) {
1319 return $this->allViews;
1320 }
1321
1322 $filteredViews = [];
1323 foreach ( $this->allViews as $viewName ) {
1324 // Does the name of this VIEW start with the table-prefix?
1325 if ( strpos( $viewName, $prefix ) === 0 ) {
1326 array_push( $filteredViews, $viewName );
1327 }
1328 }
1329
1330 return $filteredViews;
1331 }
1332
1333 /**
1334 * Differentiates between a TABLE and a VIEW.
1335 *
1336 * @param string $name Name of the TABLE/VIEW to test
1337 * @param string $prefix
1338 * @return bool
1339 * @since 1.22
1340 */
1341 public function isView( $name, $prefix = null ) {
1342 return in_array( $name, $this->listViews( $prefix ) );
1343 }
1344 }
1345
1346 /**
1347 * Utility class.
1348 * @ingroup Database
1349 */
1350 class MySQLField implements Field {
1351 private $name, $tablename, $default, $max_length, $nullable,
1352 $is_pk, $is_unique, $is_multiple, $is_key, $type, $binary,
1353 $is_numeric, $is_blob, $is_unsigned, $is_zerofill;
1354
1355 function __construct( $info ) {
1356 $this->name = $info->name;
1357 $this->tablename = $info->table;
1358 $this->default = $info->def;
1359 $this->max_length = $info->max_length;
1360 $this->nullable = !$info->not_null;
1361 $this->is_pk = $info->primary_key;
1362 $this->is_unique = $info->unique_key;
1363 $this->is_multiple = $info->multiple_key;
1364 $this->is_key = ( $this->is_pk || $this->is_unique || $this->is_multiple );
1365 $this->type = $info->type;
1366 $this->binary = isset( $info->binary ) ? $info->binary : false;
1367 $this->is_numeric = isset( $info->numeric ) ? $info->numeric : false;
1368 $this->is_blob = isset( $info->blob ) ? $info->blob : false;
1369 $this->is_unsigned = isset( $info->unsigned ) ? $info->unsigned : false;
1370 $this->is_zerofill = isset( $info->zerofill ) ? $info->zerofill : false;
1371 }
1372
1373 /**
1374 * @return string
1375 */
1376 function name() {
1377 return $this->name;
1378 }
1379
1380 /**
1381 * @return string
1382 */
1383 function tableName() {
1384 return $this->tablename;
1385 }
1386
1387 /**
1388 * @return string
1389 */
1390 function type() {
1391 return $this->type;
1392 }
1393
1394 /**
1395 * @return bool
1396 */
1397 function isNullable() {
1398 return $this->nullable;
1399 }
1400
1401 function defaultValue() {
1402 return $this->default;
1403 }
1404
1405 /**
1406 * @return bool
1407 */
1408 function isKey() {
1409 return $this->is_key;
1410 }
1411
1412 /**
1413 * @return bool
1414 */
1415 function isMultipleKey() {
1416 return $this->is_multiple;
1417 }
1418
1419 /**
1420 * @return bool
1421 */
1422 function isBinary() {
1423 return $this->binary;
1424 }
1425
1426 /**
1427 * @return bool
1428 */
1429 function isNumeric() {
1430 return $this->is_numeric;
1431 }
1432
1433 /**
1434 * @return bool
1435 */
1436 function isBlob() {
1437 return $this->is_blob;
1438 }
1439
1440 /**
1441 * @return bool
1442 */
1443 function isUnsigned() {
1444 return $this->is_unsigned;
1445 }
1446
1447 /**
1448 * @return bool
1449 */
1450 function isZerofill() {
1451 return $this->is_zerofill;
1452 }
1453 }
1454
1455 /**
1456 * DBMasterPos class for MySQL/MariaDB
1457 *
1458 * Note that master positions and sync logic here make some assumptions:
1459 * - Binlog-based usage assumes single-source replication and non-hierarchical replication.
1460 * - GTID-based usage allows getting/syncing with multi-source replication. It is assumed
1461 * that GTID sets are complete (e.g. include all domains on the server).
1462 */
1463 class MySQLMasterPos implements DBMasterPos {
1464 /** @var string Binlog file */
1465 public $file;
1466 /** @var int Binglog file position */
1467 public $pos;
1468 /** @var string[] GTID list */
1469 public $gtids = [];
1470 /** @var float UNIX timestamp */
1471 public $asOfTime = 0.0;
1472
1473 /**
1474 * @param string $file Binlog file name
1475 * @param integer $pos Binlog position
1476 * @param string $gtid Comma separated GTID set [optional]
1477 */
1478 function __construct( $file, $pos, $gtid = '' ) {
1479 $this->file = $file;
1480 $this->pos = $pos;
1481 $this->gtids = array_map( 'trim', explode( ',', $gtid ) );
1482 $this->asOfTime = microtime( true );
1483 }
1484
1485 /**
1486 * @return string <binlog file>/<position>, e.g db1034-bin.000976/843431247
1487 */
1488 function __toString() {
1489 return "{$this->file}/{$this->pos}";
1490 }
1491
1492 function asOfTime() {
1493 return $this->asOfTime;
1494 }
1495
1496 function hasReached( DBMasterPos $pos ) {
1497 if ( !( $pos instanceof self ) ) {
1498 throw new InvalidArgumentException( "Position not an instance of " . __CLASS__ );
1499 }
1500
1501 // Prefer GTID comparisons, which work with multi-tier replication
1502 $thisPosByDomain = $this->getGtidCoordinates();
1503 $thatPosByDomain = $pos->getGtidCoordinates();
1504 if ( $thisPosByDomain && $thatPosByDomain ) {
1505 $reached = true;
1506 // Check that this has positions GTE all of those in $pos for all domains in $pos
1507 foreach ( $thatPosByDomain as $domain => $thatPos ) {
1508 $thisPos = isset( $thisPosByDomain[$domain] ) ? $thisPosByDomain[$domain] : -1;
1509 $reached = $reached && ( $thatPos <= $thisPos );
1510 }
1511
1512 return $reached;
1513 }
1514
1515 // Fallback to the binlog file comparisons
1516 $thisBinPos = $this->getBinlogCoordinates();
1517 $thatBinPos = $pos->getBinlogCoordinates();
1518 if ( $thisBinPos && $thatBinPos && $thisBinPos['binlog'] === $thatBinPos['binlog'] ) {
1519 return ( $thisBinPos['pos'] >= $thatBinPos['pos'] );
1520 }
1521
1522 // Comparing totally different binlogs does not make sense
1523 return false;
1524 }
1525
1526 function channelsMatch( DBMasterPos $pos ) {
1527 if ( !( $pos instanceof self ) ) {
1528 throw new InvalidArgumentException( "Position not an instance of " . __CLASS__ );
1529 }
1530
1531 // Prefer GTID comparisons, which work with multi-tier replication
1532 $thisPosDomains = array_keys( $this->getGtidCoordinates() );
1533 $thatPosDomains = array_keys( $pos->getGtidCoordinates() );
1534 if ( $thisPosDomains && $thatPosDomains ) {
1535 // Check that this has GTIDs for all domains in $pos
1536 return !array_diff( $thatPosDomains, $thisPosDomains );
1537 }
1538
1539 // Fallback to the binlog file comparisons
1540 $thisBinPos = $this->getBinlogCoordinates();
1541 $thatBinPos = $pos->getBinlogCoordinates();
1542
1543 return ( $thisBinPos && $thatBinPos && $thisBinPos['binlog'] === $thatBinPos['binlog'] );
1544 }
1545
1546 /**
1547 * @note: this returns false for multi-source replication GTID sets
1548 * @see https://mariadb.com/kb/en/mariadb/gtid
1549 * @see https://dev.mysql.com/doc/refman/5.6/en/replication-gtids-concepts.html
1550 * @return array Map of (domain => integer position) or false
1551 */
1552 protected function getGtidCoordinates() {
1553 $gtidInfos = [];
1554 foreach ( $this->gtids as $gtid ) {
1555 $m = [];
1556 // MariaDB style: <domain>-<server id>-<sequence number>
1557 if ( preg_match( '!^(\d+)-\d+-(\d+)$!', $gtid, $m ) ) {
1558 $gtidInfos[(int)$m[1]] = (int)$m[2];
1559 // MySQL style: <UUID domain>:<sequence number>
1560 } elseif ( preg_match( '!^(\w{8}-\w{4}-\w{4}-\w{4}-\w{12}):(\d+)$!', $gtid, $m ) ) {
1561 $gtidInfos[$m[1]] = (int)$m[2];
1562 } else {
1563 $gtidInfos = [];
1564 break; // unrecognized GTID
1565 }
1566
1567 }
1568
1569 return $gtidInfos;
1570 }
1571
1572 /**
1573 * @see http://dev.mysql.com/doc/refman/5.7/en/show-master-status.html
1574 * @see http://dev.mysql.com/doc/refman/5.7/en/show-slave-status.html
1575 * @return array|bool (binlog, (integer file number, integer position)) or false
1576 */
1577 protected function getBinlogCoordinates() {
1578 $m = [];
1579 if ( preg_match( '!^(.+)\.(\d+)/(\d+)$!', (string)$this, $m ) ) {
1580 return [ 'binlog' => $m[1], 'pos' => [ (int)$m[2], (int)$m[3] ] ];
1581 }
1582
1583 return false;
1584 }
1585 }