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