fix typo in comment
[lhc/web/wiklou.git] / includes / specials / SpecialProtectedpages.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 * @todo document
9 * @ingroup SpecialPage
10 */
11 class ProtectedPagesForm {
12
13 protected $IdLevel = 'level';
14 protected $IdType = 'type';
15
16 public function showList( $msg = '' ) {
17 global $wgOut, $wgRequest;
18
19 if( "" != $msg ) {
20 $wgOut->setSubtitle( $msg );
21 }
22
23 // Purge expired entries on one in every 10 queries
24 if( !mt_rand( 0, 10 ) ) {
25 Title::purgeExpiredRestrictions();
26 }
27
28 $type = $wgRequest->getVal( $this->IdType );
29 $level = $wgRequest->getVal( $this->IdLevel );
30 $sizetype = $wgRequest->getVal( 'sizetype' );
31 $size = $wgRequest->getIntOrNull( 'size' );
32 $NS = $wgRequest->getIntOrNull( 'namespace' );
33 $indefOnly = $wgRequest->getBool( 'indefonly' ) ? 1 : 0;
34 $cascadeOnly = $wgRequest->getBool('cascadeonly') ? 1 : 0;
35
36 $pager = new ProtectedPagesPager( $this, array(), $type, $level, $NS, $sizetype, $size, $indefOnly, $cascadeOnly );
37
38 $wgOut->addHTML( $this->showOptions( $NS, $type, $level, $sizetype, $size, $indefOnly, $cascadeOnly ) );
39
40 if( $pager->getNumRows() ) {
41 $s = $pager->getNavigationBar();
42 $s .= "<ul>" .
43 $pager->getBody() .
44 "</ul>";
45 $s .= $pager->getNavigationBar();
46 } else {
47 $s = '<p>' . wfMsgHtml( 'protectedpagesempty' ) . '</p>';
48 }
49 $wgOut->addHTML( $s );
50 }
51
52 /**
53 * Callback function to output a restriction
54 * @param $row object Protected title
55 * @return string Formatted <li> element
56 */
57 public function formatRow( $row ) {
58 global $wgUser, $wgLang, $wgContLang;
59
60 wfProfileIn( __METHOD__ );
61
62 static $skin=null;
63
64 if( is_null( $skin ) )
65 $skin = $wgUser->getSkin();
66
67 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
68 $link = $skin->link( $title );
69
70 $description_items = array ();
71
72 $protType = wfMsgHtml( 'restriction-level-' . $row->pr_level );
73
74 $description_items[] = $protType;
75
76 if( $row->pr_cascade ) {
77 $description_items[] = wfMsg( 'protect-summary-cascade' );
78 }
79
80 $expiry_description = '';
81 $stxt = '';
82
83 if( $row->pr_expiry != 'infinity' && strlen($row->pr_expiry) ) {
84 $expiry = Block::decodeExpiry( $row->pr_expiry );
85
86 $expiry_description = wfMsg( 'protect-expiring' , $wgLang->timeanddate( $expiry ) ,
87 $wgLang->date( $expiry ) , $wgLang->time( $expiry ) );
88
89 $description_items[] = htmlspecialchars($expiry_description);
90 }
91
92 if(!is_null($size = $row->page_len)) {
93 $stxt = $wgContLang->getDirMark() . ' ' . $skin->formatRevisionSize( $size );
94 }
95
96 # Show a link to the change protection form for allowed users otherwise a link to the protection log
97 if( $wgUser->isAllowed( 'protect' ) ) {
98 $changeProtection = ' (' . $skin->linkKnown(
99 $title,
100 wfMsgHtml( 'protect_change' ),
101 array(),
102 array( 'action' => 'unprotect' )
103 ) . ')';
104 } else {
105 $ltitle = SpecialPage::getTitleFor( 'Log' );
106 $changeProtection = ' (' . $skin->linkKnown(
107 $ltitle,
108 wfMsgHtml( 'protectlogpage' ),
109 array(),
110 array(
111 'type' => 'protect',
112 'page' => $title->getPrefixedText()
113 )
114 ) . ')';
115 }
116
117 wfProfileOut( __METHOD__ );
118
119 return '<li>' . wfSpecialList( $link . $stxt, implode( $description_items, ', ' ) ) . $changeProtection . "</li>\n";
120 }
121
122 /**
123 * @param $namespace int
124 * @param $type string
125 * @param $level string
126 * @param $minsize int
127 * @param $indefOnly bool
128 * @param $cascadeOnly bool
129 * @return string Input form
130 * @private
131 */
132 protected function showOptions( $namespace, $type='edit', $level, $sizetype, $size, $indefOnly, $cascadeOnly ) {
133 global $wgScript;
134 $title = SpecialPage::getTitleFor( 'ProtectedPages' );
135 return Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
136 Xml::openElement( 'fieldset' ) .
137 Xml::element( 'legend', array(), wfMsg( 'protectedpages' ) ) .
138 Xml::hidden( 'title', $title->getPrefixedDBkey() ) . "\n" .
139 $this->getNamespaceMenu( $namespace ) . "&nbsp;\n" .
140 $this->getTypeMenu( $type ) . "&nbsp;\n" .
141 $this->getLevelMenu( $level ) . "&nbsp;\n" .
142 "<br/><span style='white-space: nowrap'>" .
143 $this->getExpiryCheck( $indefOnly ) . "&nbsp;\n" .
144 $this->getCascadeCheck( $cascadeOnly ) . "&nbsp;\n" .
145 "</span><br/><span style='white-space: nowrap'>" .
146 $this->getSizeLimit( $sizetype, $size ) . "&nbsp;\n" .
147 "</span>" .
148 "&nbsp;" . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
149 Xml::closeElement( 'fieldset' ) .
150 Xml::closeElement( 'form' );
151 }
152
153 /**
154 * Prepare the namespace filter drop-down; standard namespace
155 * selector, sans the MediaWiki namespace
156 *
157 * @param mixed $namespace Pre-select namespace
158 * @return string
159 */
160 protected function getNamespaceMenu( $namespace = null ) {
161 return "<span style='white-space: nowrap'>" .
162 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;'
163 . Xml::namespaceSelector( $namespace, '' ) . "</span>";
164 }
165
166 /**
167 * @return string Formatted HTML
168 */
169 protected function getExpiryCheck( $indefOnly ) {
170 return
171 Xml::checkLabel( wfMsg('protectedpages-indef'), 'indefonly', 'indefonly', $indefOnly ) . "\n";
172 }
173
174 /**
175 * @return string Formatted HTML
176 */
177 protected function getCascadeCheck( $cascadeOnly ) {
178 return
179 Xml::checkLabel( wfMsg('protectedpages-cascade'), 'cascadeonly', 'cascadeonly', $cascadeOnly ) . "\n";
180 }
181
182 /**
183 * @return string Formatted HTML
184 */
185 protected function getSizeLimit( $sizetype, $size ) {
186 $max = $sizetype === 'max';
187
188 return
189 Xml::radioLabel( wfMsg('minimum-size'), 'sizetype', 'min', 'wpmin', !$max ) .
190 '&nbsp;' .
191 Xml::radioLabel( wfMsg('maximum-size'), 'sizetype', 'max', 'wpmax', $max ) .
192 '&nbsp;' .
193 Xml::input( 'size', 9, $size, array( 'id' => 'wpsize' ) ) .
194 '&nbsp;' .
195 Xml::label( wfMsg('pagesize'), 'wpsize' );
196 }
197
198 /**
199 * @return string Formatted HTML
200 */
201 protected function getTypeMenu( $pr_type ) {
202 global $wgRestrictionTypes;
203
204 $m = array(); // Temporary array
205 $options = array();
206
207 // First pass to load the log names
208 foreach( $wgRestrictionTypes as $type ) {
209 $text = wfMsg("restriction-$type");
210 $m[$text] = $type;
211 }
212
213 // Third pass generates sorted XHTML content
214 foreach( $m as $text => $type ) {
215 $selected = ($type == $pr_type );
216 $options[] = Xml::option( $text, $type, $selected ) . "\n";
217 }
218
219 return "<span style='white-space: nowrap'>" .
220 Xml::label( wfMsg('restriction-type') , $this->IdType ) . '&nbsp;' .
221 Xml::tags( 'select',
222 array( 'id' => $this->IdType, 'name' => $this->IdType ),
223 implode( "\n", $options ) ) . "</span>";
224 }
225
226 /**
227 * @return string Formatted HTML
228 */
229 protected function getLevelMenu( $pr_level ) {
230 global $wgRestrictionLevels;
231
232 $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
233 $options = array();
234
235 // First pass to load the log names
236 foreach( $wgRestrictionLevels as $type ) {
237 // Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
238 if( $type !='' && $type !='*') {
239 $text = wfMsg("restriction-level-$type");
240 $m[$text] = $type;
241 }
242 }
243
244 // Third pass generates sorted XHTML content
245 foreach( $m as $text => $type ) {
246 $selected = ($type == $pr_level );
247 $options[] = Xml::option( $text, $type, $selected );
248 }
249
250 return
251 Xml::label( wfMsg('restriction-level') , $this->IdLevel ) . '&nbsp;' .
252 Xml::tags( 'select',
253 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
254 implode( "\n", $options ) );
255 }
256 }
257
258 /**
259 * @todo document
260 * @ingroup Pager
261 */
262 class ProtectedPagesPager extends AlphabeticPager {
263 public $mForm, $mConds;
264 private $type, $level, $namespace, $sizetype, $size, $indefonly;
265
266 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0,
267 $indefonly = false, $cascadeonly = false )
268 {
269 $this->mForm = $form;
270 $this->mConds = $conds;
271 $this->type = ( $type ) ? $type : 'edit';
272 $this->level = $level;
273 $this->namespace = $namespace;
274 $this->sizetype = $sizetype;
275 $this->size = intval($size);
276 $this->indefonly = (bool)$indefonly;
277 $this->cascadeonly = (bool)$cascadeonly;
278 parent::__construct();
279 }
280
281 function getStartBody() {
282 # Do a link batch query
283 $lb = new LinkBatch;
284 while( $row = $this->mResult->fetchObject() ) {
285 $lb->add( $row->page_namespace, $row->page_title );
286 }
287 $lb->execute();
288 return '';
289 }
290
291 function formatRow( $row ) {
292 return $this->mForm->formatRow( $row );
293 }
294
295 function getQueryInfo() {
296 $conds = $this->mConds;
297 $conds[] = '(pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
298 'OR pr_expiry IS NULL)';
299 $conds[] = 'page_id=pr_page';
300 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
301
302 if( $this->sizetype=='min' ) {
303 $conds[] = 'page_len>=' . $this->size;
304 } else if( $this->sizetype=='max' ) {
305 $conds[] = 'page_len<=' . $this->size;
306 }
307
308 if( $this->indefonly ) {
309 $conds[] = "pr_expiry = 'infinity' OR pr_expiry IS NULL";
310 }
311 if( $this->cascadeonly ) {
312 $conds[] = "pr_cascade = '1'";
313 }
314
315 if( $this->level )
316 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
317 if( !is_null($this->namespace) )
318 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
319 return array(
320 'tables' => array( 'page_restrictions', 'page' ),
321 'fields' => 'pr_id,page_namespace,page_title,page_len,pr_type,pr_level,pr_expiry,pr_cascade',
322 'conds' => $conds
323 );
324 }
325
326 function getIndexField() {
327 return 'pr_id';
328 }
329 }
330
331 /**
332 * Constructor
333 */
334 function wfSpecialProtectedpages() {
335 $ppForm = new ProtectedPagesForm();
336 $ppForm->showList();
337 }