This is the rework I was talking about in r104318 for 1.19. Instead of having Pager...
[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->filter = $request->getVal( 'wpFilter', 'all' );
66 $this->prefix = $request->getVal( 'wpPrefix', '' );
67
68 $this->table = new AllmessagesTablePager(
69 $this,
70 array(),
71 wfGetLangObj( $request->getVal( 'wpLanguage', $par ) )
72 );
73
74 $this->langcode = $this->table->lang->getCode();
75
76 $out->addHTML( $this->table->buildHTMLForm() .
77 $this->table->getNavigationBar() .
78 $this->table->getBody() .
79 $this->table->getNavigationBar() );
80
81 }
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 if( $request->getVal( 'wpFilter', 'all' ) === 'all' ){
124 $this->custom = null; // So won't match in either case
125 } else {
126 $this->custom = ($request->getVal( 'wpFilter' ) == 'unmodified');
127 }
128
129 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'wpPrefix', '' ) );
130 $prefix = $prefix != '' ? Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'wpPrefix', null ) ) : null;
131 if( $prefix !== null ){
132 $this->displayPrefix = $prefix->getDBkey();
133 $this->prefix = '/^' . preg_quote( $this->displayPrefix ) . '/i';
134 } else {
135 $this->displayPrefix = false;
136 $this->prefix = false;
137 }
138
139 // The suffix that may be needed for message names if we're in a
140 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
141 if( $this->foreign ) {
142 $this->suffix = '/' . $this->langcode;
143 } else {
144 $this->suffix = '';
145 }
146 }
147
148 protected function getHTMLFormFields() {
149 $f = array(
150 'Prefix' => array(
151 'type' => 'text',
152 'label-message' => 'allmessages-prefix',
153 'size' => 20,
154 ),
155 'Filter' => array(
156 'type' => 'radio',
157 'label-message' => 'allmessages-filter',
158 'options' => array(
159 $this->msg( 'allmessages-filter-unmodified' )->text() => 'unmodified',
160 $this->msg( 'allmessages-filter-all' )->text() => 'all',
161 $this->msg( 'allmessages-filter-modified' )->text() => 'modified',
162 ),
163 'flatlist' => true,
164 ),
165 'Language' => array(
166 'type' => 'select',
167 'label-message' => 'allmessages-language',
168 'options' => array(), // This is filled in below
169 'default' => $this->langcode,
170 ),
171 'Limit' => $this->getHTMLFormLimitSelect(),
172 );
173
174 $languages = Language::getLanguageNames( false );
175 ksort( $languages );
176
177 foreach( $languages as $code => $name ) {
178 $f['Language']['options'][ "$code - $name" ] = $code;
179 }
180
181 return $f;
182 }
183
184 protected function getHTMLFormLegend() {
185 return 'allmessages-filter-legend';
186 }
187
188 protected function getHTMLFormSubmit() {
189 return 'allmessages-filter-submit';
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
204 wfProfileOut( __METHOD__ );
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 * @param array $messageNames
215 * @param string $langcode What language code
216 * @param bool $foreign Whether the $langcode is not the content language
217 * @return array: a 'pages' and 'talks' array with the keys of existing pages
218 */
219 public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
220 // FIXME: This function should be moved to Language:: or something.
221 wfProfileIn( __METHOD__ . '-db' );
222
223 $dbr = wfGetDB( DB_SLAVE );
224 $res = $dbr->select( 'page',
225 array( 'page_namespace', 'page_title' ),
226 array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
227 __METHOD__,
228 array( 'USE INDEX' => 'name_title' )
229 );
230 $xNames = array_flip( $messageNames );
231
232 $pageFlags = $talkFlags = array();
233
234 foreach ( $res as $s ) {
235 $exists = false;
236 if( $foreign ) {
237 $title = explode( '/', $s->page_title );
238 if( count( $title ) === 2 && $langcode == $title[1]
239 && isset( $xNames[$title[0]] ) ) {
240 $exists = $title[0];
241 }
242 } elseif( isset( $xNames[$s->page_title] ) ) {
243 $exists = $s->page_title;
244 }
245 if( $exists && $s->page_namespace == NS_MEDIAWIKI ) {
246 $pageFlags[$exists] = true;
247 } elseif( $exists && $s->page_namespace == NS_MEDIAWIKI_TALK ) {
248 $talkFlags[$exists] = true;
249 }
250 }
251
252 wfProfileOut( __METHOD__ . '-db' );
253
254 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
255 }
256
257 /**
258 * This function normally does a database query to get the results; we need
259 * to make a pretend result using a FakeResultWrapper.
260 */
261 function reallyDoQuery( $offset, $limit, $descending ) {
262 $result = new FakeResultWrapper( array() );
263
264 $messageNames = $this->getAllMessages( $descending );
265 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
266
267 $count = 0;
268 foreach( $messageNames as $key ) {
269 $customised = isset( $statuses['pages'][$key] );
270 if( $customised !== $this->custom &&
271 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
272 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
273 ) {
274 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
275 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
276 $result->result[] = array(
277 'am_title' => $key,
278 'am_actual' => $actual,
279 'am_default' => $default,
280 'am_customised' => $customised,
281 'am_talk_exists' => isset( $statuses['talks'][$key] )
282 );
283 $count++;
284 }
285 if( $count == $limit ) {
286 break;
287 }
288 }
289 return $result;
290 }
291
292 function getStartBody() {
293 return Xml::openElement( 'table', array( 'class' => 'mw-datatable TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
294 "<thead><tr>
295 <th rowspan=\"2\">" .
296 $this->msg( 'allmessagesname' )->escaped() . "
297 </th>
298 <th>" .
299 $this->msg( 'allmessagesdefault' )->escaped() .
300 "</th>
301 </tr>\n
302 <tr>
303 <th>" .
304 $this->msg( 'allmessagescurrent' )->escaped() .
305 "</th>
306 </tr></thead><tbody>\n";
307 }
308
309 function formatValue( $field, $value ){
310 switch( $field ){
311
312 case 'am_title' :
313
314 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
315 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
316
317 if( $this->mCurrentRow->am_customised ){
318 $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
319 } else {
320 $title = Linker::link(
321 $title,
322 $this->getLanguage()->lcfirst( $value ),
323 array(),
324 array(),
325 array( 'broken' )
326 );
327 }
328 if ( $this->mCurrentRow->am_talk_exists ) {
329 $talk = Linker::linkKnown( $talk , $this->talk );
330 } else {
331 $talk = Linker::link(
332 $talk,
333 $this->talk,
334 array(),
335 array(),
336 array( 'broken' )
337 );
338 }
339 return $title . ' (' . $talk . ')';
340
341 case 'am_default' :
342 case 'am_actual' :
343 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
344 }
345 return '';
346 }
347
348 function formatRow( $row ){
349 // Do all the normal stuff
350 $s = parent::formatRow( $row );
351
352 // But if there's a customised message, add that too.
353 if( $row->am_customised ){
354 $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
355 $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
356 if ( $formatted == '' ) {
357 $formatted = '&#160;';
358 }
359 $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
360 . "</tr>\n";
361 }
362 return $s;
363 }
364
365 function getRowAttrs( $row, $isSecond = false ){
366 $arr = array();
367 if( $row->am_customised ){
368 $arr['class'] = 'allmessages-customised';
369 }
370 if( !$isSecond ){
371 $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
372 }
373 return $arr;
374 }
375
376 function getCellAttrs( $field, $value ){
377 if( $this->mCurrentRow->am_customised && $field == 'am_title' ){
378 return array( 'rowspan' => '2', 'class' => $field );
379 } elseif( $field == 'am_title' ) {
380 return array( 'class' => $field );
381 } else {
382 return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir(), 'class' => $field );
383 }
384 }
385
386 // This is not actually used, as getStartBody is overridden above
387 function getFieldNames() {
388 return array(
389 'am_title' => $this->msg( 'allmessagesname' )->text(),
390 'am_default' => $this->msg( 'allmessagesdefault' )->text()
391 );
392 }
393
394 function getTitle() {
395 return SpecialPage::getTitleFor( 'Allmessages', false );
396 }
397
398 function isFieldSortable( $x ){
399 return false;
400 }
401
402 function getDefaultSort(){
403 return '';
404 }
405
406 function getQueryInfo(){
407 return '';
408 }
409 }
410