Follow-up r53286: No need to create a label without following input element.
[lhc/web/wiklou.git] / includes / specials / SpecialAllmessages.php
1 <?php
2 /**
3 * Use this special page to get a list of the MediaWiki system messages.
4 * @file
5 * @ingroup SpecialPage
6 */
7 class SpecialAllmessages extends SpecialPage {
8
9 /**
10 * Constructor
11 */
12 public function __construct() {
13 parent::__construct( 'Allmessages' );
14 }
15
16 /**
17 * Execute
18 */
19 function execute( $par ) {
20 global $wgOut, $wgRequest;
21
22 $this->setHeaders();
23
24 global $wgUseDatabaseMessages;
25 if( !$wgUseDatabaseMessages ) {
26 $wgOut->addWikiMsg( 'allmessagesnotsupportedDB' );
27 return;
28 } else {
29 $this->outputHeader( 'allmessagestext' );
30 }
31
32 $this->filter = $wgRequest->getVal( 'filter', 'all' );
33 $this->prefix = $wgRequest->getVal( 'prefix', '' );
34
35 $this->table = new AllmessagesTablePager( $this,
36 $conds = array(),
37 wfGetLangObj( $wgRequest->getVal( 'lang', $par ) ) );
38
39 $this->langCode = $this->table->lang->getCode();
40
41 $wgOut->addHTML( $this->buildForm() .
42 $this->table->getNavigationBar() .
43 $this->table->getLimitForm() .
44 $this->table->getBody() .
45 $this->table->getNavigationBar() );
46
47 }
48
49 function buildForm() {
50 global $wgScript;
51
52 $languages = Language::getLanguageNames( false );
53 ksort( $languages );
54
55 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-allmessages-form' ) ) .
56 Xml::fieldset( wfMsg( 'allmessages-filter-legend' ) ) .
57 Xml::hidden( 'title', $this->getTitle() ) .
58 Xml::openElement( 'table', array( 'class' => 'mw-allmessages-table' ) ) . "\n" .
59 '<tr>
60 <td class="mw-label">' .
61 Xml::label( wfMsg( 'allmessages-prefix' ), 'mw-allmessages-form-prefix' ) .
62 "</td>\n
63 <td class=\"mw-input\">" .
64 Xml::input( 'prefix', 20, str_replace( '_', ' ', $this->prefix ), array( 'id' => 'mw-allmessages-form-prefix' ) ) .
65 "</td>\n
66 </tr>
67 <tr>\n
68 <td class='mw-label'>" .
69 wfMsg( 'allmessages-filter' ) .
70 "</td>\n
71 <td class='mw-input'>" .
72 Xml::radioLabel( wfMsg( 'allmessages-filter-unmodified' ),
73 'filter',
74 'unmodified',
75 'mw-allmessages-form-filter-unmodified',
76 ( $this->filter == 'unmodified' ? true : false )
77 ) .
78 Xml::radioLabel( wfMsg( 'allmessages-filter-all' ),
79 'filter',
80 'all',
81 'mw-allmessages-form-filter-all',
82 ( $this->filter == 'all' ? true : false )
83 ) .
84 Xml::radioLabel( wfMsg( 'allmessages-filter-modified' ),
85 'filter',
86 'modified',
87 'mw-allmessages-form-filter-modified',
88 ( $this->filter == 'modified' ? true : false )
89 ) .
90 "</td>\n
91 </tr>
92 <tr>\n
93 <td class=\"mw-label\">" .
94 Xml::label( wfMsg( 'allmessages-language' ), 'mw-allmessages-form-lang' ) .
95 "</td>\n
96 <td class=\"mw-input\">" .
97 Xml::openElement( 'select', array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ) );
98
99 foreach( $languages as $lang => $name ) {
100 $selected = $lang == $this->langCode ? true : false;
101 $out .= Xml::option( $lang . ' - ' . $name, $lang, $selected ) . "\n";
102 }
103 $out .= Xml::closeElement( 'select' ) .
104 "</td>\n
105 </tr>
106 <tr>\n
107 <td></td>
108 <td>" .
109 Xml::submitButton( wfMsg( 'allmessages-filter-submit' ) ) .
110 "</td>\n
111 </tr>" .
112 Xml::closeElement( 'table' ) .
113 $this->table->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang' ) ) .
114 Xml::closeElement( 'fieldset' ) .
115 Xml::closeElement( 'form' );
116 return $out;
117 }
118 }
119
120 /* use TablePager for prettified output. We have to pretend that we're
121 * getting data from a table when in fact not all of it comes from the database.
122 */
123 class AllmessagesTablePager extends TablePager {
124
125 var $messages = NULL;
126 var $talkPages = NULL;
127
128 function __construct( $page, $conds, $langObj = null ) {
129 parent::__construct();
130 $this->mIndexField = 'am_title';
131 $this->mPage = $page;
132 $this->mConds = $conds;
133 $this->mDefaultDirection = true; //always sort ascending
134
135 global $wgLang, $wgContLang, $wgRequest;
136
137 $this->talk = $wgLang->lc( htmlspecialchars( wfMsg( 'talkpagelinktext' ) ) );
138
139 $this->lang = ( $langObj ? $langObj : $wgContLang );
140 $this->langcode = $this->lang->getCode();
141 $this->foreign = $this->langcode != $wgContLang->getCode();
142
143 if( $wgRequest->getVal( 'filter', 'all' ) === 'all' ){
144 $this->custom = NULL; //So won't match in either case
145 } else {
146 $this->custom = $wgRequest->getVal( 'filter' ) == 'unmodified' ? 1 : 0;
147 }
148
149 $prefix = $wgLang->ucfirst( $wgRequest->getVal( 'prefix', '' ) );
150 $prefix = $prefix != '' ? Title::makeTitleSafe( NS_MEDIAWIKI, $wgRequest->getVal( 'prefix', NULL ) ) : NULL;
151 if( $prefix !== NULL ){
152 $this->prefix = '/^' . preg_quote( $prefix->getDBkey() ) . '/i';
153 } else {
154 $this->prefix = false;
155 }
156 $this->getSkin();
157
158 //The suffix that may be needed for message names if we're in a
159 //different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
160 if( $this->foreign ) {
161 $this->suffix = '/' . $this->langcode;
162 } else {
163 $this->suffix = '';
164 }
165 }
166
167 function getAllMessages( $desc ){
168
169 wfProfileIn( __METHOD__ . '-cache' );
170
171 # Make sure all extension messages are available
172 global $wgMessageCache;
173 $wgMessageCache->loadAllMessages( 'en' );
174 $sortedArray = Language::getMessagesFor( 'en' );
175 if( $desc ){
176 krsort( $sortedArray );
177 } else {
178 ksort( $sortedArray );
179 }
180
181 $this->messages = array();
182 foreach( $sortedArray as $key => $value ) {
183 // All messages start with lowercase, but wikis might have both
184 // upper and lowercase MediaWiki: pages if $wgCapitalLinks=false.
185 $ukey = $this->lang->ucfirst( $key );
186
187 // The value without any overrides from the MediaWiki: namespace
188 $this->messages[$ukey]['default'] = wfMsgGetKey( $key, /*useDB*/false, $this->langcode, false );
189
190 // The message that's actually used by the site
191 $this->messages[$ukey]['actual'] = wfMsgGetKey( $key, /*useDB*/true, $this->langcode, false );
192
193 $this->messages[$ukey]['customised'] = 0; //for now
194
195 $sortedArray[$key] = NULL; // trade bytes from $sortedArray to this
196 }
197
198 wfProfileOut( __METHOD__ . '-cache' );
199
200 return true;
201 }
202
203 # We only need a list of which messages have *been* customised;
204 # their content is already in the message cache.
205 function markCustomisedMessages(){
206 $this->talkPages = array();
207
208 wfProfileIn( __METHOD__ . "-db" );
209
210 $dbr = wfGetDB( DB_SLAVE );
211 $res = $dbr->select( 'page',
212 array( 'page_namespace', 'page_title' ),
213 array( 'page_namespace' => array(NS_MEDIAWIKI,NS_MEDIAWIKI_TALK) ),
214 __METHOD__,
215 array( 'USE INDEX' => 'name_title' )
216 );
217
218 while( $s = $dbr->fetchObject( $res ) ) {
219 if( $s->page_namespace == NS_MEDIAWIKI ){
220 if( $this->foreign ){
221 $title = explode( '/', $s->page_title );
222 if( count($title) === 2 && $this->langcode == $title[1] && array_key_exists( $title[0], $this->messages ) ){
223 $this->messages["{$title[0]}"]['customised'] = 1;
224 }
225 } else if( array_key_exists( $s->page_title , $this->messages ) ){
226 $this->messages[$s->page_title]['customised'] = 1;
227 }
228 } else if( $s->page_namespace == NS_MEDIAWIKI_TALK ){
229 $this->talkPages[$s->page_title] = 1;
230 }
231 }
232 $dbr->freeResult( $res );
233
234 wfProfileOut( __METHOD__ . "-db" );
235
236 return true;
237 }
238
239 /* This function normally does a database query to get the results; we need
240 * to make a pretend result using a FakeResultWrapper.
241 */
242 function reallyDoQuery( $offset , $limit , $descending ){
243 $mResult = new FakeResultWrapper( array() );
244
245 if( !$this->messages ) $this->getAllMessages( $descending );
246 if( $this->talkPages === NULL ) $this->markCustomisedMessages();
247
248 $count = 0;
249 foreach( $this->messages as $key => $value ){
250 if( $value['customised'] !== $this->custom &&
251 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
252 (( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
253 ){
254 $mResult->result[] = array(
255 'am_title' => $key,
256 'am_actual' => $value['actual'],
257 'am_default' => $value['default'],
258 'am_customised' => $value['customised'],
259 );
260 unset( $this->messages[$key] ); // save a few bytes
261 $count++;
262 }
263 if( $count == $limit ) break;
264 }
265 unset( $this->messages ); //no longer needed, free up some memory
266 return $mResult;
267 }
268
269 function getStartBody() {
270 return Xml::openElement( 'table', array( 'class' => 'TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
271 "<tr>
272 <th rowspan=\"2\">" .
273 wfMsg( 'allmessagesname' ) . "
274 </th>
275 <th>" .
276 wfMsg( 'allmessagesdefault' ) .
277 "</th>
278 </tr>\n
279 <tr>
280 <th>" .
281 wfMsg( 'allmessagescurrent' ) .
282 "</th>
283 </tr>\n";
284 }
285
286 function formatValue( $field , $value ){
287 global $wgLang;
288 switch( $field ){
289
290 case 'am_title' :
291
292 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
293 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
294
295 if( $this->mCurrentRow->am_customised ){
296 $title = $this->mSkin->linkKnown( $title, $wgLang->lcfirst( $value ) );
297 } else {
298 $title = $this->mSkin->link( $title,
299 $wgLang->lcfirst( $value ),
300 array(),
301 array(),
302 array( 'broken' ) );
303 }
304 if( array_key_exists( $talk->getDBkey() , $this->talkPages ) ) {
305 $talk = $this->mSkin->linkKnown( $talk , $this->talk );
306 } else {
307 $talk = $this->mSkin->link( $talk,
308 $this->talk,
309 array(),
310 array(),
311 array( 'broken' ) );
312 }
313 return $title . ' (' . $talk . ')';
314
315 case 'am_default' :
316 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
317 case 'am_actual' :
318 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
319 }
320 return '';
321 }
322
323 function formatRow( $row ){
324 //Do all the normal stuff
325 $s = parent::formatRow( $row );
326
327 //But if there's a customised message, add that too.
328 if( $row->am_customised ){
329 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
330 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
331 if ( $formatted == '' ) {
332 $formatted = '&nbsp;';
333 }
334 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
335 . "</tr>\n";
336 }
337 return $s;
338 }
339
340 function getRowAttrs( $row, $isSecond=false ){
341 $arr = array();
342 global $wgLang;
343 if( $row->am_customised ){
344 $arr['class'] = 'allmessages-customised';
345 }
346 if( !$isSecond ){
347 $arr['id'] = Sanitizer::escapeId( 'msg_' . $wgLang->lcfirst( $row->am_title ) );
348 }
349 return $arr;
350 }
351
352 function getCellAttrs( $field, $value ){
353 if( $this->mCurrentRow->am_customised && $field == 'am_title' ){
354 return array( 'rowspan' => '2', 'class' => $field );
355 } else {
356 return array( 'class' => $field );
357 }
358 }
359
360 // This is not actually used, as getStartBody is overridden above
361 function getFieldNames() {
362 return array( 'am_title' => wfMsg('allmessagesname'),
363 'am_default' => wfMsg('allmessagesdefault') );
364 }
365 function getTitle() {
366 return SpecialPage::getTitleFor( 'Allmessages', false );
367 }
368 function isFieldSortable( $x ){
369 return false;
370 }
371 function getDefaultSort(){
372 return '';
373 }
374 function getQueryInfo(){
375 return '';
376 }
377 }
378 /* Overloads the relevant methods of the real ResultsWrapper so it
379 * doesn't go anywhere near an actual database.
380 */
381 class FakeResultWrapper extends ResultWrapper {
382
383 var $result = array();
384 var $db = NULL; //And it's going to stay that way :D
385 var $pos = 0;
386 var $currentRow = NULL;
387
388 function __construct( $array ){
389 $this->result = $array;
390 }
391
392 function numRows() {
393 return count( $this->result );
394 }
395
396 function fetchRow() {
397 $this->currentRow = $this->result[$this->pos++];
398 return $this->currentRow;
399 }
400
401 function seek( $row ) {
402 $this->pos = $row;
403 }
404
405 function free() {}
406
407 // Callers want to be able to access fields with $this->fieldName
408 function fetchObject(){
409 $this->currentRow = $this->result[$this->pos++];
410 return (object)$this->currentRow;
411 }
412
413 function rewind() {
414 $this->pos = 0;
415 $this->currentRow = NULL;
416 }
417 }