Fix some bad "Implements Special:*" comments in includes/specials/ files.
[lhc/web/wiklou.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 * @return string
75 */
76 function formatRow( $row ) {
77 global $wgLang;
78
79 wfProfileIn( __METHOD__ );
80
81 static $skin = null, $infinity = null;
82
83 if( is_null( $skin ) ){
84 $skin = $this->getSkin();
85 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
86 }
87
88 $title = Title::makeTitleSafe( $row->pt_namespace, $row->pt_title );
89 $link = $skin->link( $title );
90
91 $description_items = array ();
92
93 $protType = wfMsgHtml( 'restriction-level-' . $row->pt_create_perm );
94
95 $description_items[] = $protType;
96
97 $expiry = strlen( $row->pt_expiry ) ? $wgLang->formatExpiry( $row->pt_expiry, TS_MW ) : $infinity;
98 if( $expiry != $infinity ) {
99
100 $expiry_description = wfMsg(
101 'protect-expiring-local',
102 $wgLang->timeanddate( $expiry, true ),
103 $wgLang->date( $expiry, true ),
104 $wgLang->time( $expiry, true )
105 );
106
107 $description_items[] = htmlspecialchars($expiry_description);
108 }
109
110 wfProfileOut( __METHOD__ );
111
112 return '<li>' . wfSpecialList( $link, implode( $description_items, ', ' ) ) . "</li>\n";
113 }
114
115 /**
116 * @param $namespace Integer:
117 * @param $type string
118 * @param $level string
119 * @private
120 */
121 function showOptions( $namespace, $type='edit', $level ) {
122 global $wgScript;
123 $action = htmlspecialchars( $wgScript );
124 $title = SpecialPage::getTitleFor( 'Protectedtitles' );
125 $special = htmlspecialchars( $title->getPrefixedDBkey() );
126 return "<form action=\"$action\" method=\"get\">\n" .
127 '<fieldset>' .
128 Xml::element( 'legend', array(), wfMsg( 'protectedtitles' ) ) .
129 Html::hidden( 'title', $special ) . "&#160;\n" .
130 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
131 $this->getLevelMenu( $level ) . "&#160;\n" .
132 "&#160;" . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
133 "</fieldset></form>";
134 }
135
136 /**
137 * Prepare the namespace filter drop-down; standard namespace
138 * selector, sans the MediaWiki namespace
139 *
140 * @param $namespace Mixed: pre-select namespace
141 * @return string
142 */
143 function getNamespaceMenu( $namespace = null ) {
144 return Xml::label( wfMsg( 'namespace' ), 'namespace' )
145 . '&#160;'
146 . Xml::namespaceSelector( $namespace, '' );
147 }
148
149 /**
150 * @return string Formatted HTML
151 * @private
152 */
153 function getLevelMenu( $pr_level ) {
154 global $wgRestrictionLevels;
155
156 $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
157 $options = array();
158
159 // First pass to load the log names
160 foreach( $wgRestrictionLevels as $type ) {
161 if ( $type !='' && $type !='*') {
162 $text = wfMsg("restriction-level-$type");
163 $m[$text] = $type;
164 }
165 }
166 // Is there only one level (aside from "all")?
167 if( count($m) <= 2 ) {
168 return '';
169 }
170 // Third pass generates sorted XHTML content
171 foreach( $m as $text => $type ) {
172 $selected = ($type == $pr_level );
173 $options[] = Xml::option( $text, $type, $selected );
174 }
175
176 return
177 Xml::label( wfMsg('restriction-level') , $this->IdLevel ) . '&#160;' .
178 Xml::tags( 'select',
179 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
180 implode( "\n", $options ) );
181 }
182 }
183
184 /**
185 * @todo document
186 * @ingroup Pager
187 */
188 class ProtectedTitlesPager extends AlphabeticPager {
189 public $mForm, $mConds;
190
191 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0 ) {
192 $this->mForm = $form;
193 $this->mConds = $conds;
194 $this->level = $level;
195 $this->namespace = $namespace;
196 $this->size = intval($size);
197 parent::__construct();
198 }
199
200 function getStartBody() {
201 wfProfileIn( __METHOD__ );
202 # Do a link batch query
203 $this->mResult->seek( 0 );
204 $lb = new LinkBatch;
205
206 foreach ( $this->mResult as $row ) {
207 $lb->add( $row->pt_namespace, $row->pt_title );
208 }
209
210 $lb->execute();
211 wfProfileOut( __METHOD__ );
212 return '';
213 }
214
215 function getTitle() {
216 return SpecialPage::getTitleFor( 'Protectedtitles' );
217 }
218
219 function formatRow( $row ) {
220 return $this->mForm->formatRow( $row );
221 }
222
223 function getQueryInfo() {
224 $conds = $this->mConds;
225 $conds[] = 'pt_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
226 if( $this->level )
227 $conds['pt_create_perm'] = $this->level;
228 if( !is_null($this->namespace) )
229 $conds[] = 'pt_namespace=' . $this->mDb->addQuotes( $this->namespace );
230 return array(
231 'tables' => 'protected_titles',
232 'fields' => 'pt_namespace,pt_title,pt_create_perm,pt_expiry,pt_timestamp',
233 'conds' => $conds
234 );
235 }
236
237 function getIndexField() {
238 return 'pt_timestamp';
239 }
240 }
241