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 MediaWiki\Linker\LinkRenderer
;
24 use Wikimedia\Rdbms\FakeResultWrapper
;
27 * Use TablePager for prettified output. We have to pretend that we're
28 * getting data from a table when in fact not all of it comes from the database.
32 class AllMessagesTablePager
extends TablePager
{
60 * @param IContextSource|null $context
61 * @param FormOptions $opts
62 * @param LinkRenderer $linkRenderer
64 public function __construct( IContextSource
$context = null, FormOptions
$opts,
65 LinkRenderer
$linkRenderer
67 parent
::__construct( $context, $linkRenderer );
69 $this->mIndexField
= 'am_title';
70 // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
71 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
73 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
74 $this->lang
= wfGetLangObj( $opts->getValue( 'lang' ) );
76 $this->langcode
= $this->lang
->getCode();
77 $this->foreign
= !$this->lang
->equals( $contLang );
79 $filter = $opts->getValue( 'filter' );
80 if ( $filter === 'all' ) {
81 $this->custom
= null; // So won't match in either case
83 $this->custom
= ( $filter === 'unmodified' );
86 $prefix = $this->getLanguage()->ucfirst( $opts->getValue( 'prefix' ) );
87 $prefix = $prefix !== '' ?
88 Title
::makeTitleSafe( NS_MEDIAWIKI
, $opts->getValue( 'prefix' ) ) :
91 if ( $prefix !== null ) {
92 $displayPrefix = $prefix->getDBkey();
93 $this->prefix
= '/^' . preg_quote( $displayPrefix, '/' ) . '/i';
95 $this->prefix
= false;
98 // The suffix that may be needed for message names if we're in a
99 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
100 if ( $this->foreign
) {
101 $this->suffix
= '/' . $this->langcode
;
107 function getAllMessages( $descending ) {
108 $messageNames = Language
::getLocalisationCache()->getSubitemList( 'en', 'messages' );
110 // Normalise message names so they look like page titles and sort correctly - T86139
111 $messageNames = array_map( [ $this->lang
, 'ucfirst' ], $messageNames );
114 rsort( $messageNames );
116 asort( $messageNames );
119 return $messageNames;
123 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
124 * Returns [ 'pages' => ..., 'talks' => ... ], where the subarrays have
125 * an entry for each existing page, with the key being the message name and
128 * @param array $messageNames
129 * @param string $langcode What language code
130 * @param bool $foreign Whether the $langcode is not the content language
131 * @return array A 'pages' and 'talks' array with the keys of existing pages
133 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
134 // FIXME: This function should be moved to Language:: or something.
136 $dbr = wfGetDB( DB_REPLICA
);
137 $res = $dbr->select( 'page',
138 [ 'page_namespace', 'page_title' ],
139 [ 'page_namespace' => [ NS_MEDIAWIKI
, NS_MEDIAWIKI_TALK
] ],
141 [ 'USE INDEX' => 'name_title' ]
143 $xNames = array_flip( $messageNames );
145 $pageFlags = $talkFlags = [];
147 foreach ( $res as $s ) {
151 $titleParts = explode( '/', $s->page_title
);
152 if ( count( $titleParts ) === 2 &&
153 $langcode === $titleParts[1] &&
154 isset( $xNames[$titleParts[0]] )
156 $exists = $titleParts[0];
158 } elseif ( isset( $xNames[$s->page_title
] ) ) {
159 $exists = $s->page_title
;
162 $title = Title
::newFromRow( $s );
163 if ( $exists && $title->inNamespace( NS_MEDIAWIKI
) ) {
164 $pageFlags[$exists] = true;
165 } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK
) ) {
166 $talkFlags[$exists] = true;
170 return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
174 * This function normally does a database query to get the results; we need
175 * to make a pretend result using a FakeResultWrapper.
176 * @param string $offset
179 * @return FakeResultWrapper
181 function reallyDoQuery( $offset, $limit, $order ) {
182 $asc = ( $order === self
::QUERY_ASCENDING
);
184 $messageNames = $this->getAllMessages( $order );
185 $statuses = self
::getCustomisedStatuses( $messageNames, $this->langcode
, $this->foreign
);
189 foreach ( $messageNames as $key ) {
190 $customised = isset( $statuses['pages'][$key] );
191 if ( $customised !== $this->custom
&&
192 ( $asc && ( $key < $offset ||
!$offset ) ||
!$asc && $key > $offset ) &&
193 ( ( $this->prefix
&& preg_match( $this->prefix
, $key ) ) ||
$this->prefix
=== false )
195 $actual = $this->msg( $key )->inLanguage( $this->lang
)->plain();
196 $default = $this->msg( $key )->inLanguage( $this->lang
)->useDatabase( false )->plain();
199 'am_actual' => $actual,
200 'am_default' => $default,
201 'am_customised' => $customised,
202 'am_talk_exists' => isset( $statuses['talks'][$key] )
207 if ( $count === $limit ) {
212 return new FakeResultWrapper( $rows );
215 protected function getStartBody() {
216 $tableClass = $this->getTableClass();
217 return Xml
::openElement( 'table', [
218 'class' => "mw-datatable $tableClass",
219 'id' => 'mw-allmessagestable'
223 <th rowspan=\"2\">" .
224 $this->msg( 'allmessagesname' )->escaped() . "
227 $this->msg( 'allmessagesdefault' )->escaped() .
232 $this->msg( 'allmessagescurrent' )->escaped() .
237 function getEndBody() {
238 return Html
::closeElement( 'table' );
241 function formatValue( $field, $value ) {
242 $linkRenderer = $this->getLinkRenderer();
245 $title = Title
::makeTitle( NS_MEDIAWIKI
, $value . $this->suffix
);
246 $talk = Title
::makeTitle( NS_MEDIAWIKI_TALK
, $value . $this->suffix
);
247 $translation = Linker
::makeExternalLink(
248 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
249 'title' => 'Special:SearchTranslations',
250 'group' => 'mediawiki',
251 'grouppath' => 'mediawiki',
252 'language' => $this->getLanguage()->getCode(),
253 'query' => $value . ' ' . $this->msg( $value )->plain()
255 $this->msg( 'allmessages-filter-translate' )->text()
257 $talkLink = $this->msg( 'talkpagelinktext' )->escaped();
259 if ( $this->mCurrentRow
->am_customised
) {
260 $title = $linkRenderer->makeKnownLink( $title, $this->getLanguage()->lcfirst( $value ) );
262 $title = $linkRenderer->makeBrokenLink(
263 $title, $this->getLanguage()->lcfirst( $value )
266 if ( $this->mCurrentRow
->am_talk_exists
) {
267 $talk = $linkRenderer->makeKnownLink( $talk, $talkLink );
269 $talk = $linkRenderer->makeBrokenLink(
275 return $title . ' ' .
276 $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
278 $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
282 return Sanitizer
::escapeHtmlAllowEntities( $value );
289 * @param stdClass $row
290 * @return string HTML
292 function formatRow( $row ) {
293 // Do all the normal stuff
294 $s = parent
::formatRow( $row );
296 // But if there's a customised message, add that too.
297 if ( $row->am_customised
) {
298 $s .= Html
::openElement( 'tr', $this->getRowAttrs( $row, true ) );
299 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual
) );
301 if ( $formatted === '' ) {
302 $formatted = "\u{00A0}";
305 $s .= Html
::element( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual
), $formatted )
306 . Html
::closeElement( 'tr' );
309 return Html
::rawElement( 'tbody', [], $s );
312 function getRowAttrs( $row ) {
317 * @param string $field
318 * @param string $value
319 * @return array HTML attributes
321 function getCellAttrs( $field, $value ) {
323 if ( $field === 'am_title' ) {
324 if ( $this->mCurrentRow
->am_customised
) {
325 $attr +
= [ 'rowspan' => '2' ];
329 'lang' => $this->lang
->getHtmlCode(),
330 'dir' => $this->lang
->getDir(),
332 if ( $this->mCurrentRow
->am_customised
) {
333 // CSS class: am_default, am_actual
334 $attr +
= [ 'class' => $field ];
340 // This is not actually used, as getStartBody is overridden above
341 function getFieldNames() {
343 'am_title' => $this->msg( 'allmessagesname' )->text(),
344 'am_default' => $this->msg( 'allmessagesdefault' )->text()
348 function getTitle() {
349 return SpecialPage
::getTitleFor( 'Allmessages', false );
352 function isFieldSortable( $x ) {
356 function getDefaultSort() {
360 function getQueryInfo() {