* Allow IndexPager->mLimitsShown to be an associative array of limit => text-to-displ...
[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 public $mLimitsShown;
132
133 function __construct( $page, $conds, $langObj = null ) {
134 parent::__construct();
135 $this->mIndexField = 'am_title';
136 $this->mPage = $page;
137 $this->mConds = $conds;
138 $this->mDefaultDirection = true; // always sort ascending
139 // We want to have an option for people to view *all* the messages,
140 // so they can use Ctrl+F to search them. 5000 is the maximum that
141 // will get through WebRequest::getLimitOffset().
142 $this->mLimitsShown = array( 20, 50, 100, 250, 500, 5000 => wfMsg('messagesall') );
143
144 global $wgLang, $wgContLang, $wgRequest;
145
146 $this->talk = $wgLang->lc( htmlspecialchars( wfMsg( 'talkpagelinktext' ) ) );
147
148 $this->lang = ( $langObj ? $langObj : $wgContLang );
149 $this->langcode = $this->lang->getCode();
150 $this->foreign = $this->langcode != $wgContLang->getCode();
151
152 if( $wgRequest->getVal( 'filter', 'all' ) === 'all' ){
153 $this->custom = null; // So won't match in either case
154 } else {
155 $this->custom = $wgRequest->getVal( 'filter' ) == 'unmodified' ? 1 : 0;
156 }
157
158 $prefix = $wgLang->ucfirst( $wgRequest->getVal( 'prefix', '' ) );
159 $prefix = $prefix != '' ? Title::makeTitleSafe( NS_MEDIAWIKI, $wgRequest->getVal( 'prefix', null ) ) : null;
160 if( $prefix !== null ){
161 $this->prefix = '/^' . preg_quote( $prefix->getDBkey() ) . '/i';
162 } else {
163 $this->prefix = false;
164 }
165 $this->getSkin();
166
167 // The suffix that may be needed for message names if we're in a
168 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
169 if( $this->foreign ) {
170 $this->suffix = '/' . $this->langcode;
171 } else {
172 $this->suffix = '';
173 }
174 }
175
176 function getAllMessages( $desc ){
177 wfProfileIn( __METHOD__ . '-cache' );
178
179 # Make sure all extension messages are available
180 global $wgMessageCache;
181 $wgMessageCache->loadAllMessages( 'en' );
182 $sortedArray = Language::getMessagesFor( 'en' );
183 if( $desc ){
184 krsort( $sortedArray );
185 } else {
186 ksort( $sortedArray );
187 }
188
189 $this->messages = array();
190 foreach( $sortedArray as $key => $value ) {
191 // All messages start with lowercase, but wikis might have both
192 // upper and lowercase MediaWiki: pages if $wgCapitalLinks=false.
193 $ukey = $this->lang->ucfirst( $key );
194
195 // The value without any overrides from the MediaWiki: namespace
196 $this->messages[$ukey]['default'] = wfMsgGetKey( $key, /*useDB*/false, $this->langcode, false );
197
198 // The message that's actually used by the site
199 $this->messages[$ukey]['actual'] = wfMsgGetKey( $key, /*useDB*/true, $this->langcode, false );
200
201 $this->messages[$ukey]['customised'] = 0; //for now
202
203 $sortedArray[$key] = null; // trade bytes from $sortedArray to this
204 }
205
206 wfProfileOut( __METHOD__ . '-cache' );
207
208 return true;
209 }
210
211 # We only need a list of which messages have *been* customised;
212 # their content is already in the message cache.
213 function markCustomisedMessages(){
214 $this->talkPages = array();
215
216 wfProfileIn( __METHOD__ . '-db' );
217
218 $dbr = wfGetDB( DB_SLAVE );
219 $res = $dbr->select( 'page',
220 array( 'page_namespace', 'page_title' ),
221 array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
222 __METHOD__,
223 array( 'USE INDEX' => 'name_title' )
224 );
225
226 while( $s = $dbr->fetchObject( $res ) ) {
227 if( $s->page_namespace == NS_MEDIAWIKI ){
228 if( $this->foreign ){
229 $title = explode( '/', $s->page_title );
230 if( count( $title ) === 2 && $this->langcode == $title[1] && array_key_exists( $title[0], $this->messages ) ){
231 $this->messages["{$title[0]}"]['customised'] = 1;
232 }
233 } else if( array_key_exists( $s->page_title, $this->messages ) ){
234 $this->messages[$s->page_title]['customised'] = 1;
235 }
236 } else if( $s->page_namespace == NS_MEDIAWIKI_TALK ){
237 $this->talkPages[$s->page_title] = 1;
238 }
239 }
240 $dbr->freeResult( $res );
241
242 wfProfileOut( __METHOD__ . '-db' );
243
244 return true;
245 }
246
247 /* This function normally does a database query to get the results; we need
248 * to make a pretend result using a FakeResultWrapper.
249 */
250 function reallyDoQuery( $offset, $limit, $descending ){
251 $mResult = new FakeResultWrapper( array() );
252
253 if( !$this->messages ) $this->getAllMessages( $descending );
254 if( $this->talkPages === null ) $this->markCustomisedMessages();
255
256 $count = 0;
257 foreach( $this->messages as $key => $value ){
258 if( $value['customised'] !== $this->custom &&
259 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
260 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
261 ){
262 $mResult->result[] = array(
263 'am_title' => $key,
264 'am_actual' => $value['actual'],
265 'am_default' => $value['default'],
266 'am_customised' => $value['customised'],
267 );
268 unset( $this->messages[$key] ); // save a few bytes
269 $count++;
270 }
271 if( $count == $limit ) break;
272 }
273 unset( $this->messages ); // no longer needed, free up some memory
274 return $mResult;
275 }
276
277 function getStartBody() {
278 return Xml::openElement( 'table', array( 'class' => 'TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
279 "<thead><tr>
280 <th rowspan=\"2\">" .
281 wfMsg( 'allmessagesname' ) . "
282 </th>
283 <th>" .
284 wfMsg( 'allmessagesdefault' ) .
285 "</th>
286 </tr>\n
287 <tr>
288 <th>" .
289 wfMsg( 'allmessagescurrent' ) .
290 "</th>
291 </tr></thead><tbody>\n";
292 }
293
294 function formatValue( $field, $value ){
295 global $wgLang;
296 switch( $field ){
297
298 case 'am_title' :
299
300 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
301 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
302
303 if( $this->mCurrentRow->am_customised ){
304 $title = $this->mSkin->linkKnown( $title, $wgLang->lcfirst( $value ) );
305 } else {
306 $title = $this->mSkin->link(
307 $title,
308 $wgLang->lcfirst( $value ),
309 array(),
310 array(),
311 array( 'broken' )
312 );
313 }
314 if( array_key_exists( $talk->getDBkey() , $this->talkPages ) ) {
315 $talk = $this->mSkin->linkKnown( $talk , $this->talk );
316 } else {
317 $talk = $this->mSkin->link(
318 $talk,
319 $this->talk,
320 array(),
321 array(),
322 array( 'broken' )
323 );
324 }
325 return $title . ' (' . $talk . ')';
326
327 case 'am_default' :
328 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
329 case 'am_actual' :
330 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
331 }
332 return '';
333 }
334
335 function formatRow( $row ){
336 // Do all the normal stuff
337 $s = parent::formatRow( $row );
338
339 // But if there's a customised message, add that too.
340 if( $row->am_customised ){
341 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
342 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
343 if ( $formatted == '' ) {
344 $formatted = '&nbsp;';
345 }
346 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
347 . "</tr>\n";
348 }
349 return $s;
350 }
351
352 function getRowAttrs( $row, $isSecond = false ){
353 $arr = array();
354 global $wgLang;
355 if( $row->am_customised ){
356 $arr['class'] = 'allmessages-customised';
357 }
358 if( !$isSecond ){
359 $arr['id'] = Sanitizer::escapeId( 'msg_' . $wgLang->lcfirst( $row->am_title ) );
360 }
361 return $arr;
362 }
363
364 function getCellAttrs( $field, $value ){
365 if( $this->mCurrentRow->am_customised && $field == 'am_title' ){
366 return array( 'rowspan' => '2', 'class' => $field );
367 } else {
368 return array( 'class' => $field );
369 }
370 }
371
372 // This is not actually used, as getStartBody is overridden above
373 function getFieldNames() {
374 return array(
375 'am_title' => wfMsg( 'allmessagesname' ),
376 'am_default' => wfMsg( 'allmessagesdefault' )
377 );
378 }
379 function getTitle() {
380 return SpecialPage::getTitleFor( 'Allmessages', false );
381 }
382 function isFieldSortable( $x ){
383 return false;
384 }
385 function getDefaultSort(){
386 return '';
387 }
388 function getQueryInfo(){
389 return '';
390 }
391 }
392 /* Overloads the relevant methods of the real ResultsWrapper so it
393 * doesn't go anywhere near an actual database.
394 */
395 class FakeResultWrapper extends ResultWrapper {
396
397 var $result = array();
398 var $db = null; // And it's going to stay that way :D
399 var $pos = 0;
400 var $currentRow = null;
401
402 function __construct( $array ){
403 $this->result = $array;
404 }
405
406 function numRows() {
407 return count( $this->result );
408 }
409
410 function fetchRow() {
411 $this->currentRow = $this->result[$this->pos++];
412 return $this->currentRow;
413 }
414
415 function seek( $row ) {
416 $this->pos = $row;
417 }
418
419 function free() {}
420
421 // Callers want to be able to access fields with $this->fieldName
422 function fetchObject(){
423 $this->currentRow = $this->result[$this->pos++];
424 return (object)$this->currentRow;
425 }
426
427 function rewind() {
428 $this->pos = 0;
429 $this->currentRow = null;
430 }
431 }