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