4 * A read-only mode service which does not depend on LoadBalancer.
5 * To obtain an instance, use MediaWikiServices::getConfiguredReadOnlyMode().
9 class ConfiguredReadOnlyMode
{
13 /** @var string|bool|null */
16 /** @var string|null */
17 private $overrideReason;
19 public function __construct( Config
$config ) {
20 $this->config
= $config;
24 * Check whether the wiki is in read-only mode.
28 public function isReadOnly() {
29 return $this->getReason() !== false;
33 * Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
35 * @return string|bool String when in read-only mode; false otherwise
37 public function getReason() {
38 if ( $this->overrideReason
!== null ) {
39 return $this->overrideReason
;
41 $confReason = $this->config
->get( 'ReadOnly' );
42 if ( $confReason !== null ) {
45 if ( $this->fileReason
=== null ) {
46 // Cache for faster access next time
47 $readOnlyFile = $this->config
->get( 'ReadOnlyFile' );
48 if ( is_file( $readOnlyFile ) && filesize( $readOnlyFile ) > 0 ) {
49 $this->fileReason
= file_get_contents( $readOnlyFile );
51 $this->fileReason
= false;
54 return $this->fileReason
;
58 * Set the read-only mode, which will apply for the remainder of the
59 * request or until a service reset.
61 * @param string|null $msg
63 public function setReason( $msg ) {
64 $this->overrideReason
= $msg;
68 * Clear the cache of the read only file
70 public function clearCache() {
71 $this->fileReason
= null;