]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialProtectedpages.php
MediaWiki 1.14.0
[autoinstallsdev/mediawiki.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                 if ( "" != $msg ) {
20                         $wgOut->setSubtitle( $msg );
21                 }
22
23                 // Purge expired entries on one in every 10 queries
24                 if ( !mt_rand( 0, 10 ) ) {
25                         Title::purgeExpiredRestrictions();
26                 }
27
28                 $type = $wgRequest->getVal( $this->IdType );
29                 $level = $wgRequest->getVal( $this->IdLevel );
30                 $sizetype = $wgRequest->getVal( 'sizetype' );
31                 $size = $wgRequest->getIntOrNull( 'size' );
32                 $NS = $wgRequest->getIntOrNull( 'namespace' );
33                 $indefOnly = $wgRequest->getBool( 'indefonly' ) ? 1 : 0;
34                 $cascadeOnly = $wgRequest->getBool('cascadeonly') ? 1 : 0;
35
36                 $pager = new ProtectedPagesPager( $this, array(), $type, $level, $NS, $sizetype, $size, $indefOnly, $cascadeOnly );
37
38                 $wgOut->addHTML( $this->showOptions( $NS, $type, $level, $sizetype, $size, $indefOnly, $cascadeOnly ) );
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 = wfMsg( 'protect-expiring' , $wgLang->timeanddate( $expiry ) , $wgLang->date( $expiry ) , $wgLang->time( $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          * @param $cascadeOnly bool
115          * @return string Input form
116          * @private
117          */
118         protected function showOptions( $namespace, $type='edit', $level, $sizetype, $size, $indefOnly, $cascadeOnly ) {
119                 global $wgScript;
120                 $title = SpecialPage::getTitleFor( 'ProtectedPages' );
121                 return Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
122                         Xml::openElement( 'fieldset' ) .
123                         Xml::element( 'legend', array(), wfMsg( 'protectedpages' ) ) .
124                         Xml::hidden( 'title', $title->getPrefixedDBkey() ) . "\n" .
125                         $this->getNamespaceMenu( $namespace ) . "&nbsp;\n" .
126                         $this->getTypeMenu( $type ) . "&nbsp;\n" .
127                         $this->getLevelMenu( $level ) . "&nbsp;\n" .
128                         "<br/><span style='white-space: nowrap'>" .
129                         $this->getExpiryCheck( $indefOnly ) . "&nbsp;\n" .
130                         $this->getCascadeCheck( $cascadeOnly ) . "&nbsp;\n" .
131                         "</span><br/><span style='white-space: nowrap'>" .
132                         $this->getSizeLimit( $sizetype, $size ) . "&nbsp;\n" .
133                         "</span>" .
134                         "&nbsp;" . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
135                         Xml::closeElement( 'fieldset' ) .
136                         Xml::closeElement( 'form' );
137         }
138
139         /**
140          * Prepare the namespace filter drop-down; standard namespace
141          * selector, sans the MediaWiki namespace
142          *
143          * @param mixed $namespace Pre-select namespace
144          * @return string
145          */
146         protected function getNamespaceMenu( $namespace = null ) {
147                 return "<span style='white-space: nowrap'>" .
148                         Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;'
149                         . Xml::namespaceSelector( $namespace, '' ) . "</span>";
150         }
151
152         /**
153          * @return string Formatted HTML
154          */
155         protected function getExpiryCheck( $indefOnly ) {
156                 return
157                         Xml::checkLabel( wfMsg('protectedpages-indef'), 'indefonly', 'indefonly', $indefOnly ) . "\n";
158         }
159         
160         /**
161          * @return string Formatted HTML
162          */
163         protected function getCascadeCheck( $cascadeOnly ) {
164                 return
165                         Xml::checkLabel( wfMsg('protectedpages-cascade'), 'cascadeonly', 'cascadeonly', $cascadeOnly ) . "\n";
166         }
167
168         /**
169          * @return string Formatted HTML
170          */
171         protected function getSizeLimit( $sizetype, $size ) {
172                 $max = $sizetype === 'max';
173
174                 return
175                         Xml::radioLabel( wfMsg('minimum-size'), 'sizetype', 'min', 'wpmin', !$max ) .
176                         '&nbsp;' .
177                         Xml::radioLabel( wfMsg('maximum-size'), 'sizetype', 'max', 'wpmax', $max ) .
178                         '&nbsp;' .
179                         Xml::input( 'size', 9, $size, array( 'id' => 'wpsize' ) ) .
180                         '&nbsp;' .
181                         Xml::label( wfMsg('pagesize'), 'wpsize' );
182         }
183
184         /**
185          * @return string Formatted HTML
186          */
187         protected function getTypeMenu( $pr_type ) {
188                 global $wgRestrictionTypes;
189
190                 $m = array(); // Temporary array
191                 $options = array();
192
193                 // First pass to load the log names
194                 foreach( $wgRestrictionTypes as $type ) {
195                         $text = wfMsg("restriction-$type");
196                         $m[$text] = $type;
197                 }
198
199                 // Third pass generates sorted XHTML content
200                 foreach( $m as $text => $type ) {
201                         $selected = ($type == $pr_type );
202                         $options[] = Xml::option( $text, $type, $selected ) . "\n";
203                 }
204
205                 return "<span style='white-space: nowrap'>" .
206                         Xml::label( wfMsg('restriction-type') , $this->IdType ) . '&nbsp;' .
207                         Xml::tags( 'select',
208                                 array( 'id' => $this->IdType, 'name' => $this->IdType ),
209                                 implode( "\n", $options ) ) . "</span>";
210         }
211
212         /**
213          * @return string Formatted HTML
214          */
215         protected function getLevelMenu( $pr_level ) {
216                 global $wgRestrictionLevels;
217
218                 $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
219                 $options = array();
220
221                 // First pass to load the log names
222                 foreach( $wgRestrictionLevels as $type ) {
223                         if ( $type !='' && $type !='*') {
224                                 $text = wfMsg("restriction-level-$type");
225                                 $m[$text] = $type;
226                         }
227                 }
228
229                 // Third pass generates sorted XHTML content
230                 foreach( $m as $text => $type ) {
231                         $selected = ($type == $pr_level );
232                         $options[] = Xml::option( $text, $type, $selected );
233                 }
234
235                 return
236                         Xml::label( wfMsg('restriction-level') , $this->IdLevel ) . '&nbsp;' .
237                         Xml::tags( 'select',
238                                 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
239                                 implode( "\n", $options ) );
240         }
241 }
242
243 /**
244  * @todo document
245  * @ingroup Pager
246  */
247 class ProtectedPagesPager extends AlphabeticPager {
248         public $mForm, $mConds;
249         private $type, $level, $namespace, $sizetype, $size, $indefonly;
250
251         function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', 
252                                                         $size=0, $indefonly = false, $cascadeonly = false ) {
253                 $this->mForm = $form;
254                 $this->mConds = $conds;
255                 $this->type = ( $type ) ? $type : 'edit';
256                 $this->level = $level;
257                 $this->namespace = $namespace;
258                 $this->sizetype = $sizetype;
259                 $this->size = intval($size);
260                 $this->indefonly = (bool)$indefonly;
261                 $this->cascadeonly = (bool)$cascadeonly;
262                 parent::__construct();
263         }
264
265         function getStartBody() {
266                 wfProfileIn( __METHOD__ );
267                 # Do a link batch query
268                 $lb = new LinkBatch;
269                 while( $row = $this->mResult->fetchObject() ) {
270                         $lb->add( $row->page_namespace, $row->page_title );
271                 }
272                 $lb->execute();
273
274                 wfProfileOut( __METHOD__ );
275                 return '';
276         }
277
278         function formatRow( $row ) {
279                 return $this->mForm->formatRow( $row );
280         }
281
282         function getQueryInfo() {
283                 $conds = $this->mConds;
284                 $conds[] = 'pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
285                 $conds[] = 'page_id=pr_page';
286                 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
287
288                 if( $this->sizetype=='min' ) {
289                         $conds[] = 'page_len>=' . $this->size;
290                 } else if( $this->sizetype=='max' ) {
291                         $conds[] = 'page_len<=' . $this->size;
292                 }
293
294                 if( $this->indefonly ) {
295                         $conds[] = "pr_expiry = 'infinity' OR pr_expiry IS NULL";
296                 }
297                 if ( $this->cascadeonly ) {
298                         $conds[] = "pr_cascade = '1'";
299                 }
300
301                 if( $this->level )
302                         $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
303                 if( !is_null($this->namespace) )
304                         $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
305                 return array(
306                         'tables' => array( 'page_restrictions', 'page' ),
307                         'fields' => 'pr_id,page_namespace,page_title,page_len,pr_type,pr_level,pr_expiry,pr_cascade',
308                         'conds' => $conds
309                 );
310         }
311
312         function getIndexField() {
313                 return 'pr_id';
314         }
315 }
316
317 /**
318  * Constructor
319  */
320 function wfSpecialProtectedpages() {
321
322         $ppForm = new ProtectedPagesForm();
323
324         $ppForm->showList();
325 }