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 Wikimedia\Rdbms\FakeResultWrapper
;
25 * Use TablePager for prettified output. We have to pretend that we're
26 * getting data from a table when in fact not all of it comes from the database.
30 use MediaWiki\MediaWikiServices
;
32 class AllMessagesTablePager
extends TablePager
{
34 protected $filter, $prefix, $langcode, $displayPrefix;
48 function __construct( $page, $conds, $langObj = null ) {
49 parent
::__construct( $page->getContext() );
50 $this->mIndexField
= 'am_title';
52 $this->mConds
= $conds;
53 // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
54 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
55 $this->mLimitsShown
= [ 20, 50, 100, 250, 500, 5000 ];
59 $this->talk
= $this->msg( 'talkpagelinktext' )->escaped();
61 $this->lang
= ( $langObj ?
$langObj : $wgContLang );
62 $this->langcode
= $this->lang
->getCode();
63 $this->foreign
= !$this->lang
->equals( $wgContLang );
65 $request = $this->getRequest();
67 $this->filter
= $request->getVal( 'filter', 'all' );
68 if ( $this->filter
=== 'all' ) {
69 $this->custom
= null; // So won't match in either case
71 $this->custom
= ( $this->filter
=== 'unmodified' );
74 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
75 $prefix = $prefix !== '' ?
76 Title
::makeTitleSafe( NS_MEDIAWIKI
, $request->getVal( 'prefix', null ) ) :
79 if ( $prefix !== null ) {
80 $this->displayPrefix
= $prefix->getDBkey();
81 $this->prefix
= '/^' . preg_quote( $this->displayPrefix
, '/' ) . '/i';
83 $this->displayPrefix
= false;
84 $this->prefix
= false;
87 // The suffix that may be needed for message names if we're in a
88 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
89 if ( $this->foreign
) {
90 $this->suffix
= '/' . $this->langcode
;
96 function buildForm() {
97 $attrs = [ 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ];
98 $msg = wfMessage( 'allmessages-language' );
99 $langSelect = Xml
::languageSelector( $this->langcode
, false, null, $attrs, $msg );
101 $out = Xml
::openElement( 'form', [
103 'action' => $this->getConfig()->get( 'Script' ),
104 'id' => 'mw-allmessages-form'
106 Xml
::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
107 Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
108 Xml
::openElement( 'table', [ 'class' => 'mw-allmessages-table' ] ) . "\n" .
110 <td class="mw-label">' .
111 Xml
::label( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
113 <td class=\"mw-input\">" .
117 str_replace( '_', ' ', $this->displayPrefix
),
118 [ 'id' => 'mw-allmessages-form-prefix' ]
123 <td class='mw-label'>" .
124 $this->msg( 'allmessages-filter' )->escaped() .
126 <td class='mw-input'>" .
127 Xml
::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
130 'mw-allmessages-form-filter-unmodified',
131 ( $this->filter
=== 'unmodified' )
133 Xml
::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
136 'mw-allmessages-form-filter-all',
137 ( $this->filter
=== 'all' )
139 Xml
::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
142 'mw-allmessages-form-filter-modified',
143 ( $this->filter
=== 'modified' )
148 <td class=\"mw-label\">" . $langSelect[0] . "</td>\n
149 <td class=\"mw-input\">" . $langSelect[1] . "</td>\n
153 <td class="mw-label">' .
154 Xml
::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
156 <td class="mw-input">' .
157 $this->getLimitSelect( [ 'id' => 'mw-table_pager_limit_label' ] ) .
162 Xml
::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
166 Xml
::closeElement( 'table' ) .
167 $this->getHiddenFields( [ 'title', 'prefix', 'filter', 'lang', 'limit' ] ) .
168 Xml
::closeElement( 'fieldset' ) .
169 Xml
::closeElement( 'form' );
174 function getAllMessages( $descending ) {
175 $messageNames = Language
::getLocalisationCache()->getSubitemList( 'en', 'messages' );
177 // Normalise message names so they look like page titles and sort correctly - T86139
178 $messageNames = array_map( [ $this->lang
, 'ucfirst' ], $messageNames );
181 rsort( $messageNames );
183 asort( $messageNames );
186 return $messageNames;
190 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
191 * Returns [ 'pages' => ..., 'talks' => ... ], where the subarrays have
192 * an entry for each existing page, with the key being the message name and
195 * @param array $messageNames
196 * @param string $langcode What language code
197 * @param bool $foreign Whether the $langcode is not the content language
198 * @return array A 'pages' and 'talks' array with the keys of existing pages
200 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
201 // FIXME: This function should be moved to Language:: or something.
203 $dbr = wfGetDB( DB_REPLICA
);
204 $res = $dbr->select( 'page',
205 [ 'page_namespace', 'page_title' ],
206 [ 'page_namespace' => [ NS_MEDIAWIKI
, NS_MEDIAWIKI_TALK
] ],
208 [ 'USE INDEX' => 'name_title' ]
210 $xNames = array_flip( $messageNames );
212 $pageFlags = $talkFlags = [];
214 foreach ( $res as $s ) {
218 $titleParts = explode( '/', $s->page_title
);
219 if ( count( $titleParts ) === 2 &&
220 $langcode === $titleParts[1] &&
221 isset( $xNames[$titleParts[0]] )
223 $exists = $titleParts[0];
225 } elseif ( isset( $xNames[$s->page_title
] ) ) {
226 $exists = $s->page_title
;
229 $title = Title
::newFromRow( $s );
230 if ( $exists && $title->inNamespace( NS_MEDIAWIKI
) ) {
231 $pageFlags[$exists] = true;
232 } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK
) ) {
233 $talkFlags[$exists] = true;
237 return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
241 * This function normally does a database query to get the results; we need
242 * to make a pretend result using a FakeResultWrapper.
243 * @param string $offset
245 * @param bool $descending
246 * @return FakeResultWrapper
248 function reallyDoQuery( $offset, $limit, $descending ) {
249 $result = new FakeResultWrapper( [] );
251 $messageNames = $this->getAllMessages( $descending );
252 $statuses = self
::getCustomisedStatuses( $messageNames, $this->langcode
, $this->foreign
);
255 foreach ( $messageNames as $key ) {
256 $customised = isset( $statuses['pages'][$key] );
257 if ( $customised !== $this->custom
&&
258 ( $descending && ( $key < $offset ||
!$offset ) ||
!$descending && $key > $offset ) &&
259 ( ( $this->prefix
&& preg_match( $this->prefix
, $key ) ) ||
$this->prefix
=== false )
261 $actual = wfMessage( $key )->inLanguage( $this->langcode
)->plain();
262 $default = wfMessage( $key )->inLanguage( $this->langcode
)->useDatabase( false )->plain();
263 $result->result
[] = [
265 'am_actual' => $actual,
266 'am_default' => $default,
267 'am_customised' => $customised,
268 'am_talk_exists' => isset( $statuses['talks'][$key] )
273 if ( $count === $limit ) {
281 function getStartBody() {
282 $tableClass = $this->getTableClass();
283 return Xml
::openElement( 'table', [
284 'class' => "mw-datatable $tableClass",
285 'id' => 'mw-allmessagestable'
289 <th rowspan=\"2\">" .
290 $this->msg( 'allmessagesname' )->escaped() . "
293 $this->msg( 'allmessagesdefault' )->escaped() .
298 $this->msg( 'allmessagescurrent' )->escaped() .
300 </tr></thead><tbody>\n";
303 function formatValue( $field, $value ) {
304 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
307 $title = Title
::makeTitle( NS_MEDIAWIKI
, $value . $this->suffix
);
308 $talk = Title
::makeTitle( NS_MEDIAWIKI_TALK
, $value . $this->suffix
);
309 $translation = Linker
::makeExternalLink(
310 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
311 'title' => 'Special:SearchTranslations',
312 'group' => 'mediawiki',
313 'grouppath' => 'mediawiki',
314 'language' => $this->getLanguage()->getCode(),
315 'query' => $value . ' ' . $this->msg( $value )->plain()
317 $this->msg( 'allmessages-filter-translate' )->text()
320 if ( $this->mCurrentRow
->am_customised
) {
321 $title = $linkRenderer->makeKnownLink( $title, $this->getLanguage()->lcfirst( $value ) );
323 $title = $linkRenderer->makeBrokenLink(
325 $this->getLanguage()->lcfirst( $value )
328 if ( $this->mCurrentRow
->am_talk_exists
) {
329 $talk = $linkRenderer->makeKnownLink( $talk, $this->talk
);
331 $talk = $linkRenderer->makeBrokenLink(
337 return $title . ' ' .
338 $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
340 $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
344 return Sanitizer
::escapeHtmlAllowEntities( $value );
350 function formatRow( $row ) {
351 // Do all the normal stuff
352 $s = parent
::formatRow( $row );
354 // But if there's a customised message, add that too.
355 if ( $row->am_customised
) {
356 $s .= Xml
::openElement( 'tr', $this->getRowAttrs( $row, true ) );
357 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual
) );
359 if ( $formatted === '' ) {
360 $formatted = ' ';
363 $s .= Xml
::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual
), $formatted )
370 function getRowAttrs( $row, $isSecond = false ) {
373 if ( $row->am_customised
) {
374 $arr['class'] = 'allmessages-customised';
378 $arr['id'] = Sanitizer
::escapeIdForAttribute(
379 'msg_' . $this->getLanguage()->lcfirst( $row->am_title
)
386 function getCellAttrs( $field, $value ) {
387 if ( $this->mCurrentRow
->am_customised
&& $field === 'am_title' ) {
388 return [ 'rowspan' => '2', 'class' => $field ];
389 } elseif ( $field === 'am_title' ) {
390 return [ 'class' => $field ];
393 'lang' => $this->lang
->getHtmlCode(),
394 'dir' => $this->lang
->getDir(),
400 // This is not actually used, as getStartBody is overridden above
401 function getFieldNames() {
403 'am_title' => $this->msg( 'allmessagesname' )->text(),
404 'am_default' => $this->msg( 'allmessagesdefault' )->text()
408 function getTitle() {
409 return SpecialPage
::getTitleFor( 'Allmessages', false );
412 function isFieldSortable( $x ) {
416 function getDefaultSort() {
420 function getQueryInfo() {