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