]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/ReadOnlyMode.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.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 }