Restore backwards-compatibility for old types of restrictions; wipe page_restrictions...
[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 * @package MediaWiki
22 * @subpackage SpecialPage
23 */
24
25 class ProtectionForm {
26 var $mRestrictions = array();
27 var $mReason = '';
28 var $mCascade = false;
29
30 function ProtectionForm( &$article ) {
31 global $wgRequest, $wgUser;
32 global $wgRestrictionTypes, $wgRestrictionLevels;
33 $this->mArticle =& $article;
34 $this->mTitle =& $article->mTitle;
35
36 if( $this->mTitle ) {
37 foreach( $wgRestrictionTypes as $action ) {
38 // Fixme: this form currently requires individual selections,
39 // but the db allows multiples separated by commas.
40 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
41 }
42
43 $this->mCascade = $this->mTitle->areRestrictionsCascading();
44 }
45
46 // The form will be available in read-only to show levels.
47 $this->disabled = !$wgUser->isAllowed( 'protect' ) || wfReadOnly() || $wgUser->isBlocked();
48 $this->disabledAttrib = $this->disabled
49 ? array( 'disabled' => 'disabled' )
50 : array();
51
52 if( $wgRequest->wasPosted() ) {
53 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
54 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
55 foreach( $wgRestrictionTypes as $action ) {
56 $val = $wgRequest->getVal( "mwProtect-level-$action" );
57 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
58 $this->mRestrictions[$action] = $val;
59 }
60 }
61 }
62 }
63
64 function show() {
65 global $wgOut;
66
67 $wgOut->setRobotpolicy( 'noindex,nofollow' );
68
69 if( is_null( $this->mTitle ) ||
70 !$this->mTitle->exists() ||
71 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
72 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
73 return;
74 }
75
76 if( $this->save() ) {
77 $wgOut->redirect( $this->mTitle->getFullUrl() );
78 return;
79 }
80
81 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
82 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
83
84 $wgOut->addWikiText(
85 wfMsg( $this->disabled ? "protect-viewtext" : "protect-text",
86 wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ) );
87
88 $wgOut->addHTML( $this->buildForm() );
89
90 $this->showLogExtract( $wgOut );
91 }
92
93 function save() {
94 global $wgRequest, $wgUser, $wgOut;
95 if( !$wgRequest->wasPosted() ) {
96 return false;
97 }
98
99 if( $this->disabled ) {
100 return false;
101 }
102
103 $token = $wgRequest->getVal( 'wpEditToken' );
104 if( !$wgUser->matchEditToken( $token ) ) {
105 throw new FatalError( wfMsg( 'sessionfailure' ) );
106 }
107
108 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade );
109 if( !$ok ) {
110 throw new FatalError( "Unknown error at restriction save time." );
111 }
112 return $ok;
113 }
114
115 function buildForm() {
116 global $wgUser;
117
118 $out = '';
119 if( !$this->disabled ) {
120 $out .= $this->buildScript();
121 // The submission needs to reenable the move permission selector
122 // if it's in locked mode, or some browsers won't submit the data.
123 $out .= wfOpenElement( 'form', array(
124 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
125 'method' => 'post',
126 'onsubmit' => 'protectEnable(true)' ) );
127
128 $out .= wfElement( 'input', array(
129 'type' => 'hidden',
130 'name' => 'wpEditToken',
131 'value' => $wgUser->editToken() ) );
132 }
133
134 $out .= "<table id='mwProtectSet'>";
135 $out .= "<tbody>";
136 $out .= "<tr>\n";
137 foreach( $this->mRestrictions as $action => $required ) {
138 /* Not all languages have V_x <-> N_x relation */
139 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
140 }
141 $out .= "</tr>\n";
142 $out .= "<tr>\n";
143 foreach( $this->mRestrictions as $action => $selected ) {
144 $out .= "<td>\n";
145 $out .= $this->buildSelector( $action, $selected );
146 $out .= "</td>\n";
147 }
148 $out .= "</tr>\n";
149
150 // JavaScript will add another row with a value-chaining checkbox
151
152 $out .= "</tbody>\n";
153 $out .= "</table>\n";
154
155 global $wgEnableCascadingProtection;
156
157 if ($wgEnableCascadingProtection)
158 $out .= $this->buildCascadeInput();
159
160 if( !$this->disabled ) {
161 $out .= "<table>\n";
162 $out .= "<tbody>\n";
163 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
164 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
165 $out .= "</tbody>\n";
166 $out .= "</table>\n";
167 $out .= "</form>\n";
168 $out .= $this->buildCleanupScript();
169 }
170
171 return $out;
172 }
173
174 function buildSelector( $action, $selected ) {
175 global $wgRestrictionLevels;
176 $id = 'mwProtect-level-' . $action;
177 $attribs = array(
178 'id' => $id,
179 'name' => $id,
180 'size' => count( $wgRestrictionLevels ),
181 'onchange' => 'protectLevelsUpdate(this)',
182 ) + $this->disabledAttrib;
183
184 $out = wfOpenElement( 'select', $attribs );
185 foreach( $wgRestrictionLevels as $key ) {
186 $out .= $this->buildOption( $key, $selected );
187 }
188 $out .= "</select>\n";
189 return $out;
190 }
191
192 function buildOption( $key, $selected ) {
193 $text = ( $key == '' )
194 ? wfMsg( 'protect-default' )
195 : wfMsg( "protect-level-$key" );
196 $selectedAttrib = ($selected == $key)
197 ? array( 'selected' => 'selected' )
198 : array();
199 return wfElement( 'option',
200 array( 'value' => $key ) + $selectedAttrib,
201 $text );
202 }
203
204 function buildReasonInput() {
205 $id = 'mwProtect-reason';
206 return wfElement( 'label', array(
207 'id' => "$id-label",
208 'for' => $id ),
209 wfMsg( 'protectcomment' ) ) .
210 '</td><td>' .
211 wfElement( 'input', array(
212 'size' => 60,
213 'name' => $id,
214 'id' => $id ) );
215 }
216
217 function buildCascadeInput() {
218 $id = 'mwProtect-cascade';
219 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
220
221 return $ci;
222 }
223
224 function buildSubmit() {
225 return wfElement( 'input', array(
226 'type' => 'submit',
227 'value' => wfMsg( 'confirm' ) ) );
228 }
229
230 function buildScript() {
231 global $wgStylePath, $wgStyleVersion;
232 return '<script type="text/javascript" src="' .
233 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
234 '"></script>';
235 }
236
237 function buildCleanupScript() {
238 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
239 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
240 }
241
242 /**
243 * @param OutputPage $out
244 * @access private
245 */
246 function showLogExtract( &$out ) {
247 # Show relevant lines from the deletion log:
248 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
249 $logViewer = new LogViewer(
250 new LogReader(
251 new FauxRequest(
252 array( 'page' => $this->mTitle->getPrefixedText(),
253 'type' => 'protect' ) ) ) );
254 $logViewer->showList( $out );
255 }
256 }
257
258
259 ?>