Ununsed variables
[lhc/web/wiklou.git] / includes / specials / SpecialAllmessages.php
1 <?php
2 /**
3 * Implements Special:Allmessages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Use this special page to get a list of the MediaWiki system messages.
26 *
27 * @file
28 * @ingroup SpecialPage
29 */
30 class SpecialAllmessages extends SpecialPage {
31
32 /**
33 * @var AllmessagesTablePager
34 */
35 protected $table;
36
37 /**
38 * Constructor
39 */
40 public function __construct() {
41 parent::__construct( 'Allmessages' );
42 }
43
44 /**
45 * Show the special page
46 *
47 * @param $par Mixed: parameter passed to the page or null
48 */
49 public function execute( $par ) {
50 global $wgOut, $wgRequest;
51
52 $this->setHeaders();
53
54 global $wgUseDatabaseMessages;
55 if( !$wgUseDatabaseMessages ) {
56 $wgOut->addWikiMsg( 'allmessagesnotsupportedDB' );
57 return;
58 } else {
59 $this->outputHeader( 'allmessagestext' );
60 }
61
62 $this->filter = $wgRequest->getVal( 'filter', 'all' );
63 $this->prefix = $wgRequest->getVal( 'prefix', '' );
64
65 $this->table = new AllmessagesTablePager(
66 $this,
67 array(),
68 wfGetLangObj( $wgRequest->getVal( 'lang', $par ) )
69 );
70
71 $this->langCode = $this->table->lang->getCode();
72
73 $wgOut->addHTML( $this->buildForm() .
74 $this->table->getNavigationBar() .
75 $this->table->getLimitForm() .
76 $this->table->getBody() .
77 $this->table->getNavigationBar() );
78
79 }
80
81 function buildForm() {
82 global $wgScript;
83
84 $languages = Language::getLanguageNames( false );
85 ksort( $languages );
86
87 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-allmessages-form' ) ) .
88 Xml::fieldset( wfMsg( 'allmessages-filter-legend' ) ) .
89 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
90 Xml::openElement( 'table', array( 'class' => 'mw-allmessages-table' ) ) . "\n" .
91 '<tr>
92 <td class="mw-label">' .
93 Xml::label( wfMsg( 'allmessages-prefix' ), 'mw-allmessages-form-prefix' ) .
94 "</td>\n
95 <td class=\"mw-input\">" .
96 Xml::input( 'prefix', 20, str_replace( '_', ' ', $this->prefix ), array( 'id' => 'mw-allmessages-form-prefix' ) ) .
97 "</td>\n
98 </tr>
99 <tr>\n
100 <td class='mw-label'>" .
101 wfMsg( 'allmessages-filter' ) .
102 "</td>\n
103 <td class='mw-input'>" .
104 Xml::radioLabel( wfMsg( 'allmessages-filter-unmodified' ),
105 'filter',
106 'unmodified',
107 'mw-allmessages-form-filter-unmodified',
108 ( $this->filter == 'unmodified' )
109 ) .
110 Xml::radioLabel( wfMsg( 'allmessages-filter-all' ),
111 'filter',
112 'all',
113 'mw-allmessages-form-filter-all',
114 ( $this->filter == 'all' )
115 ) .
116 Xml::radioLabel( wfMsg( 'allmessages-filter-modified' ),
117 'filter',
118 'modified',
119 'mw-allmessages-form-filter-modified',
120 ( $this->filter == 'modified' )
121 ) .
122 "</td>\n
123 </tr>
124 <tr>\n
125 <td class=\"mw-label\">" .
126 Xml::label( wfMsg( 'allmessages-language' ), 'mw-allmessages-form-lang' ) .
127 "</td>\n
128 <td class=\"mw-input\">" .
129 Xml::openElement( 'select', array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ) );
130
131 foreach( $languages as $lang => $name ) {
132 $selected = $lang == $this->langCode;
133 $out .= Xml::option( $lang . ' - ' . $name, $lang, $selected ) . "\n";
134 }
135 $out .= Xml::closeElement( 'select' ) .
136 "</td>\n
137 </tr>
138 <tr>\n
139 <td></td>
140 <td>" .
141 Xml::submitButton( wfMsg( 'allmessages-filter-submit' ) ) .
142 "</td>\n
143 </tr>" .
144 Xml::closeElement( 'table' ) .
145 $this->table->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang' ) ) .
146 Xml::closeElement( 'fieldset' ) .
147 Xml::closeElement( 'form' );
148 return $out;
149 }
150 }
151
152 /* use TablePager for prettified output. We have to pretend that we're
153 * getting data from a table when in fact not all of it comes from the database.
154 */
155 class AllmessagesTablePager extends TablePager {
156
157 public $mLimitsShown;
158
159 /**
160 * @var Language
161 */
162 public $lang;
163
164 function __construct( $page, $conds, $langObj = null ) {
165 parent::__construct();
166 $this->mIndexField = 'am_title';
167 $this->mPage = $page;
168 $this->mConds = $conds;
169 $this->mDefaultDirection = true; // always sort ascending
170 $this->mLimitsShown = array( 20, 50, 100, 250, 500, 5000 );
171
172 global $wgLang, $wgContLang, $wgRequest;
173
174 $this->talk = htmlspecialchars( wfMsg( 'talkpagelinktext' ) );
175
176 $this->lang = ( $langObj ? $langObj : $wgContLang );
177 $this->langcode = $this->lang->getCode();
178 $this->foreign = $this->langcode != $wgContLang->getCode();
179
180 if( $wgRequest->getVal( 'filter', 'all' ) === 'all' ){
181 $this->custom = null; // So won't match in either case
182 } else {
183 $this->custom = ($wgRequest->getVal( 'filter' ) == 'unmodified');
184 }
185
186 $prefix = $wgLang->ucfirst( $wgRequest->getVal( 'prefix', '' ) );
187 $prefix = $prefix != '' ? Title::makeTitleSafe( NS_MEDIAWIKI, $wgRequest->getVal( 'prefix', null ) ) : null;
188 if( $prefix !== null ){
189 $this->prefix = '/^' . preg_quote( $prefix->getDBkey() ) . '/i';
190 } else {
191 $this->prefix = false;
192 }
193 $this->getSkin();
194
195 // The suffix that may be needed for message names if we're in a
196 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
197 if( $this->foreign ) {
198 $this->suffix = '/' . $this->langcode;
199 } else {
200 $this->suffix = '';
201 }
202 }
203
204 function getAllMessages( $descending ) {
205 wfProfileIn( __METHOD__ );
206 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
207 if( $descending ){
208 rsort( $messageNames );
209 } else {
210 asort( $messageNames );
211 }
212
213 // Normalise message names so they look like page titles
214 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
215
216 wfProfileOut( __METHOD__ );
217 return $messageNames;
218 }
219
220 /**
221 * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
222 * Returns array( 'pages' => ..., 'talks' => ... ), where the subarrays have
223 * an entry for each existing page, with the key being the message name and
224 * value arbitrary.
225 */
226 function getCustomisedStatuses( $messageNames ) {
227 wfProfileIn( __METHOD__ . '-db' );
228
229 $dbr = wfGetDB( DB_SLAVE );
230 $res = $dbr->select( 'page',
231 array( 'page_namespace', 'page_title' ),
232 array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
233 __METHOD__,
234 array( 'USE INDEX' => 'name_title' )
235 );
236 $xNames = array_flip( $messageNames );
237
238 $pageFlags = $talkFlags = array();
239
240 foreach ( $res as $s ) {
241 if( $s->page_namespace == NS_MEDIAWIKI ) {
242 if( $this->foreign ) {
243 $title = explode( '/', $s->page_title );
244 if( count( $title ) === 2 && $this->langcode == $title[1]
245 && isset( $xNames[$title[0]] ) )
246 {
247 $pageFlags["{$title[0]}"] = true;
248 }
249 } elseif( isset( $xNames[$s->page_title] ) ) {
250 $pageFlags[$s->page_title] = true;
251 }
252 } else if( $s->page_namespace == NS_MEDIAWIKI_TALK ){
253 $talkFlags[$s->page_title] = true;
254 }
255 }
256
257 wfProfileOut( __METHOD__ . '-db' );
258
259 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
260 }
261
262 /* This function normally does a database query to get the results; we need
263 * to make a pretend result using a FakeResultWrapper.
264 */
265 function reallyDoQuery( $offset, $limit, $descending ) {
266 $result = new FakeResultWrapper( array() );
267
268 $messageNames = $this->getAllMessages( $descending );
269 $statuses = $this->getCustomisedStatuses( $messageNames );
270
271 $count = 0;
272 foreach( $messageNames as $key ) {
273 $customised = isset( $statuses['pages'][$key] );
274 if( $customised !== $this->custom &&
275 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
276 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
277 ){
278 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
279 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
280 $result->result[] = array(
281 'am_title' => $key,
282 'am_actual' => $actual,
283 'am_default' => $default,
284 'am_customised' => $customised,
285 'am_talk_exists' => isset( $statuses['talks'][$key] )
286 );
287 $count++;
288 }
289 if( $count == $limit ) break;
290 }
291 return $result;
292 }
293
294 function getStartBody() {
295 return Xml::openElement( 'table', array( 'class' => 'TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
296 "<thead><tr>
297 <th rowspan=\"2\">" .
298 wfMsg( 'allmessagesname' ) . "
299 </th>
300 <th>" .
301 wfMsg( 'allmessagesdefault' ) .
302 "</th>
303 </tr>\n
304 <tr>
305 <th>" .
306 wfMsg( 'allmessagescurrent' ) .
307 "</th>
308 </tr></thead><tbody>\n";
309 }
310
311 function formatValue( $field, $value ){
312 global $wgLang;
313 switch( $field ){
314
315 case 'am_title' :
316
317 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
318 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
319
320 if( $this->mCurrentRow->am_customised ){
321 $title = $this->mSkin->linkKnown( $title, $wgLang->lcfirst( $value ) );
322 } else {
323 $title = $this->mSkin->link(
324 $title,
325 $wgLang->lcfirst( $value ),
326 array(),
327 array(),
328 array( 'broken' )
329 );
330 }
331 if ( $this->mCurrentRow->am_talk_exists ) {
332 $talk = $this->mSkin->linkKnown( $talk , $this->talk );
333 } else {
334 $talk = $this->mSkin->link(
335 $talk,
336 $this->talk,
337 array(),
338 array(),
339 array( 'broken' )
340 );
341 }
342 return $title . ' (' . $talk . ')';
343
344 case 'am_default' :
345 case 'am_actual' :
346 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
347 }
348 return '';
349 }
350
351 function formatRow( $row ){
352 // Do all the normal stuff
353 $s = parent::formatRow( $row );
354
355 // But if there's a customised message, add that too.
356 if( $row->am_customised ){
357 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
358 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
359 if ( $formatted == '' ) {
360 $formatted = '&#160;';
361 }
362 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
363 . "</tr>\n";
364 }
365 return $s;
366 }
367
368 function getRowAttrs( $row, $isSecond = false ){
369 $arr = array();
370 global $wgLang;
371 if( $row->am_customised ){
372 $arr['class'] = 'allmessages-customised';
373 }
374 if( !$isSecond ){
375 $arr['id'] = Sanitizer::escapeId( 'msg_' . $wgLang->lcfirst( $row->am_title ) );
376 }
377 return $arr;
378 }
379
380 function getCellAttrs( $field, $value ){
381 if( $this->mCurrentRow->am_customised && $field == 'am_title' ){
382 return array( 'rowspan' => '2', 'class' => $field );
383 } else {
384 return array( 'class' => $field );
385 }
386 }
387
388 // This is not actually used, as getStartBody is overridden above
389 function getFieldNames() {
390 return array(
391 'am_title' => wfMsg( 'allmessagesname' ),
392 'am_default' => wfMsg( 'allmessagesdefault' )
393 );
394 }
395 function getTitle() {
396 return SpecialPage::getTitleFor( 'Allmessages', false );
397 }
398 function isFieldSortable( $x ){
399 return false;
400 }
401 function getDefaultSort(){
402 return '';
403 }
404 function getQueryInfo(){
405 return '';
406 }
407 }
408