Merge "Perform a permission check on the title when changing the page language"
[lhc/web/wiklou.git] / includes / libs / rdbms / connectionmanager / SessionConsistentConnectionManager.php
1 <?php
2
3 namespace Wikimedia\Rdbms;
4
5 /**
6 * Database connection manager.
7 *
8 * This manages access to master and replica databases. It also manages state that indicates whether
9 * the replica databases are possibly outdated after a write operation, and thus the master database
10 * should be used for subsequent read operations.
11 *
12 * @note: Services that access overlapping sets of database tables, or interact with logically
13 * related sets of data in the database, should share a SessionConsistentConnectionManager.
14 * Services accessing unrelated sets of information may prefer to not share a
15 * SessionConsistentConnectionManager, so they can still perform read operations against replica
16 * databases after a (unrelated, per the assumption) write operation to the master database.
17 * Generally, sharing a SessionConsistentConnectionManager improves consistency (by avoiding race
18 * conditions due to replication lag), but can reduce performance (by directing more read
19 * operations to the master database server).
20 *
21 * @since 1.29
22 *
23 * @license GPL-2.0+
24 * @author Daniel Kinzler
25 * @author Addshore
26 */
27 class SessionConsistentConnectionManager extends ConnectionManager {
28
29 /**
30 * @var bool
31 */
32 private $forceWriteConnection = false;
33
34 /**
35 * Forces all future calls to getReadConnection() to return a write connection.
36 * Use this before performing read operations that are critical for a future update.
37 *
38 * @since 1.29
39 */
40 public function prepareForUpdates() {
41 $this->forceWriteConnection = true;
42 }
43
44 /**
45 * @since 1.29
46 *
47 * @param string[]|null $groups
48 *
49 * @return Database
50 */
51 public function getReadConnection( array $groups = null ) {
52 if ( $this->forceWriteConnection ) {
53 return parent::getWriteConnection();
54 }
55
56 return parent::getReadConnection( $groups );
57 }
58
59 /**
60 * @since 1.29
61 *
62 * @return Database
63 */
64 public function getWriteConnection() {
65 $this->prepareForUpdates();
66 return parent::getWriteConnection();
67 }
68
69 /**
70 * @since 1.29
71 *
72 * @param string[]|null $groups
73 *
74 * @return DBConnRef
75 */
76 public function getReadConnectionRef( array $groups = null ) {
77 if ( $this->forceWriteConnection ) {
78 return parent::getWriteConnectionRef();
79 }
80
81 return parent::getReadConnectionRef( $groups );
82 }
83
84 /**
85 * @since 1.29
86 *
87 * @return DBConnRef
88 */
89 public function getWriteConnectionRef() {
90 $this->prepareForUpdates();
91 return parent::getWriteConnectionRef();
92 }
93
94 }