Debian Codegraph @

includes/libs/rdbms/ConfiguredReadOnlyMode.php

raw symbols SCIP
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 * To obtain an instance, use \MediaWiki\MediaWikiServices::getConfiguredReadOnlyMode().
12 * This service can be configured via $wgReadOnly and $wgReadOnlyFile.
13 *
14 * @since 1.29
15 */
16 class ConfiguredReadOnlyMode {
17 /** @var string|false|null */
18 private $reason;
19
20 /** @var string|null */
21 private $reasonFile;
22
23 /**
24 * @param string|false|null $reason Current reason for read-only mode, if known. null means look
25 * in $reasonFile instead.
26 * @param string|null $reasonFile A file to look in for a reason, if $reason is null. If it
27 * exists and is non-empty, its contents are treated as the reason for read-only mode.
28 * Otherwise, the site is not read-only.
29 */
30 public function __construct( $reason, ?string $reasonFile = null ) {
31 $this->reason = $reason;
32 $this->reasonFile = $reasonFile;
33 }
34
35 /**
36 * Check whether the site is in read-only mode.
37 *
38 * @return bool
39 */
40 public function isReadOnly(): bool {
41 return $this->getReason() !== false;
42 }
43
44 /**
45 * @return string|false String when in read-only mode; false otherwise
46 */
47 public function getReason() {
48 if ( $this->reason !== null ) {
49 return $this->reason;
50 }
51 if ( $this->reasonFile === null ) {
52 return false;
53 }
54 // Try the reason file
55 if ( is_file( $this->reasonFile ) && filesize( $this->reasonFile ) > 0 ) {
56 $this->reason = file_get_contents( $this->reasonFile );
57 }
58 // No need to try the reason file again
59 $this->reasonFile = null;
60 return $this->reason ?? false;
61 }
62
63 /**
64 * Set the read-only mode, which will apply for the remainder of the
65 * request or until a service reset.
66 *
67 * @param string|false|null $msg
68 */
69 public function setReason( $msg ): void {
70 $this->reason = $msg;
71 }
72 }
73
74 /** @deprecated class alias since 1.41 */
75 class_alias( ConfiguredReadOnlyMode::class, 'ConfiguredReadOnlyMode' );