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