[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 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
36 $pager = new ProtectedPagesPager( $this, array(), $type, $level, $NS, $sizetype, $size, $indefOnly );
37
38 $wgOut->addHTML( $this->showOptions( $NS, $type, $level, $sizetype, $size, $indefOnly ) );
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 */
55 public function formatRow( $row ) {
56 global $wgUser, $wgLang, $wgContLang;
57
58 wfProfileIn( __METHOD__ );
59
60 static $skin=null;
61
62 if( is_null( $skin ) )
63 $skin = $wgUser->getSkin();
64
65 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
66 $link = $skin->makeLinkObj( $title );
67
68 $description_items = array ();
69
70 $protType = wfMsgHtml( 'restriction-level-' . $row->pr_level );
71
72 $description_items[] = $protType;
73
74 if ( $row->pr_cascade ) {
75 $description_items[] = wfMsg( 'protect-summary-cascade' );
76 }
77
78 $expiry_description = ''; $stxt = '';
79
80 if ( $row->pr_expiry != 'infinity' && strlen($row->pr_expiry) ) {
81 $expiry = Block::decodeExpiry( $row->pr_expiry );
82
83 $expiry_description = wfMsgForContent( 'protect-expiring', $wgLang->timeanddate( $expiry ) );
84
85 $description_items[] = $expiry_description;
86 }
87
88 if (!is_null($size = $row->page_len)) {
89 if ($size == 0)
90 $stxt = ' <small>' . wfMsgHtml('historyempty') . '</small>';
91 else
92 $stxt = ' <small>' . wfMsgHtml('historysize', $wgLang->formatNum( $size ) ) . '</small>';
93 $stxt = $wgContLang->getDirMark() . $stxt;
94 }
95 wfProfileOut( __METHOD__ );
96
97 return '<li>' . wfSpecialList( $link . $stxt, implode( $description_items, ', ' ) ) . "</li>\n";
98 }
99
100 /**
101 * @param $namespace int
102 * @param $type string
103 * @param $level string
104 * @param $minsize int
105 * @param $indefOnly bool
106 * @private
107 */
108 protected function showOptions( $namespace, $type='edit', $level, $sizetype, $size, $indefOnly ) {
109 global $wgScript;
110 $action = htmlspecialchars( $wgScript );
111 $title = SpecialPage::getTitleFor( 'ProtectedPages' );
112 $special = htmlspecialchars( $title->getPrefixedDBkey() );
113 return "<form action=\"$action\" method=\"get\">\n" .
114 '<fieldset>' .
115 Xml::element( 'legend', array(), wfMsg( 'protectedpages' ) ) .
116 Xml::hidden( 'title', $special ) . "&nbsp;\n" .
117 $this->getNamespaceMenu( $namespace ) . "&nbsp;\n" .
118 $this->getTypeMenu( $type ) . "&nbsp;\n" .
119 $this->getLevelMenu( $level ) . "&nbsp;\n" .
120 "<span style='white-space: nowrap'>" .
121 $this->getExpiryCheck( $indefOnly ) . "&nbsp;\n" .
122 $this->getSizeLimit( $sizetype, $size ) . "\n" .
123 "</span>" .
124 "&nbsp;" . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
125 "</fieldset></form>";
126 }
127
128 /**
129 * Prepare the namespace filter drop-down; standard namespace
130 * selector, sans the MediaWiki namespace
131 *
132 * @param mixed $namespace Pre-select namespace
133 * @return string
134 */
135 protected function getNamespaceMenu( $namespace = null ) {
136 return "<span style='white-space: nowrap'>" .
137 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;'
138 . Xml::namespaceSelector( $namespace, '' ) . "</span>";
139 }
140
141 /**
142 * @return string Formatted HTML
143 */
144 protected function getExpiryCheck( $indefOnly ) {
145 $out = Xml::check( 'indefonly', $indefOnly, array('id' => 'indefonly') ) . ' ';
146 $out .= Xml::label( wfMsg("protectedpages-indef"), 'indefonly' ) . "</p>\n";
147 return $out;
148 }
149
150 /**
151 * @return string Formatted HTML
152 */
153 protected function getSizeLimit( $sizetype, $size ) {
154 $out = Xml::radio( 'sizetype', 'min', ($sizetype=='min'), array('id' => 'wpmin') );
155 $out .= Xml::label( wfMsg("minimum-size"), 'wpmin' );
156 $out .= "&nbsp;".Xml::radio( 'sizetype', 'max', ($sizetype=='max'), array('id' => 'wpmax') );
157 $out .= Xml::label( wfMsg("maximum-size"), 'wpmax' );
158 $out .= "&nbsp;".Xml::input('size', 9, $size, array( 'id' => 'wpsize' ) );
159 $out .= ' '.wfMsgHtml('pagesize');
160 return $out;
161 }
162
163 /**
164 * @return string Formatted HTML
165 */
166 protected function getTypeMenu( $pr_type ) {
167 global $wgRestrictionTypes;
168
169 $m = array(); // Temporary array
170 $options = array();
171
172 // First pass to load the log names
173 foreach( $wgRestrictionTypes as $type ) {
174 $text = wfMsg("restriction-$type");
175 $m[$text] = $type;
176 }
177
178 // Third pass generates sorted XHTML content
179 foreach( $m as $text => $type ) {
180 $selected = ($type == $pr_type );
181 $options[] = Xml::option( $text, $type, $selected ) . "\n";
182 }
183
184 return "<span style='white-space: nowrap'>" .
185 Xml::label( wfMsg('restriction-type') , $this->IdType ) . '&nbsp;' .
186 Xml::tags( 'select',
187 array( 'id' => $this->IdType, 'name' => $this->IdType ),
188 implode( "\n", $options ) ) . "</span>";
189 }
190
191 /**
192 * @return string Formatted HTML
193 */
194 protected function getLevelMenu( $pr_level ) {
195 global $wgRestrictionLevels;
196
197 $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
198 $options = array();
199
200 // First pass to load the log names
201 foreach( $wgRestrictionLevels as $type ) {
202 if ( $type !='' && $type !='*') {
203 $text = wfMsg("restriction-level-$type");
204 $m[$text] = $type;
205 }
206 }
207
208 // Third pass generates sorted XHTML content
209 foreach( $m as $text => $type ) {
210 $selected = ($type == $pr_level );
211 $options[] = Xml::option( $text, $type, $selected );
212 }
213
214 return
215 Xml::label( wfMsg('restriction-level') , $this->IdLevel ) . '&nbsp;' .
216 Xml::tags( 'select',
217 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
218 implode( "\n", $options ) );
219 }
220 }
221
222 /**
223 * @todo document
224 * @addtogroup Pager
225 */
226 class ProtectedPagesPager extends AlphabeticPager {
227 public $mForm, $mConds;
228 private $type, $level, $namespace, $sizetype, $size, $indefonly;
229
230 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0, $indefonly=false ) {
231 $this->mForm = $form;
232 $this->mConds = $conds;
233 $this->type = ( $type ) ? $type : 'edit';
234 $this->level = $level;
235 $this->namespace = $namespace;
236 $this->sizetype = $sizetype;
237 $this->size = intval($size);
238 $this->indefonly = (bool)$indefonly;
239 parent::__construct();
240 }
241
242 function getStartBody() {
243 wfProfileIn( __METHOD__ );
244 # Do a link batch query
245 $this->mResult->seek( 0 );
246 $lb = new LinkBatch;
247
248 while ( $row = $this->mResult->fetchObject() ) {
249 $lb->add( $row->page_namespace, $row->page_title );
250 }
251
252 $lb->execute();
253 wfProfileOut( __METHOD__ );
254 return '';
255 }
256
257 function formatRow( $row ) {
258 return $this->mForm->formatRow( $row );
259 }
260
261 function getQueryInfo() {
262 $conds = $this->mConds;
263 $conds[] = 'pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
264 $conds[] = 'page_id=pr_page';
265 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
266
267 if( $this->sizetype=='min' ) {
268 $conds[] = 'page_len>=' . $this->size;
269 } else if( $this->sizetype=='max' ) {
270 $conds[] = 'page_len<=' . $this->size;
271 }
272
273 if( $this->indefonly ) {
274 $conds[] = "pr_expiry = 'infinity' OR pr_expiry IS NULL";
275 }
276
277 if( $this->level )
278 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
279 if( !is_null($this->namespace) )
280 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
281 return array(
282 'tables' => array( 'page_restrictions', 'page' ),
283 'fields' => 'pr_id,page_namespace,page_title,page_len,pr_type,pr_level,pr_expiry,pr_cascade',
284 'conds' => $conds
285 );
286 }
287
288 function getIndexField() {
289 return 'pr_id';
290 }
291 }
292
293 /**
294 * Constructor
295 */
296 function wfSpecialProtectedpages() {
297
298 $ppForm = new ProtectedPagesForm();
299
300 $ppForm->showList();
301 }
302
303
304