Changes to Special:Lockdb and Special:Unlockdb:
[lhc/web/wiklou.git] / includes / specials / SpecialLockdb.php
1 <?php
2
3 /**
4 * A form to make the database readonly (eg for maintenance purposes).
5 *
6 * @ingroup SpecialPage
7 */
8 class SpecialLockdb extends SpecialPage {
9 var $reason = '';
10
11 public function __construct() {
12 parent::__construct( 'Lockdb', 'siteadmin' );
13 }
14
15 public function execute( $par ) {
16 global $wgUser, $wgOut, $wgRequest;
17
18 $this->setHeaders();
19
20 if( !$wgUser->isAllowed( 'siteadmin' ) ) {
21 $wgOut->permissionRequired( 'siteadmin' );
22 return;
23 }
24
25 $this->outputHeader();
26
27 # If the lock file isn't writable, we can do sweet bugger all
28 global $wgReadOnlyFile;
29 if( !is_writable( dirname( $wgReadOnlyFile ) ) ) {
30 self::notWritable();
31 return;
32 }
33
34 $action = $wgRequest->getVal( 'action' );
35 $this->reason = $wgRequest->getVal( 'wpLockReason', '' );
36
37 if ( $action == 'success' ) {
38 $this->showSuccess();
39 } else if ( $action == 'submit' && $wgRequest->wasPosted() &&
40 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
41 $this->doSubmit();
42 } else {
43 $this->showForm();
44 }
45 }
46
47 private function showForm( $err = '' ) {
48 global $wgOut, $wgUser;
49
50 $wgOut->addWikiMsg( 'lockdbtext' );
51
52 if ( $err != '' ) {
53 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
54 $wgOut->addHTML( '<p class="error">' . htmlspecialchars( $err ) . "</p>\n" );
55 }
56
57 $wgOut->addHTML(
58 Html::openElement( 'form', array( 'id' => 'lockdb', 'method' => 'POST',
59 'action' => $this->getTitle()->getLocalURL( 'action=submit' ) ) ). "\n" .
60 wfMsgHtml( 'enterlockreason' ) . ":\n" .
61 Html::textarea( 'wpLockReason', $this->reason, array( 'rows' => 4 ) ). "
62 <table>
63 <tr>
64 " . Html::openElement( 'td', array( 'style' => 'text-align:right' ) ) . "
65 " . Html::input( 'wpLockConfirm', null, 'checkbox' ) . "
66 </td>
67 " . Html::openElement( 'td', array( 'style' => 'text-align:left' ) ) .
68 wfMsgHtml( 'lockconfirm' ) . "</td>
69 </tr>
70 <tr>
71 <td>&#160;</td>
72 " . Html::openElement( 'td', array( 'style' => 'text-align:left' ) ) . "
73 " . Html::input( 'wpLock', wfMsg( 'lockbtn' ), 'submit' ) . "
74 </td>
75 </tr>
76 </table>\n" .
77 Html::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n" .
78 Html::closeElement( 'form' )
79 );
80
81 }
82
83 private function doSubmit() {
84 global $wgOut, $wgUser, $wgContLang, $wgRequest;
85 global $wgReadOnlyFile;
86
87 if ( ! $wgRequest->getCheck( 'wpLockConfirm' ) ) {
88 $this->showForm( wfMsg( 'locknoconfirm' ) );
89 return;
90 }
91 $fp = @fopen( $wgReadOnlyFile, 'w' );
92
93 if ( false === $fp ) {
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 self::notWritable();
98 return;
99 }
100 fwrite( $fp, $this->reason );
101 fwrite( $fp, "\n<p>(by " . $wgUser->getName() . " at " .
102 $wgContLang->timeanddate( wfTimestampNow() ) . ")</p>\n" );
103 fclose( $fp );
104
105 $wgOut->redirect( $this->getTitle()->getFullURL( 'action=success' ) );
106 }
107
108 private function showSuccess() {
109 global $wgOut;
110
111 $wgOut->setPagetitle( wfMsg( 'lockdb' ) );
112 $wgOut->setSubtitle( wfMsg( 'lockdbsuccesssub' ) );
113 $wgOut->addWikiMsg( 'lockdbsuccesstext' );
114 }
115
116 public static function notWritable() {
117 global $wgOut;
118 $wgOut->showErrorPage( 'lockdb', 'lockfilenotwritable' );
119 }
120 }