Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
75.00% |
9 / 12 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
ReadOnlyMode | |
81.82% |
9 / 11 |
|
66.67% |
4 / 6 |
7.29 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isReadOnly | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getReason | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getConfiguredReason | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isConfiguredReadOnly | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setReason | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Wikimedia\Rdbms; |
4 | |
5 | /** |
6 | * Determine whether a site is currently in read-only mode. |
7 | * |
8 | * To obtain an instance, use \MediaWiki\MediaWikiServices::getReadOnlyMode(). |
9 | * |
10 | * @since 1.29 |
11 | */ |
12 | class ReadOnlyMode { |
13 | /** @var ConfiguredReadOnlyMode */ |
14 | private $configuredReadOnly; |
15 | |
16 | /** @var ILBFactory */ |
17 | private $lbFactory; |
18 | |
19 | /** |
20 | * @internal For ServiceWiring only |
21 | * @param ConfiguredReadOnlyMode $cro |
22 | * @param ILBFactory $lbFactory |
23 | */ |
24 | public function __construct( ConfiguredReadOnlyMode $cro, ILBFactory $lbFactory ) { |
25 | $this->configuredReadOnly = $cro; |
26 | $this->lbFactory = $lbFactory; |
27 | } |
28 | |
29 | /** |
30 | * Check whether the site is in read-only mode. |
31 | * |
32 | * @param string|false $domain Domain ID, or false for the current domain |
33 | * @return bool |
34 | */ |
35 | public function isReadOnly( $domain = false ): bool { |
36 | return $this->getReason( $domain ) !== false; |
37 | } |
38 | |
39 | /** |
40 | * Check if the site is in read-only mode and return the message if so |
41 | * |
42 | * This checks both statically configured read-only mode, and (cached) |
43 | * whether the primary database host accepting writes. |
44 | * |
45 | * Calling this may result in database connection. |
46 | * |
47 | * This method accepts virtual domains |
48 | * ({@see \MediaWiki\MainConfigSchema::VirtualDomainsMapping}). |
49 | * |
50 | * @param string|false $domain Domain ID, or false for the current domain |
51 | * @return string|false String when in read-only mode; false otherwise |
52 | */ |
53 | public function getReason( $domain = false ) { |
54 | $reason = $this->configuredReadOnly->getReason(); |
55 | if ( $reason !== false ) { |
56 | return $reason; |
57 | } |
58 | $reason = $this->lbFactory->getLoadBalancer( $domain )->getReadOnlyReason(); |
59 | return $reason ?? false; |
60 | } |
61 | |
62 | /** |
63 | * @since 1.41 |
64 | * @return string|false String when site is configured to be in read-only mode; false otherwise |
65 | */ |
66 | public function getConfiguredReason() { |
67 | return $this->configuredReadOnly->getReason(); |
68 | } |
69 | |
70 | /** |
71 | * Check whether the site is configured to be in read-only mode. |
72 | * |
73 | * @since 1.41 |
74 | * @return bool |
75 | */ |
76 | public function isConfiguredReadOnly() { |
77 | return $this->configuredReadOnly->getReason() !== false; |
78 | } |
79 | |
80 | /** |
81 | * Override the read-only mode, which will apply for the remainder of the |
82 | * request or until a service reset. |
83 | * |
84 | * @param string|false|null $msg |
85 | */ |
86 | public function setReason( $msg ): void { |
87 | $this->configuredReadOnly->setReason( $msg ); |
88 | } |
89 | |
90 | } |
91 | |
92 | /** @deprecated class alias since 1.41 */ |
93 | class_alias( ReadOnlyMode::class, 'ReadOnlyMode' ); |