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
{
65 * @param IContextSource|null $context
66 * @param FormOptions $opts
67 * @param LinkRenderer $linkRenderer
69 public function __construct( IContextSource
$context = null, FormOptions
$opts,
70 LinkRenderer
$linkRenderer
72 parent
::__construct( $context, $linkRenderer );
74 $this->mIndexField
= 'am_title';
75 // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
76 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
78 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
79 $this->lang
= wfGetLangObj( $opts->getValue( 'lang' ) );
81 $this->langcode
= $this->lang
->getCode();
82 $this->foreign
= !$this->lang
->equals( $contLang );
84 $filter = $opts->getValue( 'filter' );
85 if ( $filter === 'all' ) {
86 $this->custom
= null; // So won't match in either case
88 $this->custom
= ( $filter === 'unmodified' );
91 $prefix = $this->getLanguage()->ucfirst( $opts->getValue( 'prefix' ) );
92 $prefix = $prefix !== '' ?
93 Title
::makeTitleSafe( NS_MEDIAWIKI
, $opts->getValue( 'prefix' ) ) :
96 if ( $prefix !== null ) {
97 $displayPrefix = $prefix->getDBkey();
98 $this->prefix
= '/^' . preg_quote( $displayPrefix, '/' ) . '/i';
100 $this->prefix
= false;
103 // The suffix that may be needed for message names if we're in a
104 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
105 if ( $this->foreign
) {
106 $this->suffix
= '/' . $this->langcode
;
112 function getAllMessages( $descending ) {
113 $messageNames = Language
::getLocalisationCache()->getSubitemList( 'en', 'messages' );
115 // Normalise message names so they look like page titles and sort correctly - T86139
116 $messageNames = array_map( [ $this->lang
, 'ucfirst' ], $messageNames );
119 rsort( $messageNames );
121 asort( $messageNames );
124 return $messageNames;
128 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
129 * Returns [ 'pages' => ..., 'talks' => ... ], where the subarrays have
130 * an entry for each existing page, with the key being the message name and
133 * @param array $messageNames
134 * @param string $langcode What language code
135 * @param bool $foreign Whether the $langcode is not the content language
136 * @return array A 'pages' and 'talks' array with the keys of existing pages
138 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
139 // FIXME: This function should be moved to Language:: or something.
141 $dbr = wfGetDB( DB_REPLICA
);
142 $res = $dbr->select( 'page',
143 [ 'page_namespace', 'page_title' ],
144 [ 'page_namespace' => [ NS_MEDIAWIKI
, NS_MEDIAWIKI_TALK
] ],
146 [ 'USE INDEX' => 'name_title' ]
148 $xNames = array_flip( $messageNames );
150 $pageFlags = $talkFlags = [];
152 foreach ( $res as $s ) {
156 $titleParts = explode( '/', $s->page_title
);
157 if ( count( $titleParts ) === 2 &&
158 $langcode === $titleParts[1] &&
159 isset( $xNames[$titleParts[0]] )
161 $exists = $titleParts[0];
163 } elseif ( isset( $xNames[$s->page_title
] ) ) {
164 $exists = $s->page_title
;
167 $title = Title
::newFromRow( $s );
168 if ( $exists && $title->inNamespace( NS_MEDIAWIKI
) ) {
169 $pageFlags[$exists] = true;
170 } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK
) ) {
171 $talkFlags[$exists] = true;
175 return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
179 * This function normally does a database query to get the results; we need
180 * to make a pretend result using a FakeResultWrapper.
181 * @param string $offset
184 * @return FakeResultWrapper
186 function reallyDoQuery( $offset, $limit, $order ) {
187 $asc = ( $order === self
::QUERY_ASCENDING
);
189 $messageNames = $this->getAllMessages( $order );
190 $statuses = self
::getCustomisedStatuses( $messageNames, $this->langcode
, $this->foreign
);
194 foreach ( $messageNames as $key ) {
195 $customised = isset( $statuses['pages'][$key] );
196 if ( $customised !== $this->custom
&&
197 ( $asc && ( $key < $offset ||
!$offset ) ||
!$asc && $key > $offset ) &&
198 ( ( $this->prefix
&& preg_match( $this->prefix
, $key ) ) ||
$this->prefix
=== false )
200 $actual = $this->msg( $key )->inLanguage( $this->lang
)->plain();
201 $default = $this->msg( $key )->inLanguage( $this->lang
)->useDatabase( false )->plain();
204 'am_actual' => $actual,
205 'am_default' => $default,
206 'am_customised' => $customised,
207 'am_talk_exists' => isset( $statuses['talks'][$key] )
212 if ( $count === $limit ) {
217 return new FakeResultWrapper( $rows );
220 protected function getStartBody() {
221 $tableClass = $this->getTableClass();
222 return Xml
::openElement( 'table', [
223 'class' => "mw-datatable $tableClass",
224 'id' => 'mw-allmessagestable'
228 <th rowspan=\"2\">" .
229 $this->msg( 'allmessagesname' )->escaped() . "
232 $this->msg( 'allmessagesdefault' )->escaped() .
237 $this->msg( 'allmessagescurrent' )->escaped() .
242 function getEndBody() {
243 return Html
::closeElement( 'table' );
246 function formatValue( $field, $value ) {
247 $linkRenderer = $this->getLinkRenderer();
250 $title = Title
::makeTitle( NS_MEDIAWIKI
, $value . $this->suffix
);
251 $talk = Title
::makeTitle( NS_MEDIAWIKI_TALK
, $value . $this->suffix
);
252 $translation = Linker
::makeExternalLink(
253 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
254 'title' => 'Special:SearchTranslations',
255 'group' => 'mediawiki',
256 'grouppath' => 'mediawiki',
257 'language' => $this->getLanguage()->getCode(),
258 'query' => $value . ' ' . $this->msg( $value )->plain()
260 $this->msg( 'allmessages-filter-translate' )->text()
262 $talkLink = $this->msg( 'talkpagelinktext' )->escaped();
264 if ( $this->mCurrentRow
->am_customised
) {
265 $title = $linkRenderer->makeKnownLink( $title, $this->getLanguage()->lcfirst( $value ) );
267 $title = $linkRenderer->makeBrokenLink(
268 $title, $this->getLanguage()->lcfirst( $value )
271 if ( $this->mCurrentRow
->am_talk_exists
) {
272 $talk = $linkRenderer->makeKnownLink( $talk, $talkLink );
274 $talk = $linkRenderer->makeBrokenLink(
280 return $title . ' ' .
281 $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
283 $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
287 return Sanitizer
::escapeHtmlAllowEntities( $value );
294 * @param stdClass $row
295 * @return string HTML
297 function formatRow( $row ) {
298 // Do all the normal stuff
299 $s = parent
::formatRow( $row );
301 // But if there's a customised message, add that too.
302 if ( $row->am_customised
) {
303 $s .= Html
::openElement( 'tr', $this->getRowAttrs( $row, true ) );
304 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual
) );
306 if ( $formatted === '' ) {
307 $formatted = "\u{00A0}";
310 $s .= Html
::element( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual
), $formatted )
311 . Html
::closeElement( 'tr' );
314 return Html
::rawElement( 'tbody', [], $s );
317 function getRowAttrs( $row ) {
322 * @param string $field
323 * @param string $value
324 * @return array HTML attributes
326 function getCellAttrs( $field, $value ) {
328 if ( $field === 'am_title' ) {
329 if ( $this->mCurrentRow
->am_customised
) {
330 $attr +
= [ 'rowspan' => '2' ];
334 'lang' => $this->lang
->getHtmlCode(),
335 'dir' => $this->lang
->getDir(),
337 if ( $this->mCurrentRow
->am_customised
) {
338 // CSS class: am_default, am_actual
339 $attr +
= [ 'class' => $field ];
345 // This is not actually used, as getStartBody is overridden above
346 function getFieldNames() {
348 'am_title' => $this->msg( 'allmessagesname' )->text(),
349 'am_default' => $this->msg( 'allmessagesdefault' )->text()
353 function getTitle() {
354 return SpecialPage
::getTitleFor( 'Allmessages', false );
357 function isFieldSortable( $x ) {
361 function getDefaultSort() {
365 function getQueryInfo() {