Debian Codegraph @

includes/libs/rdbms/ReadOnlyMode.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Rdbms;
4
5 /**
6 * Determine whether a site is currently in read-only mode.
7 *
8 * To obtain an instance, use \MediaWiki\MediaWikiServices::getReadOnlyMode().
9 *
10 * @since 1.29
11 */
12 class ReadOnlyMode {
13 /** @var ConfiguredReadOnlyMode */
14 private $configuredReadOnly;
15
16 /** @var ILBFactory */
17 private $lbFactory;
18
19 /**
20 * @internal For ServiceWiring only
21 * @param ConfiguredReadOnlyMode $cro
22 * @param ILBFactory $lbFactory
23 */
24 public function __construct( ConfiguredReadOnlyMode $cro, ILBFactory $lbFactory ) {
25 $this->configuredReadOnly = $cro;
26 $this->lbFactory = $lbFactory;
27 }
28
29 /**
30 * Check whether the site is in read-only mode.
31 *
32 * @param string|false $domain Domain ID, or false for the current domain
33 * @return bool
34 */
35 public function isReadOnly( $domain = false ): bool {
36 return $this->getReason( $domain ) !== false;
37 }
38
39 /**
40 * Check if the site is in read-only mode and return the message if so
41 *
42 * This checks both statically configured read-only mode, and (cached)
43 * whether the primary database host accepting writes.
44 *
45 * Calling this may result in database connection.
46 *
47 * This method accepts virtual domains
48 * ({@see \MediaWiki\MainConfigSchema::VirtualDomainsMapping}).
49 *
50 * @param string|false $domain Domain ID, or false for the current domain
51 * @return string|false String when in read-only mode; false otherwise
52 */
53 public function getReason( $domain = false ) {
54 $reason = $this->configuredReadOnly->getReason();
55 if ( $reason !== false ) {
56 return $reason;
57 }
58 $reason = $this->lbFactory->getLoadBalancer( $domain )->getReadOnlyReason();
59 return $reason ?? false;
60 }
61
62 /**
63 * @since 1.41
64 * @return string|false String when site is configured to be in read-only mode; false otherwise
65 */
66 public function getConfiguredReason() {
67 return $this->configuredReadOnly->getReason();
68 }
69
70 /**
71 * Check whether the site is configured to be in read-only mode.
72 *
73 * @since 1.41
74 * @return bool
75 */
76 public function isConfiguredReadOnly() {
77 return $this->configuredReadOnly->getReason() !== false;
78 }
79
80 /**
81 * Override the read-only mode, which will apply for the remainder of the
82 * request or until a service reset.
83 *
84 * @param string|false|null $msg
85 */
86 public function setReason( $msg ): void {
87 $this->configuredReadOnly->setReason( $msg );
88 }
89
90 }
91
92 /** @deprecated class alias since 1.41 */
93 class_alias( ReadOnlyMode::class, 'ReadOnlyMode' );