| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace Wikimedia\Rdbms; |
| 25 |
|
| 26 |
use InvalidArgumentException; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class DBAccessObjectUtils implements IDBAccessObject { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public static function hasFlags( $bitfield, $flags ) { |
| 40 |
return ( $bitfield & $flags ) == $flags; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static function getDBOptions( $bitfield ) { |
| 53 |
wfDeprecated( __METHOD__, '1.43' ); |
| 54 |
if ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST_IMMUTABLE ) ) { |
| 55 |
$index = DB_REPLICA; |
| 56 |
} elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST ) ) { |
| 57 |
$index = DB_PRIMARY; |
| 58 |
} else { |
| 59 |
$index = DB_REPLICA; |
| 60 |
} |
| 61 |
|
| 62 |
$lockingOptions = []; |
| 63 |
if ( self::hasFlags( $bitfield, IDBAccessObject::READ_EXCLUSIVE ) ) { |
| 64 |
$lockingOptions[] = 'FOR UPDATE'; |
| 65 |
} elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LOCKING ) ) { |
| 66 |
$lockingOptions[] = 'LOCK IN SHARE MODE'; |
| 67 |
} |
| 68 |
|
| 69 |
return [ $index, $lockingOptions ]; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public static function getDBFromIndex( IConnectionProvider $dbProvider, int $index ): IReadableDatabase { |
| 82 |
wfDeprecated( __METHOD__, '1.42' ); |
| 83 |
if ( $index === DB_PRIMARY ) { |
| 84 |
return $dbProvider->getPrimaryDatabase(); |
| 85 |
} elseif ( $index === DB_REPLICA ) { |
| 86 |
return $dbProvider->getReplicaDatabase(); |
| 87 |
} else { |
| 88 |
throw new InvalidArgumentException( '$index must be either DB_REPLICA or DB_PRIMARY' ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public static function getDBFromRecency( IConnectionProvider $dbProvider, int $recency ): IReadableDatabase { |
| 99 |
if ( self::hasFlags( $recency, IDBAccessObject::READ_LATEST ) ) { |
| 100 |
return $dbProvider->getPrimaryDatabase(); |
| 101 |
} |
| 102 |
return $dbProvider->getReplicaDatabase(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
class_alias( DBAccessObjectUtils::class, 'DBAccessObjectUtils' ); |