Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.31% |
12 / 13 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ConfiguredReadOnlyMode | |
100.00% |
12 / 12 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| isReadOnly | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getReason | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| 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 statically configured as read-only. |
| 7 | * |
| 8 | * Unlike ReadOnlyMode, this only checks site configuration. |
| 9 | * It does not confirm whether the primary database host actively accepts writes. |
| 10 | * |
| 11 | * @since 1.29 |
| 12 | */ |
| 13 | class ConfiguredReadOnlyMode { |
| 14 | /** @var string|false|null */ |
| 15 | private $reason; |
| 16 | |
| 17 | /** @var string|null */ |
| 18 | private $reasonFile; |
| 19 | |
| 20 | /** |
| 21 | * @param string|false|null $reason Current reason for read-only mode, if known. null means look |
| 22 | * in $reasonFile instead. |
| 23 | * @param string|null $reasonFile A file to look in for a reason, if $reason is null. If it |
| 24 | * exists and is non-empty, its contents are treated as the reason for read-only mode. |
| 25 | * Otherwise, the site is not read-only. |
| 26 | */ |
| 27 | public function __construct( $reason, ?string $reasonFile = null ) { |
| 28 | $this->reason = $reason; |
| 29 | $this->reasonFile = $reasonFile; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Check whether the site is in read-only mode. |
| 34 | */ |
| 35 | public function isReadOnly(): bool { |
| 36 | return $this->getReason() !== false; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @return string|false String when in read-only mode; false otherwise |
| 41 | */ |
| 42 | public function getReason() { |
| 43 | if ( $this->reason !== null ) { |
| 44 | return $this->reason; |
| 45 | } |
| 46 | if ( $this->reasonFile === null ) { |
| 47 | return false; |
| 48 | } |
| 49 | // Try the reason file |
| 50 | if ( is_file( $this->reasonFile ) && filesize( $this->reasonFile ) > 0 ) { |
| 51 | $this->reason = file_get_contents( $this->reasonFile ); |
| 52 | } |
| 53 | // No need to try the reason file again |
| 54 | $this->reasonFile = null; |
| 55 | return $this->reason ?? false; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set the read-only mode, which will apply for the remainder of the |
| 60 | * request or until a service reset. |
| 61 | * |
| 62 | * @param string|false|null $msg |
| 63 | */ |
| 64 | public function setReason( $msg ): void { |
| 65 | $this->reason = $msg; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** @deprecated class alias since 1.41 */ |
| 70 | class_alias( ConfiguredReadOnlyMode::class, 'ConfiguredReadOnlyMode' ); |