Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.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 {
30 /**
31 * @param integer $bitfield
32 * @param integer $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 and options for a query
41 *
42 * @param integer $bitfield
43 * @return array (DB_MASTER/DB_SLAVE, SELECT options array)
44 */
45 public static function getDBOptions( $bitfield ) {
46 $index = self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST )
47 ? DB_MASTER
48 : DB_SLAVE;
49
50 $options = [];
51 if ( self::hasFlags( $bitfield, IDBAccessObject::READ_EXCLUSIVE ) ) {
52 $options[] = 'FOR UPDATE';
53 } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LOCKING ) ) {
54 $options[] = 'LOCK IN SHARE MODE';
55 }
56
57 return [ $index, $options ];
58 }
59 }