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    /** @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     * @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->getMainLB( $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' );