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