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 class AllMessagesTablePager
extends TablePager
{
30 protected $filter, $prefix, $langcode, $displayPrefix;
44 function __construct( $page, $conds, $langObj = null ) {
45 parent
::__construct( $page->getContext() );
46 $this->mIndexField
= 'am_title';
48 $this->mConds
= $conds;
49 // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
50 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
51 $this->mLimitsShown
= [ 20, 50, 100, 250, 500, 5000 ];
55 $this->talk
= $this->msg( 'talkpagelinktext' )->escaped();
57 $this->lang
= ( $langObj ?
$langObj : $wgContLang );
58 $this->langcode
= $this->lang
->getCode();
59 $this->foreign
= !$this->lang
->equals( $wgContLang );
61 $request = $this->getRequest();
63 $this->filter
= $request->getVal( 'filter', 'all' );
64 if ( $this->filter
=== 'all' ) {
65 $this->custom
= null; // So won't match in either case
67 $this->custom
= ( $this->filter
=== 'unmodified' );
70 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
71 $prefix = $prefix !== '' ?
72 Title
::makeTitleSafe( NS_MEDIAWIKI
, $request->getVal( 'prefix', null ) ) :
75 if ( $prefix !== null ) {
76 $this->displayPrefix
= $prefix->getDBkey();
77 $this->prefix
= '/^' . preg_quote( $this->displayPrefix
, '/' ) . '/i';
79 $this->displayPrefix
= false;
80 $this->prefix
= false;
83 // The suffix that may be needed for message names if we're in a
84 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
85 if ( $this->foreign
) {
86 $this->suffix
= '/' . $this->langcode
;
92 function buildForm() {
93 $attrs = [ 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ];
94 $msg = wfMessage( 'allmessages-language' );
95 $langSelect = Xml
::languageSelector( $this->langcode
, false, null, $attrs, $msg );
97 $out = Xml
::openElement( 'form', [
99 'action' => $this->getConfig()->get( 'Script' ),
100 'id' => 'mw-allmessages-form'
102 Xml
::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
103 Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
104 Xml
::openElement( 'table', [ 'class' => 'mw-allmessages-table' ] ) . "\n" .
106 <td class="mw-label">' .
107 Xml
::label( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
109 <td class=\"mw-input\">" .
113 str_replace( '_', ' ', $this->displayPrefix
),
114 [ 'id' => 'mw-allmessages-form-prefix' ]
119 <td class='mw-label'>" .
120 $this->msg( 'allmessages-filter' )->escaped() .
122 <td class='mw-input'>" .
123 Xml
::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
126 'mw-allmessages-form-filter-unmodified',
127 ( $this->filter
=== 'unmodified' )
129 Xml
::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
132 'mw-allmessages-form-filter-all',
133 ( $this->filter
=== 'all' )
135 Xml
::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
138 'mw-allmessages-form-filter-modified',
139 ( $this->filter
=== 'modified' )
144 <td class=\"mw-label\">" . $langSelect[0] . "</td>\n
145 <td class=\"mw-input\">" . $langSelect[1] . "</td>\n
149 <td class="mw-label">' .
150 Xml
::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
152 <td class="mw-input">' .
153 $this->getLimitSelect( [ 'id' => 'mw-table_pager_limit_label' ] ) .
158 Xml
::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
162 Xml
::closeElement( 'table' ) .
163 $this->getHiddenFields( [ 'title', 'prefix', 'filter', 'lang', 'limit' ] ) .
164 Xml
::closeElement( 'fieldset' ) .
165 Xml
::closeElement( 'form' );
170 function getAllMessages( $descending ) {
171 $messageNames = Language
::getLocalisationCache()->getSubitemList( 'en', 'messages' );
173 // Normalise message names so they look like page titles and sort correctly - T86139
174 $messageNames = array_map( [ $this->lang
, 'ucfirst' ], $messageNames );
177 rsort( $messageNames );
179 asort( $messageNames );
182 return $messageNames;
186 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
187 * Returns array( 'pages' => ..., 'talks' => ... ), where the subarrays have
188 * an entry for each existing page, with the key being the message name and
191 * @param array $messageNames
192 * @param string $langcode What language code
193 * @param bool $foreign Whether the $langcode is not the content language
194 * @return array A 'pages' and 'talks' array with the keys of existing pages
196 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
197 // FIXME: This function should be moved to Language:: or something.
199 $dbr = wfGetDB( DB_SLAVE
);
200 $res = $dbr->select( 'page',
201 [ 'page_namespace', 'page_title' ],
202 [ 'page_namespace' => [ NS_MEDIAWIKI
, NS_MEDIAWIKI_TALK
] ],
204 [ 'USE INDEX' => 'name_title' ]
206 $xNames = array_flip( $messageNames );
208 $pageFlags = $talkFlags = [];
210 foreach ( $res as $s ) {
214 $titleParts = explode( '/', $s->page_title
);
215 if ( count( $titleParts ) === 2 &&
216 $langcode === $titleParts[1] &&
217 isset( $xNames[$titleParts[0]] )
219 $exists = $titleParts[0];
221 } elseif ( isset( $xNames[$s->page_title
] ) ) {
222 $exists = $s->page_title
;
225 $title = Title
::newFromRow( $s );
226 if ( $exists && $title->inNamespace( NS_MEDIAWIKI
) ) {
227 $pageFlags[$exists] = true;
228 } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK
) ) {
229 $talkFlags[$exists] = true;
233 return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
237 * This function normally does a database query to get the results; we need
238 * to make a pretend result using a FakeResultWrapper.
239 * @param string $offset
241 * @param bool $descending
242 * @return FakeResultWrapper
244 function reallyDoQuery( $offset, $limit, $descending ) {
245 $result = new FakeResultWrapper( [] );
247 $messageNames = $this->getAllMessages( $descending );
248 $statuses = self
::getCustomisedStatuses( $messageNames, $this->langcode
, $this->foreign
);
251 foreach ( $messageNames as $key ) {
252 $customised = isset( $statuses['pages'][$key] );
253 if ( $customised !== $this->custom
&&
254 ( $descending && ( $key < $offset ||
!$offset ) ||
!$descending && $key > $offset ) &&
255 ( ( $this->prefix
&& preg_match( $this->prefix
, $key ) ) ||
$this->prefix
=== false )
257 $actual = wfMessage( $key )->inLanguage( $this->langcode
)->plain();
258 $default = wfMessage( $key )->inLanguage( $this->langcode
)->useDatabase( false )->plain();
259 $result->result
[] = [
261 'am_actual' => $actual,
262 'am_default' => $default,
263 'am_customised' => $customised,
264 'am_talk_exists' => isset( $statuses['talks'][$key] )
269 if ( $count === $limit ) {
277 function getStartBody() {
278 $tableClass = $this->getTableClass();
279 return Xml
::openElement( 'table', [
280 'class' => "mw-datatable $tableClass",
281 'id' => 'mw-allmessagestable'
285 <th rowspan=\"2\">" .
286 $this->msg( 'allmessagesname' )->escaped() . "
289 $this->msg( 'allmessagesdefault' )->escaped() .
294 $this->msg( 'allmessagescurrent' )->escaped() .
296 </tr></thead><tbody>\n";
299 function formatValue( $field, $value ) {
302 $title = Title
::makeTitle( NS_MEDIAWIKI
, $value . $this->suffix
);
303 $talk = Title
::makeTitle( NS_MEDIAWIKI_TALK
, $value . $this->suffix
);
304 $translation = Linker
::makeExternalLink(
305 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
306 'title' => 'Special:SearchTranslations',
307 'group' => 'mediawiki',
308 'grouppath' => 'mediawiki',
309 'language' => $this->getLanguage()->getCode(),
310 'query' => $value . ' ' . $this->msg( $value )->plain()
312 $this->msg( 'allmessages-filter-translate' )->text()
315 if ( $this->mCurrentRow
->am_customised
) {
316 $title = Linker
::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
318 $title = Linker
::link(
320 $this->getLanguage()->lcfirst( $value ),
326 if ( $this->mCurrentRow
->am_talk_exists
) {
327 $talk = Linker
::linkKnown( $talk, $this->talk
);
329 $talk = Linker
::link(
338 return $title . ' ' .
339 $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
341 $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
345 return Sanitizer
::escapeHtmlAllowEntities( $value );
351 function formatRow( $row ) {
352 // Do all the normal stuff
353 $s = parent
::formatRow( $row );
355 // But if there's a customised message, add that too.
356 if ( $row->am_customised
) {
357 $s .= Xml
::openElement( 'tr', $this->getRowAttrs( $row, true ) );
358 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual
) );
360 if ( $formatted === '' ) {
361 $formatted = ' ';
364 $s .= Xml
::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual
), $formatted )
371 function getRowAttrs( $row, $isSecond = false ) {
374 if ( $row->am_customised
) {
375 $arr['class'] = 'allmessages-customised';
379 $arr['id'] = Sanitizer
::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title
) );
385 function getCellAttrs( $field, $value ) {
386 if ( $this->mCurrentRow
->am_customised
&& $field === 'am_title' ) {
387 return [ 'rowspan' => '2', 'class' => $field ];
388 } elseif ( $field === 'am_title' ) {
389 return [ 'class' => $field ];
392 'lang' => $this->lang
->getHtmlCode(),
393 'dir' => $this->lang
->getDir(),
399 // This is not actually used, as getStartBody is overridden above
400 function getFieldNames() {
402 'am_title' => $this->msg( 'allmessagesname' )->text(),
403 'am_default' => $this->msg( 'allmessagesdefault' )->text()
407 function getTitle() {
408 return SpecialPage
::getTitleFor( 'Allmessages', false );
411 function isFieldSortable( $x ) {
415 function getDefaultSort() {
419 function getQueryInfo() {