Update git submodules
[mediawiki.git] / includes / libs / rdbms / ReadOnlyMode.php
blob6068abb6798aa66cf9e6aa5382741c97202b5ce4
1 <?php
3 namespace Wikimedia\Rdbms;
5 /**
6 * Determine whether a site is currently in read-only mode.
8 * To obtain an instance, use \MediaWiki\MediaWikiServices::getReadOnlyMode().
10 * @since 1.29
12 class ReadOnlyMode {
13 /** @var ConfiguredReadOnlyMode */
14 private $configuredReadOnly;
16 /** @var ILBFactory */
17 private $lbFactory;
19 /**
20 * @internal For ServiceWiring only
21 * @param ConfiguredReadOnlyMode $cro
22 * @param ILBFactory $lbFactory
24 public function __construct( ConfiguredReadOnlyMode $cro, ILBFactory $lbFactory ) {
25 $this->configuredReadOnly = $cro;
26 $this->lbFactory = $lbFactory;
29 /**
30 * Check whether the site is in read-only mode.
32 * @param string|false $domain Domain ID, or false for the current domain
33 * @return bool
35 public function isReadOnly( $domain = false ): bool {
36 return $this->getReason( $domain ) !== false;
39 /**
40 * Check if the site is in read-only mode and return the message if so
42 * This checks both statically configured read-only mode, and (cached)
43 * whether the primary database host accepting writes.
45 * Calling this may result in database connection.
47 * @param string|false $domain Domain ID, or false for the current domain
48 * @return string|false String when in read-only mode; false otherwise
50 public function getReason( $domain = false ) {
51 $reason = $this->configuredReadOnly->getReason();
52 if ( $reason !== false ) {
53 return $reason;
55 $reason = $this->lbFactory->getMainLB( $domain )->getReadOnlyReason();
56 return $reason ?? false;
59 /**
60 * @since 1.41
61 * @return string|false String when site is configured to be in read-only mode; false otherwise
63 public function getConfiguredReason() {
64 return $this->configuredReadOnly->getReason();
67 /**
68 * Check whether the site is configured to be in read-only mode.
70 * @since 1.41
71 * @return bool
73 public function isConfiguredReadOnly() {
74 return $this->configuredReadOnly->getReason() !== false;
77 /**
78 * Override the read-only mode, which will apply for the remainder of the
79 * request or until a service reset.
81 * @param string|false|null $msg
83 public function setReason( $msg ): void {
84 $this->configuredReadOnly->setReason( $msg );
89 /**
90 * @deprecated since 1.41
92 class_alias( ReadOnlyMode::class, 'ReadOnlyMode' );