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