Whitelist the <wbr> element.
[mediawiki.git] / includes / specials / SpecialAllmessages.php
blob35d6a0c08c91d79fdb8f8bde221b893cdcd202ef
1 <?php
2 /**
3 * Implements Special:Allmessages
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Use this special page to get a list of the MediaWiki system messages.
27 * @file
28 * @ingroup SpecialPage
30 class SpecialAllmessages extends SpecialPage {
31 /**
32 * @var AllmessagesTablePager
34 protected $table;
36 /**
37 * Constructor
39 public function __construct() {
40 parent::__construct( 'Allmessages' );
43 /**
44 * Show the special page
46 * @param $par Mixed: parameter passed to the page or null
48 public function execute( $par ) {
49 $request = $this->getRequest();
50 $out = $this->getOutput();
52 $this->setHeaders();
54 global $wgUseDatabaseMessages;
55 if ( !$wgUseDatabaseMessages ) {
56 $out->addWikiMsg( 'allmessagesnotsupportedDB' );
58 return;
59 } else {
60 $this->outputHeader( 'allmessagestext' );
63 $out->addModuleStyles( 'mediawiki.special' );
65 $this->table = new AllmessagesTablePager(
66 $this,
67 array(),
68 wfGetLangObj( $request->getVal( 'lang', $par ) )
71 $this->langcode = $this->table->lang->getCode();
73 $out->addHTML( $this->table->buildForm() .
74 $this->table->getNavigationBar() .
75 $this->table->getBody() .
76 $this->table->getNavigationBar() );
79 protected function getGroupName() {
80 return 'wiki';
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.
88 class AllmessagesTablePager extends TablePager {
89 protected $filter, $prefix, $langcode, $displayPrefix;
91 public $mLimitsShown;
93 /**
94 * @var Language
96 public $lang;
98 /**
99 * @var null|bool
101 public $custom;
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 );
111 global $wgContLang;
113 $this->talk = $this->msg( 'talkpagelinktext' )->escaped();
115 $this->lang = ( $langObj ? $langObj : $wgContLang );
116 $this->langcode = $this->lang->getCode();
117 $this->foreign = $this->langcode != $wgContLang->getCode();
119 $request = $this->getRequest();
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' );
128 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
129 $prefix = $prefix != '' ? Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) : null;
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;
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 = '';
147 function buildForm() {
148 global $wgScript;
150 $attrs = array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' );
151 $msg = wfMessage( 'allmessages-language' );
152 $langSelect = Xml::languageSelector( $this->langcode, false, null, $attrs, $msg );
154 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-allmessages-form' ) ) .
155 Xml::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
156 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
157 Xml::openElement( 'table', array( 'class' => 'mw-allmessages-table' ) ) . "\n" .
158 '<tr>
159 <td class="mw-label">' .
160 Xml::label( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
161 "</td>\n
162 <td class=\"mw-input\">" .
163 Xml::input( 'prefix', 20, str_replace( '_', ' ', $this->displayPrefix ), array( 'id' => 'mw-allmessages-form-prefix' ) ) .
164 "</td>\n
165 </tr>
166 <tr>\n
167 <td class='mw-label'>" .
168 $this->msg( 'allmessages-filter' )->escaped() .
169 "</td>\n
170 <td class='mw-input'>" .
171 Xml::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
172 'filter',
173 'unmodified',
174 'mw-allmessages-form-filter-unmodified',
175 ( $this->filter == 'unmodified' )
177 Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
178 'filter',
179 'all',
180 'mw-allmessages-form-filter-all',
181 ( $this->filter == 'all' )
183 Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
184 'filter',
185 'modified',
186 'mw-allmessages-form-filter-modified',
187 ( $this->filter == 'modified' )
189 "</td>\n
190 </tr>
191 <tr>\n
192 <td class=\"mw-label\">" . $langSelect[0] . "</td>\n
193 <td class=\"mw-input\">" . $langSelect[1] . "</td>\n
194 </tr>" .
196 '<tr>
197 <td class="mw-label">' .
198 Xml::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
199 '</td>
200 <td class="mw-input">' .
201 $this->getLimitSelect() .
202 '</td>
203 <tr>
204 <td></td>
205 <td>' .
206 Xml::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
207 "</td>\n
208 </tr>" .
210 Xml::closeElement( 'table' ) .
211 $this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit' ) ) .
212 Xml::closeElement( 'fieldset' ) .
213 Xml::closeElement( 'form' );
215 return $out;
218 function getAllMessages( $descending ) {
219 wfProfileIn( __METHOD__ );
220 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
221 if ( $descending ) {
222 rsort( $messageNames );
223 } else {
224 asort( $messageNames );
227 // Normalise message names so they look like page titles
228 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
230 wfProfileOut( __METHOD__ );
232 return $messageNames;
236 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
237 * Returns array( 'pages' => ..., 'talks' => ... ), where the subarrays have
238 * an entry for each existing page, with the key being the message name and
239 * value arbitrary.
241 * @param array $messageNames
242 * @param string $langcode What language code
243 * @param bool $foreign Whether the $langcode is not the content language
244 * @return array: a 'pages' and 'talks' array with the keys of existing pages
246 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
247 // FIXME: This function should be moved to Language:: or something.
248 wfProfileIn( __METHOD__ . '-db' );
250 $dbr = wfGetDB( DB_SLAVE );
251 $res = $dbr->select( 'page',
252 array( 'page_namespace', 'page_title' ),
253 array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
254 __METHOD__,
255 array( 'USE INDEX' => 'name_title' )
257 $xNames = array_flip( $messageNames );
259 $pageFlags = $talkFlags = array();
261 foreach ( $res as $s ) {
262 $exists = false;
263 if ( $foreign ) {
264 $title = explode( '/', $s->page_title );
265 if ( count( $title ) === 2 && $langcode == $title[1]
266 && isset( $xNames[$title[0]] )
268 $exists = $title[0];
270 } elseif ( isset( $xNames[$s->page_title] ) ) {
271 $exists = $s->page_title;
273 if ( $exists && $s->page_namespace == NS_MEDIAWIKI ) {
274 $pageFlags[$exists] = true;
275 } elseif ( $exists && $s->page_namespace == NS_MEDIAWIKI_TALK ) {
276 $talkFlags[$exists] = true;
280 wfProfileOut( __METHOD__ . '-db' );
282 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
286 * This function normally does a database query to get the results; we need
287 * to make a pretend result using a FakeResultWrapper.
288 * @param string $offset
289 * @param int $limit
290 * @param bool $descending
291 * @return FakeResultWrapper
293 function reallyDoQuery( $offset, $limit, $descending ) {
294 $result = new FakeResultWrapper( array() );
296 $messageNames = $this->getAllMessages( $descending );
297 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
299 $count = 0;
300 foreach ( $messageNames as $key ) {
301 $customised = isset( $statuses['pages'][$key] );
302 if ( $customised !== $this->custom &&
303 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
304 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
306 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
307 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
308 $result->result[] = array(
309 'am_title' => $key,
310 'am_actual' => $actual,
311 'am_default' => $default,
312 'am_customised' => $customised,
313 'am_talk_exists' => isset( $statuses['talks'][$key] )
315 $count++;
318 if ( $count == $limit ) {
319 break;
323 return $result;
326 function getStartBody() {
327 return Xml::openElement( 'table', array( 'class' => 'mw-datatable TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
328 "<thead><tr>
329 <th rowspan=\"2\">" .
330 $this->msg( 'allmessagesname' )->escaped() . "
331 </th>
332 <th>" .
333 $this->msg( 'allmessagesdefault' )->escaped() .
334 "</th>
335 </tr>\n
336 <tr>
337 <th>" .
338 $this->msg( 'allmessagescurrent' )->escaped() .
339 "</th>
340 </tr></thead><tbody>\n";
343 function formatValue( $field, $value ) {
344 switch ( $field ) {
345 case 'am_title' :
346 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
347 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
349 if ( $this->mCurrentRow->am_customised ) {
350 $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
351 } else {
352 $title = Linker::link(
353 $title,
354 $this->getLanguage()->lcfirst( $value ),
355 array(),
356 array(),
357 array( 'broken' )
360 if ( $this->mCurrentRow->am_talk_exists ) {
361 $talk = Linker::linkKnown( $talk, $this->talk );
362 } else {
363 $talk = Linker::link(
364 $talk,
365 $this->talk,
366 array(),
367 array(),
368 array( 'broken' )
372 return $title . ' ' . $this->msg( 'parentheses' )->rawParams( $talk )->escaped();
374 case 'am_default' :
375 case 'am_actual' :
376 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
378 return '';
381 function formatRow( $row ) {
382 // Do all the normal stuff
383 $s = parent::formatRow( $row );
385 // But if there's a customised message, add that too.
386 if ( $row->am_customised ) {
387 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
388 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
389 if ( $formatted == '' ) {
390 $formatted = '&#160;';
392 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
393 . "</tr>\n";
396 return $s;
399 function getRowAttrs( $row, $isSecond = false ) {
400 $arr = array();
401 if ( $row->am_customised ) {
402 $arr['class'] = 'allmessages-customised';
404 if ( !$isSecond ) {
405 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
408 return $arr;
411 function getCellAttrs( $field, $value ) {
412 if ( $this->mCurrentRow->am_customised && $field == 'am_title' ) {
413 return array( 'rowspan' => '2', 'class' => $field );
414 } elseif ( $field == 'am_title' ) {
415 return array( 'class' => $field );
416 } else {
417 return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir(), 'class' => $field );
421 // This is not actually used, as getStartBody is overridden above
422 function getFieldNames() {
423 return array(
424 'am_title' => $this->msg( 'allmessagesname' )->text(),
425 'am_default' => $this->msg( 'allmessagesdefault' )->text()
429 function getTitle() {
430 return SpecialPage::getTitleFor( 'Allmessages', false );
433 function isFieldSortable( $x ) {
434 return false;
437 function getDefaultSort() {
438 return '';
441 function getQueryInfo() {
442 return '';