MediaWiki  master
DatabaseFlags.php
Go to the documentation of this file.
1 <?php
21 
23 
29 class DatabaseFlags implements IDatabaseFlags {
31  protected $flags;
33  private $priorFlags = [];
34 
36  protected const MUTABLE_FLAGS = [
37  'DBO_DEBUG',
38  'DBO_NOBUFFER',
39  'DBO_TRX',
40  'DBO_DDLMODE',
41  ];
43  protected const DBO_MUTABLE = (
45  );
46 
47  public function __construct( $flags ) {
48  $this->flags = $flags;
49  }
50 
51  public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
52  if ( $flag & ~static::DBO_MUTABLE ) {
53  throw new DBLanguageError(
54  "Got $flag (allowed: " . implode( ', ', static::MUTABLE_FLAGS ) . ')'
55  );
56  }
57 
58  if ( $remember === self::REMEMBER_PRIOR ) {
59  $this->priorFlags[] = $this->flags;
60  }
61 
62  $this->flags |= $flag;
63  }
64 
65  public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
66  if ( $flag & ~static::DBO_MUTABLE ) {
67  throw new DBLanguageError(
68  "Got $flag (allowed: " . implode( ', ', static::MUTABLE_FLAGS ) . ')'
69  );
70  }
71 
72  if ( $remember === self::REMEMBER_PRIOR ) {
73  $this->priorFlags[] = $this->flags;
74  }
75 
76  $this->flags &= ~$flag;
77  }
78 
79  public function restoreFlags( $state = self::RESTORE_PRIOR ) {
80  if ( !$this->priorFlags ) {
81  return;
82  }
83 
84  if ( $state === self::RESTORE_INITIAL ) {
85  $this->flags = reset( $this->priorFlags );
86  $this->priorFlags = [];
87  } else {
88  $this->flags = array_pop( $this->priorFlags );
89  }
90  }
91 
92  public function getFlag( $flag ) {
93  return ( ( $this->flags & $flag ) === $flag );
94  }
95 
102  public static function contains( int $flags, int $bit ) {
103  return ( ( $flags & $bit ) === $bit );
104  }
105 
106  public function hasImplicitTrxFlag() {
107  return $this->getFlag( self::DBO_TRX );
108  }
109 
110 }
restoreFlags( $state=self::RESTORE_PRIOR)
Restore the flags to their prior state before the last setFlag/clearFlag call.
int $flags
Current bit field of class DBO_* constants.
getFlag( $flag)
Returns a boolean whether the flag $flag is set for this connection.
static contains(int $flags, int $bit)
clearFlag( $flag, $remember=self::REMEMBER_NOTHING)
Clear a flag for this connection.
setFlag( $flag, $remember=self::REMEMBER_NOTHING)
Set a flag for this connection.
const DBO_NOBUFFER
Definition: defines.php:10
const DBO_DDLMODE
Definition: defines.php:16
const DBO_DEBUG
Definition: defines.php:9
const DBO_TRX
Definition: defines.php:12