Switch some HTMLForms in special pages to OOUI
[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 protected $IdLevel = 'level';
31 protected $IdType = 'type';
32
33 public function __construct() {
34 parent::__construct( 'Protectedtitles' );
35 }
36
37 function execute( $par ) {
38 $this->setHeaders();
39 $this->outputHeader();
40
41 $request = $this->getRequest();
42 $type = $request->getVal( $this->IdType );
43 $level = $request->getVal( $this->IdLevel );
44 $sizetype = $request->getVal( 'sizetype' );
45 $size = $request->getIntOrNull( 'size' );
46 $NS = $request->getIntOrNull( 'namespace' );
47
48 $pager = new ProtectedTitlesPager( $this, array(), $type, $level, $NS, $sizetype, $size );
49
50 $this->getOutput()->addHTML( $this->showOptions( $NS, $type, $level ) );
51
52 if ( $pager->getNumRows() ) {
53 $this->getOutput()->addHTML(
54 $pager->getNavigationBar() .
55 '<ul>' . $pager->getBody() . '</ul>' .
56 $pager->getNavigationBar()
57 );
58 } else {
59 $this->getOutput()->addWikiMsg( 'protectedtitlesempty' );
60 }
61 }
62
63 /**
64 * Callback function to output a restriction
65 *
66 * @param object $row Database row
67 * @return string
68 */
69 function formatRow( $row ) {
70 $title = Title::makeTitleSafe( $row->pt_namespace, $row->pt_title );
71 if ( !$title ) {
72 return Html::rawElement(
73 'li',
74 array(),
75 Html::element(
76 'span',
77 array( 'class' => 'mw-invalidtitle' ),
78 Linker::getInvalidTitleDescription(
79 $this->getContext(),
80 $row->pt_namespace,
81 $row->pt_title
82 )
83 )
84 ) . "\n";
85 }
86
87 $link = Linker::link( $title );
88 $description_items = array();
89 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
90 $protType = $this->msg( 'restriction-level-' . $row->pt_create_perm )->escaped();
91 $description_items[] = $protType;
92 $lang = $this->getLanguage();
93 $expiry = strlen( $row->pt_expiry ) ?
94 $lang->formatExpiry( $row->pt_expiry, TS_MW ) :
95 'infinity';
96
97 if ( $expiry !== 'infinity' ) {
98 $user = $this->getUser();
99 $description_items[] = $this->msg(
100 'protect-expiring-local',
101 $lang->userTimeAndDate( $expiry, $user ),
102 $lang->userDate( $expiry, $user ),
103 $lang->userTime( $expiry, $user )
104 )->escaped();
105 }
106
107 // @todo i18n: This should use a comma separator instead of a hard coded comma, right?
108 return '<li>' . $lang->specialList( $link, implode( $description_items, ', ' ) ) . "</li>\n";
109 }
110
111 /**
112 * @param int $namespace
113 * @param string $type
114 * @param string $level
115 * @return string
116 * @private
117 */
118 function showOptions( $namespace, $type = 'edit', $level ) {
119 $action = htmlspecialchars( wfScript() );
120 $title = $this->getPageTitle();
121 $special = htmlspecialchars( $title->getPrefixedDBkey() );
122
123 return "<form action=\"$action\" method=\"get\">\n" .
124 '<fieldset>' .
125 Xml::element( 'legend', array(), $this->msg( 'protectedtitles' )->text() ) .
126 Html::hidden( 'title', $special ) . "&#160;\n" .
127 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
128 $this->getLevelMenu( $level ) . "&#160;\n" .
129 "&#160;" . Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
130 "</fieldset></form>";
131 }
132
133 /**
134 * Prepare the namespace filter drop-down; standard namespace
135 * selector, sans the MediaWiki namespace
136 *
137 * @param string|null $namespace Pre-select namespace
138 * @return string
139 */
140 function getNamespaceMenu( $namespace = null ) {
141 return Html::namespaceSelector(
142 array(
143 'selected' => $namespace,
144 'all' => '',
145 'label' => $this->msg( 'namespace' )->text()
146 ), array(
147 'name' => 'namespace',
148 'id' => 'namespace',
149 'class' => 'namespaceselector',
150 )
151 );
152 }
153
154 /**
155 * @param string $pr_level Determines which option is selected as default
156 * @return string Formatted HTML
157 * @private
158 */
159 function getLevelMenu( $pr_level ) {
160 // Temporary array
161 $m = array( $this->msg( 'restriction-level-all' )->text() => 0 );
162 $options = array();
163
164 // First pass to load the log names
165 foreach ( $this->getConfig()->get( 'RestrictionLevels' ) as $type ) {
166 if ( $type != '' && $type != '*' ) {
167 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
168 $text = $this->msg( "restriction-level-$type" )->text();
169 $m[$text] = $type;
170 }
171 }
172
173 // Is there only one level (aside from "all")?
174 if ( count( $m ) <= 2 ) {
175 return '';
176 }
177 // Third pass generates sorted XHTML content
178 foreach ( $m as $text => $type ) {
179 $selected = ( $type == $pr_level );
180 $options[] = Xml::option( $text, $type, $selected );
181 }
182
183 return Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . '&#160;' .
184 Xml::tags( 'select',
185 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
186 implode( "\n", $options ) );
187 }
188
189 protected function getGroupName() {
190 return 'maintenance';
191 }
192 }
193
194 /**
195 * @todo document
196 * @ingroup Pager
197 */
198 class ProtectedTitlesPager extends AlphabeticPager {
199 public $mForm, $mConds;
200
201 function __construct( $form, $conds = array(), $type, $level, $namespace,
202 $sizetype = '', $size = 0
203 ) {
204 $this->mForm = $form;
205 $this->mConds = $conds;
206 $this->level = $level;
207 $this->namespace = $namespace;
208 $this->size = intval( $size );
209 parent::__construct( $form->getContext() );
210 }
211
212 function getStartBody() {
213 # Do a link batch query
214 $this->mResult->seek( 0 );
215 $lb = new LinkBatch;
216
217 foreach ( $this->mResult as $row ) {
218 $lb->add( $row->pt_namespace, $row->pt_title );
219 }
220
221 $lb->execute();
222
223 return '';
224 }
225
226 /**
227 * @return Title
228 */
229 function getTitle() {
230 return $this->mForm->getTitle();
231 }
232
233 function formatRow( $row ) {
234 return $this->mForm->formatRow( $row );
235 }
236
237 /**
238 * @return array
239 */
240 function getQueryInfo() {
241 $conds = $this->mConds;
242 $conds[] = 'pt_expiry > ' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
243 ' OR pt_expiry IS NULL';
244 if ( $this->level ) {
245 $conds['pt_create_perm'] = $this->level;
246 }
247
248 if ( !is_null( $this->namespace ) ) {
249 $conds[] = 'pt_namespace=' . $this->mDb->addQuotes( $this->namespace );
250 }
251
252 return array(
253 'tables' => 'protected_titles',
254 'fields' => array( 'pt_namespace', 'pt_title', 'pt_create_perm',
255 'pt_expiry', 'pt_timestamp' ),
256 'conds' => $conds
257 );
258 }
259
260 function getIndexField() {
261 return 'pt_timestamp';
262 }
263 }