Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
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 / 44
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 / 17
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 * Implements Special:Lockdb
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24namespace MediaWiki\Specials;
25
26use ErrorPageError;
27use MediaWiki\HTMLForm\HTMLForm;
28use MediaWiki\MainConfigNames;
29use MediaWiki\SpecialPage\FormSpecialPage;
30use MediaWiki\Status\Status;
31use MediaWiki\User\User;
32use Wikimedia\AtEase\AtEase;
33
34/**
35 * A form to make the database readonly (eg for maintenance purposes).
36 *
37 * @ingroup SpecialPage
38 */
39class SpecialLockdb extends FormSpecialPage {
40
41    public function __construct() {
42        parent::__construct( 'Lockdb', 'siteadmin' );
43    }
44
45    public function doesWrites() {
46        return false;
47    }
48
49    public function requiresWrite() {
50        return false;
51    }
52
53    public function checkExecutePermissions( User $user ) {
54        parent::checkExecutePermissions( $user );
55        # If the lock file isn't writable, we can do sweet bugger all
56        if ( !is_writable( dirname( $this->getConfig()->get( MainConfigNames::ReadOnlyFile ) ) ) ) {
57            throw new ErrorPageError( 'lockdb', 'lockfilenotwritable' );
58        }
59        if ( file_exists( $this->getConfig()->get( MainConfigNames::ReadOnlyFile ) ) ) {
60            throw new ErrorPageError( 'lockdb', 'databaselocked' );
61        }
62    }
63
64    protected function getFormFields() {
65        return [
66            'Reason' => [
67                'type' => 'textarea',
68                'rows' => 4,
69                'label-message' => 'enterlockreason',
70            ],
71            'Confirm' => [
72                'type' => 'toggle',
73                'label-message' => 'lockconfirm',
74            ],
75        ];
76    }
77
78    protected function alterForm( HTMLForm $form ) {
79        $form->setWrapperLegend( false )
80            ->setHeaderHtml( $this->msg( 'lockdbtext' )->parseAsBlock() )
81            ->setSubmitTextMsg( 'lockbtn' );
82    }
83
84    public function onSubmit( array $data ) {
85        if ( !$data['Confirm'] ) {
86            return Status::newFatal( 'locknoconfirm' );
87        }
88
89        AtEase::suppressWarnings();
90        $fp = fopen( $this->getConfig()->get( MainConfigNames::ReadOnlyFile ), 'w' );
91        AtEase::restoreWarnings();
92
93        if ( $fp === false ) {
94            # This used to show a file not found error, but the likeliest reason for fopen()
95            # to fail at this point is insufficient permission to write to the file...good old
96            # is_writable() is plain wrong in some cases, it seems...
97            return Status::newFatal( 'lockfilenotwritable' );
98        }
99        fwrite( $fp, $data['Reason'] );
100        $timestamp = wfTimestampNow();
101        $contLang = $this->getContentLanguage();
102        fwrite( $fp, "\n<p>" . $this->msg( 'lockedbyandtime',
103            $this->getUser()->getName(),
104            $contLang->date( $timestamp, false, false ),
105            $contLang->time( $timestamp, false, false )
106        )->inContentLanguage()->text() . "</p>\n" );
107        fclose( $fp );
108
109        return Status::newGood();
110    }
111
112    public function onSuccess() {
113        $out = $this->getOutput();
114        $out->addSubtitle( $this->msg( 'lockdbsuccesssub' ) );
115        $out->addWikiMsg( 'lockdbsuccesstext' );
116    }
117
118    protected function getDisplayFormat() {
119        return 'ooui';
120    }
121
122    protected function getGroupName() {
123        return 'wiki';
124    }
125}
126
127/** @deprecated class alias since 1.41 */
128class_alias( SpecialLockdb::class, 'SpecialLockdb' );