Switch some HTMLForms in special pages to OOUI
[lhc/web/wiklou.git] / includes / specials / SpecialAllMessages.php
1 <?php
2 /**
3 * Implements Special:Allmessages
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 * Use this special page to get a list of the MediaWiki system messages.
26 *
27 * @file
28 * @ingroup SpecialPage
29 */
30 class SpecialAllMessages extends SpecialPage {
31 /**
32 * @var AllMessagesTablePager
33 */
34 protected $table;
35
36 /**
37 * Constructor
38 */
39 public function __construct() {
40 parent::__construct( 'Allmessages' );
41 }
42
43 /**
44 * Show the special page
45 *
46 * @param string $par Parameter passed to the page or null
47 */
48 public function execute( $par ) {
49 $request = $this->getRequest();
50 $out = $this->getOutput();
51
52 $this->setHeaders();
53
54 if ( !$this->getConfig()->get( 'UseDatabaseMessages' ) ) {
55 $out->addWikiMsg( 'allmessagesnotsupportedDB' );
56
57 return;
58 }
59
60 $this->outputHeader( 'allmessagestext' );
61 $out->addModuleStyles( 'mediawiki.special' );
62 $this->addHelpLink( 'Help:System message' );
63
64 $this->table = new AllMessagesTablePager(
65 $this,
66 array(),
67 wfGetLangObj( $request->getVal( 'lang', $par ) )
68 );
69
70 $this->langcode = $this->table->lang->getCode();
71
72 $out->addHTML( $this->table->buildForm() );
73 $out->addParserOutputContent( $this->table->getFullOutput() );
74 }
75
76 protected function getGroupName() {
77 return 'wiki';
78 }
79 }
80
81 /**
82 * Use TablePager for prettified output. We have to pretend that we're
83 * getting data from a table when in fact not all of it comes from the database.
84 */
85 class AllMessagesTablePager extends TablePager {
86 protected $filter, $prefix, $langcode, $displayPrefix;
87
88 public $mLimitsShown;
89
90 /**
91 * @var Language
92 */
93 public $lang;
94
95 /**
96 * @var null|bool
97 */
98 public $custom;
99
100 function __construct( $page, $conds, $langObj = null ) {
101 parent::__construct( $page->getContext() );
102 $this->mIndexField = 'am_title';
103 $this->mPage = $page;
104 $this->mConds = $conds;
105 // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
106 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
107 $this->mLimitsShown = array( 20, 50, 100, 250, 500, 5000 );
108
109 global $wgContLang;
110
111 $this->talk = $this->msg( 'talkpagelinktext' )->escaped();
112
113 $this->lang = ( $langObj ? $langObj : $wgContLang );
114 $this->langcode = $this->lang->getCode();
115 $this->foreign = $this->langcode !== $wgContLang->getCode();
116
117 $request = $this->getRequest();
118
119 $this->filter = $request->getVal( 'filter', 'all' );
120 if ( $this->filter === 'all' ) {
121 $this->custom = null; // So won't match in either case
122 } else {
123 $this->custom = ( $this->filter === 'unmodified' );
124 }
125
126 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
127 $prefix = $prefix !== '' ?
128 Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) :
129 null;
130
131 if ( $prefix !== null ) {
132 $this->displayPrefix = $prefix->getDBkey();
133 $this->prefix = '/^' . preg_quote( $this->displayPrefix, '/' ) . '/i';
134 } else {
135 $this->displayPrefix = false;
136 $this->prefix = false;
137 }
138
139 // The suffix that may be needed for message names if we're in a
140 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
141 if ( $this->foreign ) {
142 $this->suffix = '/' . $this->langcode;
143 } else {
144 $this->suffix = '';
145 }
146 }
147
148 function buildForm() {
149 $attrs = array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' );
150 $msg = wfMessage( 'allmessages-language' );
151 $langSelect = Xml::languageSelector( $this->langcode, false, null, $attrs, $msg );
152
153 $out = Xml::openElement( 'form', array(
154 'method' => 'get',
155 'action' => $this->getConfig()->get( 'Script' ),
156 'id' => 'mw-allmessages-form'
157 ) ) .
158 Xml::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
159 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
160 Xml::openElement( 'table', array( 'class' => 'mw-allmessages-table' ) ) . "\n" .
161 '<tr>
162 <td class="mw-label">' .
163 Xml::label( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
164 "</td>\n
165 <td class=\"mw-input\">" .
166 Xml::input(
167 'prefix',
168 20,
169 str_replace( '_', ' ', $this->displayPrefix ),
170 array( 'id' => 'mw-allmessages-form-prefix' )
171 ) .
172 "</td>\n
173 </tr>
174 <tr>\n
175 <td class='mw-label'>" .
176 $this->msg( 'allmessages-filter' )->escaped() .
177 "</td>\n
178 <td class='mw-input'>" .
179 Xml::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
180 'filter',
181 'unmodified',
182 'mw-allmessages-form-filter-unmodified',
183 ( $this->filter === 'unmodified' )
184 ) .
185 Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
186 'filter',
187 'all',
188 'mw-allmessages-form-filter-all',
189 ( $this->filter === 'all' )
190 ) .
191 Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
192 'filter',
193 'modified',
194 'mw-allmessages-form-filter-modified',
195 ( $this->filter === 'modified' )
196 ) .
197 "</td>\n
198 </tr>
199 <tr>\n
200 <td class=\"mw-label\">" . $langSelect[0] . "</td>\n
201 <td class=\"mw-input\">" . $langSelect[1] . "</td>\n
202 </tr>" .
203
204 '<tr>
205 <td class="mw-label">' .
206 Xml::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
207 '</td>
208 <td class="mw-input">' .
209 $this->getLimitSelect() .
210 '</td>
211 <tr>
212 <td></td>
213 <td>' .
214 Xml::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
215 "</td>\n
216 </tr>" .
217
218 Xml::closeElement( 'table' ) .
219 $this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit' ) ) .
220 Xml::closeElement( 'fieldset' ) .
221 Xml::closeElement( 'form' );
222
223 return $out;
224 }
225
226 function getAllMessages( $descending ) {
227 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
228
229 // Normalise message names so they look like page titles and sort correctly - T86139
230 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
231
232 if ( $descending ) {
233 rsort( $messageNames );
234 } else {
235 asort( $messageNames );
236 }
237
238 return $messageNames;
239 }
240
241 /**
242 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
243 * Returns array( 'pages' => ..., 'talks' => ... ), where the subarrays have
244 * an entry for each existing page, with the key being the message name and
245 * value arbitrary.
246 *
247 * @param array $messageNames
248 * @param string $langcode What language code
249 * @param bool $foreign Whether the $langcode is not the content language
250 * @return array A 'pages' and 'talks' array with the keys of existing pages
251 */
252 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
253 // FIXME: This function should be moved to Language:: or something.
254
255 $dbr = wfGetDB( DB_SLAVE );
256 $res = $dbr->select( 'page',
257 array( 'page_namespace', 'page_title' ),
258 array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
259 __METHOD__,
260 array( 'USE INDEX' => 'name_title' )
261 );
262 $xNames = array_flip( $messageNames );
263
264 $pageFlags = $talkFlags = array();
265
266 foreach ( $res as $s ) {
267 $exists = false;
268
269 if ( $foreign ) {
270 $titleParts = explode( '/', $s->page_title );
271 if ( count( $titleParts ) === 2 &&
272 $langcode === $titleParts[1] &&
273 isset( $xNames[$titleParts[0]] )
274 ) {
275 $exists = $titleParts[0];
276 }
277 } elseif ( isset( $xNames[$s->page_title] ) ) {
278 $exists = $s->page_title;
279 }
280
281 $title = Title::newFromRow( $s );
282 if ( $exists && $title->inNamespace( NS_MEDIAWIKI ) ) {
283 $pageFlags[$exists] = true;
284 } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK ) ) {
285 $talkFlags[$exists] = true;
286 }
287 }
288
289 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
290 }
291
292 /**
293 * This function normally does a database query to get the results; we need
294 * to make a pretend result using a FakeResultWrapper.
295 * @param string $offset
296 * @param int $limit
297 * @param bool $descending
298 * @return FakeResultWrapper
299 */
300 function reallyDoQuery( $offset, $limit, $descending ) {
301 $result = new FakeResultWrapper( array() );
302
303 $messageNames = $this->getAllMessages( $descending );
304 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
305
306 $count = 0;
307 foreach ( $messageNames as $key ) {
308 $customised = isset( $statuses['pages'][$key] );
309 if ( $customised !== $this->custom &&
310 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
311 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
312 ) {
313 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
314 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
315 $result->result[] = array(
316 'am_title' => $key,
317 'am_actual' => $actual,
318 'am_default' => $default,
319 'am_customised' => $customised,
320 'am_talk_exists' => isset( $statuses['talks'][$key] )
321 );
322 $count++;
323 }
324
325 if ( $count === $limit ) {
326 break;
327 }
328 }
329
330 return $result;
331 }
332
333 function getStartBody() {
334 $tableClass = $this->getTableClass();
335 return Xml::openElement( 'table', array(
336 'class' => "mw-datatable $tableClass",
337 'id' => 'mw-allmessagestable'
338 ) ) .
339 "\n" .
340 "<thead><tr>
341 <th rowspan=\"2\">" .
342 $this->msg( 'allmessagesname' )->escaped() . "
343 </th>
344 <th>" .
345 $this->msg( 'allmessagesdefault' )->escaped() .
346 "</th>
347 </tr>\n
348 <tr>
349 <th>" .
350 $this->msg( 'allmessagescurrent' )->escaped() .
351 "</th>
352 </tr></thead><tbody>\n";
353 }
354
355 function formatValue( $field, $value ) {
356 switch ( $field ) {
357 case 'am_title' :
358 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
359 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
360 $translation = Linker::makeExternalLink(
361 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( array(
362 'title' => 'Special:SearchTranslations',
363 'group' => 'mediawiki',
364 'grouppath' => 'mediawiki',
365 'query' => 'language:' . $this->getLanguage()->getCode() . '^25 ' .
366 'messageid:"MediaWiki:' . $value . '"^10 "' .
367 $this->msg( $value )->inLanguage( 'en' )->plain() . '"'
368 ) ),
369 $this->msg( 'allmessages-filter-translate' )->text()
370 );
371
372 if ( $this->mCurrentRow->am_customised ) {
373 $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
374 } else {
375 $title = Linker::link(
376 $title,
377 $this->getLanguage()->lcfirst( $value ),
378 array(),
379 array(),
380 array( 'broken' )
381 );
382 }
383 if ( $this->mCurrentRow->am_talk_exists ) {
384 $talk = Linker::linkKnown( $talk, $this->talk );
385 } else {
386 $talk = Linker::link(
387 $talk,
388 $this->talk,
389 array(),
390 array(),
391 array( 'broken' )
392 );
393 }
394
395 return $title . ' ' .
396 $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
397 ' ' .
398 $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
399
400 case 'am_default' :
401 case 'am_actual' :
402 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
403 }
404
405 return '';
406 }
407
408 function formatRow( $row ) {
409 // Do all the normal stuff
410 $s = parent::formatRow( $row );
411
412 // But if there's a customised message, add that too.
413 if ( $row->am_customised ) {
414 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
415 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
416
417 if ( $formatted === '' ) {
418 $formatted = '&#160;';
419 }
420
421 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
422 . "</tr>\n";
423 }
424
425 return $s;
426 }
427
428 function getRowAttrs( $row, $isSecond = false ) {
429 $arr = array();
430
431 if ( $row->am_customised ) {
432 $arr['class'] = 'allmessages-customised';
433 }
434
435 if ( !$isSecond ) {
436 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
437 }
438
439 return $arr;
440 }
441
442 function getCellAttrs( $field, $value ) {
443 if ( $this->mCurrentRow->am_customised && $field === 'am_title' ) {
444 return array( 'rowspan' => '2', 'class' => $field );
445 } elseif ( $field === 'am_title' ) {
446 return array( 'class' => $field );
447 } else {
448 return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir(), 'class' => $field );
449 }
450 }
451
452 // This is not actually used, as getStartBody is overridden above
453 function getFieldNames() {
454 return array(
455 'am_title' => $this->msg( 'allmessagesname' )->text(),
456 'am_default' => $this->msg( 'allmessagesdefault' )->text()
457 );
458 }
459
460 function getTitle() {
461 return SpecialPage::getTitleFor( 'Allmessages', false );
462 }
463
464 function isFieldSortable( $x ) {
465 return false;
466 }
467
468 function getDefaultSort() {
469 return '';
470 }
471
472 function getQueryInfo() {
473 return '';
474 }
475 }