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