MediaWiki  master
SpecialLockdb.php
Go to the documentation of this file.
1 <?php
30 
31  public function __construct() {
32  parent::__construct( 'Lockdb', 'siteadmin' );
33  }
34 
35  public function doesWrites() {
36  return false;
37  }
38 
39  public function requiresWrite() {
40  return false;
41  }
42 
43  public function checkExecutePermissions( User $user ) {
44  parent::checkExecutePermissions( $user );
45  # If the lock file isn't writable, we can do sweet bugger all
46  if ( !is_writable( dirname( $this->getConfig()->get( 'ReadOnlyFile' ) ) ) ) {
47  throw new ErrorPageError( 'lockdb', 'lockfilenotwritable' );
48  }
49  if ( file_exists( $this->getConfig()->get( 'ReadOnlyFile' ) ) ) {
50  throw new ErrorPageError( 'lockdb', 'databaselocked' );
51  }
52  }
53 
54  protected function getFormFields() {
55  return [
56  'Reason' => [
57  'type' => 'textarea',
58  'rows' => 4,
59  'label-message' => 'enterlockreason',
60  ],
61  'Confirm' => [
62  'type' => 'toggle',
63  'label-message' => 'lockconfirm',
64  ],
65  ];
66  }
67 
68  protected function alterForm( HTMLForm $form ) {
69  $form->setWrapperLegend( false )
70  ->setHeaderText( $this->msg( 'lockdbtext' )->parseAsBlock() )
71  ->setSubmitTextMsg( 'lockbtn' );
72  }
73 
74  public function onSubmit( array $data ) {
75  if ( !$data['Confirm'] ) {
76  return Status::newFatal( 'locknoconfirm' );
77  }
78 
79  Wikimedia\suppressWarnings();
80  $fp = fopen( $this->getConfig()->get( 'ReadOnlyFile' ), 'w' );
81  Wikimedia\restoreWarnings();
82 
83  if ( $fp === false ) {
84  # This used to show a file not found error, but the likeliest reason for fopen()
85  # to fail at this point is insufficient permission to write to the file...good old
86  # is_writable() is plain wrong in some cases, it seems...
87  return Status::newFatal( 'lockfilenotwritable' );
88  }
89  fwrite( $fp, $data['Reason'] );
90  $timestamp = wfTimestampNow();
91  $contLang = $this->getContentLanguage();
92  fwrite( $fp, "\n<p>" . $this->msg( 'lockedbyandtime',
93  $this->getUser()->getName(),
94  $contLang->date( $timestamp, false, false ),
95  $contLang->time( $timestamp, false, false )
96  )->inContentLanguage()->text() . "</p>\n" );
97  fclose( $fp );
98 
99  return Status::newGood();
100  }
101 
102  public function onSuccess() {
103  $out = $this->getOutput();
104  $out->addSubtitle( $this->msg( 'lockdbsuccesssub' ) );
105  $out->addWikiMsg( 'lockdbsuccesstext' );
106  }
107 
108  protected function getDisplayFormat() {
109  return 'ooui';
110  }
111 
112  protected function getGroupName() {
113  return 'wiki';
114  }
115 }
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:936
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:814
SpecialLockdb\__construct
__construct()
Definition: SpecialLockdb.php:31
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
SpecialPage\getName
getName()
Get the name of this Special Page.
Definition: SpecialPage.php:203
SpecialLockdb\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: SpecialLockdb.php:68
SpecialLockdb\requiresWrite
requiresWrite()
Whether this action requires the wiki not to be locked.
Definition: SpecialLockdb.php:39
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:902
SpecialLockdb\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialLockdb.php:35
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:824
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1678
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
HTMLForm\setWrapperLegend
setWrapperLegend( $legend)
Prompt the whole form to be wrapped in a "<fieldset>", with this text as its "<legend>" element.
Definition: HTMLForm.php:1754
SpecialLockdb
A form to make the database readonly (eg for maintenance purposes).
Definition: SpecialLockdb.php:29
SpecialLockdb\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: SpecialLockdb.php:102
SpecialLockdb\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: SpecialLockdb.php:54
SpecialLockdb\onSubmit
onSubmit(array $data)
Process the form on POST submission.
Definition: SpecialLockdb.php:74
SpecialLockdb\getDisplayFormat
getDisplayFormat()
Get display format for the form.
Definition: SpecialLockdb.php:108
SpecialPage\getContentLanguage
getContentLanguage()
Shortcut to get content language.
Definition: SpecialPage.php:864
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:30
SpecialLockdb\checkExecutePermissions
checkExecutePermissions(User $user)
Called from execute() to check if the given user can perform this action.
Definition: SpecialLockdb.php:43
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:67
SpecialLockdb\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialLockdb.php:112
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:149