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
22 use MediaWiki\MediaWikiServices
;
23 use Wikimedia\Rdbms\FakeResultWrapper
;
26 * Use TablePager for prettified output. We have to pretend that we're
27 * getting data from a table when in fact not all of it comes from the database.
31 class AllMessagesTablePager
extends TablePager
{
59 * @param IContextSource|null $context
60 * @param FormOptions $opts
62 public function __construct( IContextSource
$context = null, FormOptions
$opts ) {
63 parent
::__construct( $context );
65 $this->mIndexField
= 'am_title';
66 // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
67 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
69 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
70 $this->lang
= wfGetLangObj( $opts->getValue( 'lang' ) );
72 $this->langcode
= $this->lang
->getCode();
73 $this->foreign
= !$this->lang
->equals( $contLang );
75 $filter = $opts->getValue( 'filter' );
76 if ( $filter === 'all' ) {
77 $this->custom
= null; // So won't match in either case
79 $this->custom
= ( $filter === 'unmodified' );
82 $prefix = $this->getLanguage()->ucfirst( $opts->getValue( 'prefix' ) );
83 $prefix = $prefix !== '' ?
84 Title
::makeTitleSafe( NS_MEDIAWIKI
, $opts->getValue( 'prefix' ) ) :
87 if ( $prefix !== null ) {
88 $displayPrefix = $prefix->getDBkey();
89 $this->prefix
= '/^' . preg_quote( $displayPrefix, '/' ) . '/i';
91 $this->prefix
= false;
94 // The suffix that may be needed for message names if we're in a
95 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
96 if ( $this->foreign
) {
97 $this->suffix
= '/' . $this->langcode
;
103 function getAllMessages( $descending ) {
104 $messageNames = Language
::getLocalisationCache()->getSubitemList( 'en', 'messages' );
106 // Normalise message names so they look like page titles and sort correctly - T86139
107 $messageNames = array_map( [ $this->lang
, 'ucfirst' ], $messageNames );
110 rsort( $messageNames );
112 asort( $messageNames );
115 return $messageNames;
119 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
120 * Returns [ 'pages' => ..., 'talks' => ... ], where the subarrays have
121 * an entry for each existing page, with the key being the message name and
124 * @param array $messageNames
125 * @param string $langcode What language code
126 * @param bool $foreign Whether the $langcode is not the content language
127 * @return array A 'pages' and 'talks' array with the keys of existing pages
129 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
130 // FIXME: This function should be moved to Language:: or something.
132 $dbr = wfGetDB( DB_REPLICA
);
133 $res = $dbr->select( 'page',
134 [ 'page_namespace', 'page_title' ],
135 [ 'page_namespace' => [ NS_MEDIAWIKI
, NS_MEDIAWIKI_TALK
] ],
137 [ 'USE INDEX' => 'name_title' ]
139 $xNames = array_flip( $messageNames );
141 $pageFlags = $talkFlags = [];
143 foreach ( $res as $s ) {
147 $titleParts = explode( '/', $s->page_title
);
148 if ( count( $titleParts ) === 2 &&
149 $langcode === $titleParts[1] &&
150 isset( $xNames[$titleParts[0]] )
152 $exists = $titleParts[0];
154 } elseif ( isset( $xNames[$s->page_title
] ) ) {
155 $exists = $s->page_title
;
158 $title = Title
::newFromRow( $s );
159 if ( $exists && $title->inNamespace( NS_MEDIAWIKI
) ) {
160 $pageFlags[$exists] = true;
161 } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK
) ) {
162 $talkFlags[$exists] = true;
166 return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
170 * This function normally does a database query to get the results; we need
171 * to make a pretend result using a FakeResultWrapper.
172 * @param string $offset
175 * @return FakeResultWrapper
177 function reallyDoQuery( $offset, $limit, $order ) {
178 $asc = ( $order === self
::QUERY_ASCENDING
);
180 $messageNames = $this->getAllMessages( $order );
181 $statuses = self
::getCustomisedStatuses( $messageNames, $this->langcode
, $this->foreign
);
185 foreach ( $messageNames as $key ) {
186 $customised = isset( $statuses['pages'][$key] );
187 if ( $customised !== $this->custom
&&
188 ( $asc && ( $key < $offset ||
!$offset ) ||
!$asc && $key > $offset ) &&
189 ( ( $this->prefix
&& preg_match( $this->prefix
, $key ) ) ||
$this->prefix
=== false )
191 $actual = $this->msg( $key )->inLanguage( $this->lang
)->plain();
192 $default = $this->msg( $key )->inLanguage( $this->lang
)->useDatabase( false )->plain();
195 'am_actual' => $actual,
196 'am_default' => $default,
197 'am_customised' => $customised,
198 'am_talk_exists' => isset( $statuses['talks'][$key] )
203 if ( $count === $limit ) {
208 return new FakeResultWrapper( $rows );
211 protected function getStartBody() {
212 $tableClass = $this->getTableClass();
213 return Xml
::openElement( 'table', [
214 'class' => "mw-datatable $tableClass",
215 'id' => 'mw-allmessagestable'
219 <th rowspan=\"2\">" .
220 $this->msg( 'allmessagesname' )->escaped() . "
223 $this->msg( 'allmessagesdefault' )->escaped() .
228 $this->msg( 'allmessagescurrent' )->escaped() .
233 function getEndBody() {
234 return Html
::closeElement( 'table' );
237 function formatValue( $field, $value ) {
238 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
241 $title = Title
::makeTitle( NS_MEDIAWIKI
, $value . $this->suffix
);
242 $talk = Title
::makeTitle( NS_MEDIAWIKI_TALK
, $value . $this->suffix
);
243 $translation = Linker
::makeExternalLink(
244 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
245 'title' => 'Special:SearchTranslations',
246 'group' => 'mediawiki',
247 'grouppath' => 'mediawiki',
248 'language' => $this->getLanguage()->getCode(),
249 'query' => $value . ' ' . $this->msg( $value )->plain()
251 $this->msg( 'allmessages-filter-translate' )->text()
253 $talkLink = $this->msg( 'talkpagelinktext' )->escaped();
255 if ( $this->mCurrentRow
->am_customised
) {
256 $title = $linkRenderer->makeKnownLink( $title, $this->getLanguage()->lcfirst( $value ) );
258 $title = $linkRenderer->makeBrokenLink(
260 $this->getLanguage()->lcfirst( $value )
263 if ( $this->mCurrentRow
->am_talk_exists
) {
264 $talk = $linkRenderer->makeKnownLink( $talk, $talkLink );
266 $talk = $linkRenderer->makeBrokenLink(
272 return $title . ' ' .
273 $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
275 $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
279 return Sanitizer
::escapeHtmlAllowEntities( $value );
286 * @param stdClass $row
287 * @return string HTML
289 function formatRow( $row ) {
290 // Do all the normal stuff
291 $s = parent
::formatRow( $row );
293 // But if there's a customised message, add that too.
294 if ( $row->am_customised
) {
295 $s .= Html
::openElement( 'tr', $this->getRowAttrs( $row, true ) );
296 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual
) );
298 if ( $formatted === '' ) {
299 $formatted = "\u{00A0}";
302 $s .= Html
::element( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual
), $formatted )
303 . Html
::closeElement( 'tr' );
306 return Html
::rawElement( 'tbody', [], $s );
309 function getRowAttrs( $row ) {
314 * @param string $field
315 * @param string $value
316 * @return array HTML attributes
318 function getCellAttrs( $field, $value ) {
320 if ( $field === 'am_title' ) {
321 if ( $this->mCurrentRow
->am_customised
) {
322 $attr +
= [ 'rowspan' => '2' ];
326 'lang' => $this->lang
->getHtmlCode(),
327 'dir' => $this->lang
->getDir(),
329 if ( $this->mCurrentRow
->am_customised
) {
330 // CSS class: am_default, am_actual
331 $attr +
= [ 'class' => $field ];
337 // This is not actually used, as getStartBody is overridden above
338 function getFieldNames() {
340 'am_title' => $this->msg( 'allmessagesname' )->text(),
341 'am_default' => $this->msg( 'allmessagesdefault' )->text()
345 function getTitle() {
346 return SpecialPage
::getTitleFor( 'Allmessages', false );
349 function isFieldSortable( $x ) {
353 function getDefaultSort() {
357 function getQueryInfo() {