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