Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[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 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
70 $this->lang = wfGetLangObj( $opts->getValue( 'lang' ) );
71
72 $this->langcode = $this->lang->getCode();
73 $this->foreign = !$this->lang->equals( $contLang );
74
75 $filter = $opts->getValue( 'filter' );
76 if ( $filter === 'all' ) {
77 $this->custom = null; // So won't match in either case
78 } else {
79 $this->custom = ( $filter === 'unmodified' );
80 }
81
82 $prefix = $this->getLanguage()->ucfirst( $opts->getValue( 'prefix' ) );
83 $prefix = $prefix !== '' ?
84 Title::makeTitleSafe( NS_MEDIAWIKI, $opts->getValue( 'prefix' ) ) :
85 null;
86
87 if ( $prefix !== null ) {
88 $displayPrefix = $prefix->getDBkey();
89 $this->prefix = '/^' . preg_quote( $displayPrefix, '/' ) . '/i';
90 } else {
91 $this->prefix = false;
92 }
93
94 // The suffix that may be needed for message names if we're in a
95 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
96 if ( $this->foreign ) {
97 $this->suffix = '/' . $this->langcode;
98 } else {
99 $this->suffix = '';
100 }
101 }
102
103 function getAllMessages( $descending ) {
104 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
105
106 // Normalise message names so they look like page titles and sort correctly - T86139
107 $messageNames = array_map( [ $this->lang, 'ucfirst' ], $messageNames );
108
109 if ( $descending ) {
110 rsort( $messageNames );
111 } else {
112 asort( $messageNames );
113 }
114
115 return $messageNames;
116 }
117
118 /**
119 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
120 * Returns [ 'pages' => ..., 'talks' => ... ], where the subarrays have
121 * an entry for each existing page, with the key being the message name and
122 * value arbitrary.
123 *
124 * @param array $messageNames
125 * @param string $langcode What language code
126 * @param bool $foreign Whether the $langcode is not the content language
127 * @return array A 'pages' and 'talks' array with the keys of existing pages
128 */
129 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
130 // FIXME: This function should be moved to Language:: or something.
131
132 $dbr = wfGetDB( DB_REPLICA );
133 $res = $dbr->select( 'page',
134 [ 'page_namespace', 'page_title' ],
135 [ 'page_namespace' => [ NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ] ],
136 __METHOD__,
137 [ 'USE INDEX' => 'name_title' ]
138 );
139 $xNames = array_flip( $messageNames );
140
141 $pageFlags = $talkFlags = [];
142
143 foreach ( $res as $s ) {
144 $exists = false;
145
146 if ( $foreign ) {
147 $titleParts = explode( '/', $s->page_title );
148 if ( count( $titleParts ) === 2 &&
149 $langcode === $titleParts[1] &&
150 isset( $xNames[$titleParts[0]] )
151 ) {
152 $exists = $titleParts[0];
153 }
154 } elseif ( isset( $xNames[$s->page_title] ) ) {
155 $exists = $s->page_title;
156 }
157
158 $title = Title::newFromRow( $s );
159 if ( $exists && $title->inNamespace( NS_MEDIAWIKI ) ) {
160 $pageFlags[$exists] = true;
161 } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK ) ) {
162 $talkFlags[$exists] = true;
163 }
164 }
165
166 return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
167 }
168
169 /**
170 * This function normally does a database query to get the results; we need
171 * to make a pretend result using a FakeResultWrapper.
172 * @param string $offset
173 * @param int $limit
174 * @param bool $order
175 * @return FakeResultWrapper
176 */
177 function reallyDoQuery( $offset, $limit, $order ) {
178 $asc = ( $order === self::QUERY_ASCENDING );
179
180 $messageNames = $this->getAllMessages( $order );
181 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
182
183 $rows = [];
184 $count = 0;
185 foreach ( $messageNames as $key ) {
186 $customised = isset( $statuses['pages'][$key] );
187 if ( $customised !== $this->custom &&
188 ( $asc && ( $key < $offset || !$offset ) || !$asc && $key > $offset ) &&
189 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
190 ) {
191 $actual = $this->msg( $key )->inLanguage( $this->lang )->plain();
192 $default = $this->msg( $key )->inLanguage( $this->lang )->useDatabase( false )->plain();
193 $rows[] = [
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 new FakeResultWrapper( $rows );
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 /**
286 * @param stdClass $row
287 * @return string HTML
288 */
289 function formatRow( $row ) {
290 // Do all the normal stuff
291 $s = parent::formatRow( $row );
292
293 // But if there's a customised message, add that too.
294 if ( $row->am_customised ) {
295 $s .= Html::openElement( 'tr', $this->getRowAttrs( $row, true ) );
296 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
297
298 if ( $formatted === '' ) {
299 $formatted = "\u{00A0}";
300 }
301
302 $s .= Html::element( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
303 . Html::closeElement( 'tr' );
304 }
305
306 return Html::rawElement( 'tbody', [], $s );
307 }
308
309 function getRowAttrs( $row ) {
310 return [];
311 }
312
313 /**
314 * @param string $field
315 * @param string $value
316 * @return array HTML attributes
317 */
318 function getCellAttrs( $field, $value ) {
319 $attr = [];
320 if ( $field === 'am_title' ) {
321 if ( $this->mCurrentRow->am_customised ) {
322 $attr += [ 'rowspan' => '2' ];
323 }
324 } else {
325 $attr += [
326 'lang' => $this->lang->getHtmlCode(),
327 'dir' => $this->lang->getDir(),
328 ];
329 if ( $this->mCurrentRow->am_customised ) {
330 // CSS class: am_default, am_actual
331 $attr += [ 'class' => $field ];
332 }
333 }
334 return $attr;
335 }
336
337 // This is not actually used, as getStartBody is overridden above
338 function getFieldNames() {
339 return [
340 'am_title' => $this->msg( 'allmessagesname' )->text(),
341 'am_default' => $this->msg( 'allmessagesdefault' )->text()
342 ];
343 }
344
345 function getTitle() {
346 return SpecialPage::getTitleFor( 'Allmessages', false );
347 }
348
349 function isFieldSortable( $x ) {
350 return false;
351 }
352
353 function getDefaultSort() {
354 return '';
355 }
356
357 function getQueryInfo() {
358 return '';
359 }
360
361 }