4 * A read-only mode service which does not depend on LoadBalancer.
5 * To obtain an instance, use MediaWikiServices::getInstance()->getConfiguredReadOnlyMode().
9 class ConfiguredReadOnlyMode
{
10 /** @var string|boolean|null */
13 /** @var string|null */
17 * @param string|bool|null $reason Current reason for read-only mode, if known. null means look
18 * in $reasonFile instead.
19 * @param string|null $reasonFile A file to look in for a reason, if $reason is null. If it
20 * exists and is non-empty, its contents are treated as the reason for read-only mode.
21 * Otherwise, the wiki is not read-only.
23 public function __construct( $reason, $reasonFile = null ) {
24 if ( $reason instanceof Config
) {
25 // Before 1.34 we passed a whole Config object, which was overkill
26 wfDeprecated( __METHOD__
. ' with Config passed to constructor', '1.34' );
27 $reason = $reason->get( 'ReadOnly' );
28 $reasonFile = $reason->get( 'ReadOnlyFile' );
30 $this->reason
= $reason;
31 $this->reasonFile
= $reasonFile;
35 * Check whether the wiki is in read-only mode.
39 public function isReadOnly() {
40 return $this->getReason() !== false;
44 * Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
46 * @return string|bool String when in read-only mode; false otherwise
48 public function getReason() {
49 if ( $this->reason
!== null ) {
52 if ( $this->reasonFile
=== null ) {
55 // Try the reason file
56 if ( is_file( $this->reasonFile
) && filesize( $this->reasonFile
) > 0 ) {
57 $this->reason
= file_get_contents( $this->reasonFile
);
59 // No need to try the reason file again
60 $this->reasonFile
= null;
61 return $this->reason ??
false;
65 * Set the read-only mode, which will apply for the remainder of the
66 * request or until a service reset.
68 * @param string|null $msg
70 public function setReason( $msg ) {