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