Inject Profiler into Database objects
[lhc/web/wiklou.git] / includes / libs / rdbms / database / Database.php
1 <?php
2 /**
3 * @defgroup Database Database
4 *
5 * This file deals with database interface functions
6 * and query specifics/optimisations.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Database
25 */
26 use Psr\Log\LoggerAwareInterface;
27 use Psr\Log\LoggerInterface;
28
29 /**
30 * Database abstraction object
31 * @ingroup Database
32 */
33 abstract class Database implements IDatabase, LoggerAwareInterface {
34 /** Number of times to re-try an operation in case of deadlock */
35 const DEADLOCK_TRIES = 4;
36 /** Minimum time to wait before retry, in microseconds */
37 const DEADLOCK_DELAY_MIN = 500000;
38 /** Maximum time to wait before retry */
39 const DEADLOCK_DELAY_MAX = 1500000;
40
41 /** How long before it is worth doing a dummy query to test the connection */
42 const PING_TTL = 1.0;
43 const PING_QUERY = 'SELECT 1 AS ping';
44
45 const TINY_WRITE_SEC = .010;
46 const SLOW_WRITE_SEC = .500;
47 const SMALL_WRITE_ROWS = 100;
48
49 /** @var string SQL query */
50 protected $mLastQuery = '';
51 /** @var bool */
52 protected $mDoneWrites = false;
53 /** @var string|bool */
54 protected $mPHPError = false;
55 /** @var string */
56 protected $mServer;
57 /** @var string */
58 protected $mUser;
59 /** @var string */
60 protected $mPassword;
61 /** @var string */
62 protected $mDBname;
63 /** @var array[] $aliases Map of (table => (dbname, schema, prefix) map) */
64 protected $tableAliases = [];
65 /** @var bool Whether this PHP instance is for a CLI script */
66 protected $cliMode;
67 /** @var string Agent name for query profiling */
68 protected $agent;
69
70 /** @var BagOStuff APC cache */
71 protected $srvCache;
72 /** @var LoggerInterface */
73 protected $connLogger;
74 /** @var LoggerInterface */
75 protected $queryLogger;
76 /** @var callback Error logging callback */
77 protected $errorLogger;
78
79 /** @var resource Database connection */
80 protected $mConn = null;
81 /** @var bool */
82 protected $mOpened = false;
83
84 /** @var array[] List of (callable, method name) */
85 protected $mTrxIdleCallbacks = [];
86 /** @var array[] List of (callable, method name) */
87 protected $mTrxPreCommitCallbacks = [];
88 /** @var array[] List of (callable, method name) */
89 protected $mTrxEndCallbacks = [];
90 /** @var callable[] Map of (name => callable) */
91 protected $mTrxRecurringCallbacks = [];
92 /** @var bool Whether to suppress triggering of transaction end callbacks */
93 protected $mTrxEndCallbacksSuppressed = false;
94
95 /** @var string */
96 protected $mTablePrefix = '';
97 /** @var string */
98 protected $mSchema = '';
99 /** @var integer */
100 protected $mFlags;
101 /** @var array */
102 protected $mLBInfo = [];
103 /** @var bool|null */
104 protected $mDefaultBigSelects = null;
105 /** @var array|bool */
106 protected $mSchemaVars = false;
107 /** @var array */
108 protected $mSessionVars = [];
109 /** @var array|null */
110 protected $preparedArgs;
111 /** @var string|bool|null Stashed value of html_errors INI setting */
112 protected $htmlErrors;
113 /** @var string */
114 protected $delimiter = ';';
115
116 /**
117 * Either 1 if a transaction is active or 0 otherwise.
118 * The other Trx fields may not be meaningfull if this is 0.
119 *
120 * @var int
121 */
122 protected $mTrxLevel = 0;
123 /**
124 * Either a short hexidecimal string if a transaction is active or ""
125 *
126 * @var string
127 * @see DatabaseBase::mTrxLevel
128 */
129 protected $mTrxShortId = '';
130 /**
131 * The UNIX time that the transaction started. Callers can assume that if
132 * snapshot isolation is used, then the data is *at least* up to date to that
133 * point (possibly more up-to-date since the first SELECT defines the snapshot).
134 *
135 * @var float|null
136 * @see DatabaseBase::mTrxLevel
137 */
138 private $mTrxTimestamp = null;
139 /** @var float Lag estimate at the time of BEGIN */
140 private $mTrxReplicaLag = null;
141 /**
142 * Remembers the function name given for starting the most recent transaction via begin().
143 * Used to provide additional context for error reporting.
144 *
145 * @var string
146 * @see DatabaseBase::mTrxLevel
147 */
148 private $mTrxFname = null;
149 /**
150 * Record if possible write queries were done in the last transaction started
151 *
152 * @var bool
153 * @see DatabaseBase::mTrxLevel
154 */
155 private $mTrxDoneWrites = false;
156 /**
157 * Record if the current transaction was started implicitly due to DBO_TRX being set.
158 *
159 * @var bool
160 * @see DatabaseBase::mTrxLevel
161 */
162 private $mTrxAutomatic = false;
163 /**
164 * Array of levels of atomicity within transactions
165 *
166 * @var array
167 */
168 private $mTrxAtomicLevels = [];
169 /**
170 * Record if the current transaction was started implicitly by DatabaseBase::startAtomic
171 *
172 * @var bool
173 */
174 private $mTrxAutomaticAtomic = false;
175 /**
176 * Track the write query callers of the current transaction
177 *
178 * @var string[]
179 */
180 private $mTrxWriteCallers = [];
181 /**
182 * @var float Seconds spent in write queries for the current transaction
183 */
184 private $mTrxWriteDuration = 0.0;
185 /**
186 * @var integer Number of write queries for the current transaction
187 */
188 private $mTrxWriteQueryCount = 0;
189 /**
190 * @var float Like mTrxWriteQueryCount but excludes lock-bound, easy to replicate, queries
191 */
192 private $mTrxWriteAdjDuration = 0.0;
193 /**
194 * @var integer Number of write queries counted in mTrxWriteAdjDuration
195 */
196 private $mTrxWriteAdjQueryCount = 0;
197 /**
198 * @var float RTT time estimate
199 */
200 private $mRTTEstimate = 0.0;
201
202 /** @var array Map of (name => 1) for locks obtained via lock() */
203 private $mNamedLocksHeld = [];
204
205 /** @var IDatabase|null Lazy handle to the master DB this server replicates from */
206 private $lazyMasterHandle;
207
208 /**
209 * @since 1.21
210 * @var resource File handle for upgrade
211 */
212 protected $fileHandle = null;
213
214 /**
215 * @since 1.22
216 * @var string[] Process cache of VIEWs names in the database
217 */
218 protected $allViews = null;
219
220 /** @var float UNIX timestamp */
221 protected $lastPing = 0.0;
222
223 /** @var int[] Prior mFlags values */
224 private $priorFlags = [];
225
226 /** @var object|string Class name or object With profileIn/profileOut methods */
227 protected $profiler;
228 /** @var TransactionProfiler */
229 protected $trxProfiler;
230
231 /**
232 * Constructor.
233 *
234 * FIXME: It is possible to construct a Database object with no associated
235 * connection object, by specifying no parameters to __construct(). This
236 * feature is deprecated and should be removed.
237 *
238 * IDatabase classes should not be constructed directly in external
239 * code. DatabaseBase::factory() should be used instead.
240 *
241 * @param array $params Parameters passed from DatabaseBase::factory()
242 */
243 function __construct( array $params ) {
244 $server = $params['host'];
245 $user = $params['user'];
246 $password = $params['password'];
247 $dbName = $params['dbname'];
248 $flags = $params['flags'];
249
250 $this->mSchema = $params['schema'];
251 $this->mTablePrefix = $params['tablePrefix'];
252
253 $this->cliMode = isset( $params['cliMode'] )
254 ? $params['cliMode']
255 : ( PHP_SAPI === 'cli' );
256 $this->agent = isset( $params['agent'] )
257 ? str_replace( '/', '-', $params['agent'] ) // escape for comment
258 : '';
259
260 $this->mFlags = $flags;
261 if ( $this->mFlags & DBO_DEFAULT ) {
262 if ( $this->cliMode ) {
263 $this->mFlags &= ~DBO_TRX;
264 } else {
265 $this->mFlags |= DBO_TRX;
266 }
267 }
268
269 $this->mSessionVars = $params['variables'];
270
271 $this->srvCache = isset( $params['srvCache'] )
272 ? $params['srvCache']
273 : new HashBagOStuff();
274
275 $this->profiler = isset( $params['profiler'] ) ? $params['profiler'] : null;
276 $this->trxProfiler = isset( $params['trxProfiler'] )
277 ? $params['trxProfiler']
278 : new TransactionProfiler();
279 $this->connLogger = isset( $params['connLogger'] )
280 ? $params['connLogger']
281 : new \Psr\Log\NullLogger();
282 $this->queryLogger = isset( $params['queryLogger'] )
283 ? $params['queryLogger']
284 : new \Psr\Log\NullLogger();
285
286 if ( $user ) {
287 $this->open( $server, $user, $password, $dbName );
288 }
289 }
290
291 /**
292 * Given a DB type, construct the name of the appropriate child class of
293 * IDatabase. This is designed to replace all of the manual stuff like:
294 * $class = 'Database' . ucfirst( strtolower( $dbType ) );
295 * as well as validate against the canonical list of DB types we have
296 *
297 * This factory function is mostly useful for when you need to connect to a
298 * database other than the MediaWiki default (such as for external auth,
299 * an extension, et cetera). Do not use this to connect to the MediaWiki
300 * database. Example uses in core:
301 * @see LoadBalancer::reallyOpenConnection()
302 * @see ForeignDBRepo::getMasterDB()
303 * @see WebInstallerDBConnect::execute()
304 *
305 * @since 1.18
306 *
307 * @param string $dbType A possible DB type
308 * @param array $p An array of options to pass to the constructor.
309 * Valid options are: host, user, password, dbname, flags, tablePrefix, schema, driver
310 * @return IDatabase|null If the database driver or extension cannot be found
311 * @throws InvalidArgumentException If the database driver or extension cannot be found
312 */
313 final public static function factory( $dbType, $p = [] ) {
314 $canonicalDBTypes = [
315 'mysql' => [ 'mysqli', 'mysql' ],
316 'postgres' => [],
317 'sqlite' => [],
318 'oracle' => [],
319 'mssql' => [],
320 ];
321
322 $driver = false;
323 $dbType = strtolower( $dbType );
324 if ( isset( $canonicalDBTypes[$dbType] ) && $canonicalDBTypes[$dbType] ) {
325 $possibleDrivers = $canonicalDBTypes[$dbType];
326 if ( !empty( $p['driver'] ) ) {
327 if ( in_array( $p['driver'], $possibleDrivers ) ) {
328 $driver = $p['driver'];
329 } else {
330 throw new InvalidArgumentException( __METHOD__ .
331 " type '$dbType' does not support driver '{$p['driver']}'" );
332 }
333 } else {
334 foreach ( $possibleDrivers as $posDriver ) {
335 if ( extension_loaded( $posDriver ) ) {
336 $driver = $posDriver;
337 break;
338 }
339 }
340 }
341 } else {
342 $driver = $dbType;
343 }
344 if ( $driver === false ) {
345 throw new InvalidArgumentException( __METHOD__ .
346 " no viable database extension found for type '$dbType'" );
347 }
348
349 // Determine schema defaults. Currently Microsoft SQL Server uses $wgDBmwschema,
350 // and everything else doesn't use a schema (e.g. null)
351 // Although postgres and oracle support schemas, we don't use them (yet)
352 // to maintain backwards compatibility
353 $defaultSchemas = [
354 'mssql' => 'get from global',
355 ];
356
357 $class = 'Database' . ucfirst( $driver );
358 if ( class_exists( $class ) && is_subclass_of( $class, 'IDatabase' ) ) {
359 // Resolve some defaults for b/c
360 $p['host'] = isset( $p['host'] ) ? $p['host'] : false;
361 $p['user'] = isset( $p['user'] ) ? $p['user'] : false;
362 $p['password'] = isset( $p['password'] ) ? $p['password'] : false;
363 $p['dbname'] = isset( $p['dbname'] ) ? $p['dbname'] : false;
364 $p['flags'] = isset( $p['flags'] ) ? $p['flags'] : 0;
365 $p['variables'] = isset( $p['variables'] ) ? $p['variables'] : [];
366 $p['tablePrefix'] = isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : '';
367 if ( !isset( $p['schema'] ) ) {
368 $p['schema'] = isset( $defaultSchemas[$dbType] ) ? $defaultSchemas[$dbType] : '';
369 }
370 $p['foreign'] = isset( $p['foreign'] ) ? $p['foreign'] : false;
371
372 $conn = new $class( $p );
373 if ( isset( $p['connLogger'] ) ) {
374 $conn->connLogger = $p['connLogger'];
375 }
376 if ( isset( $p['queryLogger'] ) ) {
377 $conn->queryLogger = $p['queryLogger'];
378 }
379 if ( isset( $p['errorLogger'] ) ) {
380 $conn->errorLogger = $p['errorLogger'];
381 } else {
382 $conn->errorLogger = function ( Exception $e ) {
383 trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_WARNING );
384 };
385 }
386 } else {
387 $conn = null;
388 }
389
390 return $conn;
391 }
392
393 public function setLogger( LoggerInterface $logger ) {
394 $this->queryLogger = $logger;
395 }
396
397 public function getServerInfo() {
398 return $this->getServerVersion();
399 }
400
401 public function bufferResults( $buffer = null ) {
402 $res = !$this->getFlag( DBO_NOBUFFER );
403 if ( $buffer !== null ) {
404 $buffer ? $this->clearFlag( DBO_NOBUFFER ) : $this->setFlag( DBO_NOBUFFER );
405 }
406
407 return $res;
408 }
409
410 /**
411 * Turns on (false) or off (true) the automatic generation and sending
412 * of a "we're sorry, but there has been a database error" page on
413 * database errors. Default is on (false). When turned off, the
414 * code should use lastErrno() and lastError() to handle the
415 * situation as appropriate.
416 *
417 * Do not use this function outside of the Database classes.
418 *
419 * @param null|bool $ignoreErrors
420 * @return bool The previous value of the flag.
421 */
422 protected function ignoreErrors( $ignoreErrors = null ) {
423 $res = $this->getFlag( DBO_IGNORE );
424 if ( $ignoreErrors !== null ) {
425 $ignoreErrors ? $this->setFlag( DBO_IGNORE ) : $this->clearFlag( DBO_IGNORE );
426 }
427
428 return $res;
429 }
430
431 public function trxLevel() {
432 return $this->mTrxLevel;
433 }
434
435 public function trxTimestamp() {
436 return $this->mTrxLevel ? $this->mTrxTimestamp : null;
437 }
438
439 public function tablePrefix( $prefix = null ) {
440 $old = $this->mTablePrefix;
441 if ( $prefix !== null ) {
442 $this->mTablePrefix = $prefix;
443 }
444
445 return $old;
446 }
447
448 public function dbSchema( $schema = null ) {
449 $old = $this->mSchema;
450 if ( $schema !== null ) {
451 $this->mSchema = $schema;
452 }
453
454 return $old;
455 }
456
457 /**
458 * Set the filehandle to copy write statements to.
459 *
460 * @param resource $fh File handle
461 */
462 public function setFileHandle( $fh ) {
463 $this->fileHandle = $fh;
464 }
465
466 public function getLBInfo( $name = null ) {
467 if ( is_null( $name ) ) {
468 return $this->mLBInfo;
469 } else {
470 if ( array_key_exists( $name, $this->mLBInfo ) ) {
471 return $this->mLBInfo[$name];
472 } else {
473 return null;
474 }
475 }
476 }
477
478 public function setLBInfo( $name, $value = null ) {
479 if ( is_null( $value ) ) {
480 $this->mLBInfo = $name;
481 } else {
482 $this->mLBInfo[$name] = $value;
483 }
484 }
485
486 public function setLazyMasterHandle( IDatabase $conn ) {
487 $this->lazyMasterHandle = $conn;
488 }
489
490 /**
491 * @return IDatabase|null
492 * @see setLazyMasterHandle()
493 * @since 1.27
494 */
495 public function getLazyMasterHandle() {
496 return $this->lazyMasterHandle;
497 }
498
499 public function implicitGroupby() {
500 return true;
501 }
502
503 public function implicitOrderby() {
504 return true;
505 }
506
507 public function lastQuery() {
508 return $this->mLastQuery;
509 }
510
511 public function doneWrites() {
512 return (bool)$this->mDoneWrites;
513 }
514
515 public function lastDoneWrites() {
516 return $this->mDoneWrites ?: false;
517 }
518
519 public function writesPending() {
520 return $this->mTrxLevel && $this->mTrxDoneWrites;
521 }
522
523 public function writesOrCallbacksPending() {
524 return $this->mTrxLevel && (
525 $this->mTrxDoneWrites || $this->mTrxIdleCallbacks || $this->mTrxPreCommitCallbacks
526 );
527 }
528
529 public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
530 if ( !$this->mTrxLevel ) {
531 return false;
532 } elseif ( !$this->mTrxDoneWrites ) {
533 return 0.0;
534 }
535
536 switch ( $type ) {
537 case self::ESTIMATE_DB_APPLY:
538 $this->ping( $rtt );
539 $rttAdjTotal = $this->mTrxWriteAdjQueryCount * $rtt;
540 $applyTime = max( $this->mTrxWriteAdjDuration - $rttAdjTotal, 0 );
541 // For omitted queries, make them count as something at least
542 $omitted = $this->mTrxWriteQueryCount - $this->mTrxWriteAdjQueryCount;
543 $applyTime += self::TINY_WRITE_SEC * $omitted;
544
545 return $applyTime;
546 default: // everything
547 return $this->mTrxWriteDuration;
548 }
549 }
550
551 public function pendingWriteCallers() {
552 return $this->mTrxLevel ? $this->mTrxWriteCallers : [];
553 }
554
555 protected function pendingWriteAndCallbackCallers() {
556 if ( !$this->mTrxLevel ) {
557 return [];
558 }
559
560 $fnames = $this->mTrxWriteCallers;
561 foreach ( [
562 $this->mTrxIdleCallbacks,
563 $this->mTrxPreCommitCallbacks,
564 $this->mTrxEndCallbacks
565 ] as $callbacks ) {
566 foreach ( $callbacks as $callback ) {
567 $fnames[] = $callback[1];
568 }
569 }
570
571 return $fnames;
572 }
573
574 public function isOpen() {
575 return $this->mOpened;
576 }
577
578 public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
579 if ( $remember === self::REMEMBER_PRIOR ) {
580 array_push( $this->priorFlags, $this->mFlags );
581 }
582 $this->mFlags |= $flag;
583 }
584
585 public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
586 if ( $remember === self::REMEMBER_PRIOR ) {
587 array_push( $this->priorFlags, $this->mFlags );
588 }
589 $this->mFlags &= ~$flag;
590 }
591
592 public function restoreFlags( $state = self::RESTORE_PRIOR ) {
593 if ( !$this->priorFlags ) {
594 return;
595 }
596
597 if ( $state === self::RESTORE_INITIAL ) {
598 $this->mFlags = reset( $this->priorFlags );
599 $this->priorFlags = [];
600 } else {
601 $this->mFlags = array_pop( $this->priorFlags );
602 }
603 }
604
605 public function getFlag( $flag ) {
606 return !!( $this->mFlags & $flag );
607 }
608
609 public function getProperty( $name ) {
610 return $this->$name;
611 }
612
613 public function getDomainID() {
614 if ( $this->mTablePrefix != '' ) {
615 return "{$this->mDBname}-{$this->mTablePrefix}";
616 } else {
617 return $this->mDBname;
618 }
619 }
620
621 final public function getWikiID() {
622 return $this->getDomainID();
623 }
624
625 /**
626 * Get information about an index into an object
627 * @param string $table Table name
628 * @param string $index Index name
629 * @param string $fname Calling function name
630 * @return mixed Database-specific index description class or false if the index does not exist
631 */
632 abstract function indexInfo( $table, $index, $fname = __METHOD__ );
633
634 /**
635 * Wrapper for addslashes()
636 *
637 * @param string $s String to be slashed.
638 * @return string Slashed string.
639 */
640 abstract function strencode( $s );
641
642 protected function installErrorHandler() {
643 $this->mPHPError = false;
644 $this->htmlErrors = ini_set( 'html_errors', '0' );
645 set_error_handler( [ $this, 'connectionerrorLogger' ] );
646 }
647
648 /**
649 * @return bool|string
650 */
651 protected function restoreErrorHandler() {
652 restore_error_handler();
653 if ( $this->htmlErrors !== false ) {
654 ini_set( 'html_errors', $this->htmlErrors );
655 }
656 if ( $this->mPHPError ) {
657 $error = preg_replace( '!\[<a.*</a>\]!', '', $this->mPHPError );
658 $error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error );
659
660 return $error;
661 } else {
662 return false;
663 }
664 }
665
666 /**
667 * @param int $errno
668 * @param string $errstr
669 */
670 public function connectionerrorLogger( $errno, $errstr ) {
671 $this->mPHPError = $errstr;
672 }
673
674 /**
675 * Create a log context to pass to PSR logging functions.
676 *
677 * @param array $extras Additional data to add to context
678 * @return array
679 */
680 protected function getLogContext( array $extras = [] ) {
681 return array_merge(
682 [
683 'db_server' => $this->mServer,
684 'db_name' => $this->mDBname,
685 'db_user' => $this->mUser,
686 ],
687 $extras
688 );
689 }
690
691 public function close() {
692 if ( $this->mConn ) {
693 if ( $this->trxLevel() ) {
694 $this->commit( __METHOD__, self::FLUSHING_INTERNAL );
695 }
696
697 $closed = $this->closeConnection();
698 $this->mConn = false;
699 } elseif ( $this->mTrxIdleCallbacks || $this->mTrxEndCallbacks ) { // sanity
700 throw new RuntimeException( "Transaction callbacks still pending." );
701 } else {
702 $closed = true;
703 }
704 $this->mOpened = false;
705
706 return $closed;
707 }
708
709 /**
710 * Make sure isOpen() returns true as a sanity check
711 *
712 * @throws DBUnexpectedError
713 */
714 protected function assertOpen() {
715 if ( !$this->isOpen() ) {
716 throw new DBUnexpectedError( $this, "DB connection was already closed." );
717 }
718 }
719
720 /**
721 * Closes underlying database connection
722 * @since 1.20
723 * @return bool Whether connection was closed successfully
724 */
725 abstract protected function closeConnection();
726
727 function reportConnectionError( $error = 'Unknown error' ) {
728 $myError = $this->lastError();
729 if ( $myError ) {
730 $error = $myError;
731 }
732
733 # New method
734 throw new DBConnectionError( $this, $error );
735 }
736
737 /**
738 * The DBMS-dependent part of query()
739 *
740 * @param string $sql SQL query.
741 * @return ResultWrapper|bool Result object to feed to fetchObject,
742 * fetchRow, ...; or false on failure
743 */
744 abstract protected function doQuery( $sql );
745
746 /**
747 * Determine whether a query writes to the DB.
748 * Should return true if unsure.
749 *
750 * @param string $sql
751 * @return bool
752 */
753 protected function isWriteQuery( $sql ) {
754 return !preg_match(
755 '/^(?:SELECT|BEGIN|ROLLBACK|COMMIT|SET|SHOW|EXPLAIN|\(SELECT)\b/i', $sql );
756 }
757
758 /**
759 * @param $sql
760 * @return string|null
761 */
762 protected function getQueryVerb( $sql ) {
763 return preg_match( '/^\s*([a-z]+)/i', $sql, $m ) ? strtoupper( $m[1] ) : null;
764 }
765
766 /**
767 * Determine whether a SQL statement is sensitive to isolation level.
768 * A SQL statement is considered transactable if its result could vary
769 * depending on the transaction isolation level. Operational commands
770 * such as 'SET' and 'SHOW' are not considered to be transactable.
771 *
772 * @param string $sql
773 * @return bool
774 */
775 protected function isTransactableQuery( $sql ) {
776 $verb = $this->getQueryVerb( $sql );
777 return !in_array( $verb, [ 'BEGIN', 'COMMIT', 'ROLLBACK', 'SHOW', 'SET' ], true );
778 }
779
780 public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) {
781 $priorWritesPending = $this->writesOrCallbacksPending();
782 $this->mLastQuery = $sql;
783
784 $isWrite = $this->isWriteQuery( $sql );
785 if ( $isWrite ) {
786 $reason = $this->getReadOnlyReason();
787 if ( $reason !== false ) {
788 throw new DBReadOnlyError( $this, "Database is read-only: $reason" );
789 }
790 # Set a flag indicating that writes have been done
791 $this->mDoneWrites = microtime( true );
792 }
793
794 // Add trace comment to the begin of the sql string, right after the operator.
795 // Or, for one-word queries (like "BEGIN" or COMMIT") add it to the end (bug 42598)
796 $commentedSql = preg_replace( '/\s|$/', " /* $fname {$this->agent} */ ", $sql, 1 );
797
798 # Start implicit transactions that wrap the request if DBO_TRX is enabled
799 if ( !$this->mTrxLevel && $this->getFlag( DBO_TRX )
800 && $this->isTransactableQuery( $sql )
801 ) {
802 $this->begin( __METHOD__ . " ($fname)", self::TRANSACTION_INTERNAL );
803 $this->mTrxAutomatic = true;
804 }
805
806 # Keep track of whether the transaction has write queries pending
807 if ( $this->mTrxLevel && !$this->mTrxDoneWrites && $isWrite ) {
808 $this->mTrxDoneWrites = true;
809 $this->trxProfiler->transactionWritingIn(
810 $this->mServer, $this->mDBname, $this->mTrxShortId );
811 }
812
813 if ( $this->getFlag( DBO_DEBUG ) ) {
814 $this->queryLogger->debug( "{$this->mDBname} {$commentedSql}" );
815 }
816
817 # Avoid fatals if close() was called
818 $this->assertOpen();
819
820 # Send the query to the server
821 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
822
823 # Try reconnecting if the connection was lost
824 if ( false === $ret && $this->wasErrorReissuable() ) {
825 $recoverable = $this->canRecoverFromDisconnect( $sql, $priorWritesPending );
826 # Stash the last error values before anything might clear them
827 $lastError = $this->lastError();
828 $lastErrno = $this->lastErrno();
829 # Update state tracking to reflect transaction loss due to disconnection
830 $this->handleTransactionLoss();
831 if ( $this->reconnect() ) {
832 $msg = __METHOD__ . ": lost connection to {$this->getServer()}; reconnected";
833 $this->connLogger->warning( $msg );
834 $this->queryLogger->warning(
835 "$msg:\n" . ( new RuntimeException() )->getTraceAsString() );
836
837 if ( !$recoverable ) {
838 # Callers may catch the exception and continue to use the DB
839 $this->reportQueryError( $lastError, $lastErrno, $sql, $fname );
840 } else {
841 # Should be safe to silently retry the query
842 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
843 }
844 } else {
845 $msg = __METHOD__ . ": lost connection to {$this->getServer()} permanently";
846 $this->connLogger->error( $msg );
847 }
848 }
849
850 if ( false === $ret ) {
851 # Deadlocks cause the entire transaction to abort, not just the statement.
852 # http://dev.mysql.com/doc/refman/5.7/en/innodb-error-handling.html
853 # https://www.postgresql.org/docs/9.1/static/explicit-locking.html
854 if ( $this->wasDeadlock() ) {
855 if ( $this->explicitTrxActive() || $priorWritesPending ) {
856 $tempIgnore = false; // not recoverable
857 }
858 # Update state tracking to reflect transaction loss
859 $this->handleTransactionLoss();
860 }
861
862 $this->reportQueryError(
863 $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore );
864 }
865
866 $res = $this->resultObject( $ret );
867
868 return $res;
869 }
870
871 private function doProfiledQuery( $sql, $commentedSql, $isWrite, $fname ) {
872 $isMaster = !is_null( $this->getLBInfo( 'master' ) );
873 # generalizeSQL() will probably cut down the query to reasonable
874 # logging size most of the time. The substr is really just a sanity check.
875 if ( $isMaster ) {
876 $queryProf = 'query-m: ' . substr( self::generalizeSQL( $sql ), 0, 255 );
877 } else {
878 $queryProf = 'query: ' . substr( self::generalizeSQL( $sql ), 0, 255 );
879 }
880
881 # Include query transaction state
882 $queryProf .= $this->mTrxShortId ? " [TRX#{$this->mTrxShortId}]" : "";
883
884 $startTime = microtime( true );
885 if ( $this->profiler ) {
886 call_user_func( [ $this->profiler, 'profileIn' ], $queryProf );
887 }
888 $ret = $this->doQuery( $commentedSql );
889 if ( $this->profiler ) {
890 call_user_func( [ $this->profiler, 'profileOut' ], $queryProf );
891 }
892 $queryRuntime = max( microtime( true ) - $startTime, 0.0 );
893
894 unset( $queryProfSection ); // profile out (if set)
895
896 if ( $ret !== false ) {
897 $this->lastPing = $startTime;
898 if ( $isWrite && $this->mTrxLevel ) {
899 $this->updateTrxWriteQueryTime( $sql, $queryRuntime );
900 $this->mTrxWriteCallers[] = $fname;
901 }
902 }
903
904 if ( $sql === self::PING_QUERY ) {
905 $this->mRTTEstimate = $queryRuntime;
906 }
907
908 $this->trxProfiler->recordQueryCompletion(
909 $queryProf, $startTime, $isWrite, $this->affectedRows()
910 );
911 MWDebug::query( $sql, $fname, $isMaster, $queryRuntime );
912
913 return $ret;
914 }
915
916 /**
917 * Update the estimated run-time of a query, not counting large row lock times
918 *
919 * LoadBalancer can be set to rollback transactions that will create huge replication
920 * lag. It bases this estimate off of pendingWriteQueryDuration(). Certain simple
921 * queries, like inserting a row can take a long time due to row locking. This method
922 * uses some simple heuristics to discount those cases.
923 *
924 * @param string $sql A SQL write query
925 * @param float $runtime Total runtime, including RTT
926 */
927 private function updateTrxWriteQueryTime( $sql, $runtime ) {
928 // Whether this is indicative of replica DB runtime (except for RBR or ws_repl)
929 $indicativeOfReplicaRuntime = true;
930 if ( $runtime > self::SLOW_WRITE_SEC ) {
931 $verb = $this->getQueryVerb( $sql );
932 // insert(), upsert(), replace() are fast unless bulky in size or blocked on locks
933 if ( $verb === 'INSERT' ) {
934 $indicativeOfReplicaRuntime = $this->affectedRows() > self::SMALL_WRITE_ROWS;
935 } elseif ( $verb === 'REPLACE' ) {
936 $indicativeOfReplicaRuntime = $this->affectedRows() > self::SMALL_WRITE_ROWS / 2;
937 }
938 }
939
940 $this->mTrxWriteDuration += $runtime;
941 $this->mTrxWriteQueryCount += 1;
942 if ( $indicativeOfReplicaRuntime ) {
943 $this->mTrxWriteAdjDuration += $runtime;
944 $this->mTrxWriteAdjQueryCount += 1;
945 }
946 }
947
948 private function canRecoverFromDisconnect( $sql, $priorWritesPending ) {
949 # Transaction dropped; this can mean lost writes, or REPEATABLE-READ snapshots.
950 # Dropped connections also mean that named locks are automatically released.
951 # Only allow error suppression in autocommit mode or when the lost transaction
952 # didn't matter anyway (aside from DBO_TRX snapshot loss).
953 if ( $this->mNamedLocksHeld ) {
954 return false; // possible critical section violation
955 } elseif ( $sql === 'COMMIT' ) {
956 return !$priorWritesPending; // nothing written anyway? (T127428)
957 } elseif ( $sql === 'ROLLBACK' ) {
958 return true; // transaction lost...which is also what was requested :)
959 } elseif ( $this->explicitTrxActive() ) {
960 return false; // don't drop atomocity
961 } elseif ( $priorWritesPending ) {
962 return false; // prior writes lost from implicit transaction
963 }
964
965 return true;
966 }
967
968 private function handleTransactionLoss() {
969 $this->mTrxLevel = 0;
970 $this->mTrxIdleCallbacks = []; // bug 65263
971 $this->mTrxPreCommitCallbacks = []; // bug 65263
972 try {
973 // Handle callbacks in mTrxEndCallbacks
974 $this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
975 $this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK );
976 return null;
977 } catch ( Exception $e ) {
978 // Already logged; move on...
979 return $e;
980 }
981 }
982
983 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
984 if ( $this->ignoreErrors() || $tempIgnore ) {
985 $this->queryLogger->debug( "SQL ERROR (ignored): $error\n" );
986 } else {
987 $sql1line = mb_substr( str_replace( "\n", "\\n", $sql ), 0, 5 * 1024 );
988 $this->queryLogger->error(
989 "{fname}\t{db_server}\t{errno}\t{error}\t{sql1line}",
990 $this->getLogContext( [
991 'method' => __METHOD__,
992 'errno' => $errno,
993 'error' => $error,
994 'sql1line' => $sql1line,
995 'fname' => $fname,
996 ] )
997 );
998 $this->queryLogger->debug( "SQL ERROR: " . $error . "\n" );
999 throw new DBQueryError( $this, $error, $errno, $sql, $fname );
1000 }
1001 }
1002
1003 public function freeResult( $res ) {
1004 }
1005
1006 public function selectField(
1007 $table, $var, $cond = '', $fname = __METHOD__, $options = []
1008 ) {
1009 if ( $var === '*' ) { // sanity
1010 throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
1011 }
1012
1013 if ( !is_array( $options ) ) {
1014 $options = [ $options ];
1015 }
1016
1017 $options['LIMIT'] = 1;
1018
1019 $res = $this->select( $table, $var, $cond, $fname, $options );
1020 if ( $res === false || !$this->numRows( $res ) ) {
1021 return false;
1022 }
1023
1024 $row = $this->fetchRow( $res );
1025
1026 if ( $row !== false ) {
1027 return reset( $row );
1028 } else {
1029 return false;
1030 }
1031 }
1032
1033 public function selectFieldValues(
1034 $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
1035 ) {
1036 if ( $var === '*' ) { // sanity
1037 throw new DBUnexpectedError( $this, "Cannot use a * field" );
1038 } elseif ( !is_string( $var ) ) { // sanity
1039 throw new DBUnexpectedError( $this, "Cannot use an array of fields" );
1040 }
1041
1042 if ( !is_array( $options ) ) {
1043 $options = [ $options ];
1044 }
1045
1046 $res = $this->select( $table, $var, $cond, $fname, $options, $join_conds );
1047 if ( $res === false ) {
1048 return false;
1049 }
1050
1051 $values = [];
1052 foreach ( $res as $row ) {
1053 $values[] = $row->$var;
1054 }
1055
1056 return $values;
1057 }
1058
1059 /**
1060 * Returns an optional USE INDEX clause to go after the table, and a
1061 * string to go at the end of the query.
1062 *
1063 * @param array $options Associative array of options to be turned into
1064 * an SQL query, valid keys are listed in the function.
1065 * @return array
1066 * @see DatabaseBase::select()
1067 */
1068 public function makeSelectOptions( $options ) {
1069 $preLimitTail = $postLimitTail = '';
1070 $startOpts = '';
1071
1072 $noKeyOptions = [];
1073
1074 foreach ( $options as $key => $option ) {
1075 if ( is_numeric( $key ) ) {
1076 $noKeyOptions[$option] = true;
1077 }
1078 }
1079
1080 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1081
1082 $preLimitTail .= $this->makeOrderBy( $options );
1083
1084 // if (isset($options['LIMIT'])) {
1085 // $tailOpts .= $this->limitResult('', $options['LIMIT'],
1086 // isset($options['OFFSET']) ? $options['OFFSET']
1087 // : false);
1088 // }
1089
1090 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1091 $postLimitTail .= ' FOR UPDATE';
1092 }
1093
1094 if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) {
1095 $postLimitTail .= ' LOCK IN SHARE MODE';
1096 }
1097
1098 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1099 $startOpts .= 'DISTINCT';
1100 }
1101
1102 # Various MySQL extensions
1103 if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) {
1104 $startOpts .= ' /*! STRAIGHT_JOIN */';
1105 }
1106
1107 if ( isset( $noKeyOptions['HIGH_PRIORITY'] ) ) {
1108 $startOpts .= ' HIGH_PRIORITY';
1109 }
1110
1111 if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) {
1112 $startOpts .= ' SQL_BIG_RESULT';
1113 }
1114
1115 if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) {
1116 $startOpts .= ' SQL_BUFFER_RESULT';
1117 }
1118
1119 if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) {
1120 $startOpts .= ' SQL_SMALL_RESULT';
1121 }
1122
1123 if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) {
1124 $startOpts .= ' SQL_CALC_FOUND_ROWS';
1125 }
1126
1127 if ( isset( $noKeyOptions['SQL_CACHE'] ) ) {
1128 $startOpts .= ' SQL_CACHE';
1129 }
1130
1131 if ( isset( $noKeyOptions['SQL_NO_CACHE'] ) ) {
1132 $startOpts .= ' SQL_NO_CACHE';
1133 }
1134
1135 if ( isset( $options['USE INDEX'] ) && is_string( $options['USE INDEX'] ) ) {
1136 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1137 } else {
1138 $useIndex = '';
1139 }
1140 if ( isset( $options['IGNORE INDEX'] ) && is_string( $options['IGNORE INDEX'] ) ) {
1141 $ignoreIndex = $this->ignoreIndexClause( $options['IGNORE INDEX'] );
1142 } else {
1143 $ignoreIndex = '';
1144 }
1145
1146 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1147 }
1148
1149 /**
1150 * Returns an optional GROUP BY with an optional HAVING
1151 *
1152 * @param array $options Associative array of options
1153 * @return string
1154 * @see DatabaseBase::select()
1155 * @since 1.21
1156 */
1157 public function makeGroupByWithHaving( $options ) {
1158 $sql = '';
1159 if ( isset( $options['GROUP BY'] ) ) {
1160 $gb = is_array( $options['GROUP BY'] )
1161 ? implode( ',', $options['GROUP BY'] )
1162 : $options['GROUP BY'];
1163 $sql .= ' GROUP BY ' . $gb;
1164 }
1165 if ( isset( $options['HAVING'] ) ) {
1166 $having = is_array( $options['HAVING'] )
1167 ? $this->makeList( $options['HAVING'], LIST_AND )
1168 : $options['HAVING'];
1169 $sql .= ' HAVING ' . $having;
1170 }
1171
1172 return $sql;
1173 }
1174
1175 /**
1176 * Returns an optional ORDER BY
1177 *
1178 * @param array $options Associative array of options
1179 * @return string
1180 * @see DatabaseBase::select()
1181 * @since 1.21
1182 */
1183 public function makeOrderBy( $options ) {
1184 if ( isset( $options['ORDER BY'] ) ) {
1185 $ob = is_array( $options['ORDER BY'] )
1186 ? implode( ',', $options['ORDER BY'] )
1187 : $options['ORDER BY'];
1188
1189 return ' ORDER BY ' . $ob;
1190 }
1191
1192 return '';
1193 }
1194
1195 // See IDatabase::select for the docs for this function
1196 public function select( $table, $vars, $conds = '', $fname = __METHOD__,
1197 $options = [], $join_conds = [] ) {
1198 $sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
1199
1200 return $this->query( $sql, $fname );
1201 }
1202
1203 public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__,
1204 $options = [], $join_conds = []
1205 ) {
1206 if ( is_array( $vars ) ) {
1207 $vars = implode( ',', $this->fieldNamesWithAlias( $vars ) );
1208 }
1209
1210 $options = (array)$options;
1211 $useIndexes = ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
1212 ? $options['USE INDEX']
1213 : [];
1214 $ignoreIndexes = ( isset( $options['IGNORE INDEX'] ) && is_array( $options['IGNORE INDEX'] ) )
1215 ? $options['IGNORE INDEX']
1216 : [];
1217
1218 if ( is_array( $table ) ) {
1219 $from = ' FROM ' .
1220 $this->tableNamesWithIndexClauseOrJOIN( $table, $useIndexes, $ignoreIndexes, $join_conds );
1221 } elseif ( $table != '' ) {
1222 if ( $table[0] == ' ' ) {
1223 $from = ' FROM ' . $table;
1224 } else {
1225 $from = ' FROM ' .
1226 $this->tableNamesWithIndexClauseOrJOIN( [ $table ], $useIndexes, $ignoreIndexes, [] );
1227 }
1228 } else {
1229 $from = '';
1230 }
1231
1232 list( $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ) =
1233 $this->makeSelectOptions( $options );
1234
1235 if ( !empty( $conds ) ) {
1236 if ( is_array( $conds ) ) {
1237 $conds = $this->makeList( $conds, LIST_AND );
1238 }
1239 $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex WHERE $conds $preLimitTail";
1240 } else {
1241 $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex $preLimitTail";
1242 }
1243
1244 if ( isset( $options['LIMIT'] ) ) {
1245 $sql = $this->limitResult( $sql, $options['LIMIT'],
1246 isset( $options['OFFSET'] ) ? $options['OFFSET'] : false );
1247 }
1248 $sql = "$sql $postLimitTail";
1249
1250 if ( isset( $options['EXPLAIN'] ) ) {
1251 $sql = 'EXPLAIN ' . $sql;
1252 }
1253
1254 return $sql;
1255 }
1256
1257 public function selectRow( $table, $vars, $conds, $fname = __METHOD__,
1258 $options = [], $join_conds = []
1259 ) {
1260 $options = (array)$options;
1261 $options['LIMIT'] = 1;
1262 $res = $this->select( $table, $vars, $conds, $fname, $options, $join_conds );
1263
1264 if ( $res === false ) {
1265 return false;
1266 }
1267
1268 if ( !$this->numRows( $res ) ) {
1269 return false;
1270 }
1271
1272 $obj = $this->fetchObject( $res );
1273
1274 return $obj;
1275 }
1276
1277 public function estimateRowCount(
1278 $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = []
1279 ) {
1280 $rows = 0;
1281 $res = $this->select( $table, [ 'rowcount' => 'COUNT(*)' ], $conds, $fname, $options );
1282
1283 if ( $res ) {
1284 $row = $this->fetchRow( $res );
1285 $rows = ( isset( $row['rowcount'] ) ) ? (int)$row['rowcount'] : 0;
1286 }
1287
1288 return $rows;
1289 }
1290
1291 public function selectRowCount(
1292 $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
1293 ) {
1294 $rows = 0;
1295 $sql = $this->selectSQLText( $tables, '1', $conds, $fname, $options, $join_conds );
1296 $res = $this->query( "SELECT COUNT(*) AS rowcount FROM ($sql) tmp_count", $fname );
1297
1298 if ( $res ) {
1299 $row = $this->fetchRow( $res );
1300 $rows = ( isset( $row['rowcount'] ) ) ? (int)$row['rowcount'] : 0;
1301 }
1302
1303 return $rows;
1304 }
1305
1306 /**
1307 * Removes most variables from an SQL query and replaces them with X or N for numbers.
1308 * It's only slightly flawed. Don't use for anything important.
1309 *
1310 * @param string $sql A SQL Query
1311 *
1312 * @return string
1313 */
1314 protected static function generalizeSQL( $sql ) {
1315 # This does the same as the regexp below would do, but in such a way
1316 # as to avoid crashing php on some large strings.
1317 # $sql = preg_replace( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql );
1318
1319 $sql = str_replace( "\\\\", '', $sql );
1320 $sql = str_replace( "\\'", '', $sql );
1321 $sql = str_replace( "\\\"", '', $sql );
1322 $sql = preg_replace( "/'.*'/s", "'X'", $sql );
1323 $sql = preg_replace( '/".*"/s', "'X'", $sql );
1324
1325 # All newlines, tabs, etc replaced by single space
1326 $sql = preg_replace( '/\s+/', ' ', $sql );
1327
1328 # All numbers => N,
1329 # except the ones surrounded by characters, e.g. l10n
1330 $sql = preg_replace( '/-?\d+(,-?\d+)+/s', 'N,...,N', $sql );
1331 $sql = preg_replace( '/(?<![a-zA-Z])-?\d+(?![a-zA-Z])/s', 'N', $sql );
1332
1333 return $sql;
1334 }
1335
1336 public function fieldExists( $table, $field, $fname = __METHOD__ ) {
1337 $info = $this->fieldInfo( $table, $field );
1338
1339 return (bool)$info;
1340 }
1341
1342 public function indexExists( $table, $index, $fname = __METHOD__ ) {
1343 if ( !$this->tableExists( $table ) ) {
1344 return null;
1345 }
1346
1347 $info = $this->indexInfo( $table, $index, $fname );
1348 if ( is_null( $info ) ) {
1349 return null;
1350 } else {
1351 return $info !== false;
1352 }
1353 }
1354
1355 public function tableExists( $table, $fname = __METHOD__ ) {
1356 $table = $this->tableName( $table );
1357 $old = $this->ignoreErrors( true );
1358 $res = $this->query( "SELECT 1 FROM $table LIMIT 1", $fname );
1359 $this->ignoreErrors( $old );
1360
1361 return (bool)$res;
1362 }
1363
1364 public function indexUnique( $table, $index ) {
1365 $indexInfo = $this->indexInfo( $table, $index );
1366
1367 if ( !$indexInfo ) {
1368 return null;
1369 }
1370
1371 return !$indexInfo[0]->Non_unique;
1372 }
1373
1374 /**
1375 * Helper for DatabaseBase::insert().
1376 *
1377 * @param array $options
1378 * @return string
1379 */
1380 protected function makeInsertOptions( $options ) {
1381 return implode( ' ', $options );
1382 }
1383
1384 public function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
1385 # No rows to insert, easy just return now
1386 if ( !count( $a ) ) {
1387 return true;
1388 }
1389
1390 $table = $this->tableName( $table );
1391
1392 if ( !is_array( $options ) ) {
1393 $options = [ $options ];
1394 }
1395
1396 $fh = null;
1397 if ( isset( $options['fileHandle'] ) ) {
1398 $fh = $options['fileHandle'];
1399 }
1400 $options = $this->makeInsertOptions( $options );
1401
1402 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
1403 $multi = true;
1404 $keys = array_keys( $a[0] );
1405 } else {
1406 $multi = false;
1407 $keys = array_keys( $a );
1408 }
1409
1410 $sql = 'INSERT ' . $options .
1411 " INTO $table (" . implode( ',', $keys ) . ') VALUES ';
1412
1413 if ( $multi ) {
1414 $first = true;
1415 foreach ( $a as $row ) {
1416 if ( $first ) {
1417 $first = false;
1418 } else {
1419 $sql .= ',';
1420 }
1421 $sql .= '(' . $this->makeList( $row ) . ')';
1422 }
1423 } else {
1424 $sql .= '(' . $this->makeList( $a ) . ')';
1425 }
1426
1427 if ( $fh !== null && false === fwrite( $fh, $sql ) ) {
1428 return false;
1429 } elseif ( $fh !== null ) {
1430 return true;
1431 }
1432
1433 return (bool)$this->query( $sql, $fname );
1434 }
1435
1436 /**
1437 * Make UPDATE options array for DatabaseBase::makeUpdateOptions
1438 *
1439 * @param array $options
1440 * @return array
1441 */
1442 protected function makeUpdateOptionsArray( $options ) {
1443 if ( !is_array( $options ) ) {
1444 $options = [ $options ];
1445 }
1446
1447 $opts = [];
1448
1449 if ( in_array( 'IGNORE', $options ) ) {
1450 $opts[] = 'IGNORE';
1451 }
1452
1453 return $opts;
1454 }
1455
1456 /**
1457 * Make UPDATE options for the DatabaseBase::update function
1458 *
1459 * @param array $options The options passed to DatabaseBase::update
1460 * @return string
1461 */
1462 protected function makeUpdateOptions( $options ) {
1463 $opts = $this->makeUpdateOptionsArray( $options );
1464
1465 return implode( ' ', $opts );
1466 }
1467
1468 function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
1469 $table = $this->tableName( $table );
1470 $opts = $this->makeUpdateOptions( $options );
1471 $sql = "UPDATE $opts $table SET " . $this->makeList( $values, LIST_SET );
1472
1473 if ( $conds !== [] && $conds !== '*' ) {
1474 $sql .= " WHERE " . $this->makeList( $conds, LIST_AND );
1475 }
1476
1477 return $this->query( $sql, $fname );
1478 }
1479
1480 public function makeList( $a, $mode = LIST_COMMA ) {
1481 if ( !is_array( $a ) ) {
1482 throw new DBUnexpectedError( $this, __METHOD__ . ' called with incorrect parameters' );
1483 }
1484
1485 $first = true;
1486 $list = '';
1487
1488 foreach ( $a as $field => $value ) {
1489 if ( !$first ) {
1490 if ( $mode == LIST_AND ) {
1491 $list .= ' AND ';
1492 } elseif ( $mode == LIST_OR ) {
1493 $list .= ' OR ';
1494 } else {
1495 $list .= ',';
1496 }
1497 } else {
1498 $first = false;
1499 }
1500
1501 if ( ( $mode == LIST_AND || $mode == LIST_OR ) && is_numeric( $field ) ) {
1502 $list .= "($value)";
1503 } elseif ( ( $mode == LIST_SET ) && is_numeric( $field ) ) {
1504 $list .= "$value";
1505 } elseif ( ( $mode == LIST_AND || $mode == LIST_OR ) && is_array( $value ) ) {
1506 // Remove null from array to be handled separately if found
1507 $includeNull = false;
1508 foreach ( array_keys( $value, null, true ) as $nullKey ) {
1509 $includeNull = true;
1510 unset( $value[$nullKey] );
1511 }
1512 if ( count( $value ) == 0 && !$includeNull ) {
1513 throw new InvalidArgumentException( __METHOD__ . ": empty input for field $field" );
1514 } elseif ( count( $value ) == 0 ) {
1515 // only check if $field is null
1516 $list .= "$field IS NULL";
1517 } else {
1518 // IN clause contains at least one valid element
1519 if ( $includeNull ) {
1520 // Group subconditions to ensure correct precedence
1521 $list .= '(';
1522 }
1523 if ( count( $value ) == 1 ) {
1524 // Special-case single values, as IN isn't terribly efficient
1525 // Don't necessarily assume the single key is 0; we don't
1526 // enforce linear numeric ordering on other arrays here.
1527 $value = array_values( $value )[0];
1528 $list .= $field . " = " . $this->addQuotes( $value );
1529 } else {
1530 $list .= $field . " IN (" . $this->makeList( $value ) . ") ";
1531 }
1532 // if null present in array, append IS NULL
1533 if ( $includeNull ) {
1534 $list .= " OR $field IS NULL)";
1535 }
1536 }
1537 } elseif ( $value === null ) {
1538 if ( $mode == LIST_AND || $mode == LIST_OR ) {
1539 $list .= "$field IS ";
1540 } elseif ( $mode == LIST_SET ) {
1541 $list .= "$field = ";
1542 }
1543 $list .= 'NULL';
1544 } else {
1545 if ( $mode == LIST_AND || $mode == LIST_OR || $mode == LIST_SET ) {
1546 $list .= "$field = ";
1547 }
1548 $list .= $mode == LIST_NAMES ? $value : $this->addQuotes( $value );
1549 }
1550 }
1551
1552 return $list;
1553 }
1554
1555 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
1556 $conds = [];
1557
1558 foreach ( $data as $base => $sub ) {
1559 if ( count( $sub ) ) {
1560 $conds[] = $this->makeList(
1561 [ $baseKey => $base, $subKey => array_keys( $sub ) ],
1562 LIST_AND );
1563 }
1564 }
1565
1566 if ( $conds ) {
1567 return $this->makeList( $conds, LIST_OR );
1568 } else {
1569 // Nothing to search for...
1570 return false;
1571 }
1572 }
1573
1574 /**
1575 * Return aggregated value alias
1576 *
1577 * @param array $valuedata
1578 * @param string $valuename
1579 *
1580 * @return string
1581 */
1582 public function aggregateValue( $valuedata, $valuename = 'value' ) {
1583 return $valuename;
1584 }
1585
1586 public function bitNot( $field ) {
1587 return "(~$field)";
1588 }
1589
1590 public function bitAnd( $fieldLeft, $fieldRight ) {
1591 return "($fieldLeft & $fieldRight)";
1592 }
1593
1594 public function bitOr( $fieldLeft, $fieldRight ) {
1595 return "($fieldLeft | $fieldRight)";
1596 }
1597
1598 public function buildConcat( $stringList ) {
1599 return 'CONCAT(' . implode( ',', $stringList ) . ')';
1600 }
1601
1602 public function buildGroupConcatField(
1603 $delim, $table, $field, $conds = '', $join_conds = []
1604 ) {
1605 $fld = "GROUP_CONCAT($field SEPARATOR " . $this->addQuotes( $delim ) . ')';
1606
1607 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1608 }
1609
1610 /**
1611 * @param string $field Field or column to cast
1612 * @return string
1613 * @since 1.28
1614 */
1615 public function buildStringCast( $field ) {
1616 return $field;
1617 }
1618
1619 public function selectDB( $db ) {
1620 # Stub. Shouldn't cause serious problems if it's not overridden, but
1621 # if your database engine supports a concept similar to MySQL's
1622 # databases you may as well.
1623 $this->mDBname = $db;
1624
1625 return true;
1626 }
1627
1628 public function getDBname() {
1629 return $this->mDBname;
1630 }
1631
1632 public function getServer() {
1633 return $this->mServer;
1634 }
1635
1636 /**
1637 * Format a table name ready for use in constructing an SQL query
1638 *
1639 * This does two important things: it quotes the table names to clean them up,
1640 * and it adds a table prefix if only given a table name with no quotes.
1641 *
1642 * All functions of this object which require a table name call this function
1643 * themselves. Pass the canonical name to such functions. This is only needed
1644 * when calling query() directly.
1645 *
1646 * @note This function does not sanitize user input. It is not safe to use
1647 * this function to escape user input.
1648 * @param string $name Database table name
1649 * @param string $format One of:
1650 * quoted - Automatically pass the table name through addIdentifierQuotes()
1651 * so that it can be used in a query.
1652 * raw - Do not add identifier quotes to the table name
1653 * @return string Full database name
1654 */
1655 public function tableName( $name, $format = 'quoted' ) {
1656 # Skip the entire process when we have a string quoted on both ends.
1657 # Note that we check the end so that we will still quote any use of
1658 # use of `database`.table. But won't break things if someone wants
1659 # to query a database table with a dot in the name.
1660 if ( $this->isQuotedIdentifier( $name ) ) {
1661 return $name;
1662 }
1663
1664 # Lets test for any bits of text that should never show up in a table
1665 # name. Basically anything like JOIN or ON which are actually part of
1666 # SQL queries, but may end up inside of the table value to combine
1667 # sql. Such as how the API is doing.
1668 # Note that we use a whitespace test rather than a \b test to avoid
1669 # any remote case where a word like on may be inside of a table name
1670 # surrounded by symbols which may be considered word breaks.
1671 if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
1672 return $name;
1673 }
1674
1675 # Split database and table into proper variables.
1676 # We reverse the explode so that database.table and table both output
1677 # the correct table.
1678 $dbDetails = explode( '.', $name, 3 );
1679 if ( count( $dbDetails ) == 3 ) {
1680 list( $database, $schema, $table ) = $dbDetails;
1681 # We don't want any prefix added in this case
1682 $prefix = '';
1683 } elseif ( count( $dbDetails ) == 2 ) {
1684 list( $database, $table ) = $dbDetails;
1685 # We don't want any prefix added in this case
1686 # In dbs that support it, $database may actually be the schema
1687 # but that doesn't affect any of the functionality here
1688 $prefix = '';
1689 $schema = '';
1690 } else {
1691 list( $table ) = $dbDetails;
1692 if ( isset( $this->tableAliases[$table] ) ) {
1693 $database = $this->tableAliases[$table]['dbname'];
1694 $schema = is_string( $this->tableAliases[$table]['schema'] )
1695 ? $this->tableAliases[$table]['schema']
1696 : $this->mSchema;
1697 $prefix = is_string( $this->tableAliases[$table]['prefix'] )
1698 ? $this->tableAliases[$table]['prefix']
1699 : $this->mTablePrefix;
1700 } else {
1701 $database = '';
1702 $schema = $this->mSchema; # Default schema
1703 $prefix = $this->mTablePrefix; # Default prefix
1704 }
1705 }
1706
1707 # Quote $table and apply the prefix if not quoted.
1708 # $tableName might be empty if this is called from Database::replaceVars()
1709 $tableName = "{$prefix}{$table}";
1710 if ( $format == 'quoted'
1711 && !$this->isQuotedIdentifier( $tableName ) && $tableName !== ''
1712 ) {
1713 $tableName = $this->addIdentifierQuotes( $tableName );
1714 }
1715
1716 # Quote $schema and merge it with the table name if needed
1717 if ( strlen( $schema ) ) {
1718 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $schema ) ) {
1719 $schema = $this->addIdentifierQuotes( $schema );
1720 }
1721 $tableName = $schema . '.' . $tableName;
1722 }
1723
1724 # Quote $database and merge it with the table name if needed
1725 if ( $database !== '' ) {
1726 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $database ) ) {
1727 $database = $this->addIdentifierQuotes( $database );
1728 }
1729 $tableName = $database . '.' . $tableName;
1730 }
1731
1732 return $tableName;
1733 }
1734
1735 /**
1736 * Fetch a number of table names into an array
1737 * This is handy when you need to construct SQL for joins
1738 *
1739 * Example:
1740 * extract( $dbr->tableNames( 'user', 'watchlist' ) );
1741 * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
1742 * WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
1743 *
1744 * @return array
1745 */
1746 public function tableNames() {
1747 $inArray = func_get_args();
1748 $retVal = [];
1749
1750 foreach ( $inArray as $name ) {
1751 $retVal[$name] = $this->tableName( $name );
1752 }
1753
1754 return $retVal;
1755 }
1756
1757 /**
1758 * Fetch a number of table names into an zero-indexed numerical array
1759 * This is handy when you need to construct SQL for joins
1760 *
1761 * Example:
1762 * list( $user, $watchlist ) = $dbr->tableNamesN( 'user', 'watchlist' );
1763 * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
1764 * WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
1765 *
1766 * @return array
1767 */
1768 public function tableNamesN() {
1769 $inArray = func_get_args();
1770 $retVal = [];
1771
1772 foreach ( $inArray as $name ) {
1773 $retVal[] = $this->tableName( $name );
1774 }
1775
1776 return $retVal;
1777 }
1778
1779 /**
1780 * Get an aliased table name
1781 * e.g. tableName AS newTableName
1782 *
1783 * @param string $name Table name, see tableName()
1784 * @param string|bool $alias Alias (optional)
1785 * @return string SQL name for aliased table. Will not alias a table to its own name
1786 */
1787 public function tableNameWithAlias( $name, $alias = false ) {
1788 if ( !$alias || $alias == $name ) {
1789 return $this->tableName( $name );
1790 } else {
1791 return $this->tableName( $name ) . ' ' . $this->addIdentifierQuotes( $alias );
1792 }
1793 }
1794
1795 /**
1796 * Gets an array of aliased table names
1797 *
1798 * @param array $tables [ [alias] => table ]
1799 * @return string[] See tableNameWithAlias()
1800 */
1801 public function tableNamesWithAlias( $tables ) {
1802 $retval = [];
1803 foreach ( $tables as $alias => $table ) {
1804 if ( is_numeric( $alias ) ) {
1805 $alias = $table;
1806 }
1807 $retval[] = $this->tableNameWithAlias( $table, $alias );
1808 }
1809
1810 return $retval;
1811 }
1812
1813 /**
1814 * Get an aliased field name
1815 * e.g. fieldName AS newFieldName
1816 *
1817 * @param string $name Field name
1818 * @param string|bool $alias Alias (optional)
1819 * @return string SQL name for aliased field. Will not alias a field to its own name
1820 */
1821 public function fieldNameWithAlias( $name, $alias = false ) {
1822 if ( !$alias || (string)$alias === (string)$name ) {
1823 return $name;
1824 } else {
1825 return $name . ' AS ' . $this->addIdentifierQuotes( $alias ); // PostgreSQL needs AS
1826 }
1827 }
1828
1829 /**
1830 * Gets an array of aliased field names
1831 *
1832 * @param array $fields [ [alias] => field ]
1833 * @return string[] See fieldNameWithAlias()
1834 */
1835 public function fieldNamesWithAlias( $fields ) {
1836 $retval = [];
1837 foreach ( $fields as $alias => $field ) {
1838 if ( is_numeric( $alias ) ) {
1839 $alias = $field;
1840 }
1841 $retval[] = $this->fieldNameWithAlias( $field, $alias );
1842 }
1843
1844 return $retval;
1845 }
1846
1847 /**
1848 * Get the aliased table name clause for a FROM clause
1849 * which might have a JOIN and/or USE INDEX or IGNORE INDEX clause
1850 *
1851 * @param array $tables ( [alias] => table )
1852 * @param array $use_index Same as for select()
1853 * @param array $ignore_index Same as for select()
1854 * @param array $join_conds Same as for select()
1855 * @return string
1856 */
1857 protected function tableNamesWithIndexClauseOrJOIN(
1858 $tables, $use_index = [], $ignore_index = [], $join_conds = []
1859 ) {
1860 $ret = [];
1861 $retJOIN = [];
1862 $use_index = (array)$use_index;
1863 $ignore_index = (array)$ignore_index;
1864 $join_conds = (array)$join_conds;
1865
1866 foreach ( $tables as $alias => $table ) {
1867 if ( !is_string( $alias ) ) {
1868 // No alias? Set it equal to the table name
1869 $alias = $table;
1870 }
1871 // Is there a JOIN clause for this table?
1872 if ( isset( $join_conds[$alias] ) ) {
1873 list( $joinType, $conds ) = $join_conds[$alias];
1874 $tableClause = $joinType;
1875 $tableClause .= ' ' . $this->tableNameWithAlias( $table, $alias );
1876 if ( isset( $use_index[$alias] ) ) { // has USE INDEX?
1877 $use = $this->useIndexClause( implode( ',', (array)$use_index[$alias] ) );
1878 if ( $use != '' ) {
1879 $tableClause .= ' ' . $use;
1880 }
1881 }
1882 if ( isset( $ignore_index[$alias] ) ) { // has IGNORE INDEX?
1883 $ignore = $this->ignoreIndexClause( implode( ',', (array)$ignore_index[$alias] ) );
1884 if ( $ignore != '' ) {
1885 $tableClause .= ' ' . $ignore;
1886 }
1887 }
1888 $on = $this->makeList( (array)$conds, LIST_AND );
1889 if ( $on != '' ) {
1890 $tableClause .= ' ON (' . $on . ')';
1891 }
1892
1893 $retJOIN[] = $tableClause;
1894 } elseif ( isset( $use_index[$alias] ) ) {
1895 // Is there an INDEX clause for this table?
1896 $tableClause = $this->tableNameWithAlias( $table, $alias );
1897 $tableClause .= ' ' . $this->useIndexClause(
1898 implode( ',', (array)$use_index[$alias] )
1899 );
1900
1901 $ret[] = $tableClause;
1902 } elseif ( isset( $ignore_index[$alias] ) ) {
1903 // Is there an INDEX clause for this table?
1904 $tableClause = $this->tableNameWithAlias( $table, $alias );
1905 $tableClause .= ' ' . $this->ignoreIndexClause(
1906 implode( ',', (array)$ignore_index[$alias] )
1907 );
1908
1909 $ret[] = $tableClause;
1910 } else {
1911 $tableClause = $this->tableNameWithAlias( $table, $alias );
1912
1913 $ret[] = $tableClause;
1914 }
1915 }
1916
1917 // We can't separate explicit JOIN clauses with ',', use ' ' for those
1918 $implicitJoins = !empty( $ret ) ? implode( ',', $ret ) : "";
1919 $explicitJoins = !empty( $retJOIN ) ? implode( ' ', $retJOIN ) : "";
1920
1921 // Compile our final table clause
1922 return implode( ' ', [ $implicitJoins, $explicitJoins ] );
1923 }
1924
1925 /**
1926 * Get the name of an index in a given table.
1927 *
1928 * @param string $index
1929 * @return string
1930 */
1931 protected function indexName( $index ) {
1932 // Backwards-compatibility hack
1933 $renamed = [
1934 'ar_usertext_timestamp' => 'usertext_timestamp',
1935 'un_user_id' => 'user_id',
1936 'un_user_ip' => 'user_ip',
1937 ];
1938
1939 if ( isset( $renamed[$index] ) ) {
1940 return $renamed[$index];
1941 } else {
1942 return $index;
1943 }
1944 }
1945
1946 public function addQuotes( $s ) {
1947 if ( $s instanceof Blob ) {
1948 $s = $s->fetch();
1949 }
1950 if ( $s === null ) {
1951 return 'NULL';
1952 } else {
1953 # This will also quote numeric values. This should be harmless,
1954 # and protects against weird problems that occur when they really
1955 # _are_ strings such as article titles and string->number->string
1956 # conversion is not 1:1.
1957 return "'" . $this->strencode( $s ) . "'";
1958 }
1959 }
1960
1961 /**
1962 * Quotes an identifier using `backticks` or "double quotes" depending on the database type.
1963 * MySQL uses `backticks` while basically everything else uses double quotes.
1964 * Since MySQL is the odd one out here the double quotes are our generic
1965 * and we implement backticks in DatabaseMysql.
1966 *
1967 * @param string $s
1968 * @return string
1969 */
1970 public function addIdentifierQuotes( $s ) {
1971 return '"' . str_replace( '"', '""', $s ) . '"';
1972 }
1973
1974 /**
1975 * Returns if the given identifier looks quoted or not according to
1976 * the database convention for quoting identifiers .
1977 *
1978 * @note Do not use this to determine if untrusted input is safe.
1979 * A malicious user can trick this function.
1980 * @param string $name
1981 * @return bool
1982 */
1983 public function isQuotedIdentifier( $name ) {
1984 return $name[0] == '"' && substr( $name, -1, 1 ) == '"';
1985 }
1986
1987 /**
1988 * @param string $s
1989 * @return string
1990 */
1991 protected function escapeLikeInternal( $s ) {
1992 return addcslashes( $s, '\%_' );
1993 }
1994
1995 public function buildLike() {
1996 $params = func_get_args();
1997
1998 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
1999 $params = $params[0];
2000 }
2001
2002 $s = '';
2003
2004 foreach ( $params as $value ) {
2005 if ( $value instanceof LikeMatch ) {
2006 $s .= $value->toString();
2007 } else {
2008 $s .= $this->escapeLikeInternal( $value );
2009 }
2010 }
2011
2012 return " LIKE {$this->addQuotes( $s )} ";
2013 }
2014
2015 public function anyChar() {
2016 return new LikeMatch( '_' );
2017 }
2018
2019 public function anyString() {
2020 return new LikeMatch( '%' );
2021 }
2022
2023 public function nextSequenceValue( $seqName ) {
2024 return null;
2025 }
2026
2027 /**
2028 * USE INDEX clause. Unlikely to be useful for anything but MySQL. This
2029 * is only needed because a) MySQL must be as efficient as possible due to
2030 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2031 * which index to pick. Anyway, other databases might have different
2032 * indexes on a given table. So don't bother overriding this unless you're
2033 * MySQL.
2034 * @param string $index
2035 * @return string
2036 */
2037 public function useIndexClause( $index ) {
2038 return '';
2039 }
2040
2041 /**
2042 * IGNORE INDEX clause. Unlikely to be useful for anything but MySQL. This
2043 * is only needed because a) MySQL must be as efficient as possible due to
2044 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2045 * which index to pick. Anyway, other databases might have different
2046 * indexes on a given table. So don't bother overriding this unless you're
2047 * MySQL.
2048 * @param string $index
2049 * @return string
2050 */
2051 public function ignoreIndexClause( $index ) {
2052 return '';
2053 }
2054
2055 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
2056 $quotedTable = $this->tableName( $table );
2057
2058 if ( count( $rows ) == 0 ) {
2059 return;
2060 }
2061
2062 # Single row case
2063 if ( !is_array( reset( $rows ) ) ) {
2064 $rows = [ $rows ];
2065 }
2066
2067 // @FXIME: this is not atomic, but a trx would break affectedRows()
2068 foreach ( $rows as $row ) {
2069 # Delete rows which collide
2070 if ( $uniqueIndexes ) {
2071 $sql = "DELETE FROM $quotedTable WHERE ";
2072 $first = true;
2073 foreach ( $uniqueIndexes as $index ) {
2074 if ( $first ) {
2075 $first = false;
2076 $sql .= '( ';
2077 } else {
2078 $sql .= ' ) OR ( ';
2079 }
2080 if ( is_array( $index ) ) {
2081 $first2 = true;
2082 foreach ( $index as $col ) {
2083 if ( $first2 ) {
2084 $first2 = false;
2085 } else {
2086 $sql .= ' AND ';
2087 }
2088 $sql .= $col . '=' . $this->addQuotes( $row[$col] );
2089 }
2090 } else {
2091 $sql .= $index . '=' . $this->addQuotes( $row[$index] );
2092 }
2093 }
2094 $sql .= ' )';
2095 $this->query( $sql, $fname );
2096 }
2097
2098 # Now insert the row
2099 $this->insert( $table, $row, $fname );
2100 }
2101 }
2102
2103 /**
2104 * REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE
2105 * statement.
2106 *
2107 * @param string $table Table name
2108 * @param array|string $rows Row(s) to insert
2109 * @param string $fname Caller function name
2110 *
2111 * @return ResultWrapper
2112 */
2113 protected function nativeReplace( $table, $rows, $fname ) {
2114 $table = $this->tableName( $table );
2115
2116 # Single row case
2117 if ( !is_array( reset( $rows ) ) ) {
2118 $rows = [ $rows ];
2119 }
2120
2121 $sql = "REPLACE INTO $table (" . implode( ',', array_keys( $rows[0] ) ) . ') VALUES ';
2122 $first = true;
2123
2124 foreach ( $rows as $row ) {
2125 if ( $first ) {
2126 $first = false;
2127 } else {
2128 $sql .= ',';
2129 }
2130
2131 $sql .= '(' . $this->makeList( $row ) . ')';
2132 }
2133
2134 return $this->query( $sql, $fname );
2135 }
2136
2137 public function upsert( $table, array $rows, array $uniqueIndexes, array $set,
2138 $fname = __METHOD__
2139 ) {
2140 if ( !count( $rows ) ) {
2141 return true; // nothing to do
2142 }
2143
2144 if ( !is_array( reset( $rows ) ) ) {
2145 $rows = [ $rows ];
2146 }
2147
2148 if ( count( $uniqueIndexes ) ) {
2149 $clauses = []; // list WHERE clauses that each identify a single row
2150 foreach ( $rows as $row ) {
2151 foreach ( $uniqueIndexes as $index ) {
2152 $index = is_array( $index ) ? $index : [ $index ]; // columns
2153 $rowKey = []; // unique key to this row
2154 foreach ( $index as $column ) {
2155 $rowKey[$column] = $row[$column];
2156 }
2157 $clauses[] = $this->makeList( $rowKey, LIST_AND );
2158 }
2159 }
2160 $where = [ $this->makeList( $clauses, LIST_OR ) ];
2161 } else {
2162 $where = false;
2163 }
2164
2165 $useTrx = !$this->mTrxLevel;
2166 if ( $useTrx ) {
2167 $this->begin( $fname, self::TRANSACTION_INTERNAL );
2168 }
2169 try {
2170 # Update any existing conflicting row(s)
2171 if ( $where !== false ) {
2172 $ok = $this->update( $table, $set, $where, $fname );
2173 } else {
2174 $ok = true;
2175 }
2176 # Now insert any non-conflicting row(s)
2177 $ok = $this->insert( $table, $rows, $fname, [ 'IGNORE' ] ) && $ok;
2178 } catch ( Exception $e ) {
2179 if ( $useTrx ) {
2180 $this->rollback( $fname, self::FLUSHING_INTERNAL );
2181 }
2182 throw $e;
2183 }
2184 if ( $useTrx ) {
2185 $this->commit( $fname, self::FLUSHING_INTERNAL );
2186 }
2187
2188 return $ok;
2189 }
2190
2191 public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
2192 $fname = __METHOD__
2193 ) {
2194 if ( !$conds ) {
2195 throw new DBUnexpectedError( $this, __METHOD__ . ' called with empty $conds' );
2196 }
2197
2198 $delTable = $this->tableName( $delTable );
2199 $joinTable = $this->tableName( $joinTable );
2200 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
2201 if ( $conds != '*' ) {
2202 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND );
2203 }
2204 $sql .= ')';
2205
2206 $this->query( $sql, $fname );
2207 }
2208
2209 /**
2210 * Returns the size of a text field, or -1 for "unlimited"
2211 *
2212 * @param string $table
2213 * @param string $field
2214 * @return int
2215 */
2216 public function textFieldSize( $table, $field ) {
2217 $table = $this->tableName( $table );
2218 $sql = "SHOW COLUMNS FROM $table LIKE \"$field\";";
2219 $res = $this->query( $sql, __METHOD__ );
2220 $row = $this->fetchObject( $res );
2221
2222 $m = [];
2223
2224 if ( preg_match( '/\((.*)\)/', $row->Type, $m ) ) {
2225 $size = $m[1];
2226 } else {
2227 $size = -1;
2228 }
2229
2230 return $size;
2231 }
2232
2233 public function delete( $table, $conds, $fname = __METHOD__ ) {
2234 if ( !$conds ) {
2235 throw new DBUnexpectedError( $this, __METHOD__ . ' called with no conditions' );
2236 }
2237
2238 $table = $this->tableName( $table );
2239 $sql = "DELETE FROM $table";
2240
2241 if ( $conds != '*' ) {
2242 if ( is_array( $conds ) ) {
2243 $conds = $this->makeList( $conds, LIST_AND );
2244 }
2245 $sql .= ' WHERE ' . $conds;
2246 }
2247
2248 return $this->query( $sql, $fname );
2249 }
2250
2251 public function insertSelect(
2252 $destTable, $srcTable, $varMap, $conds,
2253 $fname = __METHOD__, $insertOptions = [], $selectOptions = []
2254 ) {
2255 if ( $this->cliMode ) {
2256 // For massive migrations with downtime, we don't want to select everything
2257 // into memory and OOM, so do all this native on the server side if possible.
2258 return $this->nativeInsertSelect(
2259 $destTable,
2260 $srcTable,
2261 $varMap,
2262 $conds,
2263 $fname,
2264 $insertOptions,
2265 $selectOptions
2266 );
2267 }
2268
2269 // For web requests, do a locking SELECT and then INSERT. This puts the SELECT burden
2270 // on only the master (without needing row-based-replication). It also makes it easy to
2271 // know how big the INSERT is going to be.
2272 $fields = [];
2273 foreach ( $varMap as $dstColumn => $sourceColumnOrSql ) {
2274 $fields[] = $this->fieldNameWithAlias( $sourceColumnOrSql, $dstColumn );
2275 }
2276 $selectOptions[] = 'FOR UPDATE';
2277 $res = $this->select( $srcTable, implode( ',', $fields ), $conds, $fname, $selectOptions );
2278 if ( !$res ) {
2279 return false;
2280 }
2281
2282 $rows = [];
2283 foreach ( $res as $row ) {
2284 $rows[] = (array)$row;
2285 }
2286
2287 return $this->insert( $destTable, $rows, $fname, $insertOptions );
2288 }
2289
2290 public function nativeInsertSelect( $destTable, $srcTable, $varMap, $conds,
2291 $fname = __METHOD__,
2292 $insertOptions = [], $selectOptions = []
2293 ) {
2294 $destTable = $this->tableName( $destTable );
2295
2296 if ( !is_array( $insertOptions ) ) {
2297 $insertOptions = [ $insertOptions ];
2298 }
2299
2300 $insertOptions = $this->makeInsertOptions( $insertOptions );
2301
2302 if ( !is_array( $selectOptions ) ) {
2303 $selectOptions = [ $selectOptions ];
2304 }
2305
2306 list( $startOpts, $useIndex, $tailOpts, $ignoreIndex ) = $this->makeSelectOptions(
2307 $selectOptions );
2308
2309 if ( is_array( $srcTable ) ) {
2310 $srcTable = implode( ',', array_map( [ &$this, 'tableName' ], $srcTable ) );
2311 } else {
2312 $srcTable = $this->tableName( $srcTable );
2313 }
2314
2315 $sql = "INSERT $insertOptions INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
2316 " SELECT $startOpts " . implode( ',', $varMap ) .
2317 " FROM $srcTable $useIndex $ignoreIndex ";
2318
2319 if ( $conds != '*' ) {
2320 if ( is_array( $conds ) ) {
2321 $conds = $this->makeList( $conds, LIST_AND );
2322 }
2323 $sql .= " WHERE $conds";
2324 }
2325
2326 $sql .= " $tailOpts";
2327
2328 return $this->query( $sql, $fname );
2329 }
2330
2331 /**
2332 * Construct a LIMIT query with optional offset. This is used for query
2333 * pages. The SQL should be adjusted so that only the first $limit rows
2334 * are returned. If $offset is provided as well, then the first $offset
2335 * rows should be discarded, and the next $limit rows should be returned.
2336 * If the result of the query is not ordered, then the rows to be returned
2337 * are theoretically arbitrary.
2338 *
2339 * $sql is expected to be a SELECT, if that makes a difference.
2340 *
2341 * The version provided by default works in MySQL and SQLite. It will very
2342 * likely need to be overridden for most other DBMSes.
2343 *
2344 * @param string $sql SQL query we will append the limit too
2345 * @param int $limit The SQL limit
2346 * @param int|bool $offset The SQL offset (default false)
2347 * @throws DBUnexpectedError
2348 * @return string
2349 */
2350 public function limitResult( $sql, $limit, $offset = false ) {
2351 if ( !is_numeric( $limit ) ) {
2352 throw new DBUnexpectedError( $this, "Invalid non-numeric limit passed to limitResult()\n" );
2353 }
2354
2355 return "$sql LIMIT "
2356 . ( ( is_numeric( $offset ) && $offset != 0 ) ? "{$offset}," : "" )
2357 . "{$limit} ";
2358 }
2359
2360 public function unionSupportsOrderAndLimit() {
2361 return true; // True for almost every DB supported
2362 }
2363
2364 public function unionQueries( $sqls, $all ) {
2365 $glue = $all ? ') UNION ALL (' : ') UNION (';
2366
2367 return '(' . implode( $glue, $sqls ) . ')';
2368 }
2369
2370 public function conditional( $cond, $trueVal, $falseVal ) {
2371 if ( is_array( $cond ) ) {
2372 $cond = $this->makeList( $cond, LIST_AND );
2373 }
2374
2375 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
2376 }
2377
2378 public function strreplace( $orig, $old, $new ) {
2379 return "REPLACE({$orig}, {$old}, {$new})";
2380 }
2381
2382 public function getServerUptime() {
2383 return 0;
2384 }
2385
2386 public function wasDeadlock() {
2387 return false;
2388 }
2389
2390 public function wasLockTimeout() {
2391 return false;
2392 }
2393
2394 public function wasErrorReissuable() {
2395 return false;
2396 }
2397
2398 public function wasReadOnlyError() {
2399 return false;
2400 }
2401
2402 /**
2403 * Determines if the given query error was a connection drop
2404 * STUB
2405 *
2406 * @param integer|string $errno
2407 * @return bool
2408 */
2409 public function wasConnectionError( $errno ) {
2410 return false;
2411 }
2412
2413 /**
2414 * Perform a deadlock-prone transaction.
2415 *
2416 * This function invokes a callback function to perform a set of write
2417 * queries. If a deadlock occurs during the processing, the transaction
2418 * will be rolled back and the callback function will be called again.
2419 *
2420 * Avoid using this method outside of Job or Maintenance classes.
2421 *
2422 * Usage:
2423 * $dbw->deadlockLoop( callback, ... );
2424 *
2425 * Extra arguments are passed through to the specified callback function.
2426 * This method requires that no transactions are already active to avoid
2427 * causing premature commits or exceptions.
2428 *
2429 * Returns whatever the callback function returned on its successful,
2430 * iteration, or false on error, for example if the retry limit was
2431 * reached.
2432 *
2433 * @return mixed
2434 * @throws DBUnexpectedError
2435 * @throws Exception
2436 */
2437 public function deadlockLoop() {
2438 $args = func_get_args();
2439 $function = array_shift( $args );
2440 $tries = self::DEADLOCK_TRIES;
2441
2442 $this->begin( __METHOD__ );
2443
2444 $retVal = null;
2445 /** @var Exception $e */
2446 $e = null;
2447 do {
2448 try {
2449 $retVal = call_user_func_array( $function, $args );
2450 break;
2451 } catch ( DBQueryError $e ) {
2452 if ( $this->wasDeadlock() ) {
2453 // Retry after a randomized delay
2454 usleep( mt_rand( self::DEADLOCK_DELAY_MIN, self::DEADLOCK_DELAY_MAX ) );
2455 } else {
2456 // Throw the error back up
2457 throw $e;
2458 }
2459 }
2460 } while ( --$tries > 0 );
2461
2462 if ( $tries <= 0 ) {
2463 // Too many deadlocks; give up
2464 $this->rollback( __METHOD__ );
2465 throw $e;
2466 } else {
2467 $this->commit( __METHOD__ );
2468
2469 return $retVal;
2470 }
2471 }
2472
2473 public function masterPosWait( DBMasterPos $pos, $timeout ) {
2474 # Real waits are implemented in the subclass.
2475 return 0;
2476 }
2477
2478 public function getSlavePos() {
2479 # Stub
2480 return false;
2481 }
2482
2483 public function getMasterPos() {
2484 # Stub
2485 return false;
2486 }
2487
2488 public function serverIsReadOnly() {
2489 return false;
2490 }
2491
2492 final public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
2493 if ( !$this->mTrxLevel ) {
2494 throw new DBUnexpectedError( $this, "No transaction is active." );
2495 }
2496 $this->mTrxEndCallbacks[] = [ $callback, $fname ];
2497 }
2498
2499 final public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
2500 $this->mTrxIdleCallbacks[] = [ $callback, $fname ];
2501 if ( !$this->mTrxLevel ) {
2502 $this->runOnTransactionIdleCallbacks( self::TRIGGER_IDLE );
2503 }
2504 }
2505
2506 final public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
2507 if ( $this->mTrxLevel ) {
2508 $this->mTrxPreCommitCallbacks[] = [ $callback, $fname ];
2509 } else {
2510 // If no transaction is active, then make one for this callback
2511 $this->startAtomic( __METHOD__ );
2512 try {
2513 call_user_func( $callback );
2514 $this->endAtomic( __METHOD__ );
2515 } catch ( Exception $e ) {
2516 $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
2517 throw $e;
2518 }
2519 }
2520 }
2521
2522 final public function setTransactionListener( $name, callable $callback = null ) {
2523 if ( $callback ) {
2524 $this->mTrxRecurringCallbacks[$name] = $callback;
2525 } else {
2526 unset( $this->mTrxRecurringCallbacks[$name] );
2527 }
2528 }
2529
2530 /**
2531 * Whether to disable running of post-COMMIT/ROLLBACK callbacks
2532 *
2533 * This method should not be used outside of Database/LoadBalancer
2534 *
2535 * @param bool $suppress
2536 * @since 1.28
2537 */
2538 final public function setTrxEndCallbackSuppression( $suppress ) {
2539 $this->mTrxEndCallbacksSuppressed = $suppress;
2540 }
2541
2542 /**
2543 * Actually run and consume any "on transaction idle/resolution" callbacks.
2544 *
2545 * This method should not be used outside of Database/LoadBalancer
2546 *
2547 * @param integer $trigger IDatabase::TRIGGER_* constant
2548 * @since 1.20
2549 * @throws Exception
2550 */
2551 public function runOnTransactionIdleCallbacks( $trigger ) {
2552 if ( $this->mTrxEndCallbacksSuppressed ) {
2553 return;
2554 }
2555
2556 $autoTrx = $this->getFlag( DBO_TRX ); // automatic begin() enabled?
2557 /** @var Exception $e */
2558 $e = null; // first exception
2559 do { // callbacks may add callbacks :)
2560 $callbacks = array_merge(
2561 $this->mTrxIdleCallbacks,
2562 $this->mTrxEndCallbacks // include "transaction resolution" callbacks
2563 );
2564 $this->mTrxIdleCallbacks = []; // consumed (and recursion guard)
2565 $this->mTrxEndCallbacks = []; // consumed (recursion guard)
2566 foreach ( $callbacks as $callback ) {
2567 try {
2568 list( $phpCallback ) = $callback;
2569 $this->clearFlag( DBO_TRX ); // make each query its own transaction
2570 call_user_func_array( $phpCallback, [ $trigger ] );
2571 if ( $autoTrx ) {
2572 $this->setFlag( DBO_TRX ); // restore automatic begin()
2573 } else {
2574 $this->clearFlag( DBO_TRX ); // restore auto-commit
2575 }
2576 } catch ( Exception $ex ) {
2577 call_user_func( $this->errorLogger, $ex );
2578 $e = $e ?: $ex;
2579 // Some callbacks may use startAtomic/endAtomic, so make sure
2580 // their transactions are ended so other callbacks don't fail
2581 if ( $this->trxLevel() ) {
2582 $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
2583 }
2584 }
2585 }
2586 } while ( count( $this->mTrxIdleCallbacks ) );
2587
2588 if ( $e instanceof Exception ) {
2589 throw $e; // re-throw any first exception
2590 }
2591 }
2592
2593 /**
2594 * Actually run and consume any "on transaction pre-commit" callbacks.
2595 *
2596 * This method should not be used outside of Database/LoadBalancer
2597 *
2598 * @since 1.22
2599 * @throws Exception
2600 */
2601 public function runOnTransactionPreCommitCallbacks() {
2602 $e = null; // first exception
2603 do { // callbacks may add callbacks :)
2604 $callbacks = $this->mTrxPreCommitCallbacks;
2605 $this->mTrxPreCommitCallbacks = []; // consumed (and recursion guard)
2606 foreach ( $callbacks as $callback ) {
2607 try {
2608 list( $phpCallback ) = $callback;
2609 call_user_func( $phpCallback );
2610 } catch ( Exception $ex ) {
2611 call_user_func( $this->errorLogger, $ex );
2612 $e = $e ?: $ex;
2613 }
2614 }
2615 } while ( count( $this->mTrxPreCommitCallbacks ) );
2616
2617 if ( $e instanceof Exception ) {
2618 throw $e; // re-throw any first exception
2619 }
2620 }
2621
2622 /**
2623 * Actually run any "transaction listener" callbacks.
2624 *
2625 * This method should not be used outside of Database/LoadBalancer
2626 *
2627 * @param integer $trigger IDatabase::TRIGGER_* constant
2628 * @throws Exception
2629 * @since 1.20
2630 */
2631 public function runTransactionListenerCallbacks( $trigger ) {
2632 if ( $this->mTrxEndCallbacksSuppressed ) {
2633 return;
2634 }
2635
2636 /** @var Exception $e */
2637 $e = null; // first exception
2638
2639 foreach ( $this->mTrxRecurringCallbacks as $phpCallback ) {
2640 try {
2641 $phpCallback( $trigger, $this );
2642 } catch ( Exception $ex ) {
2643 call_user_func( $this->errorLogger, $ex );
2644 $e = $e ?: $ex;
2645 }
2646 }
2647
2648 if ( $e instanceof Exception ) {
2649 throw $e; // re-throw any first exception
2650 }
2651 }
2652
2653 final public function startAtomic( $fname = __METHOD__ ) {
2654 if ( !$this->mTrxLevel ) {
2655 $this->begin( $fname, self::TRANSACTION_INTERNAL );
2656 // If DBO_TRX is set, a series of startAtomic/endAtomic pairs will result
2657 // in all changes being in one transaction to keep requests transactional.
2658 if ( !$this->getFlag( DBO_TRX ) ) {
2659 $this->mTrxAutomaticAtomic = true;
2660 }
2661 }
2662
2663 $this->mTrxAtomicLevels[] = $fname;
2664 }
2665
2666 final public function endAtomic( $fname = __METHOD__ ) {
2667 if ( !$this->mTrxLevel ) {
2668 throw new DBUnexpectedError( $this, "No atomic transaction is open (got $fname)." );
2669 }
2670 if ( !$this->mTrxAtomicLevels ||
2671 array_pop( $this->mTrxAtomicLevels ) !== $fname
2672 ) {
2673 throw new DBUnexpectedError( $this, "Invalid atomic section ended (got $fname)." );
2674 }
2675
2676 if ( !$this->mTrxAtomicLevels && $this->mTrxAutomaticAtomic ) {
2677 $this->commit( $fname, self::FLUSHING_INTERNAL );
2678 }
2679 }
2680
2681 final public function doAtomicSection( $fname, callable $callback ) {
2682 $this->startAtomic( $fname );
2683 try {
2684 $res = call_user_func_array( $callback, [ $this, $fname ] );
2685 } catch ( Exception $e ) {
2686 $this->rollback( $fname, self::FLUSHING_INTERNAL );
2687 throw $e;
2688 }
2689 $this->endAtomic( $fname );
2690
2691 return $res;
2692 }
2693
2694 final public function begin( $fname = __METHOD__, $mode = self::TRANSACTION_EXPLICIT ) {
2695 // Protect against mismatched atomic section, transaction nesting, and snapshot loss
2696 if ( $this->mTrxLevel ) {
2697 if ( $this->mTrxAtomicLevels ) {
2698 $levels = implode( ', ', $this->mTrxAtomicLevels );
2699 $msg = "$fname: Got explicit BEGIN while atomic section(s) $levels are open.";
2700 throw new DBUnexpectedError( $this, $msg );
2701 } elseif ( !$this->mTrxAutomatic ) {
2702 $msg = "$fname: Explicit transaction already active (from {$this->mTrxFname}).";
2703 throw new DBUnexpectedError( $this, $msg );
2704 } else {
2705 // @TODO: make this an exception at some point
2706 $msg = "$fname: Implicit transaction already active (from {$this->mTrxFname}).";
2707 $this->queryLogger->error( $msg );
2708 return; // join the main transaction set
2709 }
2710 } elseif ( $this->getFlag( DBO_TRX ) && $mode !== self::TRANSACTION_INTERNAL ) {
2711 // @TODO: make this an exception at some point
2712 $msg = "$fname: Implicit transaction expected (DBO_TRX set).";
2713 $this->queryLogger->error( $msg );
2714 return; // let any writes be in the main transaction
2715 }
2716
2717 // Avoid fatals if close() was called
2718 $this->assertOpen();
2719
2720 $this->doBegin( $fname );
2721 $this->mTrxTimestamp = microtime( true );
2722 $this->mTrxFname = $fname;
2723 $this->mTrxDoneWrites = false;
2724 $this->mTrxAutomatic = ( $mode === self::TRANSACTION_INTERNAL );
2725 $this->mTrxAutomaticAtomic = false;
2726 $this->mTrxAtomicLevels = [];
2727 $this->mTrxShortId = sprintf( '%06x', mt_rand( 0, 0xffffff ) );
2728 $this->mTrxWriteDuration = 0.0;
2729 $this->mTrxWriteQueryCount = 0;
2730 $this->mTrxWriteAdjDuration = 0.0;
2731 $this->mTrxWriteAdjQueryCount = 0;
2732 $this->mTrxWriteCallers = [];
2733 // First SELECT after BEGIN will establish the snapshot in REPEATABLE-READ.
2734 // Get an estimate of the replica DB lag before then, treating estimate staleness
2735 // as lag itself just to be safe
2736 $status = $this->getApproximateLagStatus();
2737 $this->mTrxReplicaLag = $status['lag'] + ( microtime( true ) - $status['since'] );
2738 }
2739
2740 /**
2741 * Issues the BEGIN command to the database server.
2742 *
2743 * @see DatabaseBase::begin()
2744 * @param string $fname
2745 */
2746 protected function doBegin( $fname ) {
2747 $this->query( 'BEGIN', $fname );
2748 $this->mTrxLevel = 1;
2749 }
2750
2751 final public function commit( $fname = __METHOD__, $flush = '' ) {
2752 if ( $this->mTrxLevel && $this->mTrxAtomicLevels ) {
2753 // There are still atomic sections open. This cannot be ignored
2754 $levels = implode( ', ', $this->mTrxAtomicLevels );
2755 throw new DBUnexpectedError(
2756 $this,
2757 "$fname: Got COMMIT while atomic sections $levels are still open."
2758 );
2759 }
2760
2761 if ( $flush === self::FLUSHING_INTERNAL || $flush === self::FLUSHING_ALL_PEERS ) {
2762 if ( !$this->mTrxLevel ) {
2763 return; // nothing to do
2764 } elseif ( !$this->mTrxAutomatic ) {
2765 throw new DBUnexpectedError(
2766 $this,
2767 "$fname: Flushing an explicit transaction, getting out of sync."
2768 );
2769 }
2770 } else {
2771 if ( !$this->mTrxLevel ) {
2772 $this->queryLogger->error( "$fname: No transaction to commit, something got out of sync." );
2773 return; // nothing to do
2774 } elseif ( $this->mTrxAutomatic ) {
2775 // @TODO: make this an exception at some point
2776 $msg = "$fname: Explicit commit of implicit transaction.";
2777 $this->queryLogger->error( $msg );
2778 return; // wait for the main transaction set commit round
2779 }
2780 }
2781
2782 // Avoid fatals if close() was called
2783 $this->assertOpen();
2784
2785 $this->runOnTransactionPreCommitCallbacks();
2786 $writeTime = $this->pendingWriteQueryDuration( self::ESTIMATE_DB_APPLY );
2787 $this->doCommit( $fname );
2788 if ( $this->mTrxDoneWrites ) {
2789 $this->mDoneWrites = microtime( true );
2790 $this->trxProfiler->transactionWritingOut(
2791 $this->mServer, $this->mDBname, $this->mTrxShortId, $writeTime );
2792 }
2793
2794 $this->runOnTransactionIdleCallbacks( self::TRIGGER_COMMIT );
2795 $this->runTransactionListenerCallbacks( self::TRIGGER_COMMIT );
2796 }
2797
2798 /**
2799 * Issues the COMMIT command to the database server.
2800 *
2801 * @see DatabaseBase::commit()
2802 * @param string $fname
2803 */
2804 protected function doCommit( $fname ) {
2805 if ( $this->mTrxLevel ) {
2806 $this->query( 'COMMIT', $fname );
2807 $this->mTrxLevel = 0;
2808 }
2809 }
2810
2811 final public function rollback( $fname = __METHOD__, $flush = '' ) {
2812 if ( $flush === self::FLUSHING_INTERNAL || $flush === self::FLUSHING_ALL_PEERS ) {
2813 if ( !$this->mTrxLevel ) {
2814 return; // nothing to do
2815 }
2816 } else {
2817 if ( !$this->mTrxLevel ) {
2818 $this->queryLogger->error(
2819 "$fname: No transaction to rollback, something got out of sync." );
2820 return; // nothing to do
2821 } elseif ( $this->getFlag( DBO_TRX ) ) {
2822 throw new DBUnexpectedError(
2823 $this,
2824 "$fname: Expected mass rollback of all peer databases (DBO_TRX set)."
2825 );
2826 }
2827 }
2828
2829 // Avoid fatals if close() was called
2830 $this->assertOpen();
2831
2832 $this->doRollback( $fname );
2833 $this->mTrxAtomicLevels = [];
2834 if ( $this->mTrxDoneWrites ) {
2835 $this->trxProfiler->transactionWritingOut(
2836 $this->mServer, $this->mDBname, $this->mTrxShortId );
2837 }
2838
2839 $this->mTrxIdleCallbacks = []; // clear
2840 $this->mTrxPreCommitCallbacks = []; // clear
2841 $this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
2842 $this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK );
2843 }
2844
2845 /**
2846 * Issues the ROLLBACK command to the database server.
2847 *
2848 * @see DatabaseBase::rollback()
2849 * @param string $fname
2850 */
2851 protected function doRollback( $fname ) {
2852 if ( $this->mTrxLevel ) {
2853 # Disconnects cause rollback anyway, so ignore those errors
2854 $ignoreErrors = true;
2855 $this->query( 'ROLLBACK', $fname, $ignoreErrors );
2856 $this->mTrxLevel = 0;
2857 }
2858 }
2859
2860 public function flushSnapshot( $fname = __METHOD__ ) {
2861 if ( $this->writesOrCallbacksPending() || $this->explicitTrxActive() ) {
2862 // This only flushes transactions to clear snapshots, not to write data
2863 $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
2864 throw new DBUnexpectedError(
2865 $this,
2866 "$fname: Cannot COMMIT to clear snapshot because writes are pending ($fnames)."
2867 );
2868 }
2869
2870 $this->commit( $fname, self::FLUSHING_INTERNAL );
2871 }
2872
2873 public function explicitTrxActive() {
2874 return $this->mTrxLevel && ( $this->mTrxAtomicLevels || !$this->mTrxAutomatic );
2875 }
2876
2877 /**
2878 * Creates a new table with structure copied from existing table
2879 * Note that unlike most database abstraction functions, this function does not
2880 * automatically append database prefix, because it works at a lower
2881 * abstraction level.
2882 * The table names passed to this function shall not be quoted (this
2883 * function calls addIdentifierQuotes when needed).
2884 *
2885 * @param string $oldName Name of table whose structure should be copied
2886 * @param string $newName Name of table to be created
2887 * @param bool $temporary Whether the new table should be temporary
2888 * @param string $fname Calling function name
2889 * @throws RuntimeException
2890 * @return bool True if operation was successful
2891 */
2892 public function duplicateTableStructure( $oldName, $newName, $temporary = false,
2893 $fname = __METHOD__
2894 ) {
2895 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
2896 }
2897
2898 function listTables( $prefix = null, $fname = __METHOD__ ) {
2899 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
2900 }
2901
2902 /**
2903 * Reset the views process cache set by listViews()
2904 * @since 1.22
2905 */
2906 final public function clearViewsCache() {
2907 $this->allViews = null;
2908 }
2909
2910 /**
2911 * Lists all the VIEWs in the database
2912 *
2913 * For caching purposes the list of all views should be stored in
2914 * $this->allViews. The process cache can be cleared with clearViewsCache()
2915 *
2916 * @param string $prefix Only show VIEWs with this prefix, eg. unit_test_
2917 * @param string $fname Name of calling function
2918 * @throws RuntimeException
2919 * @return array
2920 * @since 1.22
2921 */
2922 public function listViews( $prefix = null, $fname = __METHOD__ ) {
2923 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
2924 }
2925
2926 /**
2927 * Differentiates between a TABLE and a VIEW
2928 *
2929 * @param string $name Name of the database-structure to test.
2930 * @throws RuntimeException
2931 * @return bool
2932 * @since 1.22
2933 */
2934 public function isView( $name ) {
2935 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
2936 }
2937
2938 public function timestamp( $ts = 0 ) {
2939 $t = new ConvertableTimestamp( $ts );
2940 // Let errors bubble up to avoid putting garbage in the DB
2941 return $t->getTimestamp( TS_MW );
2942 }
2943
2944 public function timestampOrNull( $ts = null ) {
2945 if ( is_null( $ts ) ) {
2946 return null;
2947 } else {
2948 return $this->timestamp( $ts );
2949 }
2950 }
2951
2952 /**
2953 * Take the result from a query, and wrap it in a ResultWrapper if
2954 * necessary. Boolean values are passed through as is, to indicate success
2955 * of write queries or failure.
2956 *
2957 * Once upon a time, DatabaseBase::query() returned a bare MySQL result
2958 * resource, and it was necessary to call this function to convert it to
2959 * a wrapper. Nowadays, raw database objects are never exposed to external
2960 * callers, so this is unnecessary in external code.
2961 *
2962 * @param bool|ResultWrapper|resource|object $result
2963 * @return bool|ResultWrapper
2964 */
2965 protected function resultObject( $result ) {
2966 if ( !$result ) {
2967 return false;
2968 } elseif ( $result instanceof ResultWrapper ) {
2969 return $result;
2970 } elseif ( $result === true ) {
2971 // Successful write query
2972 return $result;
2973 } else {
2974 return new ResultWrapper( $this, $result );
2975 }
2976 }
2977
2978 public function ping( &$rtt = null ) {
2979 // Avoid hitting the server if it was hit recently
2980 if ( $this->isOpen() && ( microtime( true ) - $this->lastPing ) < self::PING_TTL ) {
2981 if ( !func_num_args() || $this->mRTTEstimate > 0 ) {
2982 $rtt = $this->mRTTEstimate;
2983 return true; // don't care about $rtt
2984 }
2985 }
2986
2987 // This will reconnect if possible or return false if not
2988 $this->clearFlag( DBO_TRX, self::REMEMBER_PRIOR );
2989 $ok = ( $this->query( self::PING_QUERY, __METHOD__, true ) !== false );
2990 $this->restoreFlags( self::RESTORE_PRIOR );
2991
2992 if ( $ok ) {
2993 $rtt = $this->mRTTEstimate;
2994 }
2995
2996 return $ok;
2997 }
2998
2999 /**
3000 * @return bool
3001 */
3002 protected function reconnect() {
3003 $this->closeConnection();
3004 $this->mOpened = false;
3005 $this->mConn = false;
3006 try {
3007 $this->open( $this->mServer, $this->mUser, $this->mPassword, $this->mDBname );
3008 $this->lastPing = microtime( true );
3009 $ok = true;
3010 } catch ( DBConnectionError $e ) {
3011 $ok = false;
3012 }
3013
3014 return $ok;
3015 }
3016
3017 public function getSessionLagStatus() {
3018 return $this->getTransactionLagStatus() ?: $this->getApproximateLagStatus();
3019 }
3020
3021 /**
3022 * Get the replica DB lag when the current transaction started
3023 *
3024 * This is useful when transactions might use snapshot isolation
3025 * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data
3026 * is this lag plus transaction duration. If they don't, it is still
3027 * safe to be pessimistic. This returns null if there is no transaction.
3028 *
3029 * @return array|null ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN)
3030 * @since 1.27
3031 */
3032 public function getTransactionLagStatus() {
3033 return $this->mTrxLevel
3034 ? [ 'lag' => $this->mTrxReplicaLag, 'since' => $this->trxTimestamp() ]
3035 : null;
3036 }
3037
3038 /**
3039 * Get a replica DB lag estimate for this server
3040 *
3041 * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of estimate)
3042 * @since 1.27
3043 */
3044 public function getApproximateLagStatus() {
3045 return [
3046 'lag' => $this->getLBInfo( 'replica' ) ? $this->getLag() : 0,
3047 'since' => microtime( true )
3048 ];
3049 }
3050
3051 /**
3052 * Merge the result of getSessionLagStatus() for several DBs
3053 * using the most pessimistic values to estimate the lag of
3054 * any data derived from them in combination
3055 *
3056 * This is information is useful for caching modules
3057 *
3058 * @see WANObjectCache::set()
3059 * @see WANObjectCache::getWithSetCallback()
3060 *
3061 * @param IDatabase $db1
3062 * @param IDatabase ...
3063 * @return array Map of values:
3064 * - lag: highest lag of any of the DBs or false on error (e.g. replication stopped)
3065 * - since: oldest UNIX timestamp of any of the DB lag estimates
3066 * - pending: whether any of the DBs have uncommitted changes
3067 * @since 1.27
3068 */
3069 public static function getCacheSetOptions( IDatabase $db1 ) {
3070 $res = [ 'lag' => 0, 'since' => INF, 'pending' => false ];
3071 foreach ( func_get_args() as $db ) {
3072 /** @var IDatabase $db */
3073 $status = $db->getSessionLagStatus();
3074 if ( $status['lag'] === false ) {
3075 $res['lag'] = false;
3076 } elseif ( $res['lag'] !== false ) {
3077 $res['lag'] = max( $res['lag'], $status['lag'] );
3078 }
3079 $res['since'] = min( $res['since'], $status['since'] );
3080 $res['pending'] = $res['pending'] ?: $db->writesPending();
3081 }
3082
3083 return $res;
3084 }
3085
3086 public function getLag() {
3087 return 0;
3088 }
3089
3090 function maxListLen() {
3091 return 0;
3092 }
3093
3094 public function encodeBlob( $b ) {
3095 return $b;
3096 }
3097
3098 public function decodeBlob( $b ) {
3099 if ( $b instanceof Blob ) {
3100 $b = $b->fetch();
3101 }
3102 return $b;
3103 }
3104
3105 public function setSessionOptions( array $options ) {
3106 }
3107
3108 /**
3109 * Read and execute SQL commands from a file.
3110 *
3111 * Returns true on success, error string or exception on failure (depending
3112 * on object's error ignore settings).
3113 *
3114 * @param string $filename File name to open
3115 * @param bool|callable $lineCallback Optional function called before reading each line
3116 * @param bool|callable $resultCallback Optional function called for each MySQL result
3117 * @param bool|string $fname Calling function name or false if name should be
3118 * generated dynamically using $filename
3119 * @param bool|callable $inputCallback Optional function called for each
3120 * complete line sent
3121 * @return bool|string
3122 * @throws Exception
3123 */
3124 public function sourceFile(
3125 $filename, $lineCallback = false, $resultCallback = false, $fname = false, $inputCallback = false
3126 ) {
3127 MediaWiki\suppressWarnings();
3128 $fp = fopen( $filename, 'r' );
3129 MediaWiki\restoreWarnings();
3130
3131 if ( false === $fp ) {
3132 throw new RuntimeException( "Could not open \"{$filename}\".\n" );
3133 }
3134
3135 if ( !$fname ) {
3136 $fname = __METHOD__ . "( $filename )";
3137 }
3138
3139 try {
3140 $error = $this->sourceStream( $fp, $lineCallback, $resultCallback, $fname, $inputCallback );
3141 } catch ( Exception $e ) {
3142 fclose( $fp );
3143 throw $e;
3144 }
3145
3146 fclose( $fp );
3147
3148 return $error;
3149 }
3150
3151 public function setSchemaVars( $vars ) {
3152 $this->mSchemaVars = $vars;
3153 }
3154
3155 /**
3156 * Read and execute commands from an open file handle.
3157 *
3158 * Returns true on success, error string or exception on failure (depending
3159 * on object's error ignore settings).
3160 *
3161 * @param resource $fp File handle
3162 * @param bool|callable $lineCallback Optional function called before reading each query
3163 * @param bool|callable $resultCallback Optional function called for each MySQL result
3164 * @param string $fname Calling function name
3165 * @param bool|callable $inputCallback Optional function called for each complete query sent
3166 * @return bool|string
3167 */
3168 public function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
3169 $fname = __METHOD__, $inputCallback = false
3170 ) {
3171 $cmd = '';
3172
3173 while ( !feof( $fp ) ) {
3174 if ( $lineCallback ) {
3175 call_user_func( $lineCallback );
3176 }
3177
3178 $line = trim( fgets( $fp ) );
3179
3180 if ( $line == '' ) {
3181 continue;
3182 }
3183
3184 if ( '-' == $line[0] && '-' == $line[1] ) {
3185 continue;
3186 }
3187
3188 if ( $cmd != '' ) {
3189 $cmd .= ' ';
3190 }
3191
3192 $done = $this->streamStatementEnd( $cmd, $line );
3193
3194 $cmd .= "$line\n";
3195
3196 if ( $done || feof( $fp ) ) {
3197 $cmd = $this->replaceVars( $cmd );
3198
3199 if ( ( $inputCallback && call_user_func( $inputCallback, $cmd ) ) || !$inputCallback ) {
3200 $res = $this->query( $cmd, $fname );
3201
3202 if ( $resultCallback ) {
3203 call_user_func( $resultCallback, $res, $this );
3204 }
3205
3206 if ( false === $res ) {
3207 $err = $this->lastError();
3208
3209 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
3210 }
3211 }
3212 $cmd = '';
3213 }
3214 }
3215
3216 return true;
3217 }
3218
3219 /**
3220 * Called by sourceStream() to check if we've reached a statement end
3221 *
3222 * @param string $sql SQL assembled so far
3223 * @param string $newLine New line about to be added to $sql
3224 * @return bool Whether $newLine contains end of the statement
3225 */
3226 public function streamStatementEnd( &$sql, &$newLine ) {
3227 if ( $this->delimiter ) {
3228 $prev = $newLine;
3229 $newLine = preg_replace( '/' . preg_quote( $this->delimiter, '/' ) . '$/', '', $newLine );
3230 if ( $newLine != $prev ) {
3231 return true;
3232 }
3233 }
3234
3235 return false;
3236 }
3237
3238 /**
3239 * Database independent variable replacement. Replaces a set of variables
3240 * in an SQL statement with their contents as given by $this->getSchemaVars().
3241 *
3242 * Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables.
3243 *
3244 * - '{$var}' should be used for text and is passed through the database's
3245 * addQuotes method.
3246 * - `{$var}` should be used for identifiers (e.g. table and database names).
3247 * It is passed through the database's addIdentifierQuotes method which
3248 * can be overridden if the database uses something other than backticks.
3249 * - / *_* / or / *$wgDBprefix* / passes the name that follows through the
3250 * database's tableName method.
3251 * - / *i* / passes the name that follows through the database's indexName method.
3252 * - In all other cases, / *$var* / is left unencoded. Except for table options,
3253 * its use should be avoided. In 1.24 and older, string encoding was applied.
3254 *
3255 * @param string $ins SQL statement to replace variables in
3256 * @return string The new SQL statement with variables replaced
3257 */
3258 protected function replaceVars( $ins ) {
3259 $vars = $this->getSchemaVars();
3260 return preg_replace_callback(
3261 '!
3262 /\* (\$wgDBprefix|[_i]) \*/ (\w*) | # 1-2. tableName, indexName
3263 \'\{\$ (\w+) }\' | # 3. addQuotes
3264 `\{\$ (\w+) }` | # 4. addIdentifierQuotes
3265 /\*\$ (\w+) \*/ # 5. leave unencoded
3266 !x',
3267 function ( $m ) use ( $vars ) {
3268 // Note: Because of <https://bugs.php.net/bug.php?id=51881>,
3269 // check for both nonexistent keys *and* the empty string.
3270 if ( isset( $m[1] ) && $m[1] !== '' ) {
3271 if ( $m[1] === 'i' ) {
3272 return $this->indexName( $m[2] );
3273 } else {
3274 return $this->tableName( $m[2] );
3275 }
3276 } elseif ( isset( $m[3] ) && $m[3] !== '' && array_key_exists( $m[3], $vars ) ) {
3277 return $this->addQuotes( $vars[$m[3]] );
3278 } elseif ( isset( $m[4] ) && $m[4] !== '' && array_key_exists( $m[4], $vars ) ) {
3279 return $this->addIdentifierQuotes( $vars[$m[4]] );
3280 } elseif ( isset( $m[5] ) && $m[5] !== '' && array_key_exists( $m[5], $vars ) ) {
3281 return $vars[$m[5]];
3282 } else {
3283 return $m[0];
3284 }
3285 },
3286 $ins
3287 );
3288 }
3289
3290 /**
3291 * Get schema variables. If none have been set via setSchemaVars(), then
3292 * use some defaults from the current object.
3293 *
3294 * @return array
3295 */
3296 protected function getSchemaVars() {
3297 if ( $this->mSchemaVars ) {
3298 return $this->mSchemaVars;
3299 } else {
3300 return $this->getDefaultSchemaVars();
3301 }
3302 }
3303
3304 /**
3305 * Get schema variables to use if none have been set via setSchemaVars().
3306 *
3307 * Override this in derived classes to provide variables for tables.sql
3308 * and SQL patch files.
3309 *
3310 * @return array
3311 */
3312 protected function getDefaultSchemaVars() {
3313 return [];
3314 }
3315
3316 public function lockIsFree( $lockName, $method ) {
3317 return true;
3318 }
3319
3320 public function lock( $lockName, $method, $timeout = 5 ) {
3321 $this->mNamedLocksHeld[$lockName] = 1;
3322
3323 return true;
3324 }
3325
3326 public function unlock( $lockName, $method ) {
3327 unset( $this->mNamedLocksHeld[$lockName] );
3328
3329 return true;
3330 }
3331
3332 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
3333 if ( $this->writesOrCallbacksPending() ) {
3334 // This only flushes transactions to clear snapshots, not to write data
3335 $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
3336 throw new DBUnexpectedError(
3337 $this,
3338 "$fname: Cannot COMMIT to clear snapshot because writes are pending ($fnames)."
3339 );
3340 }
3341
3342 if ( !$this->lock( $lockKey, $fname, $timeout ) ) {
3343 return null;
3344 }
3345
3346 $unlocker = new ScopedCallback( function () use ( $lockKey, $fname ) {
3347 if ( $this->trxLevel() ) {
3348 // There is a good chance an exception was thrown, causing any early return
3349 // from the caller. Let any error handler get a chance to issue rollback().
3350 // If there isn't one, let the error bubble up and trigger server-side rollback.
3351 $this->onTransactionResolution(
3352 function () use ( $lockKey, $fname ) {
3353 $this->unlock( $lockKey, $fname );
3354 },
3355 $fname
3356 );
3357 } else {
3358 $this->unlock( $lockKey, $fname );
3359 }
3360 } );
3361
3362 $this->commit( $fname, self::FLUSHING_INTERNAL );
3363
3364 return $unlocker;
3365 }
3366
3367 public function namedLocksEnqueue() {
3368 return false;
3369 }
3370
3371 /**
3372 * Lock specific tables
3373 *
3374 * @param array $read Array of tables to lock for read access
3375 * @param array $write Array of tables to lock for write access
3376 * @param string $method Name of caller
3377 * @param bool $lowPriority Whether to indicate writes to be LOW PRIORITY
3378 * @return bool
3379 */
3380 public function lockTables( $read, $write, $method, $lowPriority = true ) {
3381 return true;
3382 }
3383
3384 /**
3385 * Unlock specific tables
3386 *
3387 * @param string $method The caller
3388 * @return bool
3389 */
3390 public function unlockTables( $method ) {
3391 return true;
3392 }
3393
3394 /**
3395 * Delete a table
3396 * @param string $tableName
3397 * @param string $fName
3398 * @return bool|ResultWrapper
3399 * @since 1.18
3400 */
3401 public function dropTable( $tableName, $fName = __METHOD__ ) {
3402 if ( !$this->tableExists( $tableName, $fName ) ) {
3403 return false;
3404 }
3405 $sql = "DROP TABLE " . $this->tableName( $tableName ) . " CASCADE";
3406
3407 return $this->query( $sql, $fName );
3408 }
3409
3410 public function getInfinity() {
3411 return 'infinity';
3412 }
3413
3414 public function encodeExpiry( $expiry ) {
3415 return ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() )
3416 ? $this->getInfinity()
3417 : $this->timestamp( $expiry );
3418 }
3419
3420 public function decodeExpiry( $expiry, $format = TS_MW ) {
3421 if ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() ) {
3422 return 'infinity';
3423 }
3424
3425 try {
3426 $t = new ConvertableTimestamp( $expiry );
3427
3428 return $t->getTimestamp( $format );
3429 } catch ( TimestampException $e ) {
3430 return false;
3431 }
3432 }
3433
3434 public function setBigSelects( $value = true ) {
3435 // no-op
3436 }
3437
3438 public function isReadOnly() {
3439 return ( $this->getReadOnlyReason() !== false );
3440 }
3441
3442 /**
3443 * @return string|bool Reason this DB is read-only or false if it is not
3444 */
3445 protected function getReadOnlyReason() {
3446 $reason = $this->getLBInfo( 'readOnlyReason' );
3447
3448 return is_string( $reason ) ? $reason : false;
3449 }
3450
3451 public function setTableAliases( array $aliases ) {
3452 $this->tableAliases = $aliases;
3453 }
3454
3455 /**
3456 * @since 1.19
3457 * @return string
3458 */
3459 public function __toString() {
3460 return (string)$this->mConn;
3461 }
3462
3463 /**
3464 * Called by serialize. Throw an exception when DB connection is serialized.
3465 * This causes problems on some database engines because the connection is
3466 * not restored on unserialize.
3467 */
3468 public function __sleep() {
3469 throw new RuntimeException( 'Database serialization may cause problems, since ' .
3470 'the connection is not restored on wakeup.' );
3471 }
3472
3473 /**
3474 * Run a few simple sanity checks
3475 */
3476 public function __destruct() {
3477 if ( $this->mTrxLevel && $this->mTrxDoneWrites ) {
3478 trigger_error( "Uncommitted DB writes (transaction from {$this->mTrxFname})." );
3479 }
3480
3481 $danglingWriters = $this->pendingWriteAndCallbackCallers();
3482 if ( $danglingWriters ) {
3483 $fnames = implode( ', ', $danglingWriters );
3484 trigger_error( "DB transaction writes or callbacks still pending ($fnames)." );
3485 }
3486 }
3487 }