Fix casing of langcode
[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 /**
33 * @var AllmessagesTablePager
34 */
35 protected $table;
36
37 /**
38 * Constructor
39 */
40 public function __construct() {
41 parent::__construct( 'Allmessages' );
42 }
43
44 /**
45 * Show the special page
46 *
47 * @param $par Mixed: parameter passed to the page or null
48 */
49 public function execute( $par ) {
50 $request = $this->getRequest();
51 $out = $this->getOutput();
52
53 $this->setHeaders();
54
55 global $wgUseDatabaseMessages;
56 if( !$wgUseDatabaseMessages ) {
57 $out->addWikiMsg( 'allmessagesnotsupportedDB' );
58 return;
59 } else {
60 $this->outputHeader( 'allmessagestext' );
61 }
62
63 $out->addModuleStyles( 'mediawiki.special' );
64
65 $this->filter = $request->getVal( 'filter', 'all' );
66 $this->prefix = $request->getVal( 'prefix', '' );
67
68 $this->table = new AllmessagesTablePager(
69 $this,
70 array(),
71 wfGetLangObj( $request->getVal( 'lang', $par ) )
72 );
73
74 $this->langcode = $this->table->lang->getCode();
75
76 $out->addHTML( $this->table->buildForm() .
77 $this->table->getNavigationBar() .
78 $this->table->getBody() .
79 $this->table->getNavigationBar() );
80
81 }
82
83 }
84
85 /**
86 * Use TablePager for prettified output. We have to pretend that we're
87 * getting data from a table when in fact not all of it comes from the database.
88 */
89 class AllmessagesTablePager extends TablePager {
90
91 protected $filter, $prefix, $langcode;
92
93 public $mLimitsShown;
94
95 /**
96 * @var Skin
97 */
98 protected $mSkin;
99
100 /**
101 * @var Language
102 */
103 public $lang;
104
105 /**
106 * @var null|bool
107 */
108 public $custom;
109
110 function __construct( $page, $conds, $langObj = null ) {
111 parent::__construct();
112 $this->mIndexField = 'am_title';
113 $this->mPage = $page;
114 $this->mConds = $conds;
115 $this->mDefaultDirection = true; // always sort ascending
116 $this->mLimitsShown = array( 20, 50, 100, 250, 500, 5000 );
117
118 global $wgLang, $wgContLang, $wgRequest;
119
120 $this->talk = htmlspecialchars( wfMsg( 'talkpagelinktext' ) );
121
122 $this->lang = ( $langObj ? $langObj : $wgContLang );
123 $this->langcode = $this->lang->getCode();
124 $this->foreign = $this->langcode != $wgContLang->getCode();
125
126 if( $wgRequest->getVal( 'filter', 'all' ) === 'all' ){
127 $this->custom = null; // So won't match in either case
128 } else {
129 $this->custom = ($wgRequest->getVal( 'filter' ) == 'unmodified');
130 }
131
132 $prefix = $wgLang->ucfirst( $wgRequest->getVal( 'prefix', '' ) );
133 $prefix = $prefix != '' ? Title::makeTitleSafe( NS_MEDIAWIKI, $wgRequest->getVal( 'prefix', null ) ) : null;
134 if( $prefix !== null ){
135 $this->prefix = '/^' . preg_quote( $prefix->getDBkey() ) . '/i';
136 } else {
137 $this->prefix = false;
138 }
139 $this->getSkin();
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 $languages = Language::getLanguageNames( false );
154 ksort( $languages );
155
156 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-allmessages-form' ) ) .
157 Xml::fieldset( wfMsg( 'allmessages-filter-legend' ) ) .
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( wfMsg( 'allmessages-prefix' ), 'mw-allmessages-form-prefix' ) .
163 "</td>\n
164 <td class=\"mw-input\">" .
165 Xml::input( 'prefix', 20, str_replace( '_', ' ', $this->prefix ), array( 'id' => 'mw-allmessages-form-prefix' ) ) .
166 "</td>\n
167 </tr>
168 <tr>\n
169 <td class='mw-label'>" .
170 wfMsg( 'allmessages-filter' ) .
171 "</td>\n
172 <td class='mw-input'>" .
173 Xml::radioLabel( wfMsg( 'allmessages-filter-unmodified' ),
174 'filter',
175 'unmodified',
176 'mw-allmessages-form-filter-unmodified',
177 ( $this->filter == 'unmodified' )
178 ) .
179 Xml::radioLabel( wfMsg( 'allmessages-filter-all' ),
180 'filter',
181 'all',
182 'mw-allmessages-form-filter-all',
183 ( $this->filter == 'all' )
184 ) .
185 Xml::radioLabel( wfMsg( 'allmessages-filter-modified' ),
186 'filter',
187 'modified',
188 'mw-allmessages-form-filter-modified',
189 ( $this->filter == 'modified' )
190 ) .
191 "</td>\n
192 </tr>
193 <tr>\n
194 <td class=\"mw-label\">" .
195 Xml::label( wfMsg( 'allmessages-language' ), 'mw-allmessages-form-lang' ) .
196 "</td>\n
197 <td class=\"mw-input\">" .
198 Xml::openElement( 'select', array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ) );
199
200 foreach( $languages as $lang => $name ) {
201 $selected = $lang == $this->langcode;
202 $out .= Xml::option( $lang . ' - ' . $name, $lang, $selected ) . "\n";
203 }
204 $out .= Xml::closeElement( 'select' ) .
205 "</td>\n
206 </tr>" .
207
208 '<tr>
209 <td class="mw-label">' .
210 Xml::label( wfMsg( 'table_pager_limit_label'), '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( wfMsg( 'allmessages-filter-submit' ) ) .
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 return $out;
227 }
228
229 function getAllMessages( $descending ) {
230 wfProfileIn( __METHOD__ );
231 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
232 if( $descending ){
233 rsort( $messageNames );
234 } else {
235 asort( $messageNames );
236 }
237
238 // Normalise message names so they look like page titles
239 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
240
241 wfProfileOut( __METHOD__ );
242 return $messageNames;
243 }
244
245 /**
246 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
247 * Returns array( 'pages' => ..., 'talks' => ... ), where the subarrays have
248 * an entry for each existing page, with the key being the message name and
249 * value arbitrary.
250 *
251 * @param array $messageNames
252 * @param string $langcode What language code
253 * @param bool $foreign Whether the $langcode is not the content language
254 */
255 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
256 wfProfileIn( __METHOD__ . '-db' );
257
258 $dbr = wfGetDB( DB_SLAVE );
259 $res = $dbr->select( 'page',
260 array( 'page_namespace', 'page_title' ),
261 array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
262 __METHOD__,
263 array( 'USE INDEX' => 'name_title' )
264 );
265 $xNames = array_flip( $messageNames );
266
267 $pageFlags = $talkFlags = array();
268
269 foreach ( $res as $s ) {
270 if( $s->page_namespace == NS_MEDIAWIKI ) {
271 if( $foreign ) {
272 $title = explode( '/', $s->page_title );
273 if( count( $title ) === 2 && $langcode == $title[1]
274 && isset( $xNames[$title[0]] ) ) {
275 $pageFlags["{$title[0]}"] = true;
276 }
277 } elseif( isset( $xNames[$s->page_title] ) ) {
278 $pageFlags[$s->page_title] = true;
279 }
280 } elseif( $s->page_namespace == NS_MEDIAWIKI_TALK ){
281 $talkFlags[$s->page_title] = true;
282 }
283 }
284
285 wfProfileOut( __METHOD__ . '-db' );
286
287 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
288 }
289
290 /**
291 * This function normally does a database query to get the results; we need
292 * to make a pretend result using a FakeResultWrapper.
293 */
294 function reallyDoQuery( $offset, $limit, $descending ) {
295 $result = new FakeResultWrapper( array() );
296
297 $messageNames = $this->getAllMessages( $descending );
298 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
299
300 $count = 0;
301 foreach( $messageNames as $key ) {
302 $customised = isset( $statuses['pages'][$key] );
303 if( $customised !== $this->custom &&
304 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
305 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
306 ) {
307 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
308 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
309 $result->result[] = array(
310 'am_title' => $key,
311 'am_actual' => $actual,
312 'am_default' => $default,
313 'am_customised' => $customised,
314 'am_talk_exists' => isset( $statuses['talks'][$key] )
315 );
316 $count++;
317 }
318 if( $count == $limit ) {
319 break;
320 }
321 }
322 return $result;
323 }
324
325 function getStartBody() {
326 return Xml::openElement( 'table', array( 'class' => 'TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
327 "<thead><tr>
328 <th rowspan=\"2\">" .
329 wfMsg( 'allmessagesname' ) . "
330 </th>
331 <th>" .
332 wfMsg( 'allmessagesdefault' ) .
333 "</th>
334 </tr>\n
335 <tr>
336 <th>" .
337 wfMsg( 'allmessagescurrent' ) .
338 "</th>
339 </tr></thead><tbody>\n";
340 }
341
342 function formatValue( $field, $value ){
343 global $wgLang;
344 switch( $field ){
345
346 case 'am_title' :
347
348 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
349 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
350
351 if( $this->mCurrentRow->am_customised ){
352 $title = $this->mSkin->linkKnown( $title, $wgLang->lcfirst( $value ) );
353 } else {
354 $title = $this->mSkin->link(
355 $title,
356 $wgLang->lcfirst( $value ),
357 array(),
358 array(),
359 array( 'broken' )
360 );
361 }
362 if ( $this->mCurrentRow->am_talk_exists ) {
363 $talk = $this->mSkin->linkKnown( $talk , $this->talk );
364 } else {
365 $talk = $this->mSkin->link(
366 $talk,
367 $this->talk,
368 array(),
369 array(),
370 array( 'broken' )
371 );
372 }
373 return $title . ' (' . $talk . ')';
374
375 case 'am_default' :
376 case 'am_actual' :
377 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
378 }
379 return '';
380 }
381
382 function formatRow( $row ){
383 // Do all the normal stuff
384 $s = parent::formatRow( $row );
385
386 // But if there's a customised message, add that too.
387 if( $row->am_customised ){
388 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
389 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
390 if ( $formatted == '' ) {
391 $formatted = '&#160;';
392 }
393 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
394 . "</tr>\n";
395 }
396 return $s;
397 }
398
399 function getRowAttrs( $row, $isSecond = false ){
400 $arr = array();
401 global $wgLang;
402 if( $row->am_customised ){
403 $arr['class'] = 'allmessages-customised';
404 }
405 if( !$isSecond ){
406 $arr['id'] = Sanitizer::escapeId( 'msg_' . $wgLang->lcfirst( $row->am_title ) );
407 }
408 return $arr;
409 }
410
411 function getCellAttrs( $field, $value ){
412 global $wgBetterDirectionality;
413 if( $field != 'am_title' && $wgBetterDirectionality ) {
414 return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir() );
415 } elseif( $this->mCurrentRow->am_customised && $field == 'am_title' ) {
416 return array( 'rowspan' => '2', 'class' => $field );
417 } else {
418 return array( 'class' => $field );
419 }
420 }
421
422 // This is not actually used, as getStartBody is overridden above
423 function getFieldNames() {
424 return array(
425 'am_title' => wfMsg( 'allmessagesname' ),
426 'am_default' => wfMsg( 'allmessagesdefault' )
427 );
428 }
429
430 function getTitle() {
431 return SpecialPage::getTitleFor( 'Allmessages', false );
432 }
433
434 function isFieldSortable( $x ){
435 return false;
436 }
437
438 function getDefaultSort(){
439 return '';
440 }
441
442 function getQueryInfo(){
443 return '';
444 }
445 }
446