ReadOnlyMode: Add a few doc blocks
[lhc/web/wiklou.git] / includes / ReadOnlyMode.php
1 <?php
2
3 use Wikimedia\Rdbms\LoadBalancer;
4
5 /**
6 * A service class for fetching the wiki's current read-only mode.
7 * To obtain an instance, use MediaWikiServices::getReadOnlyMode().
8 *
9 * @since 1.29
10 */
11 class ReadOnlyMode {
12 /** @var ConfiguredReadOnlyMode */
13 private $configuredReadOnly;
14
15 /** @var LoadBalancer */
16 private $loadBalancer;
17
18 public function __construct( ConfiguredReadOnlyMode $cro, LoadBalancer $loadBalancer ) {
19 $this->configuredReadOnly = $cro;
20 $this->loadBalancer = $loadBalancer;
21 }
22
23 /**
24 * Check whether the wiki is in read-only mode.
25 *
26 * @return bool
27 */
28 public function isReadOnly() {
29 return $this->getReason() !== false;
30 }
31
32 /**
33 * Check if the site is in read-only mode and return the message if so
34 *
35 * This checks the configuration and registered DB load balancers for
36 * read-only mode. This may result in DB connection being made.
37 *
38 * @return string|bool String when in read-only mode; false otherwise
39 */
40 public function getReason() {
41 $reason = $this->configuredReadOnly->getReason();
42 if ( $reason !== false ) {
43 return $reason;
44 }
45 $reason = $this->loadBalancer->getReadOnlyReason();
46 if ( $reason !== false && $reason !== null ) {
47 return $reason;
48 }
49 return false;
50 }
51
52 /**
53 * Set the read-only mode, which will apply for the remainder of the
54 * request or until a service reset.
55 *
56 * @param string|null $msg
57 */
58 public function setReason( $msg ) {
59 $this->configuredReadOnly->setReason( $msg );
60 }
61
62 /**
63 * Clear the cache of the read only file
64 */
65 public function clearCache() {
66 $this->configuredReadOnly->clearCache();
67 }
68 }
69
70 /**
71 * A read-only mode service which does not depend on LoadBalancer.
72 * To obtain an instance, use MediaWikiServices::getConfiguredReadOnlyMode().
73 *
74 * @since 1.29
75 */
76 class ConfiguredReadOnlyMode {
77 /** @var Config */
78 private $config;
79
80 /** @var string|bool|null */
81 private $fileReason;
82
83 /** @var string|null */
84 private $overrideReason;
85
86 public function __construct( Config $config ) {
87 $this->config = $config;
88 }
89
90 /**
91 * Check whether the wiki is in read-only mode.
92 *
93 * @return bool
94 */
95 public function isReadOnly() {
96 return $this->getReason() !== false;
97 }
98
99 /**
100 * Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
101 *
102 * @return string|bool String when in read-only mode; false otherwise
103 */
104 public function getReason() {
105 if ( $this->overrideReason !== null ) {
106 return $this->overrideReason;
107 }
108 $confReason = $this->config->get( 'ReadOnly' );
109 if ( $confReason !== null ) {
110 return $confReason;
111 }
112 if ( $this->fileReason === null ) {
113 // Cache for faster access next time
114 $readOnlyFile = $this->config->get( 'ReadOnlyFile' );
115 if ( is_file( $readOnlyFile ) && filesize( $readOnlyFile ) > 0 ) {
116 $this->fileReason = file_get_contents( $readOnlyFile );
117 } else {
118 $this->fileReason = false;
119 }
120 }
121 return $this->fileReason;
122 }
123
124 /**
125 * Set the read-only mode, which will apply for the remainder of the
126 * request or until a service reset.
127 *
128 * @param string|null $msg
129 */
130 public function setReason( $msg ) {
131 $this->overrideReason = $msg;
132 }
133
134 /**
135 * Clear the cache of the read only file
136 */
137 public function clearCache() {
138 $this->fileReason = null;
139 }
140 }