Merge "Spellchecked /includes directory"
[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 protected function getGroupName() {
81 return 'wiki';
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 $this->filter = $request->getVal( 'filter', 'all' );
124 if( $this->filter === 'all' ) {
125 $this->custom = null; // So won't match in either case
126 } else {
127 $this->custom = ($this->filter == 'unmodified');
128 }
129
130 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
131 $prefix = $prefix != '' ? Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) : null;
132 if( $prefix !== null ) {
133 $this->displayPrefix = $prefix->getDBkey();
134 $this->prefix = '/^' . preg_quote( $this->displayPrefix ) . '/i';
135 } else {
136 $this->displayPrefix = false;
137 $this->prefix = false;
138 }
139
140 // The suffix that may be needed for message names if we're in a
141 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
142 if( $this->foreign ) {
143 $this->suffix = '/' . $this->langcode;
144 } else {
145 $this->suffix = '';
146 }
147 }
148
149 function buildForm() {
150 global $wgScript;
151
152 $attrs = array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' );
153 $msg = wfMessage( 'allmessages-language' );
154 $langSelect = Xml::languageSelector( $this->langcode, false, null, $attrs, $msg );
155
156 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-allmessages-form' ) ) .
157 Xml::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
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( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
163 "</td>\n
164 <td class=\"mw-input\">" .
165 Xml::input( 'prefix', 20, str_replace( '_', ' ', $this->displayPrefix ), array( 'id' => 'mw-allmessages-form-prefix' ) ) .
166 "</td>\n
167 </tr>
168 <tr>\n
169 <td class='mw-label'>" .
170 $this->msg( 'allmessages-filter' )->escaped() .
171 "</td>\n
172 <td class='mw-input'>" .
173 Xml::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
174 'filter',
175 'unmodified',
176 'mw-allmessages-form-filter-unmodified',
177 ( $this->filter == 'unmodified' )
178 ) .
179 Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
180 'filter',
181 'all',
182 'mw-allmessages-form-filter-all',
183 ( $this->filter == 'all' )
184 ) .
185 Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
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\">" . $langSelect[0] . "</td>\n
195 <td class=\"mw-input\">" . $langSelect[1] . "</td>\n
196 </tr>" .
197
198 '<tr>
199 <td class="mw-label">' .
200 Xml::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
201 '</td>
202 <td class="mw-input">' .
203 $this->getLimitSelect() .
204 '</td>
205 <tr>
206 <td></td>
207 <td>' .
208 Xml::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
209 "</td>\n
210 </tr>" .
211
212 Xml::closeElement( 'table' ) .
213 $this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit' ) ) .
214 Xml::closeElement( 'fieldset' ) .
215 Xml::closeElement( 'form' );
216 return $out;
217 }
218
219 function getAllMessages( $descending ) {
220 wfProfileIn( __METHOD__ );
221 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
222 if( $descending ) {
223 rsort( $messageNames );
224 } else {
225 asort( $messageNames );
226 }
227
228 // Normalise message names so they look like page titles
229 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
230
231 wfProfileOut( __METHOD__ );
232 return $messageNames;
233 }
234
235 /**
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.
240 *
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
245 */
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' );
249
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' )
256 );
257 $xNames = array_flip( $messageNames );
258
259 $pageFlags = $talkFlags = array();
260
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]] ) ) {
267 $exists = $title[0];
268 }
269 } elseif( isset( $xNames[$s->page_title] ) ) {
270 $exists = $s->page_title;
271 }
272 if( $exists && $s->page_namespace == NS_MEDIAWIKI ) {
273 $pageFlags[$exists] = true;
274 } elseif( $exists && $s->page_namespace == NS_MEDIAWIKI_TALK ) {
275 $talkFlags[$exists] = true;
276 }
277 }
278
279 wfProfileOut( __METHOD__ . '-db' );
280
281 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
282 }
283
284 /**
285 * This function normally does a database query to get the results; we need
286 * to make a pretend result using a FakeResultWrapper.
287 * @return FakeResultWrapper
288 */
289 function reallyDoQuery( $offset, $limit, $descending ) {
290 $result = new FakeResultWrapper( array() );
291
292 $messageNames = $this->getAllMessages( $descending );
293 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
294
295 $count = 0;
296 foreach( $messageNames as $key ) {
297 $customised = isset( $statuses['pages'][$key] );
298 if( $customised !== $this->custom &&
299 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
300 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
301 ) {
302 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
303 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
304 $result->result[] = array(
305 'am_title' => $key,
306 'am_actual' => $actual,
307 'am_default' => $default,
308 'am_customised' => $customised,
309 'am_talk_exists' => isset( $statuses['talks'][$key] )
310 );
311 $count++;
312 }
313 if( $count == $limit ) {
314 break;
315 }
316 }
317 return $result;
318 }
319
320 function getStartBody() {
321 return Xml::openElement( 'table', array( 'class' => 'mw-datatable TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
322 "<thead><tr>
323 <th rowspan=\"2\">" .
324 $this->msg( 'allmessagesname' )->escaped() . "
325 </th>
326 <th>" .
327 $this->msg( 'allmessagesdefault' )->escaped() .
328 "</th>
329 </tr>\n
330 <tr>
331 <th>" .
332 $this->msg( 'allmessagescurrent' )->escaped() .
333 "</th>
334 </tr></thead><tbody>\n";
335 }
336
337 function formatValue( $field, $value ) {
338 switch( $field ) {
339 case 'am_title' :
340 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
341 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
342
343 if( $this->mCurrentRow->am_customised ) {
344 $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
345 } else {
346 $title = Linker::link(
347 $title,
348 $this->getLanguage()->lcfirst( $value ),
349 array(),
350 array(),
351 array( 'broken' )
352 );
353 }
354 if ( $this->mCurrentRow->am_talk_exists ) {
355 $talk = Linker::linkKnown( $talk, $this->talk );
356 } else {
357 $talk = Linker::link(
358 $talk,
359 $this->talk,
360 array(),
361 array(),
362 array( 'broken' )
363 );
364 }
365 return $title . ' ' . $this->msg( 'parentheses' )->rawParams( $talk )->escaped();
366
367 case 'am_default' :
368 case 'am_actual' :
369 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
370 }
371 return '';
372 }
373
374 function formatRow( $row ) {
375 // Do all the normal stuff
376 $s = parent::formatRow( $row );
377
378 // But if there's a customised message, add that too.
379 if( $row->am_customised ) {
380 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
381 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
382 if ( $formatted == '' ) {
383 $formatted = '&#160;';
384 }
385 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
386 . "</tr>\n";
387 }
388 return $s;
389 }
390
391 function getRowAttrs( $row, $isSecond = false ) {
392 $arr = array();
393 if( $row->am_customised ) {
394 $arr['class'] = 'allmessages-customised';
395 }
396 if( !$isSecond ) {
397 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
398 }
399 return $arr;
400 }
401
402 function getCellAttrs( $field, $value ) {
403 if( $this->mCurrentRow->am_customised && $field == 'am_title' ) {
404 return array( 'rowspan' => '2', 'class' => $field );
405 } elseif( $field == 'am_title' ) {
406 return array( 'class' => $field );
407 } else {
408 return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir(), 'class' => $field );
409 }
410 }
411
412 // This is not actually used, as getStartBody is overridden above
413 function getFieldNames() {
414 return array(
415 'am_title' => $this->msg( 'allmessagesname' )->text(),
416 'am_default' => $this->msg( 'allmessagesdefault' )->text()
417 );
418 }
419
420 function getTitle() {
421 return SpecialPage::getTitleFor( 'Allmessages', false );
422 }
423
424 function isFieldSortable( $x ) {
425 return false;
426 }
427
428 function getDefaultSort() {
429 return '';
430 }
431
432 function getQueryInfo() {
433 return '';
434 }
435 }