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