]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/dao/DBAccessObjectUtils.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / dao / DBAccessObjectUtils.php
1 <?php
2 /**
3  * This file contains database access object related constants.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  * @file
21  * @ingroup Database
22  */
23
24 /**
25  * Helper class for DAO classes
26  *
27  * @since 1.26
28  */
29 class DBAccessObjectUtils implements IDBAccessObject {
30         /**
31          * @param int $bitfield
32          * @param int $flags IDBAccessObject::READ_* constant
33          * @return bool Bitfield has flag $flag set
34          */
35         public static function hasFlags( $bitfield, $flags ) {
36                 return ( $bitfield & $flags ) == $flags;
37         }
38
39         /**
40          * Get an appropriate DB index, options, and fallback DB index for a query
41          *
42          * The fallback DB index and options are to be used if the entity is not found
43          * with the initial DB index, typically querying the master DB to avoid lag
44          *
45          * @param int $bitfield Bitfield of IDBAccessObject::READ_* constants
46          * @return array List of DB indexes and options in this order:
47          *   - DB_MASTER or DB_REPLICA constant for the initial query
48          *   - SELECT options array for the initial query
49          *   - DB_MASTER constant for the fallback query; null if no fallback should happen
50          *   - SELECT options array for the fallback query; empty if no fallback should happen
51          */
52         public static function getDBOptions( $bitfield ) {
53                 if ( self::hasFlags( $bitfield, self::READ_LATEST_IMMUTABLE ) ) {
54                         $index = DB_REPLICA; // override READ_LATEST if set
55                         $fallbackIndex = DB_MASTER;
56                 } elseif ( self::hasFlags( $bitfield, self::READ_LATEST ) ) {
57                         $index = DB_MASTER;
58                         $fallbackIndex = null;
59                 } else {
60                         $index = DB_REPLICA;
61                         $fallbackIndex = null;
62                 }
63
64                 $lockingOptions = [];
65                 if ( self::hasFlags( $bitfield, self::READ_EXCLUSIVE ) ) {
66                         $lockingOptions[] = 'FOR UPDATE';
67                 } elseif ( self::hasFlags( $bitfield, self::READ_LOCKING ) ) {
68                         $lockingOptions[] = 'LOCK IN SHARE MODE';
69                 }
70
71                 if ( $fallbackIndex !== null ) {
72                         $options = []; // locks on DB_REPLICA make no sense
73                         $fallbackOptions = $lockingOptions;
74                 } else {
75                         $options = $lockingOptions;
76                         $fallbackOptions = []; // no fallback
77                 }
78
79                 return [ $index, $options, $fallbackIndex, $fallbackOptions ];
80         }
81 }