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