SECURITY: rate-limit and prevent blocked users from changing email
[lhc/web/wiklou.git] / includes / ConfiguredReadOnlyMode.php
1 <?php
2
3 /**
4 * A read-only mode service which does not depend on LoadBalancer.
5 * To obtain an instance, use MediaWikiServices::getInstance()->getConfiguredReadOnlyMode().
6 *
7 * @since 1.29
8 */
9 class ConfiguredReadOnlyMode {
10 /** @var string|boolean|null */
11 private $reason;
12
13 /** @var string|null */
14 private $reasonFile;
15
16 /**
17 * @param string|bool|null $reason Current reason for read-only mode, if known. null means look
18 * in $reasonFile instead.
19 * @param string|null $reasonFile A file to look in for a reason, if $reason is null. If it
20 * exists and is non-empty, its contents are treated as the reason for read-only mode.
21 * Otherwise, the wiki is not read-only.
22 */
23 public function __construct( $reason, $reasonFile = null ) {
24 if ( $reason instanceof Config ) {
25 // Before 1.34 we passed a whole Config object, which was overkill
26 wfDeprecated( __METHOD__ . ' with Config passed to constructor', '1.34' );
27 $reason = $reason->get( 'ReadOnly' );
28 $reasonFile = $reason->get( 'ReadOnlyFile' );
29 }
30 $this->reason = $reason;
31 $this->reasonFile = $reasonFile;
32 }
33
34 /**
35 * Check whether the wiki is in read-only mode.
36 *
37 * @return bool
38 */
39 public function isReadOnly() {
40 return $this->getReason() !== false;
41 }
42
43 /**
44 * Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
45 *
46 * @return string|bool String when in read-only mode; false otherwise
47 */
48 public function getReason() {
49 if ( $this->reason !== null ) {
50 return $this->reason;
51 }
52 if ( $this->reasonFile === null ) {
53 return false;
54 }
55 // Try the reason file
56 if ( is_file( $this->reasonFile ) && filesize( $this->reasonFile ) > 0 ) {
57 $this->reason = file_get_contents( $this->reasonFile );
58 }
59 // No need to try the reason file again
60 $this->reasonFile = null;
61 return $this->reason ?? false;
62 }
63
64 /**
65 * Set the read-only mode, which will apply for the remainder of the
66 * request or until a service reset.
67 *
68 * @param string|null $msg
69 */
70 public function setReason( $msg ) {
71 $this->reason = $msg;
72 }
73 }