Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / specials / pagers / AllMessagesTablePager.php
blobefc51ef3ff98c1f4e9a2deb08b12589ee15f6e6f
1 <?php
2 /**
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
18 * @file
19 * @ingroup Pager
22 /**
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.
26 * @ingroup Pager
28 use MediaWiki\MediaWikiServices;
30 class AllMessagesTablePager extends TablePager {
32 protected $filter, $prefix, $langcode, $displayPrefix;
34 public $mLimitsShown;
36 /**
37 * @var Language
39 public $lang;
41 /**
42 * @var null|bool
44 public $custom;
46 function __construct( $page, $conds, $langObj = null ) {
47 parent::__construct( $page->getContext() );
48 $this->mIndexField = 'am_title';
49 $this->mPage = $page;
50 $this->mConds = $conds;
51 // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
52 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
53 $this->mLimitsShown = [ 20, 50, 100, 250, 500, 5000 ];
55 global $wgContLang;
57 $this->talk = $this->msg( 'talkpagelinktext' )->escaped();
59 $this->lang = ( $langObj ? $langObj : $wgContLang );
60 $this->langcode = $this->lang->getCode();
61 $this->foreign = !$this->lang->equals( $wgContLang );
63 $request = $this->getRequest();
65 $this->filter = $request->getVal( 'filter', 'all' );
66 if ( $this->filter === 'all' ) {
67 $this->custom = null; // So won't match in either case
68 } else {
69 $this->custom = ( $this->filter === 'unmodified' );
72 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
73 $prefix = $prefix !== '' ?
74 Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) :
75 null;
77 if ( $prefix !== null ) {
78 $this->displayPrefix = $prefix->getDBkey();
79 $this->prefix = '/^' . preg_quote( $this->displayPrefix, '/' ) . '/i';
80 } else {
81 $this->displayPrefix = false;
82 $this->prefix = false;
85 // The suffix that may be needed for message names if we're in a
86 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
87 if ( $this->foreign ) {
88 $this->suffix = '/' . $this->langcode;
89 } else {
90 $this->suffix = '';
94 function buildForm() {
95 $attrs = [ 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ];
96 $msg = wfMessage( 'allmessages-language' );
97 $langSelect = Xml::languageSelector( $this->langcode, false, null, $attrs, $msg );
99 $out = Xml::openElement( 'form', [
100 'method' => 'get',
101 'action' => $this->getConfig()->get( 'Script' ),
102 'id' => 'mw-allmessages-form'
103 ] ) .
104 Xml::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
105 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
106 Xml::openElement( 'table', [ 'class' => 'mw-allmessages-table' ] ) . "\n" .
107 '<tr>
108 <td class="mw-label">' .
109 Xml::label( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
110 "</td>\n
111 <td class=\"mw-input\">" .
112 Xml::input(
113 'prefix',
115 str_replace( '_', ' ', $this->displayPrefix ),
116 [ 'id' => 'mw-allmessages-form-prefix' ]
118 "</td>\n
119 </tr>
120 <tr>\n
121 <td class='mw-label'>" .
122 $this->msg( 'allmessages-filter' )->escaped() .
123 "</td>\n
124 <td class='mw-input'>" .
125 Xml::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
126 'filter',
127 'unmodified',
128 'mw-allmessages-form-filter-unmodified',
129 ( $this->filter === 'unmodified' )
131 Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
132 'filter',
133 'all',
134 'mw-allmessages-form-filter-all',
135 ( $this->filter === 'all' )
137 Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
138 'filter',
139 'modified',
140 'mw-allmessages-form-filter-modified',
141 ( $this->filter === 'modified' )
143 "</td>\n
144 </tr>
145 <tr>\n
146 <td class=\"mw-label\">" . $langSelect[0] . "</td>\n
147 <td class=\"mw-input\">" . $langSelect[1] . "</td>\n
148 </tr>" .
150 '<tr>
151 <td class="mw-label">' .
152 Xml::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
153 '</td>
154 <td class="mw-input">' .
155 $this->getLimitSelect( [ 'id' => 'mw-table_pager_limit_label' ] ) .
156 '</td>
157 <tr>
158 <td></td>
159 <td>' .
160 Xml::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
161 "</td>\n
162 </tr>" .
164 Xml::closeElement( 'table' ) .
165 $this->getHiddenFields( [ 'title', 'prefix', 'filter', 'lang', 'limit' ] ) .
166 Xml::closeElement( 'fieldset' ) .
167 Xml::closeElement( 'form' );
169 return $out;
172 function getAllMessages( $descending ) {
173 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
175 // Normalise message names so they look like page titles and sort correctly - T86139
176 $messageNames = array_map( [ $this->lang, 'ucfirst' ], $messageNames );
178 if ( $descending ) {
179 rsort( $messageNames );
180 } else {
181 asort( $messageNames );
184 return $messageNames;
188 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
189 * Returns [ 'pages' => ..., 'talks' => ... ], where the subarrays have
190 * an entry for each existing page, with the key being the message name and
191 * value arbitrary.
193 * @param array $messageNames
194 * @param string $langcode What language code
195 * @param bool $foreign Whether the $langcode is not the content language
196 * @return array A 'pages' and 'talks' array with the keys of existing pages
198 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
199 // FIXME: This function should be moved to Language:: or something.
201 $dbr = wfGetDB( DB_REPLICA );
202 $res = $dbr->select( 'page',
203 [ 'page_namespace', 'page_title' ],
204 [ 'page_namespace' => [ NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ] ],
205 __METHOD__,
206 [ 'USE INDEX' => 'name_title' ]
208 $xNames = array_flip( $messageNames );
210 $pageFlags = $talkFlags = [];
212 foreach ( $res as $s ) {
213 $exists = false;
215 if ( $foreign ) {
216 $titleParts = explode( '/', $s->page_title );
217 if ( count( $titleParts ) === 2 &&
218 $langcode === $titleParts[1] &&
219 isset( $xNames[$titleParts[0]] )
221 $exists = $titleParts[0];
223 } elseif ( isset( $xNames[$s->page_title] ) ) {
224 $exists = $s->page_title;
227 $title = Title::newFromRow( $s );
228 if ( $exists && $title->inNamespace( NS_MEDIAWIKI ) ) {
229 $pageFlags[$exists] = true;
230 } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK ) ) {
231 $talkFlags[$exists] = true;
235 return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
239 * This function normally does a database query to get the results; we need
240 * to make a pretend result using a FakeResultWrapper.
241 * @param string $offset
242 * @param int $limit
243 * @param bool $descending
244 * @return FakeResultWrapper
246 function reallyDoQuery( $offset, $limit, $descending ) {
247 $result = new FakeResultWrapper( [] );
249 $messageNames = $this->getAllMessages( $descending );
250 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
252 $count = 0;
253 foreach ( $messageNames as $key ) {
254 $customised = isset( $statuses['pages'][$key] );
255 if ( $customised !== $this->custom &&
256 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
257 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
259 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
260 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
261 $result->result[] = [
262 'am_title' => $key,
263 'am_actual' => $actual,
264 'am_default' => $default,
265 'am_customised' => $customised,
266 'am_talk_exists' => isset( $statuses['talks'][$key] )
268 $count++;
271 if ( $count === $limit ) {
272 break;
276 return $result;
279 function getStartBody() {
280 $tableClass = $this->getTableClass();
281 return Xml::openElement( 'table', [
282 'class' => "mw-datatable $tableClass",
283 'id' => 'mw-allmessagestable'
284 ] ) .
285 "\n" .
286 "<thead><tr>
287 <th rowspan=\"2\">" .
288 $this->msg( 'allmessagesname' )->escaped() . "
289 </th>
290 <th>" .
291 $this->msg( 'allmessagesdefault' )->escaped() .
292 "</th>
293 </tr>\n
294 <tr>
295 <th>" .
296 $this->msg( 'allmessagescurrent' )->escaped() .
297 "</th>
298 </tr></thead><tbody>\n";
301 function formatValue( $field, $value ) {
302 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
303 switch ( $field ) {
304 case 'am_title' :
305 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
306 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
307 $translation = Linker::makeExternalLink(
308 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
309 'title' => 'Special:SearchTranslations',
310 'group' => 'mediawiki',
311 'grouppath' => 'mediawiki',
312 'language' => $this->getLanguage()->getCode(),
313 'query' => $value . ' ' . $this->msg( $value )->plain()
314 ] ),
315 $this->msg( 'allmessages-filter-translate' )->text()
318 if ( $this->mCurrentRow->am_customised ) {
319 $title = $linkRenderer->makeKnownLink( $title, $this->getLanguage()->lcfirst( $value ) );
320 } else {
321 $title = $linkRenderer->makeBrokenLink(
322 $title,
323 $this->getLanguage()->lcfirst( $value )
326 if ( $this->mCurrentRow->am_talk_exists ) {
327 $talk = $linkRenderer->makeKnownLink( $talk, $this->talk );
328 } else {
329 $talk = $linkRenderer->makeBrokenLink(
330 $talk,
331 $this->talk
335 return $title . ' ' .
336 $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
337 ' ' .
338 $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
340 case 'am_default' :
341 case 'am_actual' :
342 return Sanitizer::escapeHtmlAllowEntities( $value );
345 return '';
348 function formatRow( $row ) {
349 // Do all the normal stuff
350 $s = parent::formatRow( $row );
352 // But if there's a customised message, add that too.
353 if ( $row->am_customised ) {
354 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
355 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
357 if ( $formatted === '' ) {
358 $formatted = '&#160;';
361 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
362 . "</tr>\n";
365 return $s;
368 function getRowAttrs( $row, $isSecond = false ) {
369 $arr = [];
371 if ( $row->am_customised ) {
372 $arr['class'] = 'allmessages-customised';
375 if ( !$isSecond ) {
376 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
379 return $arr;
382 function getCellAttrs( $field, $value ) {
383 if ( $this->mCurrentRow->am_customised && $field === 'am_title' ) {
384 return [ 'rowspan' => '2', 'class' => $field ];
385 } elseif ( $field === 'am_title' ) {
386 return [ 'class' => $field ];
387 } else {
388 return [
389 'lang' => $this->lang->getHtmlCode(),
390 'dir' => $this->lang->getDir(),
391 'class' => $field
396 // This is not actually used, as getStartBody is overridden above
397 function getFieldNames() {
398 return [
399 'am_title' => $this->msg( 'allmessagesname' )->text(),
400 'am_default' => $this->msg( 'allmessagesdefault' )->text()
404 function getTitle() {
405 return SpecialPage::getTitleFor( 'Allmessages', false );
408 function isFieldSortable( $x ) {
409 return false;
412 function getDefaultSort() {
413 return '';
416 function getQueryInfo() {
417 return '';