Further fixes to protection expiry:
[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, $wgLang;
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 $cascadeSources = $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
163 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade, $expiry );
164 if( !$ok ) {
165 throw new FatalError( "Unknown error at restriction save time." );
166 }
167 return $ok;
168 }
169
170 function buildForm() {
171 global $wgUser;
172
173 $out = '';
174 if( !$this->disabled ) {
175 $out .= $this->buildScript();
176 // The submission needs to reenable the move permission selector
177 // if it's in locked mode, or some browsers won't submit the data.
178 $out .= wfOpenElement( 'form', array(
179 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
180 'method' => 'post',
181 'onsubmit' => 'protectEnable(true)' ) );
182
183 $out .= wfElement( 'input', array(
184 'type' => 'hidden',
185 'name' => 'wpEditToken',
186 'value' => $wgUser->editToken() ) );
187 }
188
189 $out .= "<table id='mwProtectSet'>";
190 $out .= "<tbody>";
191 $out .= "<tr>\n";
192 foreach( $this->mRestrictions as $action => $required ) {
193 /* Not all languages have V_x <-> N_x relation */
194 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
195 }
196 $out .= "</tr>\n";
197 $out .= "<tr>\n";
198 foreach( $this->mRestrictions as $action => $selected ) {
199 $out .= "<td>\n";
200 $out .= $this->buildSelector( $action, $selected );
201 $out .= "</td>\n";
202 }
203 $out .= "</tr>\n";
204
205 // JavaScript will add another row with a value-chaining checkbox
206
207 $out .= "</tbody>\n";
208 $out .= "</table>\n";
209
210 $out .= "<table>\n";
211 $out .= "<tbody>\n";
212
213 global $wgEnableCascadingProtection;
214
215 if ($wgEnableCascadingProtection)
216 $out .= $this->buildCascadeInput();
217
218 $out .= $this->buildExpiryInput();
219
220 if( !$this->disabled ) {
221 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
222 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
223 }
224
225 $out .= "</tbody>\n";
226 $out .= "</table>\n";
227
228 if ( !$this->disabled ) {
229 $out .= "</form>\n";
230 $out .= $this->buildCleanupScript();
231 }
232
233 return $out;
234 }
235
236 function buildSelector( $action, $selected ) {
237 global $wgRestrictionLevels;
238 $id = 'mwProtect-level-' . $action;
239 $attribs = array(
240 'id' => $id,
241 'name' => $id,
242 'size' => count( $wgRestrictionLevels ),
243 'onchange' => 'protectLevelsUpdate(this)',
244 ) + $this->disabledAttrib;
245
246 $out = wfOpenElement( 'select', $attribs );
247 foreach( $wgRestrictionLevels as $key ) {
248 $out .= $this->buildOption( $key, $selected );
249 }
250 $out .= "</select>\n";
251 return $out;
252 }
253
254 function buildOption( $key, $selected ) {
255 $text = ( $key == '' )
256 ? wfMsg( 'protect-default' )
257 : wfMsg( "protect-level-$key" );
258 $selectedAttrib = ($selected == $key)
259 ? array( 'selected' => 'selected' )
260 : array();
261 return wfElement( 'option',
262 array( 'value' => $key ) + $selectedAttrib,
263 $text );
264 }
265
266 function buildReasonInput() {
267 $id = 'mwProtect-reason';
268 return wfElement( 'label', array(
269 'id' => "$id-label",
270 'for' => $id ),
271 wfMsg( 'protectcomment' ) ) .
272 '</td><td>' .
273 wfElement( 'input', array(
274 'size' => 60,
275 'name' => $id,
276 'id' => $id ) );
277 }
278
279 function buildCascadeInput() {
280 $id = 'mwProtect-cascade';
281 $ci = "<tr>" . wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib) . "</tr>";
282
283 return $ci;
284 }
285
286 function buildExpiryInput() {
287 $id = 'mwProtect-expiry';
288
289 $ci = "<tr> <td align=\"right\">";
290 $ci .= wfElement( 'label', array (
291 'id' => "$id-label",
292 'for' => $id ),
293 wfMsg( 'protectexpiry' ) );
294 $ci .= "</td> <td aligh=\"left\">";
295 $ci .= wfElement( 'input', array(
296 'size' => 60,
297 'name' => $id,
298 'id' => $id,
299 'value' => $this->mExpiry ) );
300 $ci .= "</td></tr>";
301
302 return $ci;
303 }
304
305 function buildSubmit() {
306 return wfElement( 'input', array(
307 'type' => 'submit',
308 'value' => wfMsg( 'confirm' ) ) );
309 }
310
311 function buildScript() {
312 global $wgStylePath, $wgStyleVersion;
313 return '<script type="text/javascript" src="' .
314 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
315 '"></script>';
316 }
317
318 function buildCleanupScript() {
319 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
320 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
321 }
322
323 /**
324 * @param OutputPage $out
325 * @access private
326 */
327 function showLogExtract( &$out ) {
328 # Show relevant lines from the deletion log:
329 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
330 $logViewer = new LogViewer(
331 new LogReader(
332 new FauxRequest(
333 array( 'page' => $this->mTitle->getPrefixedText(),
334 'type' => 'protect' ) ) ) );
335 $logViewer->showList( $out );
336 }
337 }
338
339 ?>