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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
<?php
namespace Wikimedia\Rdbms;
/**
* Determine whether a site is currently in read-only mode.
*
* To obtain an instance, use \MediaWiki\MediaWikiServices::getReadOnlyMode().
*
* @since 1.29
*/
class ReadOnlyMode {
/** @var ConfiguredReadOnlyMode */
private $configuredReadOnly;
/** @var ILBFactory */
private $lbFactory;
/**
* @internal For ServiceWiring only
* @param ConfiguredReadOnlyMode $cro
* @param ILBFactory $lbFactory
*/
public function __construct( ConfiguredReadOnlyMode $cro, ILBFactory $lbFactory ) {
$this->configuredReadOnly = $cro;
$this->lbFactory = $lbFactory;
}
/**
* Check whether the site is in read-only mode.
*
* @param string|false $domain Domain ID, or false for the current domain
* @return bool
*/
public function isReadOnly( $domain = false ): bool {
return $this->getReason( $domain ) !== false;
}
/**
* Check if the site is in read-only mode and return the message if so
*
* This checks both statically configured read-only mode, and (cached)
* whether the primary database host accepting writes.
*
* Calling this may result in database connection.
*
* This method accepts virtual domains
* ({@see \MediaWiki\MainConfigSchema::VirtualDomainsMapping}).
*
* @param string|false $domain Domain ID, or false for the current domain
* @return string|false String when in read-only mode; false otherwise
*/
public function getReason( $domain = false ) {
$reason = $this->configuredReadOnly->getReason();
if ( $reason !== false ) {
return $reason;
}
$reason = $this->lbFactory->getLoadBalancer( $domain )->getReadOnlyReason();
return $reason ?? false;
}
/**
* @since 1.41
* @return string|false String when site is configured to be in read-only mode; false otherwise
*/
public function getConfiguredReason() {
return $this->configuredReadOnly->getReason();
}
/**
* Check whether the site is configured to be in read-only mode.
*
* @since 1.41
* @return bool
*/
public function isConfiguredReadOnly() {
return $this->configuredReadOnly->getReason() !== false;
}
/**
* Override the read-only mode, which will apply for the remainder of the
* request or until a service reset.
*
* @param string|false|null $msg
*/
public function setReason( $msg ): void {
$this->configuredReadOnly->setReason( $msg );
}
}
/** @deprecated class alias since 1.41 */
class_alias( ReadOnlyMode::class, 'ReadOnlyMode' );
|