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