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