3 use Wikimedia\Rdbms\LoadBalancer
;
6 * A service class for fetching the wiki's current read-only mode.
7 * To obtain an instance, use MediaWikiServices::getReadOnlyMode().
12 /** @var ConfiguredReadOnlyMode */
13 private $configuredReadOnly;
15 /** @var LoadBalancer */
16 private $loadBalancer;
18 public function __construct( ConfiguredReadOnlyMode
$cro, LoadBalancer
$loadBalancer ) {
19 $this->configuredReadOnly
= $cro;
20 $this->loadBalancer
= $loadBalancer;
24 * Check whether the wiki is in read-only mode.
28 public function isReadOnly() {
29 return $this->getReason() !== false;
33 * Check if the site is in read-only mode and return the message if so
35 * This checks the configuration and registered DB load balancers for
36 * read-only mode. This may result in DB connection being made.
38 * @return string|bool String when in read-only mode; false otherwise
40 public function getReason() {
41 $reason = $this->configuredReadOnly
->getReason();
42 if ( $reason !== false ) {
45 $reason = $this->loadBalancer
->getReadOnlyReason();
46 if ( $reason !== false && $reason !== null ) {
53 * Set the read-only mode, which will apply for the remainder of the
54 * request or until a service reset.
56 * @param string|null $msg
58 public function setReason( $msg ) {
59 $this->configuredReadOnly
->setReason( $msg );
63 * Clear the cache of the read only file
65 public function clearCache() {
66 $this->configuredReadOnly
->clearCache();
71 * A read-only mode service which does not depend on LoadBalancer.
72 * To obtain an instance, use MediaWikiServices::getConfiguredReadOnlyMode().
76 class ConfiguredReadOnlyMode
{
80 /** @var string|bool|null */
83 /** @var string|null */
84 private $overrideReason;
86 public function __construct( Config
$config ) {
87 $this->config
= $config;
91 * Check whether the wiki is in read-only mode.
95 public function isReadOnly() {
96 return $this->getReason() !== false;
100 * Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
102 * @return string|bool String when in read-only mode; false otherwise
104 public function getReason() {
105 if ( $this->overrideReason
!== null ) {
106 return $this->overrideReason
;
108 $confReason = $this->config
->get( 'ReadOnly' );
109 if ( $confReason !== null ) {
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 );
118 $this->fileReason
= false;
121 return $this->fileReason
;
125 * Set the read-only mode, which will apply for the remainder of the
126 * request or until a service reset.
128 * @param string|null $msg
130 public function setReason( $msg ) {
131 $this->overrideReason
= $msg;
135 * Clear the cache of the read only file
137 public function clearCache() {
138 $this->fileReason
= null;