Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
DBAccessObjectUtils
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 4
132
0.00% covered (danger)
0.00%
0 / 1
 hasFlags
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDBOptions
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
 getDBFromIndex
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getDBFromRecency
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * This file contains database access object related constants.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Database
8 */
9
10namespace Wikimedia\Rdbms;
11
12use InvalidArgumentException;
13
14/**
15 * Helper class for DAO classes
16 *
17 * @since 1.26
18 */
19class DBAccessObjectUtils implements IDBAccessObject {
20    /**
21     * @param int $bitfield
22     * @param int $flags IDBAccessObject::READ_* constant
23     * @return bool Bitfield has flag $flag set
24     */
25    public static function hasFlags( $bitfield, $flags ) {
26        return ( $bitfield & $flags ) == $flags;
27    }
28
29    /**
30     * Get an appropriate DB index and options
31     *
32     * @deprecated since 1.43
33     * @param int $bitfield Bitfield of IDBAccessObject::READ_* constants
34     * @return array List of DB indexes and options in this order:
35     *   - DB_PRIMARY or DB_REPLICA constant for the initial query
36     *   - SELECT options array for the initial query
37     */
38    public static function getDBOptions( $bitfield ) {
39        wfDeprecated( __METHOD__, '1.43' );
40        if ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST_IMMUTABLE ) ) {
41            $index = DB_REPLICA; // override READ_LATEST if set
42        } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST ) ) {
43            $index = DB_PRIMARY;
44        } else {
45            $index = DB_REPLICA;
46        }
47
48        $lockingOptions = [];
49        if ( self::hasFlags( $bitfield, IDBAccessObject::READ_EXCLUSIVE ) ) {
50            $lockingOptions[] = 'FOR UPDATE';
51        } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LOCKING ) ) {
52            $lockingOptions[] = 'LOCK IN SHARE MODE';
53        }
54
55        return [ $index, $lockingOptions ];
56    }
57
58    /**
59     * Takes $index from ::getDBOptions() and return proper Database object
60     *
61     * @deprecated since 1.42
62     *
63     * @param IConnectionProvider $dbProvider
64     * @param int $index either DB_REPLICA or DB_PRIMARY
65     * @return IReadableDatabase
66     */
67    public static function getDBFromIndex( IConnectionProvider $dbProvider, int $index ): IReadableDatabase {
68        wfDeprecated( __METHOD__, '1.42' );
69        if ( $index === DB_PRIMARY ) {
70            return $dbProvider->getPrimaryDatabase();
71        } elseif ( $index === DB_REPLICA ) {
72            return $dbProvider->getReplicaDatabase();
73        } else {
74            throw new InvalidArgumentException( '$index must be either DB_REPLICA or DB_PRIMARY' );
75        }
76    }
77
78    /**
79     * @param IConnectionProvider $dbProvider
80     * @param int $recency IDBAccessObject::READ_* constant
81     * @return IReadableDatabase
82     * @since 1.42
83     */
84    public static function getDBFromRecency( IConnectionProvider $dbProvider, int $recency ): IReadableDatabase {
85        if ( self::hasFlags( $recency, IDBAccessObject::READ_LATEST ) ) {
86            return $dbProvider->getPrimaryDatabase();
87        }
88        return $dbProvider->getReplicaDatabase();
89    }
90}
91
92/** @deprecated class alias since 1.43 */
93class_alias( DBAccessObjectUtils::class, 'DBAccessObjectUtils' );