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