summaryrefslogtreecommitdiff
path: root/www/wiki/includes/ConfiguredReadOnlyMode.php
blob: af7c7cbdf50a2765ab46827d83739049003a6640 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php

/**
 * A read-only mode service which does not depend on LoadBalancer.
 * To obtain an instance, use MediaWikiServices::getConfiguredReadOnlyMode().
 *
 * @since 1.29
 */
class ConfiguredReadOnlyMode {
	/** @var Config */
	private $config;

	/** @var string|bool|null */
	private $fileReason;

	/** @var string|null */
	private $overrideReason;

	public function __construct( Config $config ) {
		$this->config = $config;
	}

	/**
	 * Check whether the wiki is in read-only mode.
	 *
	 * @return bool
	 */
	public function isReadOnly() {
		return $this->getReason() !== false;
	}

	/**
	 * Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
	 *
	 * @return string|bool String when in read-only mode; false otherwise
	 */
	public function getReason() {
		if ( $this->overrideReason !== null ) {
			return $this->overrideReason;
		}
		$confReason = $this->config->get( 'ReadOnly' );
		if ( $confReason !== null ) {
			return $confReason;
		}
		if ( $this->fileReason === null ) {
			// Cache for faster access next time
			$readOnlyFile = $this->config->get( 'ReadOnlyFile' );
			if ( is_file( $readOnlyFile ) && filesize( $readOnlyFile ) > 0 ) {
				$this->fileReason = file_get_contents( $readOnlyFile );
			} else {
				$this->fileReason = false;
			}
		}
		return $this->fileReason;
	}

	/**
	 * Set the read-only mode, which will apply for the remainder of the
	 * request or until a service reset.
	 *
	 * @param string|null $msg
	 */
	public function setReason( $msg ) {
		$this->overrideReason = $msg;
	}

	/**
	 * Clear the cache of the read only file
	 */
	public function clearCache() {
		$this->fileReason = null;
	}
}