Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / specials / SpecialAllMessages.php
blob0008b100c17f20d576bc2a30261d10c0d1455e7f
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' );
63 $this->table = new AllmessagesTablePager(
64 $this,
65 array(),
66 wfGetLangObj( $request->getVal( 'lang', $par ) )
69 $this->langcode = $this->table->lang->getCode();
71 $out->addHTML( $this->table->buildForm() );
72 $out->addParserOutputContent( $this->table->getFullOutput() );
75 protected function getGroupName() {
76 return 'wiki';
80 /**
81 * Use TablePager for prettified output. We have to pretend that we're
82 * getting data from a table when in fact not all of it comes from the database.
84 class AllMessagesTablePager extends TablePager {
85 protected $filter, $prefix, $langcode, $displayPrefix;
87 public $mLimitsShown;
89 /**
90 * @var Language
92 public $lang;
94 /**
95 * @var null|bool
97 public $custom;
99 function __construct( $page, $conds, $langObj = null ) {
100 parent::__construct( $page->getContext() );
101 $this->mIndexField = 'am_title';
102 $this->mPage = $page;
103 $this->mConds = $conds;
104 // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
105 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
106 $this->mLimitsShown = array( 20, 50, 100, 250, 500, 5000 );
108 global $wgContLang;
110 $this->talk = $this->msg( 'talkpagelinktext' )->escaped();
112 $this->lang = ( $langObj ? $langObj : $wgContLang );
113 $this->langcode = $this->lang->getCode();
114 $this->foreign = $this->langcode !== $wgContLang->getCode();
116 $request = $this->getRequest();
118 $this->filter = $request->getVal( 'filter', 'all' );
119 if ( $this->filter === 'all' ) {
120 $this->custom = null; // So won't match in either case
121 } else {
122 $this->custom = ( $this->filter === 'unmodified' );
125 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
126 $prefix = $prefix !== '' ?
127 Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) :
128 null;
130 if ( $prefix !== null ) {
131 $this->displayPrefix = $prefix->getDBkey();
132 $this->prefix = '/^' . preg_quote( $this->displayPrefix ) . '/i';
133 } else {
134 $this->displayPrefix = false;
135 $this->prefix = false;
138 // The suffix that may be needed for message names if we're in a
139 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
140 if ( $this->foreign ) {
141 $this->suffix = '/' . $this->langcode;
142 } else {
143 $this->suffix = '';
147 function buildForm() {
148 $attrs = array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' );
149 $msg = wfMessage( 'allmessages-language' );
150 $langSelect = Xml::languageSelector( $this->langcode, false, null, $attrs, $msg );
152 $out = Xml::openElement( 'form', array(
153 'method' => 'get',
154 'action' => $this->getConfig()->get( 'Script' ),
155 'id' => 'mw-allmessages-form'
156 ) ) .
157 Xml::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
158 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
159 Xml::openElement( 'table', array( 'class' => 'mw-allmessages-table' ) ) . "\n" .
160 '<tr>
161 <td class="mw-label">' .
162 Xml::label( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
163 "</td>\n
164 <td class=\"mw-input\">" .
165 Xml::input(
166 'prefix',
168 str_replace( '_', ' ', $this->displayPrefix ),
169 array( 'id' => 'mw-allmessages-form-prefix' )
171 "</td>\n
172 </tr>
173 <tr>\n
174 <td class='mw-label'>" .
175 $this->msg( 'allmessages-filter' )->escaped() .
176 "</td>\n
177 <td class='mw-input'>" .
178 Xml::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
179 'filter',
180 'unmodified',
181 'mw-allmessages-form-filter-unmodified',
182 ( $this->filter === 'unmodified' )
184 Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
185 'filter',
186 'all',
187 'mw-allmessages-form-filter-all',
188 ( $this->filter === 'all' )
190 Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
191 'filter',
192 'modified',
193 'mw-allmessages-form-filter-modified',
194 ( $this->filter === 'modified' )
196 "</td>\n
197 </tr>
198 <tr>\n
199 <td class=\"mw-label\">" . $langSelect[0] . "</td>\n
200 <td class=\"mw-input\">" . $langSelect[1] . "</td>\n
201 </tr>" .
203 '<tr>
204 <td class="mw-label">' .
205 Xml::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
206 '</td>
207 <td class="mw-input">' .
208 $this->getLimitSelect() .
209 '</td>
210 <tr>
211 <td></td>
212 <td>' .
213 Xml::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
214 "</td>\n
215 </tr>" .
217 Xml::closeElement( 'table' ) .
218 $this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit' ) ) .
219 Xml::closeElement( 'fieldset' ) .
220 Xml::closeElement( 'form' );
222 return $out;
225 function getAllMessages( $descending ) {
226 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
228 // Normalise message names so they look like page titles and sort correctly - T86139
229 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
231 if ( $descending ) {
232 rsort( $messageNames );
233 } else {
234 asort( $messageNames );
237 return $messageNames;
241 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
242 * Returns array( 'pages' => ..., 'talks' => ... ), where the subarrays have
243 * an entry for each existing page, with the key being the message name and
244 * value arbitrary.
246 * @param array $messageNames
247 * @param string $langcode What language code
248 * @param bool $foreign Whether the $langcode is not the content language
249 * @return array A 'pages' and 'talks' array with the keys of existing pages
251 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
252 // FIXME: This function should be moved to Language:: or something.
254 $dbr = wfGetDB( DB_SLAVE );
255 $res = $dbr->select( 'page',
256 array( 'page_namespace', 'page_title' ),
257 array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
258 __METHOD__,
259 array( 'USE INDEX' => 'name_title' )
261 $xNames = array_flip( $messageNames );
263 $pageFlags = $talkFlags = array();
265 foreach ( $res as $s ) {
266 $exists = false;
268 if ( $foreign ) {
269 $titleParts = explode( '/', $s->page_title );
270 if ( count( $titleParts ) === 2 &&
271 $langcode === $titleParts[1] &&
272 isset( $xNames[$titleParts[0]] )
274 $exists = $titleParts[0];
276 } elseif ( isset( $xNames[$s->page_title] ) ) {
277 $exists = $s->page_title;
280 $title = Title::newFromRow( $s );
281 if ( $exists && $title->inNamespace( NS_MEDIAWIKI ) ) {
282 $pageFlags[$exists] = true;
283 } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK ) ) {
284 $talkFlags[$exists] = true;
288 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
292 * This function normally does a database query to get the results; we need
293 * to make a pretend result using a FakeResultWrapper.
294 * @param string $offset
295 * @param int $limit
296 * @param bool $descending
297 * @return FakeResultWrapper
299 function reallyDoQuery( $offset, $limit, $descending ) {
300 $result = new FakeResultWrapper( array() );
302 $messageNames = $this->getAllMessages( $descending );
303 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
305 $count = 0;
306 foreach ( $messageNames as $key ) {
307 $customised = isset( $statuses['pages'][$key] );
308 if ( $customised !== $this->custom &&
309 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
310 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
312 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
313 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
314 $result->result[] = array(
315 'am_title' => $key,
316 'am_actual' => $actual,
317 'am_default' => $default,
318 'am_customised' => $customised,
319 'am_talk_exists' => isset( $statuses['talks'][$key] )
321 $count++;
324 if ( $count === $limit ) {
325 break;
329 return $result;
332 function getStartBody() {
333 $tableClass = $this->getTableClass();
334 return Xml::openElement( 'table', array(
335 'class' => "mw-datatable $tableClass",
336 'id' => 'mw-allmessagestable'
337 ) ) .
338 "\n" .
339 "<thead><tr>
340 <th rowspan=\"2\">" .
341 $this->msg( 'allmessagesname' )->escaped() . "
342 </th>
343 <th>" .
344 $this->msg( 'allmessagesdefault' )->escaped() .
345 "</th>
346 </tr>\n
347 <tr>
348 <th>" .
349 $this->msg( 'allmessagescurrent' )->escaped() .
350 "</th>
351 </tr></thead><tbody>\n";
354 function formatValue( $field, $value ) {
355 switch ( $field ) {
356 case 'am_title' :
357 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
358 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
359 $translation = Linker::makeExternalLink(
360 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( array(
361 'title' => 'Special:SearchTranslations',
362 'group' => 'mediawiki',
363 'grouppath' => 'mediawiki',
364 'query' => 'language:' . $this->getLanguage()->getCode() . '^25 ' .
365 'messageid:"MediaWiki:' . $value . '"^10 "' .
366 $this->msg( $value )->inLanguage( 'en' )->plain() . '"'
367 ) ),
368 $this->msg( 'allmessages-filter-translate' )->text()
371 if ( $this->mCurrentRow->am_customised ) {
372 $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
373 } else {
374 $title = Linker::link(
375 $title,
376 $this->getLanguage()->lcfirst( $value ),
377 array(),
378 array(),
379 array( 'broken' )
382 if ( $this->mCurrentRow->am_talk_exists ) {
383 $talk = Linker::linkKnown( $talk, $this->talk );
384 } else {
385 $talk = Linker::link(
386 $talk,
387 $this->talk,
388 array(),
389 array(),
390 array( 'broken' )
394 return $title . ' '
395 . $this->msg( 'parentheses' )->rawParams( $talk )->escaped()
396 . ' '
397 . $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
399 case 'am_default' :
400 case 'am_actual' :
401 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
404 return '';
407 function formatRow( $row ) {
408 // Do all the normal stuff
409 $s = parent::formatRow( $row );
411 // But if there's a customised message, add that too.
412 if ( $row->am_customised ) {
413 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
414 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
416 if ( $formatted === '' ) {
417 $formatted = '&#160;';
420 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
421 . "</tr>\n";
424 return $s;
427 function getRowAttrs( $row, $isSecond = false ) {
428 $arr = array();
430 if ( $row->am_customised ) {
431 $arr['class'] = 'allmessages-customised';
434 if ( !$isSecond ) {
435 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
438 return $arr;
441 function getCellAttrs( $field, $value ) {
442 if ( $this->mCurrentRow->am_customised && $field === 'am_title' ) {
443 return array( 'rowspan' => '2', 'class' => $field );
444 } elseif ( $field === 'am_title' ) {
445 return array( 'class' => $field );
446 } else {
447 return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir(), 'class' => $field );
451 // This is not actually used, as getStartBody is overridden above
452 function getFieldNames() {
453 return array(
454 'am_title' => $this->msg( 'allmessagesname' )->text(),
455 'am_default' => $this->msg( 'allmessagesdefault' )->text()
459 function getTitle() {
460 return SpecialPage::getTitleFor( 'Allmessages', false );
463 function isFieldSortable( $x ) {
464 return false;
467 function getDefaultSort() {
468 return '';
471 function getQueryInfo() {
472 return '';