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