A service for read-only mode
[lhc/web/wiklou.git] / includes / ReadOnlyMode.php
1 <?php
2
3 use Wikimedia\Rdbms\LoadBalancer;
4
5 /**
6 * A service class for fetching the wiki's current read-only mode.
7 * To obtain an instance, use MediaWikiServices::getReadOnlyMode().
8 *
9 * @since 1.29
10 */
11 class ReadOnlyMode {
12 private $configuredReadOnly;
13 private $loadBalancer;
14
15 public function __construct( ConfiguredReadOnlyMode $cro, LoadBalancer $loadBalancer ) {
16 $this->configuredReadOnly = $cro;
17 $this->loadBalancer = $loadBalancer;
18 }
19
20 /**
21 * Check whether the wiki is in read-only mode.
22 *
23 * @return bool
24 */
25 public function isReadOnly() {
26 return $this->getReason() !== false;
27 }
28
29 /**
30 * Check if the site is in read-only mode and return the message if so
31 *
32 * This checks the configuration and registered DB load balancers for
33 * read-only mode. This may result in DB connection being made.
34 *
35 * @return string|bool String when in read-only mode; false otherwise
36 */
37 public function getReason() {
38 $reason = $this->configuredReadOnly->getReason();
39 if ( $reason !== false ) {
40 return $reason;
41 }
42 $reason = $this->loadBalancer->getReadOnlyReason();
43 if ( $reason !== false && $reason !== null ) {
44 return $reason;
45 }
46 return false;
47 }
48
49 /**
50 * Set the read-only mode, which will apply for the remainder of the
51 * request or until a service reset.
52 */
53 public function setReason( $msg ) {
54 $this->configuredReadOnly->setReason( $msg );
55 }
56
57 /**
58 * Clear the cache of the read only file
59 */
60 public function clearCache() {
61 $this->configuredReadOnly->clearCache();
62 }
63 }
64
65 /**
66 * A read-only mode service which does not depend on LoadBalancer.
67 * To obtain an instance, use MediaWikiServices::getConfiguredReadOnlyMode().
68 *
69 * @since 1.29
70 */
71 class ConfiguredReadOnlyMode {
72 private $config;
73 private $fileReason;
74 private $overrideReason;
75
76 public function __construct( Config $config ) {
77 $this->config = $config;
78 }
79
80 /**
81 * Check whether the wiki is in read-only mode.
82 *
83 * @return bool
84 */
85 public function isReadOnly() {
86 return $this->getReason() !== false;
87 }
88
89 /**
90 * Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
91 *
92 * @return string|bool String when in read-only mode; false otherwise
93 */
94 public function getReason() {
95 if ( $this->overrideReason !== null ) {
96 return $this->overrideReason;
97 }
98 $confReason = $this->config->get( 'ReadOnly' );
99 if ( $confReason !== null ) {
100 return $confReason;
101 }
102 if ( $this->fileReason === null ) {
103 // Cache for faster access next time
104 $readOnlyFile = $this->config->get( 'ReadOnlyFile' );
105 if ( is_file( $readOnlyFile ) && filesize( $readOnlyFile ) > 0 ) {
106 $this->fileReason = file_get_contents( $readOnlyFile );
107 } else {
108 $this->fileReason = false;
109 }
110 }
111 return $this->fileReason;
112 }
113
114 /**
115 * Set the read-only mode, which will apply for the remainder of the
116 * request or until a service reset.
117 */
118 public function setReason( $msg ) {
119 $this->overrideReason = $msg;
120 }
121
122 /**
123 * Clear the cache of the read only file
124 */
125 public function clearCache() {
126 $this->fileReason = null;
127 }
128 }