Remove useless comments from search output
[mediawiki.git] / includes / specials / SpecialAllmessages.php
blob33c92b5e0a5bca6dbc2ff571bbb540be07366779
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 string $par 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 !== '' ?
130 Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) :
131 null;
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;
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 = '';
150 function buildForm() {
151 global $wgScript;
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 );
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',
173 str_replace( '_', ' ', $this->displayPrefix ),
174 array( 'id' => 'mw-allmessages-form-prefix' )
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' )
189 Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
190 'filter',
191 'all',
192 'mw-allmessages-form-filter-all',
193 ( $this->filter === 'all' )
195 Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
196 'filter',
197 'modified',
198 'mw-allmessages-form-filter-modified',
199 ( $this->filter === 'modified' )
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>" .
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>" .
222 Xml::closeElement( 'table' ) .
223 $this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit' ) ) .
224 Xml::closeElement( 'fieldset' ) .
225 Xml::closeElement( 'form' );
227 return $out;
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 );
239 // Normalise message names so they look like page titles
240 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
242 wfProfileOut( __METHOD__ );
244 return $messageNames;
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.
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
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' );
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' )
269 $xNames = array_flip( $messageNames );
271 $pageFlags = $talkFlags = array();
273 foreach ( $res as $s ) {
274 $exists = false;
275 if ( $foreign ) {
276 $title = explode( '/', $s->page_title );
277 if ( count( $title ) === 2 && $langcode === $title[1]
278 && isset( $xNames[$title[0]] )
280 $exists = $title[0];
282 } elseif ( isset( $xNames[$s->page_title] ) ) {
283 $exists = $s->page_title;
285 if ( $exists && $s->page_namespace === NS_MEDIAWIKI ) {
286 $pageFlags[$exists] = true;
287 } elseif ( $exists && $s->page_namespace === NS_MEDIAWIKI_TALK ) {
288 $talkFlags[$exists] = true;
292 wfProfileOut( __METHOD__ . '-db' );
294 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
298 * This function normally does a database query to get the results; we need
299 * to make a pretend result using a FakeResultWrapper.
300 * @param string $offset
301 * @param int $limit
302 * @param bool $descending
303 * @return FakeResultWrapper
305 function reallyDoQuery( $offset, $limit, $descending ) {
306 $result = new FakeResultWrapper( array() );
308 $messageNames = $this->getAllMessages( $descending );
309 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
311 $count = 0;
312 foreach ( $messageNames as $key ) {
313 $customised = isset( $statuses['pages'][$key] );
314 if ( $customised !== $this->custom &&
315 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
316 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
318 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
319 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
320 $result->result[] = array(
321 'am_title' => $key,
322 'am_actual' => $actual,
323 'am_default' => $default,
324 'am_customised' => $customised,
325 'am_talk_exists' => isset( $statuses['talks'][$key] )
327 $count++;
330 if ( $count === $limit ) {
331 break;
335 return $result;
338 function getStartBody() {
339 return Xml::openElement( 'table', array(
340 'class' => 'mw-datatable TablePager',
341 'id' => 'mw-allmessagestable'
342 ) ) .
343 "\n" .
344 "<thead><tr>
345 <th rowspan=\"2\">" .
346 $this->msg( 'allmessagesname' )->escaped() . "
347 </th>
348 <th>" .
349 $this->msg( 'allmessagesdefault' )->escaped() .
350 "</th>
351 </tr>\n
352 <tr>
353 <th>" .
354 $this->msg( 'allmessagescurrent' )->escaped() .
355 "</th>
356 </tr></thead><tbody>\n";
359 function formatValue( $field, $value ) {
360 switch ( $field ) {
361 case 'am_title' :
362 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
363 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
364 $translation = Linker::makeExternalLink(
365 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( array(
366 'title' => 'Special:SearchTranslations',
367 'group' => 'mediawiki',
368 'grouppath' => 'mediawiki',
369 'query' => 'language:' . $this->getLanguage()->getCode() . '^25 ' .
370 'messageid:"MediaWiki:' . $value . '"^10 "' .
371 $this->msg( $value )->inLanguage( 'en' )->plain() . '"'
372 ) ),
373 $this->msg( 'allmessages-filter-translate' )->text()
376 if ( $this->mCurrentRow->am_customised ) {
377 $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
378 } else {
379 $title = Linker::link(
380 $title,
381 $this->getLanguage()->lcfirst( $value ),
382 array(),
383 array(),
384 array( 'broken' )
387 if ( $this->mCurrentRow->am_talk_exists ) {
388 $talk = Linker::linkKnown( $talk, $this->talk );
389 } else {
390 $talk = Linker::link(
391 $talk,
392 $this->talk,
393 array(),
394 array(),
395 array( 'broken' )
399 return $title . ' '
400 . $this->msg( 'parentheses' )->rawParams( $talk )->escaped()
401 . ' '
402 . $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
404 case 'am_default' :
405 case 'am_actual' :
406 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
409 return '';
412 function formatRow( $row ) {
413 // Do all the normal stuff
414 $s = parent::formatRow( $row );
416 // But if there's a customised message, add that too.
417 if ( $row->am_customised ) {
418 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
419 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
421 if ( $formatted === '' ) {
422 $formatted = '&#160;';
425 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
426 . "</tr>\n";
429 return $s;
432 function getRowAttrs( $row, $isSecond = false ) {
433 $arr = array();
435 if ( $row->am_customised ) {
436 $arr['class'] = 'allmessages-customised';
439 if ( !$isSecond ) {
440 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
443 return $arr;
446 function getCellAttrs( $field, $value ) {
447 if ( $this->mCurrentRow->am_customised && $field === 'am_title' ) {
448 return array( 'rowspan' => '2', 'class' => $field );
449 } elseif ( $field === 'am_title' ) {
450 return array( 'class' => $field );
451 } else {
452 return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir(), 'class' => $field );
456 // This is not actually used, as getStartBody is overridden above
457 function getFieldNames() {
458 return array(
459 'am_title' => $this->msg( 'allmessagesname' )->text(),
460 'am_default' => $this->msg( 'allmessagesdefault' )->text()
464 function getTitle() {
465 return SpecialPage::getTitleFor( 'Allmessages', false );
468 function isFieldSortable( $x ) {
469 return false;
472 function getDefaultSort() {
473 return '';
476 function getQueryInfo() {
477 return '';