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