rdbms: Avoid selectDB() call in LoadMonitor new connections
[mediawiki.git] / includes / ConfiguredReadOnlyMode.php
blobaf7c7cbdf50a2765ab46827d83739049003a6640
1 <?php
3 /**
4 * A read-only mode service which does not depend on LoadBalancer.
5 * To obtain an instance, use MediaWikiServices::getConfiguredReadOnlyMode().
7 * @since 1.29
8 */
9 class ConfiguredReadOnlyMode {
10 /** @var Config */
11 private $config;
13 /** @var string|bool|null */
14 private $fileReason;
16 /** @var string|null */
17 private $overrideReason;
19 public function __construct( Config $config ) {
20 $this->config = $config;
23 /**
24 * Check whether the wiki is in read-only mode.
26 * @return bool
28 public function isReadOnly() {
29 return $this->getReason() !== false;
32 /**
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 ) {
43 return $confReason;
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 );
50 } else {
51 $this->fileReason = false;
54 return $this->fileReason;
57 /**
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;
67 /**
68 * Clear the cache of the read only file
70 public function clearCache() {
71 $this->fileReason = null;