Bug 32673: Keep the username in the input field if not existing
[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, $displayPrefix;
92
93 public $mLimitsShown;
94
95 /**
96 * @var Language
97 */
98 public $lang;
99
100 /**
101 * @var null|bool
102 */
103 public $custom;
104
105 function __construct( $page, $conds, $langObj = null ) {
106 parent::__construct( $page->getContext() );
107 $this->mIndexField = 'am_title';
108 $this->mPage = $page;
109 $this->mConds = $conds;
110 $this->mDefaultDirection = true; // always sort ascending
111 $this->mLimitsShown = array( 20, 50, 100, 250, 500, 5000 );
112
113 global $wgContLang;
114
115 $this->talk = $this->msg( 'talkpagelinktext' )->escaped();
116
117 $this->lang = ( $langObj ? $langObj : $wgContLang );
118 $this->langcode = $this->lang->getCode();
119 $this->foreign = $this->langcode != $wgContLang->getCode();
120
121 $request = $this->getRequest();
122
123 if( $request->getVal( 'filter', 'all' ) === 'all' ){
124 $this->custom = null; // So won't match in either case
125 } else {
126 $this->custom = ($request->getVal( 'filter' ) == 'unmodified');
127 }
128
129 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
130 $prefix = $prefix != '' ? Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) : null;
131 if( $prefix !== null ){
132 $this->displayPrefix = $prefix->getDBkey();
133 $this->prefix = '/^' . preg_quote( $this->displayPrefix ) . '/i';
134 } else {
135 $this->displayPrefix = false;
136 $this->prefix = false;
137 }
138
139 // The suffix that may be needed for message names if we're in a
140 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
141 if( $this->foreign ) {
142 $this->suffix = '/' . $this->langcode;
143 } else {
144 $this->suffix = '';
145 }
146 }
147
148 function buildForm() {
149 global $wgScript;
150
151 $languages = Language::getLanguageNames( false );
152 ksort( $languages );
153
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' )
176 ) .
177 Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
178 'filter',
179 'all',
180 'mw-allmessages-form-filter-all',
181 ( $this->filter == 'all' )
182 ) .
183 Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
184 'filter',
185 'modified',
186 'mw-allmessages-form-filter-modified',
187 ( $this->filter == 'modified' )
188 ) .
189 "</td>\n
190 </tr>
191 <tr>\n
192 <td class=\"mw-label\">" .
193 Xml::label( $this->msg( 'allmessages-language' )->text(), 'mw-allmessages-form-lang' ) .
194 "</td>\n
195 <td class=\"mw-input\">" .
196 Xml::openElement( 'select', array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ) );
197
198 foreach( $languages as $lang => $name ) {
199 $selected = $lang == $this->langcode;
200 $out .= Xml::option( $lang . ' - ' . $name, $lang, $selected ) . "\n";
201 }
202 $out .= Xml::closeElement( 'select' ) .
203 "</td>\n
204 </tr>" .
205
206 '<tr>
207 <td class="mw-label">' .
208 Xml::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
209 '</td>
210 <td class="mw-input">' .
211 $this->getLimitSelect() .
212 '</td>
213 <tr>
214 <td></td>
215 <td>' .
216 Xml::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
217 "</td>\n
218 </tr>" .
219
220 Xml::closeElement( 'table' ) .
221 $this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit' ) ) .
222 Xml::closeElement( 'fieldset' ) .
223 Xml::closeElement( 'form' );
224 return $out;
225 }
226
227 function getAllMessages( $descending ) {
228 wfProfileIn( __METHOD__ );
229 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
230 if( $descending ){
231 rsort( $messageNames );
232 } else {
233 asort( $messageNames );
234 }
235
236 // Normalise message names so they look like page titles
237 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
238
239 wfProfileOut( __METHOD__ );
240 return $messageNames;
241 }
242
243 /**
244 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
245 * Returns array( 'pages' => ..., 'talks' => ... ), where the subarrays have
246 * an entry for each existing page, with the key being the message name and
247 * value arbitrary.
248 *
249 * @param array $messageNames
250 * @param string $langcode What language code
251 * @param bool $foreign Whether the $langcode is not the content language
252 * @return array: a 'pages' and 'talks' array with the keys of existing pages
253 */
254 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
255 // FIXME: This function should be moved to Language:: or something.
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 $exists = false;
271 if( $foreign ) {
272 $title = explode( '/', $s->page_title );
273 if( count( $title ) === 2 && $langcode == $title[1]
274 && isset( $xNames[$title[0]] ) ) {
275 $exists = $title[0];
276 }
277 } elseif( isset( $xNames[$s->page_title] ) ) {
278 $exists = $s->page_title;
279 }
280 if( $exists && $s->page_namespace == NS_MEDIAWIKI ) {
281 $pageFlags[$exists] = true;
282 } elseif( $exists && $s->page_namespace == NS_MEDIAWIKI_TALK ) {
283 $talkFlags[$exists] = true;
284 }
285 }
286
287 wfProfileOut( __METHOD__ . '-db' );
288
289 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
290 }
291
292 /**
293 * This function normally does a database query to get the results; we need
294 * to make a pretend result using a FakeResultWrapper.
295 */
296 function reallyDoQuery( $offset, $limit, $descending ) {
297 $result = new FakeResultWrapper( array() );
298
299 $messageNames = $this->getAllMessages( $descending );
300 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
301
302 $count = 0;
303 foreach( $messageNames as $key ) {
304 $customised = isset( $statuses['pages'][$key] );
305 if( $customised !== $this->custom &&
306 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
307 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
308 ) {
309 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
310 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
311 $result->result[] = array(
312 'am_title' => $key,
313 'am_actual' => $actual,
314 'am_default' => $default,
315 'am_customised' => $customised,
316 'am_talk_exists' => isset( $statuses['talks'][$key] )
317 );
318 $count++;
319 }
320 if( $count == $limit ) {
321 break;
322 }
323 }
324 return $result;
325 }
326
327 function getStartBody() {
328 return Xml::openElement( 'table', array( 'class' => 'mw-datatable TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
329 "<thead><tr>
330 <th rowspan=\"2\">" .
331 $this->msg( 'allmessagesname' )->escaped() . "
332 </th>
333 <th>" .
334 $this->msg( 'allmessagesdefault' )->escaped() .
335 "</th>
336 </tr>\n
337 <tr>
338 <th>" .
339 $this->msg( 'allmessagescurrent' )->escaped() .
340 "</th>
341 </tr></thead><tbody>\n";
342 }
343
344 function formatValue( $field, $value ){
345 switch( $field ){
346
347 case 'am_title' :
348
349 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
350 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
351
352 if( $this->mCurrentRow->am_customised ){
353 $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
354 } else {
355 $title = Linker::link(
356 $title,
357 $this->getLanguage()->lcfirst( $value ),
358 array(),
359 array(),
360 array( 'broken' )
361 );
362 }
363 if ( $this->mCurrentRow->am_talk_exists ) {
364 $talk = Linker::linkKnown( $talk , $this->talk );
365 } else {
366 $talk = Linker::link(
367 $talk,
368 $this->talk,
369 array(),
370 array(),
371 array( 'broken' )
372 );
373 }
374 return $title . ' (' . $talk . ')';
375
376 case 'am_default' :
377 case 'am_actual' :
378 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
379 }
380 return '';
381 }
382
383 function formatRow( $row ){
384 // Do all the normal stuff
385 $s = parent::formatRow( $row );
386
387 // But if there's a customised message, add that too.
388 if( $row->am_customised ){
389 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
390 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
391 if ( $formatted == '' ) {
392 $formatted = '&#160;';
393 }
394 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
395 . "</tr>\n";
396 }
397 return $s;
398 }
399
400 function getRowAttrs( $row, $isSecond = false ){
401 $arr = array();
402 if( $row->am_customised ){
403 $arr['class'] = 'allmessages-customised';
404 }
405 if( !$isSecond ){
406 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
407 }
408 return $arr;
409 }
410
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 );
418 }
419 }
420
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()
426 );
427 }
428
429 function getTitle() {
430 return SpecialPage::getTitleFor( 'Allmessages', false );
431 }
432
433 function isFieldSortable( $x ){
434 return false;
435 }
436
437 function getDefaultSort(){
438 return '';
439 }
440
441 function getQueryInfo(){
442 return '';
443 }
444 }
445