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 private $configuredReadOnly;
13 private $loadBalancer;
15 public function __construct( ConfiguredReadOnlyMode
$cro, LoadBalancer
$loadBalancer ) {
16 $this->configuredReadOnly
= $cro;
17 $this->loadBalancer
= $loadBalancer;
21 * Check whether the wiki is in read-only mode.
25 public function isReadOnly() {
26 return $this->getReason() !== false;
30 * Check if the site is in read-only mode and return the message if so
32 * This checks the configuration and registered DB load balancers for
33 * read-only mode. This may result in DB connection being made.
35 * @return string|bool String when in read-only mode; false otherwise
37 public function getReason() {
38 $reason = $this->configuredReadOnly
->getReason();
39 if ( $reason !== false ) {
42 $reason = $this->loadBalancer
->getReadOnlyReason();
43 if ( $reason !== false && $reason !== null ) {
50 * Set the read-only mode, which will apply for the remainder of the
51 * request or until a service reset.
53 public function setReason( $msg ) {
54 $this->configuredReadOnly
->setReason( $msg );
58 * Clear the cache of the read only file
60 public function clearCache() {
61 $this->configuredReadOnly
->clearCache();
66 * A read-only mode service which does not depend on LoadBalancer.
67 * To obtain an instance, use MediaWikiServices::getConfiguredReadOnlyMode().
71 class ConfiguredReadOnlyMode
{
74 private $overrideReason;
76 public function __construct( Config
$config ) {
77 $this->config
= $config;
81 * Check whether the wiki is in read-only mode.
85 public function isReadOnly() {
86 return $this->getReason() !== false;
90 * Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
92 * @return string|bool String when in read-only mode; false otherwise
94 public function getReason() {
95 if ( $this->overrideReason
!== null ) {
96 return $this->overrideReason
;
98 $confReason = $this->config
->get( 'ReadOnly' );
99 if ( $confReason !== null ) {
102 if ( $this->fileReason
=== null ) {
103 // Cache for faster access next time
104 $readOnlyFile = $this->config
->get( 'ReadOnlyFile' );
105 if ( is_file( $readOnlyFile ) && filesize( $readOnlyFile ) > 0 ) {
106 $this->fileReason
= file_get_contents( $readOnlyFile );
108 $this->fileReason
= false;
111 return $this->fileReason
;
115 * Set the read-only mode, which will apply for the remainder of the
116 * request or until a service reset.
118 public function setReason( $msg ) {
119 $this->overrideReason
= $msg;
123 * Clear the cache of the read only file
125 public function clearCache() {
126 $this->fileReason
= null;