r100723 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100722‎ | r100723 | r100724 >
Date:18:20, 25 October 2011
Author:ialex
Status:resolved (Comments)
Tags:
Comment:
Port Special:Lockdb and Special:Unlockdb to HTMLForm using FormSpecialPage
Modified paths:
  • /trunk/phase3/includes/specials/SpecialLockdb.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialUnlockdb.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialLockdb.php
@@ -26,88 +26,53 @@
2727 *
2828 * @ingroup SpecialPage
2929 */
30 -class SpecialLockdb extends SpecialPage {
 30+class SpecialLockdb extends FormSpecialPage {
3131 var $reason = '';
3232
3333 public function __construct() {
3434 parent::__construct( 'Lockdb', 'siteadmin' );
3535 }
3636
37 - public function execute( $par ) {
38 - $this->setHeaders();
 37+ public function requiresWrite() {
 38+ return false;
 39+ }
3940
40 - # Permission check
41 - if( !$this->userCanExecute( $this->getUser() ) ) {
42 - $this->displayRestrictionError();
43 - return;
44 - }
 41+ public function userCanExecute( User $user ) {
 42+ parent::userCanExecute( $user );
4543
46 - $this->outputHeader();
47 -
4844 # If the lock file isn't writable, we can do sweet bugger all
4945 global $wgReadOnlyFile;
50 - if( !is_writable( dirname( $wgReadOnlyFile ) ) ) {
51 - $this->getOutput()->addWikiMsg( 'lockfilenotwritable' );
52 - return;
 46+ if ( !is_writable( dirname( $wgReadOnlyFile ) ) ) {
 47+ throw new ErrorPageError( 'lockdb', 'lockfilenotwritable' );
5348 }
54 -
55 - $request = $this->getRequest();
56 - $action = $request->getVal( 'action' );
57 - $this->reason = $request->getVal( 'wpLockReason', '' );
58 -
59 - if ( $action == 'success' ) {
60 - $this->showSuccess();
61 - } elseif ( $action == 'submit' && $request->wasPosted() &&
62 - $this->getUser()->matchEditToken( $request->getVal( 'wpEditToken' ) ) ) {
63 - $this->doSubmit();
64 - } else {
65 - $this->showForm();
66 - }
6749 }
6850
69 - private function showForm( $err = '' ) {
70 - $out = $this->getOutput();
71 - $out->addWikiMsg( 'lockdbtext' );
72 -
73 - if ( $err != '' ) {
74 - $out->setSubtitle( wfMsg( 'formerror' ) );
75 - $out->addHTML( '<p class="error">' . htmlspecialchars( $err ) . "</p>\n" );
76 - }
77 -
78 - $out->addHTML(
79 - Html::openElement( 'form', array( 'id' => 'lockdb', 'method' => 'POST',
80 - 'action' => $this->getTitle()->getLocalURL( 'action=submit' ) ) ). "\n" .
81 - wfMsgHtml( 'enterlockreason' ) . ":\n" .
82 - Html::textarea( 'wpLockReason', $this->reason, array( 'rows' => 4 ) ). "
83 -<table>
84 - <tr>
85 - " . Html::openElement( 'td', array( 'style' => 'text-align:right' ) ) . "
86 - " . Html::input( 'wpLockConfirm', null, 'checkbox', array( 'id' => 'mw-input-wplockconfirm' ) ) . "
87 - </td>
88 - " . Html::openElement( 'td', array( 'style' => 'text-align:left' ) ) .
89 - Html::openElement( 'label', array( 'for' => 'mw-input-wplockconfirm' ) ) .
90 - wfMsgHtml( 'lockconfirm' ) . "</label>
91 - </td>
92 - </tr>
93 - <tr>
94 - <td>&#160;</td>
95 - " . Html::openElement( 'td', array( 'style' => 'text-align:left' ) ) . "
96 - " . Html::input( 'wpLock', wfMsg( 'lockbtn' ), 'submit' ) . "
97 - </td>
98 - </tr>
99 -</table>\n" .
100 - Html::hidden( 'wpEditToken', $this->getUser()->editToken() ) . "\n" .
101 - Html::closeElement( 'form' )
 51+ protected function getFormFields() {
 52+ return array(
 53+ 'Reason' => array(
 54+ 'type' => 'textarea',
 55+ 'rows' => 4,
 56+ 'vertical-label' => true,
 57+ 'label-message' => 'enterlockreason',
 58+ ),
 59+ 'Confirm' => array(
 60+ 'type' => 'toggle',
 61+ 'label-message' => 'lockconfirm',
 62+ ),
10263 );
 64+ }
10365
 66+ protected function alterForm( HTMLForm $form ) {
 67+ $form->setWrapperLegend( false );
 68+ $form->setHeaderText( $this->msg( 'lockdbtext' )->parseAsBlock() );
 69+ $form->setSubmitTextMsg( 'lockbtn' );
10470 }
10571
106 - private function doSubmit() {
 72+ public function onSubmit( array $data ) {
10773 global $wgContLang, $wgReadOnlyFile;
10874
109 - if ( !$this->getRequest()->getCheck( 'wpLockConfirm' ) ) {
110 - $this->showForm( wfMsg( 'locknoconfirm' ) );
111 - return;
 75+ if ( !$data['Confirm'] ) {
 76+ return Status::newFatal( 'locknoconfirm' );
11277 }
11378
11479 wfSuppressWarnings();
@@ -118,10 +83,9 @@
11984 # This used to show a file not found error, but the likeliest reason for fopen()
12085 # to fail at this point is insufficient permission to write to the file...good old
12186 # is_writable() is plain wrong in some cases, it seems...
122 - $this->getOutput()->addWikiMsg( 'lockfilenotwritable' );
123 - return;
 87+ return Status::newFatal( 'lockfilenotwritable' );
12488 }
125 - fwrite( $fp, $this->reason );
 89+ fwrite( $fp, $data['Reason'] );
12690 $timestamp = wfTimestampNow();
12791 fwrite( $fp, "\n<p>" . wfMsgExt(
12892 'lockedbyandtime',
@@ -132,12 +96,11 @@
13397 ) . "</p>\n" );
13498 fclose( $fp );
13599
136 - $this->getOutput()->redirect( $this->getTitle()->getFullURL( 'action=success' ) );
 100+ return Status::newGood();
137101 }
138102
139 - private function showSuccess() {
 103+ public function onSuccess() {
140104 $out = $this->getOutput();
141 - $out->setPagetitle( wfMsg( 'lockdb' ) );
142105 $out->setSubtitle( wfMsg( 'lockdbsuccesssub' ) );
143106 $out->addWikiMsg( 'lockdbsuccesstext' );
144107 }
Index: trunk/phase3/includes/specials/SpecialUnlockdb.php
@@ -26,85 +26,46 @@
2727 *
2828 * @ingroup SpecialPage
2929 */
30 -class SpecialUnlockdb extends SpecialPage {
 30+class SpecialUnlockdb extends FormSpecialPage {
3131
3232 public function __construct() {
3333 parent::__construct( 'Unlockdb', 'siteadmin' );
3434 }
3535
36 - public function execute( $par ) {
37 - $this->setHeaders();
 36+ public function requiresWrite() {
 37+ return false;
 38+ }
3839
39 - # Permission check
40 - if( !$this->userCanExecute( $this->getUser() ) ) {
41 - $this->displayRestrictionError();
42 - return;
43 - }
 40+ public function userCanExecute( User $user ) {
 41+ parent::userCanExecute( $user );
4442
45 - $this->outputHeader();
46 -
47 - $request = $this->getRequest();
48 - $action = $request->getVal( 'action' );
49 -
50 - if ( $action == 'success' ) {
51 - $this->showSuccess();
52 - } elseif ( $action == 'submit' && $request->wasPosted() &&
53 - $this->getUser()->matchEditToken( $request->getVal( 'wpEditToken' ) ) ) {
54 - $this->doSubmit();
55 - } else {
56 - $this->showForm();
 43+ # If the lock file isn't writable, we can do sweet bugger all
 44+ global $wgReadOnlyFile;
 45+ if ( !file_exists( $wgReadOnlyFile ) ) {
 46+ throw new ErrorPageError( 'lockdb', 'databasenotlocked' );
5747 }
5848 }
5949
60 - private function showForm( $err = '' ) {
61 - global $wgReadOnlyFile;
62 -
63 - $out = $this->getOutput();
64 -
65 - if( !file_exists( $wgReadOnlyFile ) ) {
66 - $out->addWikiMsg( 'databasenotlocked' );
67 - return;
68 - }
69 -
70 - $out->addWikiMsg( 'unlockdbtext' );
71 -
72 - if ( $err != '' ) {
73 - $out->setSubtitle( wfMsg( 'formerror' ) );
74 - $out->addHTML( '<p class="error">' . htmlspecialchars( $err ) . "</p>\n" );
75 - }
76 -
77 - $out->addHTML(
78 - Html::openElement( 'form', array( 'id' => 'unlockdb', 'method' => 'POST',
79 - 'action' => $this->getTitle()->getLocalURL( 'action=submit' ) ) ) . "
80 -<table>
81 - <tr>
82 - " . Html::openElement( 'td', array( 'style' => 'text-align:right' ) ) . "
83 - " . Html::input( 'wpLockConfirm', null, 'checkbox', array( 'id' => 'mw-input-wpunlockconfirm' ) ) . "
84 - </td>
85 - " . Html::openElement( 'td', array( 'style' => 'text-align:left' ) ) .
86 - Html::openElement( 'label', array( 'for' => 'mw-input-wpunlockconfirm' ) ) .
87 - wfMsgHtml( 'unlockconfirm' ) . "</label>
88 - </td>
89 - </tr>
90 - <tr>
91 - <td>&#160;</td>
92 - " . Html::openElement( 'td', array( 'style' => 'text-align:left' ) ) . "
93 - " . Html::input( 'wpLock', wfMsg( 'unlockbtn' ), 'submit' ) . "
94 - </td>
95 - </tr>
96 -</table>\n" .
97 - Html::hidden( 'wpEditToken', $this->getUser()->editToken() ) . "\n" .
98 - Html::closeElement( 'form' )
 50+ protected function getFormFields() {
 51+ return array(
 52+ 'Confirm' => array(
 53+ 'type' => 'toggle',
 54+ 'label-message' => 'unlockconfirm',
 55+ ),
9956 );
 57+ }
10058
 59+ protected function alterForm( HTMLForm $form ) {
 60+ $form->setWrapperLegend( false );
 61+ $form->setHeaderText( $this->msg( 'unlockdbtext' )->parseAsBlock() );
 62+ $form->setSubmitTextMsg( 'unlockbtn' );
10163 }
10264
103 - private function doSubmit() {
 65+ public function onSubmit( array $data ) {
10466 global $wgReadOnlyFile;
10567
106 - if ( !$this->getRequest()->getCheck( 'wpLockConfirm' ) ) {
107 - $this->showForm( wfMsg( 'locknoconfirm' ) );
108 - return;
 68+ if ( !$data['Confirm'] ) {
 69+ return Status::newFatal( 'locknoconfirm' );
10970 }
11071
11172 wfSuppressWarnings();
@@ -112,13 +73,13 @@
11374 wfRestoreWarnings();
11475
11576 if ( $res ) {
116 - $this->getOutput()->redirect( $this->getTitle()->getFullURL( 'action=success' ) );
 77+ return Status::newGood();
11778 } else {
118 - $this->getOutput()->addWikiMsg( 'filedeleteerror', $wgReadOnlyFile );
 79+ return Status::newFatal( 'filedeleteerror', $wgReadOnlyFile );
11980 }
12081 }
12182
122 - private function showSuccess() {
 83+ public function onSuccess() {
12384 $out = $this->getOutput();
12485 $out->setSubtitle( wfMsg( 'unlockdbsuccesssub' ) );
12586 $out->addWikiMsg( 'unlockdbsuccesstext' );

Follow-up revisions

RevisionCommit summaryAuthorDate
r100795Split out checkExecutePermissions() function from userCanExecute() in FormSpe...aaron06:22, 26 October 2011

Comments

#Comment by Aaron Schulz (talk | contribs)   22:52, 25 October 2011

Wow, didn't catch this at first, viewing Special:SpecialPages:

Backtrace:

  1. 0 D:\www\MW_trunk\phase3\includes\Exception.php(279): wfDebugDieBacktrace()
#1 D:\www\MW_trunk\phase3\includes\SpecialPage.php(818): PermissionsError->__construct('siteadmin')
#2 D:\www\MW_trunk\phase3\includes\specials\SpecialLockdb.php(41): FormSpecialPage->userCanExecute(Object(User))
#3 D:\www\MW_trunk\phase3\includes\SpecialPageFactory.php(356): SpecialLockdb->userCanExecute(Object(User))
#4 D:\www\MW_trunk\phase3\includes\specials\SpecialSpecialpages.php(54): SpecialPageFactory::getUsablePages(Object(User))
#5 D:\www\MW_trunk\phase3\includes\specials\SpecialSpecialpages.php(42): SpecialSpecialpages->getPageGroups()
#6 D:\www\MW_trunk\phase3\includes\SpecialPageFactory.php(470): SpecialSpecialpages->execute(NULL)
#7 D:\www\MW_trunk\phase3\includes\Wiki.php(225): SpecialPageFactory::executePath(Object(Title), Object(RequestContext))
#8 D:\www\MW_trunk\phase3\includes\Wiki.php(600): MediaWiki->performRequest()
#9 D:\www\MW_trunk\phase3\includes\Wiki.php(519): MediaWiki->main()
#10 D:\www\MW_trunk\phase3\index.php(58): MediaWiki->run()
#11 {main}
#Comment by Aaron Schulz (talk | contribs)   05:38, 26 October 2011

SpecialPasswordReset suffers this too if you give it a right restriction in the constructor.

Status & tagging log