MediaWiki master
DBAccessObjectUtils.php
Go to the documentation of this file.
1<?php
26
38 public static function hasFlags( $bitfield, $flags ) {
39 return ( $bitfield & $flags ) == $flags;
40 }
41
55 public static function getDBOptions( $bitfield ) {
56 if ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST_IMMUTABLE ) ) {
57 $index = DB_REPLICA; // override READ_LATEST if set
58 $fallbackIndex = DB_PRIMARY;
59 } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST ) ) {
60 $index = DB_PRIMARY;
61 $fallbackIndex = null;
62 } else {
63 $index = DB_REPLICA;
64 $fallbackIndex = null;
65 }
66
67 $lockingOptions = [];
68 if ( self::hasFlags( $bitfield, IDBAccessObject::READ_EXCLUSIVE ) ) {
69 $lockingOptions[] = 'FOR UPDATE';
70 } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LOCKING ) ) {
71 $lockingOptions[] = 'LOCK IN SHARE MODE';
72 }
73
74 if ( $fallbackIndex !== null ) {
75 $options = []; // locks on DB_REPLICA make no sense
76 $fallbackOptions = $lockingOptions;
77 } else {
78 $options = $lockingOptions;
79 $fallbackOptions = []; // no fallback
80 }
81
82 return [ $index, $options, $fallbackIndex, $fallbackOptions ];
83 }
84
94 public static function getDBFromIndex( IConnectionProvider $dbProvider, int $index ): IReadableDatabase {
95 wfDeprecated( __METHOD__, '1.42' );
96 if ( $index === DB_PRIMARY ) {
97 return $dbProvider->getPrimaryDatabase();
98 } elseif ( $index === DB_REPLICA ) {
99 return $dbProvider->getReplicaDatabase();
100 } else {
101 throw new InvalidArgumentException( '$index must be either DB_REPLICA or DB_PRIMARY' );
102 }
103 }
104
111 public static function getDBFromRecency( IConnectionProvider $dbProvider, int $recency ): IReadableDatabase {
112 if ( self::hasFlags( $recency, IDBAccessObject::READ_LATEST ) ) {
113 return $dbProvider->getPrimaryDatabase();
114 }
115 return $dbProvider->getReplicaDatabase();
116 }
117}
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Helper class for DAO classes.
static getDBFromRecency(IConnectionProvider $dbProvider, int $recency)
static getDBOptions( $bitfield)
Get an appropriate DB index, options, and fallback DB index for a query.
static getDBFromIndex(IConnectionProvider $dbProvider, int $index)
Takes $index from ::getDBOptions() and return proper Database object.
static hasFlags( $bitfield, $flags)
Interface for database access objects.
Provide primary and replica IDatabase connections.
getPrimaryDatabase( $domain=false)
Get connection to the primary database.
getReplicaDatabase( $domain=false, $group=null)
Get connection to a replica database.
A database connection without write operations.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28