Merge "Add CollationFa"
[lhc/web/wiklou.git] / includes / specials / pagers / AllMessagesTablePager.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Pager
20 */
21
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.
25 *
26 * @ingroup Pager
27 */
28 use MediaWiki\MediaWikiServices;
29
30 class AllMessagesTablePager extends TablePager {
31
32 protected $filter, $prefix, $langcode, $displayPrefix;
33
34 public $mLimitsShown;
35
36 /**
37 * @var Language
38 */
39 public $lang;
40
41 /**
42 * @var null|bool
43 */
44 public $custom;
45
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 ];
54
55 global $wgContLang;
56
57 $this->talk = $this->msg( 'talkpagelinktext' )->escaped();
58
59 $this->lang = ( $langObj ? $langObj : $wgContLang );
60 $this->langcode = $this->lang->getCode();
61 $this->foreign = !$this->lang->equals( $wgContLang );
62
63 $request = $this->getRequest();
64
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' );
70 }
71
72 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
73 $prefix = $prefix !== '' ?
74 Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) :
75 null;
76
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;
83 }
84
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 = '';
91 }
92 }
93
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 );
98
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',
114 20,
115 str_replace( '_', ' ', $this->displayPrefix ),
116 [ 'id' => 'mw-allmessages-form-prefix' ]
117 ) .
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' )
130 ) .
131 Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
132 'filter',
133 'all',
134 'mw-allmessages-form-filter-all',
135 ( $this->filter === 'all' )
136 ) .
137 Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
138 'filter',
139 'modified',
140 'mw-allmessages-form-filter-modified',
141 ( $this->filter === 'modified' )
142 ) .
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>" .
149
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>" .
163
164 Xml::closeElement( 'table' ) .
165 $this->getHiddenFields( [ 'title', 'prefix', 'filter', 'lang', 'limit' ] ) .
166 Xml::closeElement( 'fieldset' ) .
167 Xml::closeElement( 'form' );
168
169 return $out;
170 }
171
172 function getAllMessages( $descending ) {
173 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
174
175 // Normalise message names so they look like page titles and sort correctly - T86139
176 $messageNames = array_map( [ $this->lang, 'ucfirst' ], $messageNames );
177
178 if ( $descending ) {
179 rsort( $messageNames );
180 } else {
181 asort( $messageNames );
182 }
183
184 return $messageNames;
185 }
186
187 /**
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.
192 *
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
197 */
198 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
199 // FIXME: This function should be moved to Language:: or something.
200
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' ]
207 );
208 $xNames = array_flip( $messageNames );
209
210 $pageFlags = $talkFlags = [];
211
212 foreach ( $res as $s ) {
213 $exists = false;
214
215 if ( $foreign ) {
216 $titleParts = explode( '/', $s->page_title );
217 if ( count( $titleParts ) === 2 &&
218 $langcode === $titleParts[1] &&
219 isset( $xNames[$titleParts[0]] )
220 ) {
221 $exists = $titleParts[0];
222 }
223 } elseif ( isset( $xNames[$s->page_title] ) ) {
224 $exists = $s->page_title;
225 }
226
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;
232 }
233 }
234
235 return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
236 }
237
238 /**
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
245 */
246 function reallyDoQuery( $offset, $limit, $descending ) {
247 $result = new FakeResultWrapper( [] );
248
249 $messageNames = $this->getAllMessages( $descending );
250 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
251
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 )
258 ) {
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] )
267 ];
268 $count++;
269 }
270
271 if ( $count === $limit ) {
272 break;
273 }
274 }
275
276 return $result;
277 }
278
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";
299 }
300
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()
316 );
317
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 )
324 );
325 }
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
332 );
333 }
334
335 return $title . ' ' .
336 $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
337 ' ' .
338 $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
339
340 case 'am_default' :
341 case 'am_actual' :
342 return Sanitizer::escapeHtmlAllowEntities( $value );
343 }
344
345 return '';
346 }
347
348 function formatRow( $row ) {
349 // Do all the normal stuff
350 $s = parent::formatRow( $row );
351
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 ) );
356
357 if ( $formatted === '' ) {
358 $formatted = '&#160;';
359 }
360
361 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
362 . "</tr>\n";
363 }
364
365 return $s;
366 }
367
368 function getRowAttrs( $row, $isSecond = false ) {
369 $arr = [];
370
371 if ( $row->am_customised ) {
372 $arr['class'] = 'allmessages-customised';
373 }
374
375 if ( !$isSecond ) {
376 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
377 }
378
379 return $arr;
380 }
381
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
392 ];
393 }
394 }
395
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()
401 ];
402 }
403
404 function getTitle() {
405 return SpecialPage::getTitleFor( 'Allmessages', false );
406 }
407
408 function isFieldSortable( $x ) {
409 return false;
410 }
411
412 function getDefaultSort() {
413 return '';
414 }
415
416 function getQueryInfo() {
417 return '';
418 }
419
420 }