* Use existing CSS classes for input form aligning
[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
22 /**
23 * @todo document, briefly.
24 * @addtogroup SpecialPage
25 */
26 class ProtectionForm {
27 var $mRestrictions = array();
28 var $mReason = '';
29 var $mCascade = false;
30 var $mExpiry = null;
31 var $mPermErrors = array();
32 var $mApplicableTypes = array();
33
34 function __construct( &$article ) {
35 global $wgRequest, $wgUser;
36 global $wgRestrictionTypes, $wgRestrictionLevels;
37 $this->mArticle =& $article;
38 $this->mTitle =& $article->mTitle;
39 $this->mApplicableTypes = $this->mTitle->exists() ? $wgRestrictionTypes : array('create');
40
41 if( $this->mTitle ) {
42 $this->mTitle->loadRestrictions();
43
44 foreach( $this->mApplicableTypes as $action ) {
45 // Fixme: this form currently requires individual selections,
46 // but the db allows multiples separated by commas.
47 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
48 }
49
50 $this->mCascade = $this->mTitle->areRestrictionsCascading();
51
52 if ( $this->mTitle->mRestrictionsExpiry == 'infinity' ) {
53 $this->mExpiry = 'infinite';
54 } else if ( strlen($this->mTitle->mRestrictionsExpiry) == 0 ) {
55 $this->mExpiry = '';
56 } else {
57 $this->mExpiry = wfTimestamp( TS_RFC2822, $this->mTitle->mRestrictionsExpiry );
58 }
59 }
60
61 // The form will be available in read-only to show levels.
62 $this->disabled = wfReadOnly() || ($this->mPermErrors = $this->mTitle->getUserPermissionsErrors('protect',$wgUser)) != array();
63 $this->disabledAttrib = $this->disabled
64 ? array( 'disabled' => 'disabled' )
65 : array();
66
67 if( $wgRequest->wasPosted() ) {
68 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
69 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
70 $this->mExpiry = $wgRequest->getText( 'mwProtect-expiry' );
71
72 foreach( $this->mApplicableTypes as $action ) {
73 $val = $wgRequest->getVal( "mwProtect-level-$action" );
74 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
75 $this->mRestrictions[$action] = $val;
76 }
77 }
78 }
79 }
80
81 function execute() {
82 global $wgRequest, $wgOut;
83 if( $wgRequest->wasPosted() ) {
84 if( $this->save() ) {
85 $article = new Article( $this->mTitle );
86 $q = $article->isRedirect() ? 'redirect=no' : '';
87 $wgOut->redirect( $this->mTitle->getFullUrl( $q ) );
88 }
89 } else {
90 $this->show();
91 }
92 }
93
94 function show( $err = null ) {
95 global $wgOut, $wgUser;
96
97 $wgOut->setRobotpolicy( 'noindex,nofollow' );
98
99 if( is_null( $this->mTitle ) ||
100 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
101 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
102 return;
103 }
104
105 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
106
107 if ( "" != $err ) {
108 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
109 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
110 }
111
112 if ( $cascadeSources && count($cascadeSources) > 0 ) {
113 $titles = '';
114
115 foreach ( $cascadeSources as $title ) {
116 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
117 }
118
119 $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
120 }
121
122 $sk = $wgUser->getSkin();
123 $titleLink = $sk->makeLinkObj( $this->mTitle );
124 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle->getPrefixedText() ) );
125 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
126
127 # Show an appropriate message if the user isn't allowed or able to change
128 # the protection settings at this time
129 if( $this->disabled ) {
130 if( wfReadOnly() ) {
131 $wgOut->readOnlyPage();
132 } elseif( $this->mPermErrors ) {
133 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors ) );
134 }
135 } else {
136 $wgOut->addWikiMsg( 'protect-text', $this->mTitle->getPrefixedText() );
137 }
138
139 $wgOut->addHTML( $this->buildForm() );
140
141 $this->showLogExtract( $wgOut );
142 }
143
144 function save() {
145 global $wgRequest, $wgUser, $wgOut;
146
147 if( $this->disabled ) {
148 $this->show();
149 return false;
150 }
151
152 $token = $wgRequest->getVal( 'wpEditToken' );
153 if( !$wgUser->matchEditToken( $token ) ) {
154 $this->show( wfMsg( 'sessionfailure' ) );
155 return false;
156 }
157
158 if ( strlen( $this->mExpiry ) == 0 ) {
159 $this->mExpiry = 'infinite';
160 }
161
162 if ( $this->mExpiry == 'infinite' || $this->mExpiry == 'indefinite' ) {
163 $expiry = Block::infinity();
164 } else {
165 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
166 $expiry = strtotime( $this->mExpiry );
167
168 if ( $expiry < 0 || $expiry === false ) {
169 $this->show( wfMsg( 'protect_expiry_invalid' ) );
170 return false;
171 }
172
173 $expiry = wfTimestamp( TS_MW, $expiry );
174
175 if ( $expiry < wfTimestampNow() ) {
176 $this->show( wfMsg( 'protect_expiry_old' ) );
177 return false;
178 }
179
180 }
181
182 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
183 # to a semi-protected page.
184 global $wgGroupPermissions;
185
186 $edit_restriction = $this->mRestrictions['edit'];
187
188 if ($this->mCascade && ($edit_restriction != 'protect') &&
189 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
190 $this->mCascade = false;
191
192 if ($this->mTitle->exists()) {
193 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade, $expiry );
194 } else {
195 $ok = $this->mTitle->updateTitleProtection( $this->mRestrictions['create'], $this->mReason, $expiry );
196 }
197
198 if( !$ok ) {
199 throw new FatalError( "Unknown error at restriction save time." );
200 }
201
202 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
203 $this->mArticle->doWatch();
204 } elseif( $this->mTitle->userIsWatching() ) {
205 $this->mArticle->doUnwatch();
206 }
207
208 return $ok;
209 }
210
211 /**
212 * Build the input form
213 *
214 * @return $out string HTML form
215 */
216 function buildForm() {
217 global $wgUser;
218
219 $out = '';
220 if( !$this->disabled ) {
221 $out .= $this->buildScript();
222 // The submission needs to reenable the move permission selector
223 // if it's in locked mode, or some browsers won't submit the data.
224 $out .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->mTitle->getLocalUrl( 'action=protect' ), 'id' => 'mw-Protect-Form', 'onsubmit' => 'protectEnable(true)' ) ) .
225 Xml::hidden( 'wpEditToken',$wgUser->editToken() );
226 }
227
228 $out .= Xml::openElement( 'fieldset' ) .
229 Xml::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
230 Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
231 Xml::openElement( 'tbody' ) .
232 "<tr>\n";
233
234 foreach( $this->mRestrictions as $action => $required ) {
235 /* Not all languages have V_x <-> N_x relation */
236 $label = Xml::element( 'label',
237 array( 'for' => "mwProtect-level-$action" ),
238 wfMsg( 'restriction-' . $action ) );
239 $out .= "<th>$label</th>";
240 }
241 $out .= "</tr>
242 <tr>\n";
243 foreach( $this->mRestrictions as $action => $selected ) {
244 $out .= "<td>" .
245 $this->buildSelector( $action, $selected ) .
246 "</td>";
247 }
248 $out .= "</tr>\n";
249
250 // JavaScript will add another row with a value-chaining checkbox
251
252 $out .= Xml::closeElement( 'tbody' ) .
253 Xml::closeElement( 'table' ) .
254 Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
255 Xml::openElement( 'tbody' );
256
257 global $wgEnableCascadingProtection;
258 if( $wgEnableCascadingProtection && $this->mTitle->exists() ) {
259 $out .= '<tr>
260 <td></td>
261 <td class="mw-input">' .
262 Xml::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade', $this->mCascade, $this->disabledAttrib ) .
263 "</td>
264 </tr>\n";
265 }
266
267 $attribs = array( 'id' => 'expires' ) + $this->disabledAttrib;
268 $out .= "<tr>
269 <td class='mw-label'>" .
270 Xml::label( wfMsgExt( 'protectexpiry', array( 'parseinline' ) ), 'expires' ) .
271 '</td>
272 <td class="mw-input">' .
273 Xml::input( 'mwProtect-expiry', 60, $this->mExpiry, $attribs ) .
274 '</td>
275 </tr>';
276
277 if( !$this->disabled ) {
278 $id = 'mwProtect-reason';
279 $out .= "<tr>
280 <td class='mw-label'>" .
281 Xml::label( wfMsg( 'protectcomment' ), $id ) .
282 '</td>
283 <td class="mw-input">' .
284 Xml::input( $id, 60, $this->mReason, array( 'type' => 'text', 'id' => $id, 'maxlength' => 255 ) ) .
285 "</td>
286 </tr>
287 <tr>
288 <td></td>
289 <td class='mw-input'>" .
290 Xml::checkLabel( wfMsg( 'watchthis' ),
291 'mwProtectWatch', 'mwProtectWatch',
292 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' ) ) .
293 "</td>
294 </tr>
295 <tr>
296 <td></td>
297 <td class='mw-submit'>" .
298 Xml::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
299 "</td>
300 </tr>\n";
301 }
302
303 $out .= Xml::closeElement( 'tbody' ) .
304 Xml::closeElement( 'table' ) .
305 Xml::closeElement( 'fieldset' );
306
307 if ( !$this->disabled ) {
308 $out .= Xml::closeElement( 'form' ) .
309 $this->buildCleanupScript();
310 }
311
312 return $out;
313 }
314
315 function buildSelector( $action, $selected ) {
316 global $wgRestrictionLevels;
317 $id = 'mwProtect-level-' . $action;
318 $attribs = array(
319 'id' => $id,
320 'name' => $id,
321 'size' => count( $wgRestrictionLevels ),
322 'onchange' => 'protectLevelsUpdate(this)',
323 ) + $this->disabledAttrib;
324
325 $out = Xml::openElement( 'select', $attribs );
326 foreach( $wgRestrictionLevels as $key ) {
327 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
328 }
329 $out .= Xml::closeElement( 'select' );
330 return $out;
331 }
332
333 /**
334 * Prepare the label for a protection selector option
335 *
336 * @param string $permission Permission required
337 * @return string
338 */
339 private function getOptionLabel( $permission ) {
340 if( $permission == '' ) {
341 return wfMsg( 'protect-default' );
342 } else {
343 $key = "protect-level-{$permission}";
344 $msg = wfMsg( $key );
345 if( wfEmptyMsg( $key, $msg ) )
346 $msg = wfMsg( 'protect-fallback', $permission );
347 return $msg;
348 }
349 }
350
351 function buildScript() {
352 global $wgStylePath, $wgStyleVersion;
353 return Xml::tags( 'script', array( 'type' => 'text/javascript', 'src' => htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) ), '' );
354 }
355
356 function buildCleanupScript() {
357 global $wgRestrictionLevels, $wgGroupPermissions;
358 $script = 'var wgCascadeableLevels=';
359 $CascadeableLevels = array();
360 foreach( $wgRestrictionLevels as $key ) {
361 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) || $key == 'protect' ) {
362 $CascadeableLevels[] = "'" . Xml::escapeJsString( $key ) . "'";
363 }
364 }
365 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
366 $script .= 'protectInitialize("mwProtectSet","' . Xml::escapeJsString( wfMsg( 'protect-unchain' ) ) . '","' . count($this->mApplicableTypes) . '")';
367 return Xml::tags( 'script', array( 'type' => 'text/javascript' ), $script );
368 }
369
370 /**
371 * @param OutputPage $out
372 * @access private
373 */
374 function showLogExtract( &$out ) {
375 # Show relevant lines from the protection log:
376 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'protect' ) ) );
377 LogEventsList::showLogExtract( $out, 'protect', $this->mTitle->getPrefixedText() );
378 }
379 }