8f41c82819603b5927dd059b18464a943979ad75
[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();
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 $wgLang, $wgContLang, $wgRequest;
114
115 $this->talk = htmlspecialchars( wfMsg( 'talkpagelinktext' ) );
116
117 $this->lang = ( $langObj ? $langObj : $wgContLang );
118 $this->langcode = $this->lang->getCode();
119 $this->foreign = $this->langcode != $wgContLang->getCode();
120
121 if( $wgRequest->getVal( 'filter', 'all' ) === 'all' ){
122 $this->custom = null; // So won't match in either case
123 } else {
124 $this->custom = ($wgRequest->getVal( 'filter' ) == 'unmodified');
125 }
126
127 $prefix = $wgLang->ucfirst( $wgRequest->getVal( 'prefix', '' ) );
128 $prefix = $prefix != '' ? Title::makeTitleSafe( NS_MEDIAWIKI, $wgRequest->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( wfMsg( 'allmessages-filter-legend' ) ) .
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( wfMsg( 'allmessages-prefix' ), '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 wfMsg( 'allmessages-filter' ) .
167 "</td>\n
168 <td class='mw-input'>" .
169 Xml::radioLabel( wfMsg( 'allmessages-filter-unmodified' ),
170 'filter',
171 'unmodified',
172 'mw-allmessages-form-filter-unmodified',
173 ( $this->filter == 'unmodified' )
174 ) .
175 Xml::radioLabel( wfMsg( 'allmessages-filter-all' ),
176 'filter',
177 'all',
178 'mw-allmessages-form-filter-all',
179 ( $this->filter == 'all' )
180 ) .
181 Xml::radioLabel( wfMsg( 'allmessages-filter-modified' ),
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( wfMsg( 'allmessages-language' ), '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( wfMsg( 'table_pager_limit_label'), '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( wfMsg( 'allmessages-filter-submit' ) ) .
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 */
251 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
252 wfProfileIn( __METHOD__ . '-db' );
253
254 $dbr = wfGetDB( DB_SLAVE );
255 $res = $dbr->select( 'page',
256 array( 'page_namespace', 'page_title' ),
257 array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
258 __METHOD__,
259 array( 'USE INDEX' => 'name_title' )
260 );
261 $xNames = array_flip( $messageNames );
262
263 $pageFlags = $talkFlags = array();
264
265 foreach ( $res as $s ) {
266 if( $s->page_namespace == NS_MEDIAWIKI ) {
267 if( $foreign ) {
268 $title = explode( '/', $s->page_title );
269 if( count( $title ) === 2 && $langcode == $title[1]
270 && isset( $xNames[$title[0]] ) ) {
271 $pageFlags["{$title[0]}"] = true;
272 }
273 } elseif( isset( $xNames[$s->page_title] ) ) {
274 $pageFlags[$s->page_title] = true;
275 }
276 } elseif( $s->page_namespace == NS_MEDIAWIKI_TALK ){
277 $talkFlags[$s->page_title] = true;
278 }
279 }
280
281 wfProfileOut( __METHOD__ . '-db' );
282
283 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
284 }
285
286 /**
287 * This function normally does a database query to get the results; we need
288 * to make a pretend result using a FakeResultWrapper.
289 */
290 function reallyDoQuery( $offset, $limit, $descending ) {
291 $result = new FakeResultWrapper( array() );
292
293 $messageNames = $this->getAllMessages( $descending );
294 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
295
296 $count = 0;
297 foreach( $messageNames as $key ) {
298 $customised = isset( $statuses['pages'][$key] );
299 if( $customised !== $this->custom &&
300 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
301 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
302 ) {
303 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
304 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
305 $result->result[] = array(
306 'am_title' => $key,
307 'am_actual' => $actual,
308 'am_default' => $default,
309 'am_customised' => $customised,
310 'am_talk_exists' => isset( $statuses['talks'][$key] )
311 );
312 $count++;
313 }
314 if( $count == $limit ) {
315 break;
316 }
317 }
318 return $result;
319 }
320
321 function getStartBody() {
322 return Xml::openElement( 'table', array( 'class' => 'TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
323 "<thead><tr>
324 <th rowspan=\"2\">" .
325 wfMsg( 'allmessagesname' ) . "
326 </th>
327 <th>" .
328 wfMsg( 'allmessagesdefault' ) .
329 "</th>
330 </tr>\n
331 <tr>
332 <th>" .
333 wfMsg( 'allmessagescurrent' ) .
334 "</th>
335 </tr></thead><tbody>\n";
336 }
337
338 function formatValue( $field, $value ){
339 global $wgLang;
340 switch( $field ){
341
342 case 'am_title' :
343
344 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
345 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
346
347 if( $this->mCurrentRow->am_customised ){
348 $title = Linker::linkKnown( $title, $wgLang->lcfirst( $value ) );
349 } else {
350 $title = Linker::link(
351 $title,
352 $wgLang->lcfirst( $value ),
353 array(),
354 array(),
355 array( 'broken' )
356 );
357 }
358 if ( $this->mCurrentRow->am_talk_exists ) {
359 $talk = Linker::linkKnown( $talk , $this->talk );
360 } else {
361 $talk = Linker::link(
362 $talk,
363 $this->talk,
364 array(),
365 array(),
366 array( 'broken' )
367 );
368 }
369 return $title . ' (' . $talk . ')';
370
371 case 'am_default' :
372 case 'am_actual' :
373 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
374 }
375 return '';
376 }
377
378 function formatRow( $row ){
379 // Do all the normal stuff
380 $s = parent::formatRow( $row );
381
382 // But if there's a customised message, add that too.
383 if( $row->am_customised ){
384 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
385 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
386 if ( $formatted == '' ) {
387 $formatted = '&#160;';
388 }
389 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
390 . "</tr>\n";
391 }
392 return $s;
393 }
394
395 function getRowAttrs( $row, $isSecond = false ){
396 $arr = array();
397 global $wgLang;
398 if( $row->am_customised ){
399 $arr['class'] = 'allmessages-customised';
400 }
401 if( !$isSecond ){
402 $arr['id'] = Sanitizer::escapeId( 'msg_' . $wgLang->lcfirst( $row->am_title ) );
403 }
404 return $arr;
405 }
406
407 function getCellAttrs( $field, $value ){
408 if( $field != 'am_title' ) {
409 return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir() );
410 } elseif( $this->mCurrentRow->am_customised && $field == 'am_title' ) {
411 return array( 'rowspan' => '2', 'class' => $field );
412 } else {
413 return array( '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' => wfMsg( 'allmessagesname' ),
421 'am_default' => wfMsg( 'allmessagesdefault' )
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 }
441