Merge "Fixed FSFileOpHandle comment"
[lhc/web/wiklou.git] / includes / specials / SpecialAllMessages.php
1 <?php
2 /**
3 * Implements Special:Allmessages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Use this special page to get a list of the MediaWiki system messages.
26 *
27 * @file
28 * @ingroup SpecialPage
29 */
30 class SpecialAllMessages extends SpecialPage {
31 /**
32 * @var AllmessagesTablePager
33 */
34 protected $table;
35
36 /**
37 * Constructor
38 */
39 public function __construct() {
40 parent::__construct( 'Allmessages' );
41 }
42
43 /**
44 * Show the special page
45 *
46 * @param string $par Parameter passed to the page or null
47 */
48 public function execute( $par ) {
49 $request = $this->getRequest();
50 $out = $this->getOutput();
51
52 $this->setHeaders();
53
54 global $wgUseDatabaseMessages;
55 if ( !$wgUseDatabaseMessages ) {
56 $out->addWikiMsg( 'allmessagesnotsupportedDB' );
57
58 return;
59 } else {
60 $this->outputHeader( 'allmessagestext' );
61 }
62
63 $out->addModuleStyles( 'mediawiki.special' );
64
65 $this->table = new AllmessagesTablePager(
66 $this,
67 array(),
68 wfGetLangObj( $request->getVal( 'lang', $par ) )
69 );
70
71 $this->langcode = $this->table->lang->getCode();
72
73 $out->addHTML( $this->table->buildForm() .
74 $this->table->getNavigationBar() .
75 $this->table->getBody() .
76 $this->table->getNavigationBar() );
77 }
78
79 protected function getGroupName() {
80 return 'wiki';
81 }
82 }
83
84 /**
85 * Use TablePager for prettified output. We have to pretend that we're
86 * getting data from a table when in fact not all of it comes from the database.
87 */
88 class AllMessagesTablePager extends TablePager {
89 protected $filter, $prefix, $langcode, $displayPrefix;
90
91 public $mLimitsShown;
92
93 /**
94 * @var Language
95 */
96 public $lang;
97
98 /**
99 * @var null|bool
100 */
101 public $custom;
102
103 function __construct( $page, $conds, $langObj = null ) {
104 parent::__construct( $page->getContext() );
105 $this->mIndexField = 'am_title';
106 $this->mPage = $page;
107 $this->mConds = $conds;
108 $this->mDefaultDirection = true; // always sort ascending
109 $this->mLimitsShown = array( 20, 50, 100, 250, 500, 5000 );
110
111 global $wgContLang;
112
113 $this->talk = $this->msg( 'talkpagelinktext' )->escaped();
114
115 $this->lang = ( $langObj ? $langObj : $wgContLang );
116 $this->langcode = $this->lang->getCode();
117 $this->foreign = $this->langcode !== $wgContLang->getCode();
118
119 $request = $this->getRequest();
120
121 $this->filter = $request->getVal( 'filter', 'all' );
122 if ( $this->filter === 'all' ) {
123 $this->custom = null; // So won't match in either case
124 } else {
125 $this->custom = ( $this->filter === 'unmodified' );
126 }
127
128 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
129 $prefix = $prefix !== '' ?
130 Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) :
131 null;
132
133 if ( $prefix !== null ) {
134 $this->displayPrefix = $prefix->getDBkey();
135 $this->prefix = '/^' . preg_quote( $this->displayPrefix ) . '/i';
136 } else {
137 $this->displayPrefix = false;
138 $this->prefix = false;
139 }
140
141 // The suffix that may be needed for message names if we're in a
142 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
143 if ( $this->foreign ) {
144 $this->suffix = '/' . $this->langcode;
145 } else {
146 $this->suffix = '';
147 }
148 }
149
150 function buildForm() {
151 global $wgScript;
152
153 $attrs = array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' );
154 $msg = wfMessage( 'allmessages-language' );
155 $langSelect = Xml::languageSelector( $this->langcode, false, null, $attrs, $msg );
156
157 $out = Xml::openElement( 'form', array(
158 'method' => 'get',
159 'action' => $wgScript,
160 'id' => 'mw-allmessages-form'
161 ) ) .
162 Xml::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
163 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
164 Xml::openElement( 'table', array( 'class' => 'mw-allmessages-table' ) ) . "\n" .
165 '<tr>
166 <td class="mw-label">' .
167 Xml::label( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
168 "</td>\n
169 <td class=\"mw-input\">" .
170 Xml::input(
171 'prefix',
172 20,
173 str_replace( '_', ' ', $this->displayPrefix ),
174 array( 'id' => 'mw-allmessages-form-prefix' )
175 ) .
176 "</td>\n
177 </tr>
178 <tr>\n
179 <td class='mw-label'>" .
180 $this->msg( 'allmessages-filter' )->escaped() .
181 "</td>\n
182 <td class='mw-input'>" .
183 Xml::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
184 'filter',
185 'unmodified',
186 'mw-allmessages-form-filter-unmodified',
187 ( $this->filter === 'unmodified' )
188 ) .
189 Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
190 'filter',
191 'all',
192 'mw-allmessages-form-filter-all',
193 ( $this->filter === 'all' )
194 ) .
195 Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
196 'filter',
197 'modified',
198 'mw-allmessages-form-filter-modified',
199 ( $this->filter === 'modified' )
200 ) .
201 "</td>\n
202 </tr>
203 <tr>\n
204 <td class=\"mw-label\">" . $langSelect[0] . "</td>\n
205 <td class=\"mw-input\">" . $langSelect[1] . "</td>\n
206 </tr>" .
207
208 '<tr>
209 <td class="mw-label">' .
210 Xml::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
211 '</td>
212 <td class="mw-input">' .
213 $this->getLimitSelect() .
214 '</td>
215 <tr>
216 <td></td>
217 <td>' .
218 Xml::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
219 "</td>\n
220 </tr>" .
221
222 Xml::closeElement( 'table' ) .
223 $this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit' ) ) .
224 Xml::closeElement( 'fieldset' ) .
225 Xml::closeElement( 'form' );
226
227 return $out;
228 }
229
230 function getAllMessages( $descending ) {
231 wfProfileIn( __METHOD__ );
232 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
233 if ( $descending ) {
234 rsort( $messageNames );
235 } else {
236 asort( $messageNames );
237 }
238
239 // Normalise message names so they look like page titles
240 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
241
242 wfProfileOut( __METHOD__ );
243
244 return $messageNames;
245 }
246
247 /**
248 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
249 * Returns array( 'pages' => ..., 'talks' => ... ), where the subarrays have
250 * an entry for each existing page, with the key being the message name and
251 * value arbitrary.
252 *
253 * @param array $messageNames
254 * @param string $langcode What language code
255 * @param bool $foreign Whether the $langcode is not the content language
256 * @return array A 'pages' and 'talks' array with the keys of existing pages
257 */
258 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
259 // FIXME: This function should be moved to Language:: or something.
260 wfProfileIn( __METHOD__ . '-db' );
261
262 $dbr = wfGetDB( DB_SLAVE );
263 $res = $dbr->select( 'page',
264 array( 'page_namespace', 'page_title' ),
265 array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
266 __METHOD__,
267 array( 'USE INDEX' => 'name_title' )
268 );
269 $xNames = array_flip( $messageNames );
270
271 $pageFlags = $talkFlags = array();
272
273 foreach ( $res as $s ) {
274 $exists = false;
275
276 if ( $foreign ) {
277 $titleParts = explode( '/', $s->page_title );
278 if ( count( $titleParts ) === 2 &&
279 $langcode === $titleParts[1] &&
280 isset( $xNames[$titleParts[0]] )
281 ) {
282 $exists = $titleParts[0];
283 }
284 } elseif ( isset( $xNames[$s->page_title] ) ) {
285 $exists = $s->page_title;
286 }
287
288 $title = Title::newFromRow( $s );
289 if ( $exists && $title->inNamespace( NS_MEDIAWIKI ) ) {
290 $pageFlags[$exists] = true;
291 } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK ) ) {
292 $talkFlags[$exists] = true;
293 }
294 }
295
296 wfProfileOut( __METHOD__ . '-db' );
297
298 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
299 }
300
301 /**
302 * This function normally does a database query to get the results; we need
303 * to make a pretend result using a FakeResultWrapper.
304 * @param string $offset
305 * @param int $limit
306 * @param bool $descending
307 * @return FakeResultWrapper
308 */
309 function reallyDoQuery( $offset, $limit, $descending ) {
310 $result = new FakeResultWrapper( array() );
311
312 $messageNames = $this->getAllMessages( $descending );
313 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
314
315 $count = 0;
316 foreach ( $messageNames as $key ) {
317 $customised = isset( $statuses['pages'][$key] );
318 if ( $customised !== $this->custom &&
319 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
320 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
321 ) {
322 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
323 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
324 $result->result[] = array(
325 'am_title' => $key,
326 'am_actual' => $actual,
327 'am_default' => $default,
328 'am_customised' => $customised,
329 'am_talk_exists' => isset( $statuses['talks'][$key] )
330 );
331 $count++;
332 }
333
334 if ( $count === $limit ) {
335 break;
336 }
337 }
338
339 return $result;
340 }
341
342 function getStartBody() {
343 return Xml::openElement( 'table', array(
344 'class' => 'mw-datatable TablePager',
345 'id' => 'mw-allmessagestable'
346 ) ) .
347 "\n" .
348 "<thead><tr>
349 <th rowspan=\"2\">" .
350 $this->msg( 'allmessagesname' )->escaped() . "
351 </th>
352 <th>" .
353 $this->msg( 'allmessagesdefault' )->escaped() .
354 "</th>
355 </tr>\n
356 <tr>
357 <th>" .
358 $this->msg( 'allmessagescurrent' )->escaped() .
359 "</th>
360 </tr></thead><tbody>\n";
361 }
362
363 function formatValue( $field, $value ) {
364 switch ( $field ) {
365 case 'am_title' :
366 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
367 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
368 $translation = Linker::makeExternalLink(
369 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( array(
370 'title' => 'Special:SearchTranslations',
371 'group' => 'mediawiki',
372 'grouppath' => 'mediawiki',
373 'query' => 'language:' . $this->getLanguage()->getCode() . '^25 ' .
374 'messageid:"MediaWiki:' . $value . '"^10 "' .
375 $this->msg( $value )->inLanguage( 'en' )->plain() . '"'
376 ) ),
377 $this->msg( 'allmessages-filter-translate' )->text()
378 );
379
380 if ( $this->mCurrentRow->am_customised ) {
381 $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
382 } else {
383 $title = Linker::link(
384 $title,
385 $this->getLanguage()->lcfirst( $value ),
386 array(),
387 array(),
388 array( 'broken' )
389 );
390 }
391 if ( $this->mCurrentRow->am_talk_exists ) {
392 $talk = Linker::linkKnown( $talk, $this->talk );
393 } else {
394 $talk = Linker::link(
395 $talk,
396 $this->talk,
397 array(),
398 array(),
399 array( 'broken' )
400 );
401 }
402
403 return $title . ' '
404 . $this->msg( 'parentheses' )->rawParams( $talk )->escaped()
405 . ' '
406 . $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
407
408 case 'am_default' :
409 case 'am_actual' :
410 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
411 }
412
413 return '';
414 }
415
416 function formatRow( $row ) {
417 // Do all the normal stuff
418 $s = parent::formatRow( $row );
419
420 // But if there's a customised message, add that too.
421 if ( $row->am_customised ) {
422 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
423 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
424
425 if ( $formatted === '' ) {
426 $formatted = '&#160;';
427 }
428
429 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
430 . "</tr>\n";
431 }
432
433 return $s;
434 }
435
436 function getRowAttrs( $row, $isSecond = false ) {
437 $arr = array();
438
439 if ( $row->am_customised ) {
440 $arr['class'] = 'allmessages-customised';
441 }
442
443 if ( !$isSecond ) {
444 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
445 }
446
447 return $arr;
448 }
449
450 function getCellAttrs( $field, $value ) {
451 if ( $this->mCurrentRow->am_customised && $field === 'am_title' ) {
452 return array( 'rowspan' => '2', 'class' => $field );
453 } elseif ( $field === 'am_title' ) {
454 return array( 'class' => $field );
455 } else {
456 return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir(), 'class' => $field );
457 }
458 }
459
460 // This is not actually used, as getStartBody is overridden above
461 function getFieldNames() {
462 return array(
463 'am_title' => $this->msg( 'allmessagesname' )->text(),
464 'am_default' => $this->msg( 'allmessagesdefault' )->text()
465 );
466 }
467
468 function getTitle() {
469 return SpecialPage::getTitleFor( 'Allmessages', false );
470 }
471
472 function isFieldSortable( $x ) {
473 return false;
474 }
475
476 function getDefaultSort() {
477 return '';
478 }
479
480 function getQueryInfo() {
481 return '';
482 }
483 }