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