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