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