resourceloader: Remove comment about XmlJsCode wrapper
[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 use MediaWiki\MediaWikiServices;
23 use MediaWiki\Linker\LinkRenderer;
24 use Wikimedia\Rdbms\FakeResultWrapper;
25
26 /**
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.
29 *
30 * @ingroup Pager
31 */
32 class AllMessagesTablePager extends TablePager {
33
34 /**
35 * @var string
36 */
37 protected $langcode;
38
39 /**
40 * @var bool
41 */
42 protected $foreign;
43
44 /**
45 * @var string
46 */
47 protected $prefix;
48
49 /**
50 * @var Language
51 */
52 public $lang;
53
54 /**
55 * @var null|bool
56 */
57 public $custom;
58
59 /**
60 * @param IContextSource|null $context
61 * @param FormOptions $opts
62 * @param LinkRenderer $linkRenderer
63 */
64 public function __construct( IContextSource $context = null, FormOptions $opts,
65 LinkRenderer $linkRenderer
66 ) {
67 parent::__construct( $context, $linkRenderer );
68
69 $this->mIndexField = 'am_title';
70 // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
71 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
72
73 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
74 $this->lang = wfGetLangObj( $opts->getValue( 'lang' ) );
75
76 $this->langcode = $this->lang->getCode();
77 $this->foreign = !$this->lang->equals( $contLang );
78
79 $filter = $opts->getValue( 'filter' );
80 if ( $filter === 'all' ) {
81 $this->custom = null; // So won't match in either case
82 } else {
83 $this->custom = ( $filter === 'unmodified' );
84 }
85
86 $prefix = $this->getLanguage()->ucfirst( $opts->getValue( 'prefix' ) );
87 $prefix = $prefix !== '' ?
88 Title::makeTitleSafe( NS_MEDIAWIKI, $opts->getValue( 'prefix' ) ) :
89 null;
90
91 if ( $prefix !== null ) {
92 $displayPrefix = $prefix->getDBkey();
93 $this->prefix = '/^' . preg_quote( $displayPrefix, '/' ) . '/i';
94 } else {
95 $this->prefix = false;
96 }
97
98 // The suffix that may be needed for message names if we're in a
99 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
100 if ( $this->foreign ) {
101 $this->suffix = '/' . $this->langcode;
102 } else {
103 $this->suffix = '';
104 }
105 }
106
107 function getAllMessages( $descending ) {
108 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
109
110 // Normalise message names so they look like page titles and sort correctly - T86139
111 $messageNames = array_map( [ $this->lang, 'ucfirst' ], $messageNames );
112
113 if ( $descending ) {
114 rsort( $messageNames );
115 } else {
116 asort( $messageNames );
117 }
118
119 return $messageNames;
120 }
121
122 /**
123 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
124 * Returns [ 'pages' => ..., 'talks' => ... ], where the subarrays have
125 * an entry for each existing page, with the key being the message name and
126 * value arbitrary.
127 *
128 * @param array $messageNames
129 * @param string $langcode What language code
130 * @param bool $foreign Whether the $langcode is not the content language
131 * @return array A 'pages' and 'talks' array with the keys of existing pages
132 */
133 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
134 // FIXME: This function should be moved to Language:: or something.
135
136 $dbr = wfGetDB( DB_REPLICA );
137 $res = $dbr->select( 'page',
138 [ 'page_namespace', 'page_title' ],
139 [ 'page_namespace' => [ NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ] ],
140 __METHOD__,
141 [ 'USE INDEX' => 'name_title' ]
142 );
143 $xNames = array_flip( $messageNames );
144
145 $pageFlags = $talkFlags = [];
146
147 foreach ( $res as $s ) {
148 $exists = false;
149
150 if ( $foreign ) {
151 $titleParts = explode( '/', $s->page_title );
152 if ( count( $titleParts ) === 2 &&
153 $langcode === $titleParts[1] &&
154 isset( $xNames[$titleParts[0]] )
155 ) {
156 $exists = $titleParts[0];
157 }
158 } elseif ( isset( $xNames[$s->page_title] ) ) {
159 $exists = $s->page_title;
160 }
161
162 $title = Title::newFromRow( $s );
163 if ( $exists && $title->inNamespace( NS_MEDIAWIKI ) ) {
164 $pageFlags[$exists] = true;
165 } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK ) ) {
166 $talkFlags[$exists] = true;
167 }
168 }
169
170 return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
171 }
172
173 /**
174 * This function normally does a database query to get the results; we need
175 * to make a pretend result using a FakeResultWrapper.
176 * @param string $offset
177 * @param int $limit
178 * @param bool $order
179 * @return FakeResultWrapper
180 */
181 function reallyDoQuery( $offset, $limit, $order ) {
182 $asc = ( $order === self::QUERY_ASCENDING );
183
184 $messageNames = $this->getAllMessages( $order );
185 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
186
187 $rows = [];
188 $count = 0;
189 foreach ( $messageNames as $key ) {
190 $customised = isset( $statuses['pages'][$key] );
191 if ( $customised !== $this->custom &&
192 ( $asc && ( $key < $offset || !$offset ) || !$asc && $key > $offset ) &&
193 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
194 ) {
195 $actual = $this->msg( $key )->inLanguage( $this->lang )->plain();
196 $default = $this->msg( $key )->inLanguage( $this->lang )->useDatabase( false )->plain();
197 $rows[] = [
198 'am_title' => $key,
199 'am_actual' => $actual,
200 'am_default' => $default,
201 'am_customised' => $customised,
202 'am_talk_exists' => isset( $statuses['talks'][$key] )
203 ];
204 $count++;
205 }
206
207 if ( $count === $limit ) {
208 break;
209 }
210 }
211
212 return new FakeResultWrapper( $rows );
213 }
214
215 protected function getStartBody() {
216 $tableClass = $this->getTableClass();
217 return Xml::openElement( 'table', [
218 'class' => "mw-datatable $tableClass",
219 'id' => 'mw-allmessagestable'
220 ] ) .
221 "\n" .
222 "<thead><tr>
223 <th rowspan=\"2\">" .
224 $this->msg( 'allmessagesname' )->escaped() . "
225 </th>
226 <th>" .
227 $this->msg( 'allmessagesdefault' )->escaped() .
228 "</th>
229 </tr>\n
230 <tr>
231 <th>" .
232 $this->msg( 'allmessagescurrent' )->escaped() .
233 "</th>
234 </tr></thead>\n";
235 }
236
237 function getEndBody() {
238 return Html::closeElement( 'table' );
239 }
240
241 function formatValue( $field, $value ) {
242 $linkRenderer = $this->getLinkRenderer();
243 switch ( $field ) {
244 case 'am_title' :
245 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
246 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
247 $translation = Linker::makeExternalLink(
248 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
249 'title' => 'Special:SearchTranslations',
250 'group' => 'mediawiki',
251 'grouppath' => 'mediawiki',
252 'language' => $this->getLanguage()->getCode(),
253 'query' => $value . ' ' . $this->msg( $value )->plain()
254 ] ),
255 $this->msg( 'allmessages-filter-translate' )->text()
256 );
257 $talkLink = $this->msg( 'talkpagelinktext' )->escaped();
258
259 if ( $this->mCurrentRow->am_customised ) {
260 $title = $linkRenderer->makeKnownLink( $title, $this->getLanguage()->lcfirst( $value ) );
261 } else {
262 $title = $linkRenderer->makeBrokenLink(
263 $title, $this->getLanguage()->lcfirst( $value )
264 );
265 }
266 if ( $this->mCurrentRow->am_talk_exists ) {
267 $talk = $linkRenderer->makeKnownLink( $talk, $talkLink );
268 } else {
269 $talk = $linkRenderer->makeBrokenLink(
270 $talk,
271 $talkLink
272 );
273 }
274
275 return $title . ' ' .
276 $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
277 ' ' .
278 $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
279
280 case 'am_default' :
281 case 'am_actual' :
282 return Sanitizer::escapeHtmlAllowEntities( $value );
283 }
284
285 return '';
286 }
287
288 /**
289 * @param stdClass $row
290 * @return string HTML
291 */
292 function formatRow( $row ) {
293 // Do all the normal stuff
294 $s = parent::formatRow( $row );
295
296 // But if there's a customised message, add that too.
297 if ( $row->am_customised ) {
298 $s .= Html::openElement( 'tr', $this->getRowAttrs( $row, true ) );
299 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
300
301 if ( $formatted === '' ) {
302 $formatted = "\u{00A0}";
303 }
304
305 $s .= Html::element( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
306 . Html::closeElement( 'tr' );
307 }
308
309 return Html::rawElement( 'tbody', [], $s );
310 }
311
312 function getRowAttrs( $row ) {
313 return [];
314 }
315
316 /**
317 * @param string $field
318 * @param string $value
319 * @return array HTML attributes
320 */
321 function getCellAttrs( $field, $value ) {
322 $attr = [];
323 if ( $field === 'am_title' ) {
324 if ( $this->mCurrentRow->am_customised ) {
325 $attr += [ 'rowspan' => '2' ];
326 }
327 } else {
328 $attr += [
329 'lang' => $this->lang->getHtmlCode(),
330 'dir' => $this->lang->getDir(),
331 ];
332 if ( $this->mCurrentRow->am_customised ) {
333 // CSS class: am_default, am_actual
334 $attr += [ 'class' => $field ];
335 }
336 }
337 return $attr;
338 }
339
340 // This is not actually used, as getStartBody is overridden above
341 function getFieldNames() {
342 return [
343 'am_title' => $this->msg( 'allmessagesname' )->text(),
344 'am_default' => $this->msg( 'allmessagesdefault' )->text()
345 ];
346 }
347
348 function getTitle() {
349 return SpecialPage::getTitleFor( 'Allmessages', false );
350 }
351
352 function isFieldSortable( $x ) {
353 return false;
354 }
355
356 function getDefaultSort() {
357 return '';
358 }
359
360 function getQueryInfo() {
361 return [];
362 }
363
364 }