* @since 1.29
*/
class ConfiguredReadOnlyMode {
- /** @var Config */
- private $config;
-
- /** @var string|bool|null */
- private $fileReason;
+ /** @var string|boolean|null */
+ private $reason;
/** @var string|null */
- private $overrideReason;
+ private $reasonFile;
- public function __construct( Config $config ) {
- $this->config = $config;
+ /**
+ * @param string|bool|null $reason Current reason for read-only mode, if known. null means look
+ * in $reasonFile instead.
+ * @param string|null $reasonFile A file to look in for a reason, if $reason is null. If it
+ * exists and is non-empty, its contents are treated as the reason for read-only mode.
+ * Otherwise, the wiki is not read-only.
+ */
+ public function __construct( $reason, $reasonFile = null ) {
+ if ( $reason instanceof Config ) {
+ // Before 1.34 we passed a whole Config object, which was overkill
+ wfDeprecated( __METHOD__ . ' with Config passed to constructor', '1.34' );
+ $reason = $reason->get( 'ReadOnly' );
+ $reasonFile = $reason->get( 'ReadOnlyFile' );
+ }
+ $this->reason = $reason;
+ $this->reasonFile = $reasonFile;
}
/**
* @return string|bool String when in read-only mode; false otherwise
*/
public function getReason() {
- if ( $this->overrideReason !== null ) {
- return $this->overrideReason;
+ if ( $this->reason !== null ) {
+ return $this->reason;
}
- $confReason = $this->config->get( 'ReadOnly' );
- if ( $confReason !== null ) {
- return $confReason;
+ if ( $this->reasonFile === null ) {
+ return false;
}
- 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;
- }
+ // Try the reason file
+ if ( is_file( $this->reasonFile ) && filesize( $this->reasonFile ) > 0 ) {
+ $this->reason = file_get_contents( $this->reasonFile );
}
- return $this->fileReason;
+ // No need to try the reason file again
+ $this->reasonFile = null;
+ return $this->reason ?? false;
}
/**
* @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;
+ $this->reason = $msg;
}
}