Moved some of the functionality from mediawiki.legacy.prefs into mediawiki.specials...
[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 global $wgUser, $wgOut, $wgRequest;
38
39 $this->setHeaders();
40
41 if( !$wgUser->isAllowed( 'siteadmin' ) ) {
42 $wgOut->permissionRequired( 'siteadmin' );
43 return;
44 }
45
46 $this->outputHeader();
47
48 # If the lock file isn't writable, we can do sweet bugger all
49 global $wgReadOnlyFile;
50 if( !is_writable( dirname( $wgReadOnlyFile ) ) ) {
51 self::notWritable();
52 return;
53 }
54
55 $action = $wgRequest->getVal( 'action' );
56 $this->reason = $wgRequest->getVal( 'wpLockReason', '' );
57
58 if ( $action == 'success' ) {
59 $this->showSuccess();
60 } else if ( $action == 'submit' && $wgRequest->wasPosted() &&
61 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
62 $this->doSubmit();
63 } else {
64 $this->showForm();
65 }
66 }
67
68 private function showForm( $err = '' ) {
69 global $wgOut, $wgUser;
70
71 $wgOut->addWikiMsg( 'lockdbtext' );
72
73 if ( $err != '' ) {
74 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
75 $wgOut->addHTML( '<p class="error">' . htmlspecialchars( $err ) . "</p>\n" );
76 }
77
78 $wgOut->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' ) . "
87 </td>
88 " . Html::openElement( 'td', array( 'style' => 'text-align:left' ) ) .
89 wfMsgHtml( 'lockconfirm' ) . "</td>
90 </tr>
91 <tr>
92 <td>&#160;</td>
93 " . Html::openElement( 'td', array( 'style' => 'text-align:left' ) ) . "
94 " . Html::input( 'wpLock', wfMsg( 'lockbtn' ), 'submit' ) . "
95 </td>
96 </tr>
97 </table>\n" .
98 Html::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n" .
99 Html::closeElement( 'form' )
100 );
101
102 }
103
104 private function doSubmit() {
105 global $wgOut, $wgUser, $wgContLang, $wgRequest;
106 global $wgReadOnlyFile;
107
108 if ( ! $wgRequest->getCheck( 'wpLockConfirm' ) ) {
109 $this->showForm( wfMsg( 'locknoconfirm' ) );
110 return;
111 }
112 $fp = @fopen( $wgReadOnlyFile, 'w' );
113
114 if ( false === $fp ) {
115 # This used to show a file not found error, but the likeliest reason for fopen()
116 # to fail at this point is insufficient permission to write to the file...good old
117 # is_writable() is plain wrong in some cases, it seems...
118 self::notWritable();
119 return;
120 }
121 fwrite( $fp, $this->reason );
122 fwrite( $fp, "\n<p>(by " . $wgUser->getName() . " at " .
123 $wgContLang->timeanddate( wfTimestampNow() ) . ")</p>\n" );
124 fclose( $fp );
125
126 $wgOut->redirect( $this->getTitle()->getFullURL( 'action=success' ) );
127 }
128
129 private function showSuccess() {
130 global $wgOut;
131
132 $wgOut->setPagetitle( wfMsg( 'lockdb' ) );
133 $wgOut->setSubtitle( wfMsg( 'lockdbsuccesssub' ) );
134 $wgOut->addWikiMsg( 'lockdbsuccesstext' );
135 }
136
137 public static function notWritable() {
138 global $wgOut;
139 $wgOut->showErrorPage( 'lockdb', 'lockfilenotwritable' );
140 }
141 }