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