85421c3d93595a1a97e71e91f7d6750acfc901a4
[lhc/web/wiklou.git] / includes / specials / SpecialLockdb.php
1 <?php
2 /**
3 * Implements Special:Lockdb
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A form to make the database readonly (eg for maintenance purposes).
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialLockdb extends SpecialPage {
30 var $reason = '';
31
32 public function __construct() {
33 parent::__construct( 'Lockdb', 'siteadmin' );
34 }
35
36 public function execute( $par ) {
37 $this->setHeaders();
38
39 # Permission check
40 if( !$this->userCanExecute( $this->getUser() ) ) {
41 $this->displayRestrictionError();
42 return;
43 }
44
45 $this->outputHeader();
46
47 # If the lock file isn't writable, we can do sweet bugger all
48 global $wgReadOnlyFile;
49 if( !is_writable( dirname( $wgReadOnlyFile ) ) ) {
50 $this->getOutput()->addWikiMsg( 'lockfilenotwritable' );
51 return;
52 }
53
54 $request = $this->getRequest();
55 $action = $request->getVal( 'action' );
56 $this->reason = $request->getVal( 'wpLockReason', '' );
57
58 if ( $action == 'success' ) {
59 $this->showSuccess();
60 } elseif ( $action == 'submit' && $request->wasPosted() &&
61 $this->getUser()->matchEditToken( $request->getVal( 'wpEditToken' ) ) ) {
62 $this->doSubmit();
63 } else {
64 $this->showForm();
65 }
66 }
67
68 private function showForm( $err = '' ) {
69 $out = $this->getOutput();
70 $out->addWikiMsg( 'lockdbtext' );
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' => 'lockdb', 'method' => 'POST',
79 'action' => $this->getTitle()->getLocalURL( 'action=submit' ) ) ). "\n" .
80 wfMsgHtml( 'enterlockreason' ) . ":\n" .
81 Html::textarea( 'wpLockReason', $this->reason, array( 'rows' => 4 ) ). "
82 <table>
83 <tr>
84 " . Html::openElement( 'td', array( 'style' => 'text-align:right' ) ) . "
85 " . Html::input( 'wpLockConfirm', null, 'checkbox', array( 'id' => 'mw-input-wplockconfirm' ) ) . "
86 </td>
87 " . Html::openElement( 'td', array( 'style' => 'text-align:left' ) ) .
88 Html::openElement( 'label', array( 'for' => 'mw-input-wplockconfirm' ) ) .
89 wfMsgHtml( 'lockconfirm' ) . "</label>
90 </td>
91 </tr>
92 <tr>
93 <td>&#160;</td>
94 " . Html::openElement( 'td', array( 'style' => 'text-align:left' ) ) . "
95 " . Html::input( 'wpLock', wfMsg( 'lockbtn' ), 'submit' ) . "
96 </td>
97 </tr>
98 </table>\n" .
99 Html::hidden( 'wpEditToken', $this->getUser()->editToken() ) . "\n" .
100 Html::closeElement( 'form' )
101 );
102
103 }
104
105 private function doSubmit() {
106 global $wgContLang, $wgReadOnlyFile;
107
108 if ( !$this->getRequest()->getCheck( 'wpLockConfirm' ) ) {
109 $this->showForm( wfMsg( 'locknoconfirm' ) );
110 return;
111 }
112
113 wfSuppressWarnings();
114 $fp = fopen( $wgReadOnlyFile, 'w' );
115 wfRestoreWarnings();
116
117 if ( false === $fp ) {
118 # This used to show a file not found error, but the likeliest reason for fopen()
119 # to fail at this point is insufficient permission to write to the file...good old
120 # is_writable() is plain wrong in some cases, it seems...
121 $this->getOutput()->addWikiMsg( 'lockfilenotwritable' );
122 return;
123 }
124 fwrite( $fp, $this->reason );
125 $timestamp = wfTimestampNow();
126 fwrite( $fp, "\n<p>" . wfMsgExt(
127 'lockedbyandtime',
128 array( 'content', 'parsemag' ),
129 $this->getUser()->getName(),
130 $wgContLang->date( $timestamp ),
131 $wgContLang->time( $timestamp )
132 ) . "</p>\n" );
133 fclose( $fp );
134
135 $this->getOutput()->redirect( $this->getTitle()->getFullURL( 'action=success' ) );
136 }
137
138 private function showSuccess() {
139 $out = $this->getOutput();
140 $out->setPagetitle( wfMsg( 'lockdb' ) );
141 $out->setSubtitle( wfMsg( 'lockdbsuccesssub' ) );
142 $out->addWikiMsg( 'lockdbsuccesstext' );
143 }
144 }