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