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