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