f333db5e11313631dbcc862a606047fbdc4ba4a9
[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 $cascadeSources = $this->mTitle->getCascadeProtectionSources();
77
78 if ( count($cascadeSources) > 0 ) {
79 $titles = '';
80
81 foreach ( $cascadeSources as $title ) {
82 $titles .= '* ' . $title->getPrefixedText() . "\r\n";
83 }
84
85 $wgOut->addWikiText( wfMsgForContent( 'protect-cascadeon', $titles ) );
86 }
87
88 if( $this->save() ) {
89 $wgOut->redirect( $this->mTitle->getFullUrl() );
90 return;
91 }
92
93 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
94 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
95
96 $wgOut->addWikiText(
97 wfMsg( $this->disabled ? "protect-viewtext" : "protect-text",
98 wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ) );
99
100 $wgOut->addHTML( $this->buildForm() );
101
102 $this->showLogExtract( $wgOut );
103 }
104
105 function save() {
106 global $wgRequest, $wgUser, $wgOut;
107 if( !$wgRequest->wasPosted() ) {
108 return false;
109 }
110
111 if( $this->disabled ) {
112 return false;
113 }
114
115 $token = $wgRequest->getVal( 'wpEditToken' );
116 if( !$wgUser->matchEditToken( $token ) ) {
117 throw new FatalError( wfMsg( 'sessionfailure' ) );
118 }
119
120 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade );
121 if( !$ok ) {
122 throw new FatalError( "Unknown error at restriction save time." );
123 }
124 return $ok;
125 }
126
127 function buildForm() {
128 global $wgUser;
129
130 $out = '';
131 if( !$this->disabled ) {
132 $out .= $this->buildScript();
133 // The submission needs to reenable the move permission selector
134 // if it's in locked mode, or some browsers won't submit the data.
135 $out .= wfOpenElement( 'form', array(
136 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
137 'method' => 'post',
138 'onsubmit' => 'protectEnable(true)' ) );
139
140 $out .= wfElement( 'input', array(
141 'type' => 'hidden',
142 'name' => 'wpEditToken',
143 'value' => $wgUser->editToken() ) );
144 }
145
146 $out .= "<table id='mwProtectSet'>";
147 $out .= "<tbody>";
148 $out .= "<tr>\n";
149 foreach( $this->mRestrictions as $action => $required ) {
150 /* Not all languages have V_x <-> N_x relation */
151 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
152 }
153 $out .= "</tr>\n";
154 $out .= "<tr>\n";
155 foreach( $this->mRestrictions as $action => $selected ) {
156 $out .= "<td>\n";
157 $out .= $this->buildSelector( $action, $selected );
158 $out .= "</td>\n";
159 }
160 $out .= "</tr>\n";
161
162 // JavaScript will add another row with a value-chaining checkbox
163
164 $out .= "</tbody>\n";
165 $out .= "</table>\n";
166
167 global $wgEnableCascadingProtection;
168
169 if ($wgEnableCascadingProtection)
170 $out .= $this->buildCascadeInput();
171
172 if( !$this->disabled ) {
173 $out .= "<table>\n";
174 $out .= "<tbody>\n";
175 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
176 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
177 $out .= "</tbody>\n";
178 $out .= "</table>\n";
179 $out .= "</form>\n";
180 $out .= $this->buildCleanupScript();
181 }
182
183 return $out;
184 }
185
186 function buildSelector( $action, $selected ) {
187 global $wgRestrictionLevels;
188 $id = 'mwProtect-level-' . $action;
189 $attribs = array(
190 'id' => $id,
191 'name' => $id,
192 'size' => count( $wgRestrictionLevels ),
193 'onchange' => 'protectLevelsUpdate(this)',
194 ) + $this->disabledAttrib;
195
196 $out = wfOpenElement( 'select', $attribs );
197 foreach( $wgRestrictionLevels as $key ) {
198 $out .= $this->buildOption( $key, $selected );
199 }
200 $out .= "</select>\n";
201 return $out;
202 }
203
204 function buildOption( $key, $selected ) {
205 $text = ( $key == '' )
206 ? wfMsg( 'protect-default' )
207 : wfMsg( "protect-level-$key" );
208 $selectedAttrib = ($selected == $key)
209 ? array( 'selected' => 'selected' )
210 : array();
211 return wfElement( 'option',
212 array( 'value' => $key ) + $selectedAttrib,
213 $text );
214 }
215
216 function buildReasonInput() {
217 $id = 'mwProtect-reason';
218 return wfElement( 'label', array(
219 'id' => "$id-label",
220 'for' => $id ),
221 wfMsg( 'protectcomment' ) ) .
222 '</td><td>' .
223 wfElement( 'input', array(
224 'size' => 60,
225 'name' => $id,
226 'id' => $id ) );
227 }
228
229 function buildCascadeInput() {
230 $id = 'mwProtect-cascade';
231 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
232
233 return $ci;
234 }
235
236 function buildSubmit() {
237 return wfElement( 'input', array(
238 'type' => 'submit',
239 'value' => wfMsg( 'confirm' ) ) );
240 }
241
242 function buildScript() {
243 global $wgStylePath, $wgStyleVersion;
244 return '<script type="text/javascript" src="' .
245 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
246 '"></script>';
247 }
248
249 function buildCleanupScript() {
250 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
251 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
252 }
253
254 /**
255 * @param OutputPage $out
256 * @access private
257 */
258 function showLogExtract( &$out ) {
259 # Show relevant lines from the deletion log:
260 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
261 $logViewer = new LogViewer(
262 new LogReader(
263 new FauxRequest(
264 array( 'page' => $this->mTitle->getPrefixedText(),
265 'type' => 'protect' ) ) ) );
266 $logViewer->showList( $out );
267 }
268 }
269
270
271 ?>