fcbac9ea5b50961b0a1bc83e9b0472d1eb8df01c
[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
245 wfProfileOut( __METHOD__ . '-db' );
246
247 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
248 }
249
250 /* This function normally does a database query to get the results; we need
251 * to make a pretend result using a FakeResultWrapper.
252 */
253 function reallyDoQuery( $offset, $limit, $descending ) {
254 $result = new FakeResultWrapper( array() );
255
256 $messageNames = $this->getAllMessages( $descending );
257 $statuses = $this->getCustomisedStatuses( $messageNames );
258
259 $count = 0;
260 foreach( $messageNames as $key ) {
261 $customised = isset( $statuses['pages'][$key] );
262 if( $customised !== $this->custom &&
263 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
264 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
265 ){
266 $result->result[] = array(
267 'am_title' => $key,
268 'am_actual' => wfMsgGetKey( $key, /*useDB*/true, $this->langcode, false ),
269 'am_default' => wfMsgGetKey( $key, /*useDB*/false, $this->langcode, false ),
270 'am_customised' => $customised,
271 'am_talk_exists' => isset( $statuses['talks'][$key] )
272 );
273 $count++;
274 }
275 if( $count == $limit ) break;
276 }
277 return $result;
278 }
279
280 function getStartBody() {
281 return Xml::openElement( 'table', array( 'class' => 'TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
282 "<thead><tr>
283 <th rowspan=\"2\">" .
284 wfMsg( 'allmessagesname' ) . "
285 </th>
286 <th>" .
287 wfMsg( 'allmessagesdefault' ) .
288 "</th>
289 </tr>\n
290 <tr>
291 <th>" .
292 wfMsg( 'allmessagescurrent' ) .
293 "</th>
294 </tr></thead><tbody>\n";
295 }
296
297 function formatValue( $field, $value ){
298 global $wgLang;
299 switch( $field ){
300
301 case 'am_title' :
302
303 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
304 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
305
306 if( $this->mCurrentRow->am_customised ){
307 $title = $this->mSkin->linkKnown( $title, $wgLang->lcfirst( $value ) );
308 } else {
309 $title = $this->mSkin->link(
310 $title,
311 $wgLang->lcfirst( $value ),
312 array(),
313 array(),
314 array( 'broken' )
315 );
316 }
317 if ( $this->mCurrentRow->am_talk_exists ) {
318 $talk = $this->mSkin->linkKnown( $talk , $this->talk );
319 } else {
320 $talk = $this->mSkin->link(
321 $talk,
322 $this->talk,
323 array(),
324 array(),
325 array( 'broken' )
326 );
327 }
328 return $title . ' (' . $talk . ')';
329
330 case 'am_default' :
331 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
332 case 'am_actual' :
333 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
334 }
335 return '';
336 }
337
338 function formatRow( $row ){
339 // Do all the normal stuff
340 $s = parent::formatRow( $row );
341
342 // But if there's a customised message, add that too.
343 if( $row->am_customised ){
344 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
345 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
346 if ( $formatted == '' ) {
347 $formatted = '&#160;';
348 }
349 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
350 . "</tr>\n";
351 }
352 return $s;
353 }
354
355 function getRowAttrs( $row, $isSecond = false ){
356 $arr = array();
357 global $wgLang;
358 if( $row->am_customised ){
359 $arr['class'] = 'allmessages-customised';
360 }
361 if( !$isSecond ){
362 $arr['id'] = Sanitizer::escapeId( 'msg_' . $wgLang->lcfirst( $row->am_title ) );
363 }
364 return $arr;
365 }
366
367 function getCellAttrs( $field, $value ){
368 if( $this->mCurrentRow->am_customised && $field == 'am_title' ){
369 return array( 'rowspan' => '2', 'class' => $field );
370 } else {
371 return array( 'class' => $field );
372 }
373 }
374
375 // This is not actually used, as getStartBody is overridden above
376 function getFieldNames() {
377 return array(
378 'am_title' => wfMsg( 'allmessagesname' ),
379 'am_default' => wfMsg( 'allmessagesdefault' )
380 );
381 }
382 function getTitle() {
383 return SpecialPage::getTitleFor( 'Allmessages', false );
384 }
385 function isFieldSortable( $x ){
386 return false;
387 }
388 function getDefaultSort(){
389 return '';
390 }
391 function getQueryInfo(){
392 return '';
393 }
394 }
395