]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/ConfiguredReadOnlyMode.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.git] / includes / ConfiguredReadOnlyMode.php
1 <?php
2
3 /**
4  * A read-only mode service which does not depend on LoadBalancer.
5  * To obtain an instance, use MediaWikiServices::getConfiguredReadOnlyMode().
6  *
7  * @since 1.29
8  */
9 class ConfiguredReadOnlyMode {
10         /** @var Config */
11         private $config;
12
13         /** @var string|bool|null */
14         private $fileReason;
15
16         /** @var string|null */
17         private $overrideReason;
18
19         public function __construct( Config $config ) {
20                 $this->config = $config;
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          * Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
34          *
35          * @return string|bool String when in read-only mode; false otherwise
36          */
37         public function getReason() {
38                 if ( $this->overrideReason !== null ) {
39                         return $this->overrideReason;
40                 }
41                 $confReason = $this->config->get( 'ReadOnly' );
42                 if ( $confReason !== null ) {
43                         return $confReason;
44                 }
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;
52                         }
53                 }
54                 return $this->fileReason;
55         }
56
57         /**
58          * Set the read-only mode, which will apply for the remainder of the
59          * request or until a service reset.
60          *
61          * @param string|null $msg
62          */
63         public function setReason( $msg ) {
64                 $this->overrideReason = $msg;
65         }
66
67         /**
68          * Clear the cache of the read only file
69          */
70         public function clearCache() {
71                 $this->fileReason = null;
72         }
73 }