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