]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialProtectedtitles.php
MediaWiki 1.17.0
[autoinstallsdev/mediawiki.git] / includes / specials / SpecialProtectedtitles.php
1 <?php
2 /**
3  * Implements Special:Protectedtitles
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  * @file
21  * @ingroup SpecialPage
22  */
23
24 /**
25  * A special page that list protected titles from creation
26  *
27  * @ingroup SpecialPage
28  */
29 class SpecialProtectedtitles extends SpecialPage {
30
31         protected $IdLevel = 'level';
32         protected $IdType  = 'type';
33
34         public function __construct() {
35                 parent::__construct( 'Protectedtitles' );
36         }
37
38         function execute( $par ) {
39                 global $wgOut, $wgRequest;
40
41                 $this->setHeaders();
42                 $this->outputHeader();
43
44                 // Purge expired entries on one in every 10 queries
45                 if ( !mt_rand( 0, 10 ) ) {
46                         Title::purgeExpiredRestrictions();
47                 }
48
49                 $type = $wgRequest->getVal( $this->IdType );
50                 $level = $wgRequest->getVal( $this->IdLevel );
51                 $sizetype = $wgRequest->getVal( 'sizetype' );
52                 $size = $wgRequest->getIntOrNull( 'size' );
53                 $NS = $wgRequest->getIntOrNull( 'namespace' );
54
55                 $pager = new ProtectedTitlesPager( $this, array(), $type, $level, $NS, $sizetype, $size );
56
57                 $wgOut->addHTML( $this->showOptions( $NS, $type, $level ) );
58
59                 if ( $pager->getNumRows() ) {
60                         $s = $pager->getNavigationBar();
61                         $s .= "<ul>" .
62                                 $pager->getBody() .
63                                 "</ul>";
64                         $s .= $pager->getNavigationBar();
65                 } else {
66                         $s = '<p>' . wfMsgHtml( 'protectedtitlesempty' ) . '</p>';
67                 }
68                 $wgOut->addHTML( $s );
69         }
70
71         /**
72          * Callback function to output a restriction
73          */
74         function formatRow( $row ) {
75                 global $wgUser, $wgLang;
76
77                 wfProfileIn( __METHOD__ );
78
79                 static $skin=null;
80
81                 if( is_null( $skin ) )
82                         $skin = $wgUser->getSkin();
83
84                 $title = Title::makeTitleSafe( $row->pt_namespace, $row->pt_title );
85                 $link = $skin->link( $title );
86
87                 $description_items = array ();
88
89                 $protType = wfMsgHtml( 'restriction-level-' . $row->pt_create_perm );
90
91                 $description_items[] = $protType;
92
93                 $stxt = '';
94
95                 if ( $row->pt_expiry != 'infinity' && strlen($row->pt_expiry) ) {
96                         $expiry = Block::decodeExpiry( $row->pt_expiry );
97
98                         $expiry_description = wfMsg( 'protect-expiring', $wgLang->timeanddate( $expiry ) , $wgLang->date( $expiry ) , $wgLang->time( $expiry ) );
99
100                         $description_items[] = $expiry_description;
101                 }
102
103                 wfProfileOut( __METHOD__ );
104
105                 return '<li>' . wfSpecialList( $link . $stxt, implode( $description_items, ', ' ) ) . "</li>\n";
106         }
107
108         /**
109          * @param $namespace Integer:
110          * @param $type string
111          * @param $level string
112          * @private
113          */
114         function showOptions( $namespace, $type='edit', $level ) {
115                 global $wgScript;
116                 $action = htmlspecialchars( $wgScript );
117                 $title = SpecialPage::getTitleFor( 'Protectedtitles' );
118                 $special = htmlspecialchars( $title->getPrefixedDBkey() );
119                 return "<form action=\"$action\" method=\"get\">\n" .
120                         '<fieldset>' .
121                         Xml::element( 'legend', array(), wfMsg( 'protectedtitles' ) ) .
122                         Html::hidden( 'title', $special ) . "&#160;\n" .
123                         $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
124                         $this->getLevelMenu( $level ) . "&#160;\n" .
125                         "&#160;" . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
126                         "</fieldset></form>";
127         }
128
129         /**
130          * Prepare the namespace filter drop-down; standard namespace
131          * selector, sans the MediaWiki namespace
132          *
133          * @param $namespace Mixed: pre-select namespace
134          * @return string
135          */
136         function getNamespaceMenu( $namespace = null ) {
137                 return Xml::label( wfMsg( 'namespace' ), 'namespace' )
138                         . '&#160;'
139                         . Xml::namespaceSelector( $namespace, '' );
140         }
141
142         /**
143          * @return string Formatted HTML
144          * @private
145          */
146         function getLevelMenu( $pr_level ) {
147                 global $wgRestrictionLevels;
148
149                 $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
150                 $options = array();
151
152                 // First pass to load the log names
153                 foreach( $wgRestrictionLevels as $type ) {
154                         if ( $type !='' && $type !='*') {
155                                 $text = wfMsg("restriction-level-$type");
156                                 $m[$text] = $type;
157                         }
158                 }
159                 // Is there only one level (aside from "all")?
160                 if( count($m) <= 2 ) {
161                         return '';
162                 }
163                 // Third pass generates sorted XHTML content
164                 foreach( $m as $text => $type ) {
165                         $selected = ($type == $pr_level );
166                         $options[] = Xml::option( $text, $type, $selected );
167                 }
168
169                 return
170                         Xml::label( wfMsg('restriction-level') , $this->IdLevel ) . '&#160;' .
171                         Xml::tags( 'select',
172                                 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
173                                 implode( "\n", $options ) );
174         }
175 }
176
177 /**
178  * @todo document
179  * @ingroup Pager
180  */
181 class ProtectedTitlesPager extends AlphabeticPager {
182         public $mForm, $mConds;
183
184         function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0 ) {
185                 $this->mForm = $form;
186                 $this->mConds = $conds;
187                 $this->level = $level;
188                 $this->namespace = $namespace;
189                 $this->size = intval($size);
190                 parent::__construct();
191         }
192
193         function getStartBody() {
194                 wfProfileIn( __METHOD__ );
195                 # Do a link batch query
196                 $this->mResult->seek( 0 );
197                 $lb = new LinkBatch;
198
199                 foreach ( $this->mResult as $row ) {
200                         $lb->add( $row->pt_namespace, $row->pt_title );
201                 }
202
203                 $lb->execute();
204                 wfProfileOut( __METHOD__ );
205                 return '';
206         }
207
208         function formatRow( $row ) {
209                 return $this->mForm->formatRow( $row );
210         }
211
212         function getQueryInfo() {
213                 $conds = $this->mConds;
214                 $conds[] = 'pt_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
215                 if( $this->level )
216                         $conds['pt_create_perm'] = $this->level;
217                 if( !is_null($this->namespace) )
218                         $conds[] = 'pt_namespace=' . $this->mDb->addQuotes( $this->namespace );
219                 return array(
220                         'tables' => 'protected_titles',
221                         'fields' => 'pt_namespace,pt_title,pt_create_perm,pt_expiry,pt_timestamp',
222                         'conds' => $conds
223                 );
224         }
225
226         function getIndexField() {
227                 return 'pt_timestamp';
228         }
229 }
230