b9f92f83942e57bc081060dd7b82747daae1c3f5
[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 $languages = Language::getLanguageNames( false );
150 ksort( $languages );
151
152 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-allmessages-form' ) ) .
153 Xml::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
154 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
155 Xml::openElement( 'table', array( 'class' => 'mw-allmessages-table' ) ) . "\n" .
156 '<tr>
157 <td class="mw-label">' .
158 Xml::label( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
159 "</td>\n
160 <td class=\"mw-input\">" .
161 Xml::input( 'prefix', 20, str_replace( '_', ' ', $this->displayPrefix ), array( 'id' => 'mw-allmessages-form-prefix' ) ) .
162 "</td>\n
163 </tr>
164 <tr>\n
165 <td class='mw-label'>" .
166 $this->msg( 'allmessages-filter' )->escaped() .
167 "</td>\n
168 <td class='mw-input'>" .
169 Xml::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
170 'filter',
171 'unmodified',
172 'mw-allmessages-form-filter-unmodified',
173 ( $this->filter == 'unmodified' )
174 ) .
175 Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
176 'filter',
177 'all',
178 'mw-allmessages-form-filter-all',
179 ( $this->filter == 'all' )
180 ) .
181 Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
182 'filter',
183 'modified',
184 'mw-allmessages-form-filter-modified',
185 ( $this->filter == 'modified' )
186 ) .
187 "</td>\n
188 </tr>
189 <tr>\n
190 <td class=\"mw-label\">" .
191 Xml::label( $this->msg( 'allmessages-language' )->text(), 'mw-allmessages-form-lang' ) .
192 "</td>\n
193 <td class=\"mw-input\">" .
194 Xml::openElement( 'select', array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ) );
195
196 foreach( $languages as $lang => $name ) {
197 $selected = $lang == $this->langcode;
198 $out .= Xml::option( $lang . ' - ' . $name, $lang, $selected ) . "\n";
199 }
200 $out .= Xml::closeElement( 'select' ) .
201 "</td>\n
202 </tr>" .
203
204 '<tr>
205 <td class="mw-label">' .
206 Xml::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
207 '</td>
208 <td class="mw-input">' .
209 $this->getLimitSelect() .
210 '</td>
211 <tr>
212 <td></td>
213 <td>' .
214 Xml::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
215 "</td>\n
216 </tr>" .
217
218 Xml::closeElement( 'table' ) .
219 $this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit' ) ) .
220 Xml::closeElement( 'fieldset' ) .
221 Xml::closeElement( 'form' );
222 return $out;
223 }
224
225 function getAllMessages( $descending ) {
226 wfProfileIn( __METHOD__ );
227 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
228 if( $descending ){
229 rsort( $messageNames );
230 } else {
231 asort( $messageNames );
232 }
233
234 // Normalise message names so they look like page titles
235 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
236
237 wfProfileOut( __METHOD__ );
238 return $messageNames;
239 }
240
241 /**
242 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
243 * Returns array( 'pages' => ..., 'talks' => ... ), where the subarrays have
244 * an entry for each existing page, with the key being the message name and
245 * value arbitrary.
246 *
247 * @param array $messageNames
248 * @param string $langcode What language code
249 * @param bool $foreign Whether the $langcode is not the content language
250 * @return array: a 'pages' and 'talks' array with the keys of existing pages
251 */
252 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
253 // FIXME: This function should be moved to Language:: or something.
254 wfProfileIn( __METHOD__ . '-db' );
255
256 $dbr = wfGetDB( DB_SLAVE );
257 $res = $dbr->select( 'page',
258 array( 'page_namespace', 'page_title' ),
259 array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
260 __METHOD__,
261 array( 'USE INDEX' => 'name_title' )
262 );
263 $xNames = array_flip( $messageNames );
264
265 $pageFlags = $talkFlags = array();
266
267 foreach ( $res as $s ) {
268 $exists = false;
269 if( $foreign ) {
270 $title = explode( '/', $s->page_title );
271 if( count( $title ) === 2 && $langcode == $title[1]
272 && isset( $xNames[$title[0]] ) ) {
273 $exists = $title[0];
274 }
275 } elseif( isset( $xNames[$s->page_title] ) ) {
276 $exists = $s->page_title;
277 }
278 if( $exists && $s->page_namespace == NS_MEDIAWIKI ) {
279 $pageFlags[$exists] = true;
280 } elseif( $exists && $s->page_namespace == NS_MEDIAWIKI_TALK ) {
281 $talkFlags[$exists] = true;
282 }
283 }
284
285 wfProfileOut( __METHOD__ . '-db' );
286
287 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
288 }
289
290 /**
291 * This function normally does a database query to get the results; we need
292 * to make a pretend result using a FakeResultWrapper.
293 * @return \FakeResultWrapper
294 */
295 function reallyDoQuery( $offset, $limit, $descending ) {
296 $result = new FakeResultWrapper( array() );
297
298 $messageNames = $this->getAllMessages( $descending );
299 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
300
301 $count = 0;
302 foreach( $messageNames as $key ) {
303 $customised = isset( $statuses['pages'][$key] );
304 if( $customised !== $this->custom &&
305 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
306 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
307 ) {
308 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
309 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
310 $result->result[] = array(
311 'am_title' => $key,
312 'am_actual' => $actual,
313 'am_default' => $default,
314 'am_customised' => $customised,
315 'am_talk_exists' => isset( $statuses['talks'][$key] )
316 );
317 $count++;
318 }
319 if( $count == $limit ) {
320 break;
321 }
322 }
323 return $result;
324 }
325
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";
341 }
342
343 function formatValue( $field, $value ){
344 switch( $field ){
345
346 case 'am_title' :
347
348 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
349 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
350
351 if( $this->mCurrentRow->am_customised ){
352 $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
353 } else {
354 $title = Linker::link(
355 $title,
356 $this->getLanguage()->lcfirst( $value ),
357 array(),
358 array(),
359 array( 'broken' )
360 );
361 }
362 if ( $this->mCurrentRow->am_talk_exists ) {
363 $talk = Linker::linkKnown( $talk , $this->talk );
364 } else {
365 $talk = Linker::link(
366 $talk,
367 $this->talk,
368 array(),
369 array(),
370 array( 'broken' )
371 );
372 }
373 return $title . ' (' . $talk . ')';
374
375 case 'am_default' :
376 case 'am_actual' :
377 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
378 }
379 return '';
380 }
381
382 function formatRow( $row ){
383 // Do all the normal stuff
384 $s = parent::formatRow( $row );
385
386 // But if there's a customised message, add that too.
387 if( $row->am_customised ){
388 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
389 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
390 if ( $formatted == '' ) {
391 $formatted = '&#160;';
392 }
393 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
394 . "</tr>\n";
395 }
396 return $s;
397 }
398
399 function getRowAttrs( $row, $isSecond = false ){
400 $arr = array();
401 if( $row->am_customised ){
402 $arr['class'] = 'allmessages-customised';
403 }
404 if( !$isSecond ){
405 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
406 }
407 return $arr;
408 }
409
410 function getCellAttrs( $field, $value ){
411 if( $this->mCurrentRow->am_customised && $field == 'am_title' ){
412 return array( 'rowspan' => '2', 'class' => $field );
413 } elseif( $field == 'am_title' ) {
414 return array( 'class' => $field );
415 } else {
416 return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir(), 'class' => $field );
417 }
418 }
419
420 // This is not actually used, as getStartBody is overridden above
421 function getFieldNames() {
422 return array(
423 'am_title' => $this->msg( 'allmessagesname' )->text(),
424 'am_default' => $this->msg( 'allmessagesdefault' )->text()
425 );
426 }
427
428 function getTitle() {
429 return SpecialPage::getTitleFor( 'Allmessages', false );
430 }
431
432 function isFieldSortable( $x ){
433 return false;
434 }
435
436 function getDefaultSort(){
437 return '';
438 }
439
440 function getQueryInfo(){
441 return '';
442 }
443 }
444