MediaWiki master
SpecialLockdb.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Specials;
25
32use Wikimedia\AtEase\AtEase;
33
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
128class_alias( SpecialLockdb::class, 'SpecialLockdb' );
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
An error page which can definitely be safely rendered using the OutputPage.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:206
setWrapperLegend( $legend)
Prompt the whole form to be wrapped in a "<fieldset>", with this text as its "<legend>" element.
A class containing constants representing the names of configuration variables.
const ReadOnlyFile
Name constant for the ReadOnlyFile setting, for use with Config::get()
Special page which uses an HTMLForm to handle processing.
getUser()
Shortcut to get the User executing this instance.
getConfig()
Shortcut to get main config object.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getContentLanguage()
Shortcut to get content language.
getName()
Get the canonical, unlocalized name of this special page without namespace.
A form to make the database readonly (eg for maintenance purposes).
checkExecutePermissions(User $user)
Called from execute() to check if the given user can perform this action.
getDisplayFormat()
Get display format for the form.
getFormFields()
Get an HTMLForm descriptor array.
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
requiresWrite()
Whether this action requires the wiki not to be locked, default to requiresPost()
onSubmit(array $data)
Process the form on submission.
doesWrites()
Indicates whether this special page may perform database writes.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
internal since 1.36
Definition User.php:93
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...