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