Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialLockdb
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 10
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doesWrites
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 requiresWrite
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 checkExecutePermissions
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getFormFields
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 alterForm
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 onSubmit
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 onSuccess
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getDisplayFormat
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Specials;
8
9use MediaWiki\Exception\ErrorPageError;
10use MediaWiki\HTMLForm\HTMLForm;
11use MediaWiki\MainConfigNames;
12use MediaWiki\SpecialPage\FormSpecialPage;
13use MediaWiki\Status\Status;
14use MediaWiki\User\User;
15
16/**
17 * A form to make the database read-only (eg for maintenance purposes).
18 *
19 * See also @ref $wgReadOnlyFile.
20 *
21 * @ingroup SpecialPage
22 */
23class SpecialLockdb extends FormSpecialPage {
24
25    public function __construct() {
26        parent::__construct( 'Lockdb', 'siteadmin' );
27    }
28
29    /** @inheritDoc */
30    public function doesWrites() {
31        return false;
32    }
33
34    /** @inheritDoc */
35    public function requiresWrite() {
36        return false;
37    }
38
39    public function checkExecutePermissions( User $user ) {
40        parent::checkExecutePermissions( $user );
41        # If the lock file isn't writable, we can do sweet bugger all
42        if ( !is_writable( dirname( $this->getConfig()->get( MainConfigNames::ReadOnlyFile ) ) ) ) {
43            throw new ErrorPageError( 'lockdb', 'lockfilenotwritable' );
44        }
45        if ( file_exists( $this->getConfig()->get( MainConfigNames::ReadOnlyFile ) ) ) {
46            throw new ErrorPageError( 'lockdb', 'databaselocked' );
47        }
48    }
49
50    /** @inheritDoc */
51    protected function getFormFields() {
52        return [
53            'Reason' => [
54                'type' => 'textarea',
55                'rows' => 4,
56                'label-message' => 'enterlockreason',
57            ],
58            'Confirm' => [
59                'type' => 'toggle',
60                'label-message' => 'lockconfirm',
61            ],
62        ];
63    }
64
65    protected function alterForm( HTMLForm $form ) {
66        $form->setWrapperLegend( false )
67            ->setHeaderHtml( $this->msg( 'lockdbtext' )->parseAsBlock() )
68            ->setSubmitTextMsg( 'lockbtn' );
69    }
70
71    /** @inheritDoc */
72    public function onSubmit( array $data ) {
73        if ( !$data['Confirm'] ) {
74            return Status::newFatal( 'locknoconfirm' );
75        }
76
77        // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
78        $fp = @fopen( $this->getConfig()->get( MainConfigNames::ReadOnlyFile ), 'w' );
79
80        if ( $fp === false ) {
81            # This used to show a file not found error, but the likeliest reason for fopen()
82            # to fail at this point is insufficient permission to write to the file...good old
83            # is_writable() is plain wrong in some cases, it seems...
84            return Status::newFatal( 'lockfilenotwritable' );
85        }
86        fwrite( $fp, $data['Reason'] );
87        $timestamp = wfTimestampNow();
88        $contLang = $this->getContentLanguage();
89        fwrite( $fp, "\n<p>" . $this->msg( 'lockedbyandtime',
90            $this->getUser()->getName(),
91            $contLang->date( $timestamp, false, false ),
92            $contLang->time( $timestamp, false, false )
93        )->inContentLanguage()->text() . "</p>\n" );
94        fclose( $fp );
95
96        return Status::newGood();
97    }
98
99    public function onSuccess() {
100        $out = $this->getOutput();
101        $out->addSubtitle( $this->msg( 'lockdbsuccesssub' ) );
102        $out->addWikiMsg( 'lockdbsuccesstext' );
103    }
104
105    /** @inheritDoc */
106    protected function getDisplayFormat() {
107        return 'ooui';
108    }
109
110    /** @inheritDoc */
111    protected function getGroupName() {
112        return 'wiki';
113    }
114}
115
116/** @deprecated class alias since 1.41 */
117class_alias( SpecialLockdb::class, 'SpecialLockdb' );