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