summaryrefslogtreecommitdiff
path: root/www/wiki/includes/ReadOnlyMode.php
blob: 547c2d5efed1e1559525d7c5e5f60940c45ffa62 (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
<?php

use Wikimedia\Rdbms\LoadBalancer;

/**
 * A service class for fetching the wiki's current read-only mode.
 * To obtain an instance, use MediaWikiServices::getReadOnlyMode().
 *
 * @since 1.29
 */
class ReadOnlyMode {
	/** @var ConfiguredReadOnlyMode */
	private $configuredReadOnly;

	/** @var LoadBalancer */
	private $loadBalancer;

	public function __construct( ConfiguredReadOnlyMode $cro, LoadBalancer $loadBalancer ) {
		$this->configuredReadOnly = $cro;
		$this->loadBalancer = $loadBalancer;
	}

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

	/**
	 * Check if the site is in read-only mode and return the message if so
	 *
	 * This checks the configuration and registered DB load balancers for
	 * read-only mode. This may result in DB connection being made.
	 *
	 * @return string|bool String when in read-only mode; false otherwise
	 */
	public function getReason() {
		$reason = $this->configuredReadOnly->getReason();
		if ( $reason !== false ) {
			return $reason;
		}
		$reason = $this->loadBalancer->getReadOnlyReason();
		if ( $reason !== false && $reason !== null ) {
			return $reason;
		}
		return false;
	}

	/**
	 * 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->configuredReadOnly->setReason( $msg );
	}

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