Merge "Use Xml::element instead of Html::element for empty elements"
[lhc/web/wiklou.git] / includes / specials / SpecialUnlockdb.php
index fe38a48..a8b97d7 100644 (file)
 <?php
 /**
+ * Implements Special:Unlockdb
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup SpecialPage
  */
 
 /**
+ * Implements Special:Unlockdb
  *
+ * @ingroup SpecialPage
  */
-function wfSpecialUnlockdb() {
-       global $wgUser, $wgOut, $wgRequest;
+class SpecialUnlockdb extends FormSpecialPage {
 
-       if( !$wgUser->isAllowed( 'siteadmin' ) ) {
-               $wgOut->permissionRequired( 'siteadmin' );
-               return;
+       public function __construct() {
+               parent::__construct( 'Unlockdb', 'siteadmin' );
        }
 
-       $action = $wgRequest->getVal( 'action' );
-       $f = new DBUnlockForm();
-
-       if ( "success" == $action ) {
-               $f->showSuccess();
-       } else if ( "submit" == $action && $wgRequest->wasPosted() &&
-               $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
-               $f->doSubmit();
-       } else {
-               $f->showForm( "" );
+       public function requiresWrite() {
+               return false;
        }
-}
-
-/**
- * @ingroup SpecialPage
- */
-class DBUnlockForm {
-       function showForm( $err )
-       {
-               global $wgOut, $wgUser;
 
-               global $wgReadOnlyFile;
-               if( !file_exists( $wgReadOnlyFile ) ) {
-                       $wgOut->addWikiMsg( 'databasenotlocked' );
-                       return;
+       public function checkExecutePermissions( User $user ) {
+               parent::checkExecutePermissions( $user );
+               # If the lock file isn't writable, we can do sweet bugger all
+               if ( !file_exists( $this->getConfig()->get( 'ReadOnlyFile' ) ) ) {
+                       throw new ErrorPageError( 'lockdb', 'databasenotlocked' );
                }
+       }
 
-               $wgOut->setPagetitle( wfMsg( "unlockdb" ) );
-               $wgOut->addWikiMsg( "unlockdbtext" );
-
-               if ( $err != "" ) {
-                       $wgOut->setSubtitle( wfMsg( "formerror" ) );
-                       $wgOut->addHTML( '<p class="error">' . htmlspecialchars( $err ) . "</p>\n" );
-               }
-               $lc = htmlspecialchars( wfMsg( "unlockconfirm" ) );
-               $lb = htmlspecialchars( wfMsg( "unlockbtn" ) );
-               $titleObj = SpecialPage::getTitleFor( "Unlockdb" );
-               $action = $titleObj->escapeLocalURL( "action=submit" );
-               $token = htmlspecialchars( $wgUser->editToken() );
-
-               $wgOut->addHTML( <<<HTML
-
-<form id="unlockdb" method="post" action="{$action}">
-<table border="0">
-       <tr>
-               <td align="right">
-                       <input type="checkbox" name="wpLockConfirm" />
-               </td>
-               <td align="left">{$lc}</td>
-       </tr>
-       <tr>
-               <td>&nbsp;</td>
-               <td align="left">
-                       <input type="submit" name="wpLock" value="{$lb}" />
-               </td>
-       </tr>
-</table>
-<input type="hidden" name="wpEditToken" value="{$token}" />
-</form>
-HTML
-);
-
+       protected function getFormFields() {
+               return array(
+                       'Confirm' => array(
+                               'type' => 'toggle',
+                               'label-message' => 'unlockconfirm',
+                       ),
+               );
        }
 
-       function doSubmit() {
-               global $wgOut, $wgRequest, $wgReadOnlyFile;
+       protected function alterForm( HTMLForm $form ) {
+               $form->setWrapperLegend( false );
+               $form->setHeaderText( $this->msg( 'unlockdbtext' )->parseAsBlock() );
+               $form->setSubmitTextMsg( 'unlockbtn' );
+       }
 
-               $wpLockConfirm = $wgRequest->getCheck( 'wpLockConfirm' );
-               if ( ! $wpLockConfirm ) {
-                       $this->showForm( wfMsg( "locknoconfirm" ) );
-                       return;
+       public function onSubmit( array $data ) {
+               if ( !$data['Confirm'] ) {
+                       return Status::newFatal( 'locknoconfirm' );
                }
-               if ( @! unlink( $wgReadOnlyFile ) ) {
-                       $wgOut->showFileDeleteError( $wgReadOnlyFile );
-                       return;
+
+               $readOnlyFile = $this->getConfig()->get( 'ReadOnlyFile' );
+               wfSuppressWarnings();
+               $res = unlink( $readOnlyFile );
+               wfRestoreWarnings();
+
+               if ( $res ) {
+                       return Status::newGood();
+               } else {
+                       return Status::newFatal( 'filedeleteerror', $readOnlyFile );
                }
-               $titleObj = SpecialPage::getTitleFor( "Unlockdb" );
-               $success = $titleObj->getFullURL( "action=success" );
-               $wgOut->redirect( $success );
        }
 
-       function showSuccess() {
-               global $wgOut;
+       public function onSuccess() {
+               $out = $this->getOutput();
+               $out->addSubtitle( $this->msg( 'unlockdbsuccesssub' ) );
+               $out->addWikiMsg( 'unlockdbsuccesstext' );
+       }
 
-               $wgOut->setPagetitle( wfMsg( "unlockdb" ) );
-               $wgOut->setSubtitle( wfMsg( "unlockdbsuccesssub" ) );
-               $wgOut->addWikiMsg( "unlockdbsuccesstext" );
+       protected function getGroupName() {
+               return 'wiki';
        }
 }