Clarification for $wgRestrictionLevels
[lhc/web/wiklou.git] / includes / SpecialProtectedpages.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * @todo document
9 * @addtogroup SpecialPage
10 */
11 class ProtectedPagesForm {
12
13 protected $IdLevel = 'level';
14 protected $IdType = 'type';
15
16 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
35 $pager = new ProtectedPagesPager( $this, array(), $type, $level, $NS, $sizetype, $size );
36
37 $wgOut->addHTML( $this->showOptions( $NS, $type, $level, $sizetype, $size ) );
38
39 if ( $pager->getNumRows() ) {
40 $s = $pager->getNavigationBar();
41 $s .= "<ul>" .
42 $pager->getBody() .
43 "</ul>";
44 $s .= $pager->getNavigationBar();
45 } else {
46 $s = '<p>' . wfMsgHtml( 'protectedpagesempty' ) . '</p>';
47 }
48 $wgOut->addHTML( $s );
49 }
50
51 /**
52 * Callback function to output a restriction
53 */
54 function formatRow( $row ) {
55 global $wgUser, $wgLang;
56
57 wfProfileIn( __METHOD__ );
58
59 static $skin=null;
60
61 if( is_null( $skin ) )
62 $skin = $wgUser->getSkin();
63
64 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
65 $link = $skin->makeLinkObj( $title );
66
67 $description_items = array ();
68
69 $protType = wfMsgHtml( 'restriction-level-' . $row->pr_level );
70
71 $description_items[] = $protType;
72
73 $expiry_description = ''; $stxt = '';
74
75 if ( $row->pr_expiry != 'infinity' && strlen($row->pr_expiry) ) {
76 $expiry = Block::decodeExpiry( $row->pr_expiry );
77
78 $expiry_description = wfMsgForContent( 'protect-expiring', $wgLang->timeanddate( $expiry ) );
79
80 $description_items[] = $expiry_description;
81 }
82
83 if (!is_null($size = $row->page_len)) {
84 if ($size == 0)
85 $stxt = ' <small>' . wfMsgHtml('historyempty') . '</small>';
86 else
87 $stxt = ' <small>' . wfMsgHtml('historysize', $wgLang->formatNum( $size ) ) . '</small>';
88 }
89 wfProfileOut( __METHOD__ );
90
91 return '<li>' . wfSpecialList( $link . $stxt, implode( $description_items, ', ' ) ) . "</li>\n";
92 }
93
94 /**
95 * @param $namespace int
96 * @param $type string
97 * @param $level string
98 * @param $minsize int
99 * @private
100 */
101 function showOptions( $namespace, $type='edit', $level, $sizetype, $size ) {
102 global $wgScript;
103 $action = htmlspecialchars( $wgScript );
104 $title = SpecialPage::getTitleFor( 'ProtectedPages' );
105 $special = htmlspecialchars( $title->getPrefixedDBkey() );
106 return "<form action=\"$action\" method=\"get\">\n" .
107 '<fieldset>' .
108 Xml::element( 'legend', array(), wfMsg( 'protectedpages' ) ) .
109 Xml::hidden( 'title', $special ) . "&nbsp\n" .
110 $this->getNamespaceMenu( $namespace ) . "&nbsp\n" .
111 $this->getTypeMenu( $type ) . "&nbsp\n" .
112 $this->getLevelMenu( $level ) . "<br/>\n" .
113 $this->getSizeLimit( $sizetype, $size ) . "\n" .
114 "&nbsp" . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
115 "</fieldset></form>";
116 }
117
118 function getNamespaceMenu( $namespace=NULL ) {
119 return "<label for='namespace'>" . wfMsgHtml('namespace') . "</label>" . HTMLnamespaceselector($namespace, '');
120 }
121
122 /**
123 * @return string Formatted HTML
124 * @private
125 */
126 function getSizeLimit( $sizetype, $size ) {
127 $out = Xml::radio( 'sizetype', 'min', ($sizetype=='min'), array('id' => 'wpmin') );
128 $out .= Xml::label( wfMsg("minimum-size"), 'wpmin' );
129 $out .= "&nbsp".Xml::radio( 'sizetype', 'max', ($sizetype=='max'), array('id' => 'wpmax') );
130 $out .= Xml::label( wfMsg("maximum-size"), 'wpmax' );
131 $out .= "&nbsp".Xml::input('size', 9, $size, array( 'id' => 'wpsize' ) );
132 $out .= ' '.wfMsgHtml('pagesize');
133 return $out;
134 }
135
136 /**
137 * @return string Formatted HTML
138 * @private
139 */
140 function getTypeMenu( $pr_type ) {
141 global $wgRestrictionTypes;
142
143 $m = array(); // Temporary array
144 $options = array();
145
146 // First pass to load the log names
147 foreach( $wgRestrictionTypes as $type ) {
148 $text = wfMsg("restriction-$type");
149 $m[$text] = $type;
150 }
151
152 // Third pass generates sorted XHTML content
153 foreach( $m as $text => $type ) {
154 $selected = ($type == $pr_type );
155 $options[] = Xml::option( $text, $type, $selected ) . "\n";
156 }
157
158 return
159 Xml::label( wfMsg('restriction-type') , $this->IdType ) . '&nbsp;' .
160 Xml::tags( 'select',
161 array( 'id' => $this->IdType, 'name' => $this->IdType ),
162 implode( "\n", $options ) );
163 }
164
165 /**
166 * @return string Formatted HTML
167 * @private
168 */
169 function getLevelMenu( $pr_level ) {
170 global $wgRestrictionLevels;
171
172 $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
173 $options = array();
174
175 // First pass to load the log names
176 foreach( $wgRestrictionLevels as $type ) {
177 if ( $type !='' && $type !='*') {
178 $text = wfMsg("restriction-level-$type");
179 $m[$text] = $type;
180 }
181 }
182
183 // Third pass generates sorted XHTML content
184 foreach( $m as $text => $type ) {
185 $selected = ($type == $pr_level );
186 $options[] = Xml::option( $text, $type, $selected );
187 }
188
189 return
190 Xml::label( wfMsg('restriction-level') , $this->IdLevel ) . '&nbsp;' .
191 Xml::tags( 'select',
192 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
193 implode( "\n", $options ) );
194 }
195 }
196
197 /**
198 * @todo document
199 * @addtogroup Pager
200 */
201 class ProtectedPagesPager extends AlphabeticPager {
202 public $mForm, $mConds;
203
204 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0 ) {
205 $this->mForm = $form;
206 $this->mConds = $conds;
207 $this->type = ( $type ) ? $type : 'edit';
208 $this->level = $level;
209 $this->namespace = $namespace;
210 $this->sizetype = $sizetype;
211 $this->size = intval($size);
212 parent::__construct();
213 }
214
215 function getStartBody() {
216 wfProfileIn( __METHOD__ );
217 # Do a link batch query
218 $this->mResult->seek( 0 );
219 $lb = new LinkBatch;
220
221 while ( $row = $this->mResult->fetchObject() ) {
222 $lb->add( $row->page_namespace, $row->page_title );
223 }
224
225 $lb->execute();
226 wfProfileOut( __METHOD__ );
227 return '';
228 }
229
230 function formatRow( $row ) {
231 $block = new Block;
232 return $this->mForm->formatRow( $row );
233 }
234
235 function getQueryInfo() {
236 $conds = $this->mConds;
237 $conds[] = 'pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
238 $conds[] = 'page_id=pr_page';
239 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
240
241 if( $this->sizetype=='min' ) {
242 $conds[] = 'page_len>=' . $this->size;
243 } else if( $this->sizetype=='max' ) {
244 $conds[] = 'page_len<=' . $this->size;
245 }
246
247 if( $this->level )
248 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
249 if( !is_null($this->namespace) )
250 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
251 return array(
252 'tables' => array( 'page_restrictions', 'page' ),
253 'fields' => 'pr_id,page_namespace,page_title,page_len,pr_type,pr_level,pr_expiry',
254 'conds' => $conds
255 );
256 }
257
258 function getIndexField() {
259 return 'pr_id';
260 }
261 }
262
263 /**
264 * Constructor
265 */
266 function wfSpecialProtectedpages() {
267
268 list( $limit, $offset ) = wfCheckLimits();
269
270 $ppForm = new ProtectedPagesForm();
271
272 $ppForm->showList();
273 }
274
275 ?>