Follow-up r53286: Fix some issues:
[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', false ) ) );
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 Xml::label( wfMsg( 'allmessages-filter' ), 'mw-allmessages-form-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 "<table border=\"1\" class=\"TablePager\" style=\"width:100%;\" id=\"allmessagestable\"><thead>\n<tr>" .
271 "<th rowspan=\"2\">" . wfMsg('allmessagesname') . "</th><th>" . wfMsg('allmessagesdefault') .
272 "</tr>\n<tr><th>" . wfMsg('allmessagescurrent') . "</th></tr>\n";
273 }
274
275 function formatValue( $field , $value ){
276 global $wgLang;
277 switch( $field ){
278
279 case 'am_title' :
280
281 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
282 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
283
284 if( $this->mCurrentRow->am_customised ){
285 $title = $this->mSkin->linkKnown( $title, $wgLang->lcfirst( $value ) );
286 } else {
287 $title = $this->mSkin->link( $title,
288 $wgLang->lcfirst( $value ),
289 array(),
290 array(),
291 array( 'broken' ) );
292 }
293 if( array_key_exists( $talk->getDBkey() , $this->talkPages ) ) {
294 $talk = $this->mSkin->linkKnown( $talk , $this->talk );
295 } else {
296 $talk = $this->mSkin->link( $talk,
297 $this->talk,
298 array(),
299 array(),
300 array( 'broken' ) );
301 }
302 return $title . ' (' . $talk . ')';
303
304 case 'am_default' :
305 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
306 case 'am_actual' :
307 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
308 }
309 return '';
310 }
311
312 function formatRow( $row ){
313 //Do all the normal stuff
314 $s = parent::formatRow( $row );
315
316 //But if there's a customised message, add that too.
317 if( $row->am_customised ){
318 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
319 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
320 if ( $formatted == '' ) {
321 $formatted = '&nbsp;';
322 }
323 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
324 . "</tr>\n";
325 }
326 return $s;
327 }
328
329 function getRowAttrs( $row, $isSecond=false ){
330 $arr = array();
331 global $wgLang;
332 if( $row->am_customised ){
333 $arr['class'] = 'allmessages-customised';
334 }
335 if( !$isSecond ){
336 $arr['id'] = Sanitizer::escapeId( 'msg_' . $wgLang->lcfirst( $row->am_title ) );
337 }
338 return $arr;
339 }
340
341 function getCellAttrs( $field, $value ){
342 if( $this->mCurrentRow->am_customised && $field == 'am_title' ){
343 return array( 'rowspan' => '2', 'class' => $field );
344 } else {
345 return array( 'class' => $field );
346 }
347 }
348
349 // This is not actually used, as getStartBody is overridden above
350 function getFieldNames() {
351 return array( 'am_title' => wfMsg('allmessagesname'),
352 'am_default' => wfMsg('allmessagesdefault') );
353 }
354 function getTitle() {
355 return SpecialPage::getTitleFor( 'Allmessages', false );
356 }
357 function isFieldSortable( $x ){
358 return false;
359 }
360 function getDefaultSort(){
361 return '';
362 }
363 function getQueryInfo(){
364 return '';
365 }
366 }
367 /* Overloads the relevant methods of the real ResultsWrapper so it
368 * doesn't go anywhere near an actual database.
369 */
370 class FakeResultWrapper extends ResultWrapper {
371
372 var $result = array();
373 var $db = NULL; //And it's going to stay that way :D
374 var $pos = 0;
375 var $currentRow = NULL;
376
377 function __construct( $array ){
378 $this->result = $array;
379 }
380
381 function numRows() {
382 return count( $this->result );
383 }
384
385 function fetchRow() {
386 $this->currentRow = $this->result[$this->pos++];
387 return $this->currentRow;
388 }
389
390 function seek( $row ) {
391 $this->pos = $row;
392 }
393
394 function free() {}
395
396 // Callers want to be able to access fields with $this->fieldName
397 function fetchObject(){
398 $this->currentRow = $this->result[$this->pos++];
399 return (object)$this->currentRow;
400 }
401
402 function rewind() {
403 $this->pos = 0;
404 $this->currentRow = NULL;
405 }
406 }