3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
23 * Use TablePager for prettified output. We have to pretend that we're
24 * getting data from a table when in fact not all of it comes from the database.
28 use MediaWiki\MediaWikiServices
;
30 class AllMessagesTablePager
extends TablePager
{
32 protected $filter, $prefix, $langcode, $displayPrefix;
46 function __construct( $page, $conds, $langObj = null ) {
47 parent
::__construct( $page->getContext() );
48 $this->mIndexField
= 'am_title';
50 $this->mConds
= $conds;
51 // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
52 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
53 $this->mLimitsShown
= [ 20, 50, 100, 250, 500, 5000 ];
57 $this->talk
= $this->msg( 'talkpagelinktext' )->escaped();
59 $this->lang
= ( $langObj ?
$langObj : $wgContLang );
60 $this->langcode
= $this->lang
->getCode();
61 $this->foreign
= !$this->lang
->equals( $wgContLang );
63 $request = $this->getRequest();
65 $this->filter
= $request->getVal( 'filter', 'all' );
66 if ( $this->filter
=== 'all' ) {
67 $this->custom
= null; // So won't match in either case
69 $this->custom
= ( $this->filter
=== 'unmodified' );
72 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
73 $prefix = $prefix !== '' ?
74 Title
::makeTitleSafe( NS_MEDIAWIKI
, $request->getVal( 'prefix', null ) ) :
77 if ( $prefix !== null ) {
78 $this->displayPrefix
= $prefix->getDBkey();
79 $this->prefix
= '/^' . preg_quote( $this->displayPrefix
, '/' ) . '/i';
81 $this->displayPrefix
= false;
82 $this->prefix
= false;
85 // The suffix that may be needed for message names if we're in a
86 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
87 if ( $this->foreign
) {
88 $this->suffix
= '/' . $this->langcode
;
94 function buildForm() {
95 $attrs = [ 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ];
96 $msg = wfMessage( 'allmessages-language' );
97 $langSelect = Xml
::languageSelector( $this->langcode
, false, null, $attrs, $msg );
99 $out = Xml
::openElement( 'form', [
101 'action' => $this->getConfig()->get( 'Script' ),
102 'id' => 'mw-allmessages-form'
104 Xml
::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
105 Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
106 Xml
::openElement( 'table', [ 'class' => 'mw-allmessages-table' ] ) . "\n" .
108 <td class="mw-label">' .
109 Xml
::label( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
111 <td class=\"mw-input\">" .
115 str_replace( '_', ' ', $this->displayPrefix
),
116 [ 'id' => 'mw-allmessages-form-prefix' ]
121 <td class='mw-label'>" .
122 $this->msg( 'allmessages-filter' )->escaped() .
124 <td class='mw-input'>" .
125 Xml
::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
128 'mw-allmessages-form-filter-unmodified',
129 ( $this->filter
=== 'unmodified' )
131 Xml
::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
134 'mw-allmessages-form-filter-all',
135 ( $this->filter
=== 'all' )
137 Xml
::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
140 'mw-allmessages-form-filter-modified',
141 ( $this->filter
=== 'modified' )
146 <td class=\"mw-label\">" . $langSelect[0] . "</td>\n
147 <td class=\"mw-input\">" . $langSelect[1] . "</td>\n
151 <td class="mw-label">' .
152 Xml
::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
154 <td class="mw-input">' .
155 $this->getLimitSelect( [ 'id' => 'mw-table_pager_limit_label' ] ) .
160 Xml
::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
164 Xml
::closeElement( 'table' ) .
165 $this->getHiddenFields( [ 'title', 'prefix', 'filter', 'lang', 'limit' ] ) .
166 Xml
::closeElement( 'fieldset' ) .
167 Xml
::closeElement( 'form' );
172 function getAllMessages( $descending ) {
173 $messageNames = Language
::getLocalisationCache()->getSubitemList( 'en', 'messages' );
175 // Normalise message names so they look like page titles and sort correctly - T86139
176 $messageNames = array_map( [ $this->lang
, 'ucfirst' ], $messageNames );
179 rsort( $messageNames );
181 asort( $messageNames );
184 return $messageNames;
188 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
189 * Returns [ 'pages' => ..., 'talks' => ... ], where the subarrays have
190 * an entry for each existing page, with the key being the message name and
193 * @param array $messageNames
194 * @param string $langcode What language code
195 * @param bool $foreign Whether the $langcode is not the content language
196 * @return array A 'pages' and 'talks' array with the keys of existing pages
198 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
199 // FIXME: This function should be moved to Language:: or something.
201 $dbr = wfGetDB( DB_REPLICA
);
202 $res = $dbr->select( 'page',
203 [ 'page_namespace', 'page_title' ],
204 [ 'page_namespace' => [ NS_MEDIAWIKI
, NS_MEDIAWIKI_TALK
] ],
206 [ 'USE INDEX' => 'name_title' ]
208 $xNames = array_flip( $messageNames );
210 $pageFlags = $talkFlags = [];
212 foreach ( $res as $s ) {
216 $titleParts = explode( '/', $s->page_title
);
217 if ( count( $titleParts ) === 2 &&
218 $langcode === $titleParts[1] &&
219 isset( $xNames[$titleParts[0]] )
221 $exists = $titleParts[0];
223 } elseif ( isset( $xNames[$s->page_title
] ) ) {
224 $exists = $s->page_title
;
227 $title = Title
::newFromRow( $s );
228 if ( $exists && $title->inNamespace( NS_MEDIAWIKI
) ) {
229 $pageFlags[$exists] = true;
230 } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK
) ) {
231 $talkFlags[$exists] = true;
235 return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
239 * This function normally does a database query to get the results; we need
240 * to make a pretend result using a FakeResultWrapper.
241 * @param string $offset
243 * @param bool $descending
244 * @return FakeResultWrapper
246 function reallyDoQuery( $offset, $limit, $descending ) {
247 $result = new FakeResultWrapper( [] );
249 $messageNames = $this->getAllMessages( $descending );
250 $statuses = self
::getCustomisedStatuses( $messageNames, $this->langcode
, $this->foreign
);
253 foreach ( $messageNames as $key ) {
254 $customised = isset( $statuses['pages'][$key] );
255 if ( $customised !== $this->custom
&&
256 ( $descending && ( $key < $offset ||
!$offset ) ||
!$descending && $key > $offset ) &&
257 ( ( $this->prefix
&& preg_match( $this->prefix
, $key ) ) ||
$this->prefix
=== false )
259 $actual = wfMessage( $key )->inLanguage( $this->langcode
)->plain();
260 $default = wfMessage( $key )->inLanguage( $this->langcode
)->useDatabase( false )->plain();
261 $result->result
[] = [
263 'am_actual' => $actual,
264 'am_default' => $default,
265 'am_customised' => $customised,
266 'am_talk_exists' => isset( $statuses['talks'][$key] )
271 if ( $count === $limit ) {
279 function getStartBody() {
280 $tableClass = $this->getTableClass();
281 return Xml
::openElement( 'table', [
282 'class' => "mw-datatable $tableClass",
283 'id' => 'mw-allmessagestable'
287 <th rowspan=\"2\">" .
288 $this->msg( 'allmessagesname' )->escaped() . "
291 $this->msg( 'allmessagesdefault' )->escaped() .
296 $this->msg( 'allmessagescurrent' )->escaped() .
298 </tr></thead><tbody>\n";
301 function formatValue( $field, $value ) {
302 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
305 $title = Title
::makeTitle( NS_MEDIAWIKI
, $value . $this->suffix
);
306 $talk = Title
::makeTitle( NS_MEDIAWIKI_TALK
, $value . $this->suffix
);
307 $translation = Linker
::makeExternalLink(
308 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
309 'title' => 'Special:SearchTranslations',
310 'group' => 'mediawiki',
311 'grouppath' => 'mediawiki',
312 'language' => $this->getLanguage()->getCode(),
313 'query' => $value . ' ' . $this->msg( $value )->plain()
315 $this->msg( 'allmessages-filter-translate' )->text()
318 if ( $this->mCurrentRow
->am_customised
) {
319 $title = $linkRenderer->makeKnownLink( $title, $this->getLanguage()->lcfirst( $value ) );
321 $title = $linkRenderer->makeBrokenLink(
323 $this->getLanguage()->lcfirst( $value )
326 if ( $this->mCurrentRow
->am_talk_exists
) {
327 $talk = $linkRenderer->makeKnownLink( $talk, $this->talk
);
329 $talk = $linkRenderer->makeBrokenLink(
335 return $title . ' ' .
336 $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
338 $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
342 return Sanitizer
::escapeHtmlAllowEntities( $value );
348 function formatRow( $row ) {
349 // Do all the normal stuff
350 $s = parent
::formatRow( $row );
352 // But if there's a customised message, add that too.
353 if ( $row->am_customised
) {
354 $s .= Xml
::openElement( 'tr', $this->getRowAttrs( $row, true ) );
355 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual
) );
357 if ( $formatted === '' ) {
358 $formatted = ' ';
361 $s .= Xml
::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual
), $formatted )
368 function getRowAttrs( $row, $isSecond = false ) {
371 if ( $row->am_customised
) {
372 $arr['class'] = 'allmessages-customised';
376 $arr['id'] = Sanitizer
::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title
) );
382 function getCellAttrs( $field, $value ) {
383 if ( $this->mCurrentRow
->am_customised
&& $field === 'am_title' ) {
384 return [ 'rowspan' => '2', 'class' => $field ];
385 } elseif ( $field === 'am_title' ) {
386 return [ 'class' => $field ];
389 'lang' => $this->lang
->getHtmlCode(),
390 'dir' => $this->lang
->getDir(),
396 // This is not actually used, as getStartBody is overridden above
397 function getFieldNames() {
399 'am_title' => $this->msg( 'allmessagesname' )->text(),
400 'am_default' => $this->msg( 'allmessagesdefault' )->text()
404 function getTitle() {
405 return SpecialPage
::getTitleFor( 'Allmessages', false );
408 function isFieldSortable( $x ) {
412 function getDefaultSort() {
416 function getQueryInfo() {