7f315ded8cc8ba86910ed99b8eea393d1d8659b0
[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 ) {
268 $exists = $title[0];
269 }
270 } elseif ( isset( $xNames[$s->page_title] ) ) {
271 $exists = $s->page_title;
272 }
273 if ( $exists && $s->page_namespace == NS_MEDIAWIKI ) {
274 $pageFlags[$exists] = true;
275 } elseif ( $exists && $s->page_namespace == NS_MEDIAWIKI_TALK ) {
276 $talkFlags[$exists] = true;
277 }
278 }
279
280 wfProfileOut( __METHOD__ . '-db' );
281
282 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
283 }
284
285 /**
286 * This function normally does a database query to get the results; we need
287 * to make a pretend result using a FakeResultWrapper.
288 * @param string $offset
289 * @param int $limit
290 * @param bool $descending
291 * @return FakeResultWrapper
292 */
293 function reallyDoQuery( $offset, $limit, $descending ) {
294 $result = new FakeResultWrapper( array() );
295
296 $messageNames = $this->getAllMessages( $descending );
297 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
298
299 $count = 0;
300 foreach ( $messageNames as $key ) {
301 $customised = isset( $statuses['pages'][$key] );
302 if ( $customised !== $this->custom &&
303 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
304 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
305 ) {
306 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
307 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
308 $result->result[] = array(
309 'am_title' => $key,
310 'am_actual' => $actual,
311 'am_default' => $default,
312 'am_customised' => $customised,
313 'am_talk_exists' => isset( $statuses['talks'][$key] )
314 );
315 $count++;
316 }
317
318 if ( $count == $limit ) {
319 break;
320 }
321 }
322 return $result;
323 }
324
325 function getStartBody() {
326 return Xml::openElement( 'table', array( 'class' => 'mw-datatable TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
327 "<thead><tr>
328 <th rowspan=\"2\">" .
329 $this->msg( 'allmessagesname' )->escaped() . "
330 </th>
331 <th>" .
332 $this->msg( 'allmessagesdefault' )->escaped() .
333 "</th>
334 </tr>\n
335 <tr>
336 <th>" .
337 $this->msg( 'allmessagescurrent' )->escaped() .
338 "</th>
339 </tr></thead><tbody>\n";
340 }
341
342 function formatValue( $field, $value ) {
343 switch ( $field ) {
344 case 'am_title' :
345 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
346 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
347
348 if ( $this->mCurrentRow->am_customised ) {
349 $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
350 } else {
351 $title = Linker::link(
352 $title,
353 $this->getLanguage()->lcfirst( $value ),
354 array(),
355 array(),
356 array( 'broken' )
357 );
358 }
359 if ( $this->mCurrentRow->am_talk_exists ) {
360 $talk = Linker::linkKnown( $talk, $this->talk );
361 } else {
362 $talk = Linker::link(
363 $talk,
364 $this->talk,
365 array(),
366 array(),
367 array( 'broken' )
368 );
369 }
370 return $title . ' ' . $this->msg( 'parentheses' )->rawParams( $talk )->escaped();
371
372 case 'am_default' :
373 case 'am_actual' :
374 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
375 }
376 return '';
377 }
378
379 function formatRow( $row ) {
380 // Do all the normal stuff
381 $s = parent::formatRow( $row );
382
383 // But if there's a customised message, add that too.
384 if ( $row->am_customised ) {
385 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
386 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
387 if ( $formatted == '' ) {
388 $formatted = '&#160;';
389 }
390 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
391 . "</tr>\n";
392 }
393 return $s;
394 }
395
396 function getRowAttrs( $row, $isSecond = false ) {
397 $arr = array();
398 if ( $row->am_customised ) {
399 $arr['class'] = 'allmessages-customised';
400 }
401 if ( !$isSecond ) {
402 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
403 }
404 return $arr;
405 }
406
407 function getCellAttrs( $field, $value ) {
408 if ( $this->mCurrentRow->am_customised && $field == 'am_title' ) {
409 return array( 'rowspan' => '2', 'class' => $field );
410 } elseif ( $field == 'am_title' ) {
411 return array( 'class' => $field );
412 } else {
413 return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir(), 'class' => $field );
414 }
415 }
416
417 // This is not actually used, as getStartBody is overridden above
418 function getFieldNames() {
419 return array(
420 'am_title' => $this->msg( 'allmessagesname' )->text(),
421 'am_default' => $this->msg( 'allmessagesdefault' )->text()
422 );
423 }
424
425 function getTitle() {
426 return SpecialPage::getTitleFor( 'Allmessages', false );
427 }
428
429 function isFieldSortable( $x ) {
430 return false;
431 }
432
433 function getDefaultSort() {
434 return '';
435 }
436
437 function getQueryInfo() {
438 return '';
439 }
440 }