wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / specials / SpecialAllMessages.php
blob8109cc993a17c673d75decc135183ea56df04ee0
1 <?php
2 /**
3 * Implements Special:Allmessages
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Use this special page to get a list of the MediaWiki system messages.
27 * @file
28 * @ingroup SpecialPage
30 class SpecialAllMessages extends SpecialPage {
31 /**
32 * @var AllMessagesTablePager
34 protected $table;
36 /**
37 * Constructor
39 public function __construct() {
40 parent::__construct( 'Allmessages' );
43 /**
44 * Show the special page
46 * @param string $par Parameter passed to the page or null
48 public function execute( $par ) {
49 $request = $this->getRequest();
50 $out = $this->getOutput();
52 $this->setHeaders();
54 if ( !$this->getConfig()->get( 'UseDatabaseMessages' ) ) {
55 $out->addWikiMsg( 'allmessagesnotsupportedDB' );
57 return;
60 $this->outputHeader( 'allmessagestext' );
61 $out->addModuleStyles( 'mediawiki.special' );
62 $this->addHelpLink( 'Help:System message' );
64 $this->table = new AllMessagesTablePager(
65 $this,
66 array(),
67 wfGetLangObj( $request->getVal( 'lang', $par ) )
70 $this->langcode = $this->table->lang->getCode();
72 $out->addHTML( $this->table->buildForm() );
73 $out->addParserOutputContent( $this->table->getFullOutput() );
76 protected function getGroupName() {
77 return 'wiki';
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.
85 class AllMessagesTablePager extends TablePager {
86 protected $filter, $prefix, $langcode, $displayPrefix;
88 public $mLimitsShown;
90 /**
91 * @var Language
93 public $lang;
95 /**
96 * @var null|bool
98 public $custom;
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 );
109 global $wgContLang;
111 $this->talk = $this->msg( 'talkpagelinktext' )->escaped();
113 $this->lang = ( $langObj ? $langObj : $wgContLang );
114 $this->langcode = $this->lang->getCode();
115 $this->foreign = $this->langcode !== $wgContLang->getCode();
117 $request = $this->getRequest();
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' );
126 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
127 $prefix = $prefix !== '' ?
128 Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) :
129 null;
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;
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 = '';
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 );
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',
169 str_replace( '_', ' ', $this->displayPrefix ),
170 array( 'id' => 'mw-allmessages-form-prefix' )
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' )
185 Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
186 'filter',
187 'all',
188 'mw-allmessages-form-filter-all',
189 ( $this->filter === 'all' )
191 Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
192 'filter',
193 'modified',
194 'mw-allmessages-form-filter-modified',
195 ( $this->filter === 'modified' )
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>" .
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( array( 'id' => 'mw-table_pager_limit_label' ) ) .
210 '</td>
211 <tr>
212 <td></td>
213 <td>' .
214 Xml::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
215 "</td>\n
216 </tr>" .
218 Xml::closeElement( 'table' ) .
219 $this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit' ) ) .
220 Xml::closeElement( 'fieldset' ) .
221 Xml::closeElement( 'form' );
223 return $out;
226 function getAllMessages( $descending ) {
227 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
229 // Normalise message names so they look like page titles and sort correctly - T86139
230 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
232 if ( $descending ) {
233 rsort( $messageNames );
234 } else {
235 asort( $messageNames );
238 return $messageNames;
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.
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
252 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
253 // FIXME: This function should be moved to Language:: or something.
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' )
262 $xNames = array_flip( $messageNames );
264 $pageFlags = $talkFlags = array();
266 foreach ( $res as $s ) {
267 $exists = false;
269 if ( $foreign ) {
270 $titleParts = explode( '/', $s->page_title );
271 if ( count( $titleParts ) === 2 &&
272 $langcode === $titleParts[1] &&
273 isset( $xNames[$titleParts[0]] )
275 $exists = $titleParts[0];
277 } elseif ( isset( $xNames[$s->page_title] ) ) {
278 $exists = $s->page_title;
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;
289 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
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
300 function reallyDoQuery( $offset, $limit, $descending ) {
301 $result = new FakeResultWrapper( array() );
303 $messageNames = $this->getAllMessages( $descending );
304 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
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 )
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] )
322 $count++;
325 if ( $count === $limit ) {
326 break;
330 return $result;
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";
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()
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' )
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' )
395 return $title . ' ' .
396 $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
397 ' ' .
398 $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
400 case 'am_default' :
401 case 'am_actual' :
402 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
405 return '';
408 function formatRow( $row ) {
409 // Do all the normal stuff
410 $s = parent::formatRow( $row );
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 ) );
417 if ( $formatted === '' ) {
418 $formatted = '&#160;';
421 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
422 . "</tr>\n";
425 return $s;
428 function getRowAttrs( $row, $isSecond = false ) {
429 $arr = array();
431 if ( $row->am_customised ) {
432 $arr['class'] = 'allmessages-customised';
435 if ( !$isSecond ) {
436 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
439 return $arr;
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(
449 'lang' => $this->lang->getHtmlCode(),
450 'dir' => $this->lang->getDir(),
451 'class' => $field
456 // This is not actually used, as getStartBody is overridden above
457 function getFieldNames() {
458 return array(
459 'am_title' => $this->msg( 'allmessagesname' )->text(),
460 'am_default' => $this->msg( 'allmessagesdefault' )->text()
464 function getTitle() {
465 return SpecialPage::getTitleFor( 'Allmessages', false );
468 function isFieldSortable( $x ) {
469 return false;
472 function getDefaultSort() {
473 return '';
476 function getQueryInfo() {
477 return '';