* Add a link to the change protection form for allowed users otherwise a link to...
[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 * @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->makeLinkObj( $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 = wfMsgForContent( 'protect-expiring', $wgLang->timeanddate( $expiry ) );
87
88 $description_items[] = $expiry_description;
89 }
90
91 if (!is_null($size = $row->page_len)) {
92 if ($size == 0)
93 $stxt = ' <small>' . wfMsgHtml('historyempty') . '</small>';
94 else
95 $stxt = ' <small>' . wfMsgHtml('historysize', $wgLang->formatNum( $size ) ) . '</small>';
96 $stxt = $wgContLang->getDirMark() . $stxt;
97 }
98
99 # Show a link to the change protection form for allowed users otherwise a link to the protection log
100 if( $wgUser->isAllowed( 'protect' ) ) {
101 $changeProtection = ' (' . $skin->makeKnownLinkObj( $title, wfMsgHtml( 'protect_change' ), 'action=unprotect' ) . ')';
102 } else {
103 $ltitle = SpecialPage::getTitleFor( 'Log' );
104 $changeProtection = ' (' . $skin->makeKnownLinkObj( $ltitle, wfMsgHtml( 'protectlogpage' ), 'type=protect&page=' . $title->getPrefixedUrl() ) . ')';
105 }
106
107 wfProfileOut( __METHOD__ );
108
109 return '<li>' . wfSpecialList( $link . $stxt, implode( $description_items, ', ' ) ) . $changeProtection . "</li>\n";
110 }
111
112 /**
113 * @param $namespace int
114 * @param $type string
115 * @param $level string
116 * @param $minsize int
117 * @param $indefOnly bool
118 * @return string Input form
119 * @private
120 */
121 protected function showOptions( $namespace, $type='edit', $level, $sizetype, $size, $indefOnly ) {
122 global $wgScript;
123 $title = SpecialPage::getTitleFor( 'ProtectedPages' );
124 return Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
125 Xml::openElement( 'fieldset' ) .
126 Xml::element( 'legend', array(), wfMsg( 'protectedpages' ) ) .
127 Xml::hidden( 'title', $title->getPrefixedDBkey() ) . "&nbsp;\n" .
128 $this->getNamespaceMenu( $namespace ) . "&nbsp;\n" .
129 $this->getTypeMenu( $type ) . "&nbsp;\n" .
130 $this->getLevelMenu( $level ) . "&nbsp;\n" .
131 "<br/><span style='white-space: nowrap'>&nbsp;&nbsp;" .
132 $this->getExpiryCheck( $indefOnly ) . "&nbsp;\n" .
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 $out = Xml::check( 'indefonly', $indefOnly, array('id' => 'indefonly') ) . ' ';
158 $out .= Xml::label( wfMsg("protectedpages-indef"), 'indefonly' ) . "\n";
159 return $out;
160 }
161
162 /**
163 * @return string Formatted HTML
164 */
165 protected function getSizeLimit( $sizetype, $size ) {
166 $out = Xml::radio( 'sizetype', 'min', ($sizetype=='min'), array('id' => 'wpmin') );
167 $out .= Xml::label( wfMsg("minimum-size"), 'wpmin' );
168 $out .= "&nbsp;".Xml::radio( 'sizetype', 'max', ($sizetype=='max'), array('id' => 'wpmax') );
169 $out .= Xml::label( wfMsg("maximum-size"), 'wpmax' );
170 $out .= "&nbsp;".Xml::input('size', 9, $size, array( 'id' => 'wpsize' ) );
171 $out .= ' '.wfMsgHtml('pagesize');
172 return $out;
173 }
174
175 /**
176 * @return string Formatted HTML
177 */
178 protected function getTypeMenu( $pr_type ) {
179 global $wgRestrictionTypes;
180
181 $m = array(); // Temporary array
182 $options = array();
183
184 // First pass to load the log names
185 foreach( $wgRestrictionTypes as $type ) {
186 $text = wfMsg("restriction-$type");
187 $m[$text] = $type;
188 }
189
190 // Third pass generates sorted XHTML content
191 foreach( $m as $text => $type ) {
192 $selected = ($type == $pr_type );
193 $options[] = Xml::option( $text, $type, $selected ) . "\n";
194 }
195
196 return "<span style='white-space: nowrap'>" .
197 Xml::label( wfMsg('restriction-type') , $this->IdType ) . '&nbsp;' .
198 Xml::tags( 'select',
199 array( 'id' => $this->IdType, 'name' => $this->IdType ),
200 implode( "\n", $options ) ) . "</span>";
201 }
202
203 /**
204 * @return string Formatted HTML
205 */
206 protected function getLevelMenu( $pr_level ) {
207 global $wgRestrictionLevels;
208
209 $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
210 $options = array();
211
212 // First pass to load the log names
213 foreach( $wgRestrictionLevels as $type ) {
214 if ( $type !='' && $type !='*') {
215 $text = wfMsg("restriction-level-$type");
216 $m[$text] = $type;
217 }
218 }
219
220 // Third pass generates sorted XHTML content
221 foreach( $m as $text => $type ) {
222 $selected = ($type == $pr_level );
223 $options[] = Xml::option( $text, $type, $selected );
224 }
225
226 return
227 Xml::label( wfMsg('restriction-level') , $this->IdLevel ) . '&nbsp;' .
228 Xml::tags( 'select',
229 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
230 implode( "\n", $options ) );
231 }
232 }
233
234 /**
235 * @todo document
236 * @addtogroup Pager
237 */
238 class ProtectedPagesPager extends AlphabeticPager {
239 public $mForm, $mConds;
240 private $type, $level, $namespace, $sizetype, $size, $indefonly;
241
242 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0, $indefonly=false ) {
243 $this->mForm = $form;
244 $this->mConds = $conds;
245 $this->type = ( $type ) ? $type : 'edit';
246 $this->level = $level;
247 $this->namespace = $namespace;
248 $this->sizetype = $sizetype;
249 $this->size = intval($size);
250 $this->indefonly = (bool)$indefonly;
251 parent::__construct();
252 }
253
254 function getStartBody() {
255 wfProfileIn( __METHOD__ );
256 # Do a link batch query
257 $lb = new LinkBatch;
258 while( $row = $this->mResult->fetchObject() ) {
259 $lb->add( $row->page_namespace, $row->page_title );
260 }
261 $lb->execute();
262
263 wfProfileOut( __METHOD__ );
264 return '';
265 }
266
267 function formatRow( $row ) {
268 return $this->mForm->formatRow( $row );
269 }
270
271 function getQueryInfo() {
272 $conds = $this->mConds;
273 $conds[] = 'pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
274 $conds[] = 'page_id=pr_page';
275 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
276
277 if( $this->sizetype=='min' ) {
278 $conds[] = 'page_len>=' . $this->size;
279 } else if( $this->sizetype=='max' ) {
280 $conds[] = 'page_len<=' . $this->size;
281 }
282
283 if( $this->indefonly ) {
284 $conds[] = "pr_expiry = 'infinity' OR pr_expiry IS NULL";
285 }
286
287 if( $this->level )
288 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
289 if( !is_null($this->namespace) )
290 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
291 return array(
292 'tables' => array( 'page_restrictions', 'page' ),
293 'fields' => 'pr_id,page_namespace,page_title,page_len,pr_type,pr_level,pr_expiry,pr_cascade',
294 'conds' => $conds
295 );
296 }
297
298 function getIndexField() {
299 return 'pr_id';
300 }
301 }
302
303 /**
304 * Constructor
305 */
306 function wfSpecialProtectedpages() {
307
308 $ppForm = new ProtectedPagesForm();
309
310 $ppForm->showList();
311 }