Fix a couple of mistyped variable names, mysql strict incompatibilities, make cascadi...
[lhc/web/wiklou.git] / includes / ProtectionForm.php
1 <?php
2 /**
3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @package MediaWiki
22 * @subpackage SpecialPage
23 */
24
25 class ProtectionForm {
26 var $mRestrictions = array();
27 var $mReason = '';
28 var $mCascade = false;
29
30 function ProtectionForm( &$article ) {
31 global $wgRequest, $wgUser;
32 global $wgRestrictionTypes, $wgRestrictionLevels;
33 $this->mArticle =& $article;
34 $this->mTitle =& $article->mTitle;
35
36 if( $this->mTitle ) {
37 foreach( $wgRestrictionTypes as $action ) {
38 // Fixme: this form currently requires individual selections,
39 // but the db allows multiples separated by commas.
40 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
41 }
42 $this->mCascade = $this->mTitle->areRestrictionsCascading();
43 }
44
45 // The form will be available in read-only to show levels.
46 $this->disabled = !$wgUser->isAllowed( 'protect' ) || wfReadOnly() || $wgUser->isBlocked();
47 $this->disabledAttrib = $this->disabled
48 ? array( 'disabled' => 'disabled' )
49 : array();
50
51 if( $wgRequest->wasPosted() ) {
52 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
53 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
54 foreach( $wgRestrictionTypes as $action ) {
55 $val = $wgRequest->getVal( "mwProtect-level-$action" );
56 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
57 $this->mRestrictions[$action] = $val;
58 }
59 }
60 }
61 }
62
63 function show() {
64 global $wgOut;
65
66 $wgOut->setRobotpolicy( 'noindex,nofollow' );
67
68 if( is_null( $this->mTitle ) ||
69 !$this->mTitle->exists() ||
70 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
71 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
72 return;
73 }
74
75 if( $this->save() ) {
76 $wgOut->redirect( $this->mTitle->getFullUrl() );
77 return;
78 }
79
80 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
81 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
82
83 $wgOut->addWikiText(
84 wfMsg( $this->disabled ? "protect-viewtext" : "protect-text",
85 wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ) );
86
87 $wgOut->addHTML( $this->buildForm() );
88
89 $this->showLogExtract( $wgOut );
90 }
91
92 function save() {
93 global $wgRequest, $wgUser, $wgOut;
94 if( !$wgRequest->wasPosted() ) {
95 return false;
96 }
97
98 if( $this->disabled ) {
99 return false;
100 }
101
102 $token = $wgRequest->getVal( 'wpEditToken' );
103 if( !$wgUser->matchEditToken( $token ) ) {
104 throw new FatalError( wfMsg( 'sessionfailure' ) );
105 }
106
107 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade );
108 if( !$ok ) {
109 throw new FatalError( "Unknown error at restriction save time." );
110 }
111 return $ok;
112 }
113
114 function buildForm() {
115 global $wgUser;
116
117 $out = '';
118 if( !$this->disabled ) {
119 $out .= $this->buildScript();
120 // The submission needs to reenable the move permission selector
121 // if it's in locked mode, or some browsers won't submit the data.
122 $out .= wfOpenElement( 'form', array(
123 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
124 'method' => 'post',
125 'onsubmit' => 'protectEnable(true)' ) );
126
127 $out .= wfElement( 'input', array(
128 'type' => 'hidden',
129 'name' => 'wpEditToken',
130 'value' => $wgUser->editToken() ) );
131 }
132
133 $out .= "<table id='mwProtectSet'>";
134 $out .= "<tbody>";
135 $out .= "<tr>\n";
136 foreach( $this->mRestrictions as $action => $required ) {
137 /* Not all languages have V_x <-> N_x relation */
138 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
139 }
140 $out .= "</tr>\n";
141 $out .= "<tr>\n";
142 foreach( $this->mRestrictions as $action => $selected ) {
143 $out .= "<td>\n";
144 $out .= $this->buildSelector( $action, $selected );
145 $out .= "</td>\n";
146 }
147 $out .= "</tr>\n";
148
149 // JavaScript will add another row with a value-chaining checkbox
150
151 $out .= "</tbody>\n";
152 $out .= "</table>\n";
153
154 global $wgEnableCascadingProtection;
155
156 if ($wgEnableCascadingProtection)
157 $out .= $this->buildCascadeInput();
158
159 if( !$this->disabled ) {
160 $out .= "<table>\n";
161 $out .= "<tbody>\n";
162 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
163 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
164 $out .= "</tbody>\n";
165 $out .= "</table>\n";
166 $out .= "</form>\n";
167 $out .= $this->buildCleanupScript();
168 }
169
170 return $out;
171 }
172
173 function buildSelector( $action, $selected ) {
174 global $wgRestrictionLevels;
175 $id = 'mwProtect-level-' . $action;
176 $attribs = array(
177 'id' => $id,
178 'name' => $id,
179 'size' => count( $wgRestrictionLevels ),
180 'onchange' => 'protectLevelsUpdate(this)',
181 ) + $this->disabledAttrib;
182
183 $out = wfOpenElement( 'select', $attribs );
184 foreach( $wgRestrictionLevels as $key ) {
185 $out .= $this->buildOption( $key, $selected );
186 }
187 $out .= "</select>\n";
188 return $out;
189 }
190
191 function buildOption( $key, $selected ) {
192 $text = ( $key == '' )
193 ? wfMsg( 'protect-default' )
194 : wfMsg( "protect-level-$key" );
195 $selectedAttrib = ($selected == $key)
196 ? array( 'selected' => 'selected' )
197 : array();
198 return wfElement( 'option',
199 array( 'value' => $key ) + $selectedAttrib,
200 $text );
201 }
202
203 function buildReasonInput() {
204 $id = 'mwProtect-reason';
205 return wfElement( 'label', array(
206 'id' => "$id-label",
207 'for' => $id ),
208 wfMsg( 'protectcomment' ) ) .
209 '</td><td>' .
210 wfElement( 'input', array(
211 'size' => 60,
212 'name' => $id,
213 'id' => $id ) );
214 }
215
216 function buildCascadeInput() {
217 $id = 'mwProtect-cascade';
218 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
219
220 return $ci;
221 }
222
223 function buildSubmit() {
224 return wfElement( 'input', array(
225 'type' => 'submit',
226 'value' => wfMsg( 'confirm' ) ) );
227 }
228
229 function buildScript() {
230 global $wgStylePath, $wgStyleVersion;
231 return '<script type="text/javascript" src="' .
232 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
233 '"></script>';
234 }
235
236 function buildCleanupScript() {
237 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
238 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
239 }
240
241 /**
242 * @param OutputPage $out
243 * @access private
244 */
245 function showLogExtract( &$out ) {
246 # Show relevant lines from the deletion log:
247 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
248 $logViewer = new LogViewer(
249 new LogReader(
250 new FauxRequest(
251 array( 'page' => $this->mTitle->getPrefixedText(),
252 'type' => 'protect' ) ) ) );
253 $logViewer->showList( $out );
254 }
255 }
256
257
258 ?>