]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/specials/SpecialAllmessages.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.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                         Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
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' )
104                                         ) .
105                                         Xml::radioLabel( wfMsg( 'allmessages-filter-all' ),
106                                                 'filter',
107                                                 'all',
108                                                 'mw-allmessages-form-filter-all',
109                                                 ( $this->filter == 'all' )
110                                         ) .
111                                         Xml::radioLabel( wfMsg( 'allmessages-filter-modified' ),
112                                                 'filter',
113                                                 'modified',
114                                                 'mw-allmessages-form-filter-modified',
115                                         ( $this->filter == 'modified' )
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;
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                 $this->mLimitsShown = array( 20, 50, 100, 250, 500, 5000 );
161
162                 global $wgLang, $wgContLang, $wgRequest;
163
164                 $this->talk = htmlspecialchars( wfMsg( 'talkpagelinktext' ) );
165
166                 $this->lang = ( $langObj ? $langObj : $wgContLang );
167                 $this->langcode = $this->lang->getCode();
168                 $this->foreign  = $this->langcode != $wgContLang->getCode();
169
170                 if( $wgRequest->getVal( 'filter', 'all' ) === 'all' ){
171                         $this->custom = null; // So won't match in either case
172                 } else {
173                         $this->custom = ($wgRequest->getVal( 'filter' ) == 'unmodified');
174                 }
175
176                 $prefix = $wgLang->ucfirst( $wgRequest->getVal( 'prefix', '' ) );
177                 $prefix = $prefix != '' ? Title::makeTitleSafe( NS_MEDIAWIKI, $wgRequest->getVal( 'prefix', null ) ) : null;
178                 if( $prefix !== null ){
179                         $this->prefix = '/^' . preg_quote( $prefix->getDBkey() ) . '/i';
180                 } else {
181                         $this->prefix = false;
182                 }
183                 $this->getSkin();
184
185                 // The suffix that may be needed for message names if we're in a
186                 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
187                 if( $this->foreign ) {
188                         $this->suffix = '/' . $this->langcode;
189                 } else {
190                         $this->suffix = '';
191                 }
192         }
193
194         function getAllMessages( $descending ) {
195                 wfProfileIn( __METHOD__ );
196                 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
197                 if( $descending ){
198                         rsort( $messageNames );
199                 } else {
200                         asort( $messageNames );
201                 }
202
203                 // Normalise message names so they look like page titles
204                 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
205
206                 wfProfileOut( __METHOD__ );
207                 return $messageNames;
208         }
209
210         /**
211          * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist. 
212          * Returns array( 'pages' => ..., 'talks' => ... ), where the subarrays have 
213          * an entry for each existing page, with the key being the message name and 
214          * value arbitrary.
215          */
216         function getCustomisedStatuses( $messageNames ) {
217                 wfProfileIn( __METHOD__ . '-db' );
218
219                 $dbr = wfGetDB( DB_SLAVE );
220                 $res = $dbr->select( 'page',
221                         array( 'page_namespace', 'page_title' ),
222                         array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
223                         __METHOD__,
224                         array( 'USE INDEX' => 'name_title' )
225                 );
226                 $xNames = array_flip( $messageNames );
227
228                 $pageFlags = $talkFlags = array();
229                 
230                 foreach ( $res as $s ) {
231                         if( $s->page_namespace == NS_MEDIAWIKI ) {
232                                 if( $this->foreign ) {
233                                         $title = explode( '/', $s->page_title );
234                                         if( count( $title ) === 2 && $this->langcode == $title[1] 
235                                                 && isset( $xNames[$title[0]] ) )
236                                         {
237                                                 $pageFlags["{$title[0]}"] = true;
238                                         }
239                                 } elseif( isset( $xNames[$s->page_title] ) ) {
240                                         $pageFlags[$s->page_title] = true;
241                                 }
242                         } else if( $s->page_namespace == NS_MEDIAWIKI_TALK ){
243                                 $talkFlags[$s->page_title] = true;
244                         }
245                 }
246
247                 wfProfileOut( __METHOD__ . '-db' );
248
249                 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
250         }
251
252         /* This function normally does a database query to get the results; we need
253          * to make a pretend result using a FakeResultWrapper.
254          */
255         function reallyDoQuery( $offset, $limit, $descending ) {
256                 $result = new FakeResultWrapper( array() );
257
258                 $messageNames = $this->getAllMessages( $descending );
259                 $statuses = $this->getCustomisedStatuses( $messageNames );
260
261                 $count = 0;
262                 foreach( $messageNames as $key ) {
263                         $customised = isset( $statuses['pages'][$key] );
264                         if( $customised !== $this->custom &&
265                                 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
266                                 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
267                         ){
268                                 $result->result[] = array(
269                                         'am_title'      => $key,
270                                         'am_actual'     => wfMsgGetKey( $key, /*useDB*/true, $this->langcode, false ),
271                                         'am_default'    => wfMsgGetKey( $key, /*useDB*/false, $this->langcode, false ),
272                                         'am_customised' => $customised,
273                                         'am_talk_exists' => isset( $statuses['talks'][$key] )
274                                 );
275                                 $count++;
276                         }
277                         if( $count == $limit ) break;
278                 }
279                 return $result;
280         }
281
282         function getStartBody() {
283                 return Xml::openElement( 'table', array( 'class' => 'TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
284                         "<thead><tr>
285                                 <th rowspan=\"2\">" .
286                                         wfMsg( 'allmessagesname' ) . "
287                                 </th>
288                                 <th>" .
289                                         wfMsg( 'allmessagesdefault' ) .
290                                 "</th>
291                         </tr>\n
292                         <tr>
293                                 <th>" .
294                                         wfMsg( 'allmessagescurrent' ) .
295                                 "</th>
296                         </tr></thead><tbody>\n";
297         }
298
299         function formatValue( $field, $value ){
300                 global $wgLang;
301                 switch( $field ){
302
303                         case 'am_title' :
304
305                                 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
306                                 $talk  = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
307
308                                 if( $this->mCurrentRow->am_customised ){
309                                         $title = $this->mSkin->linkKnown( $title, $wgLang->lcfirst( $value ) );
310                                 } else {
311                                         $title = $this->mSkin->link(
312                                                 $title,
313                                                 $wgLang->lcfirst( $value ),
314                                                 array(),
315                                                 array(),
316                                                 array( 'broken' )
317                                         );
318                                 }
319                                 if ( $this->mCurrentRow->am_talk_exists ) {
320                                         $talk = $this->mSkin->linkKnown( $talk , $this->talk );
321                                 } else {
322                                         $talk = $this->mSkin->link(
323                                                 $talk,
324                                                 $this->talk,
325                                                 array(),
326                                                 array(),
327                                                 array( 'broken' )
328                                         );
329                                 }
330                                 return $title . ' (' . $talk . ')';
331
332                         case 'am_default' :
333                                 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
334                         case 'am_actual' :
335                                 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
336                 }
337                 return '';
338         }
339
340         function formatRow( $row ){
341                 // Do all the normal stuff
342                 $s = parent::formatRow( $row );
343
344                 // But if there's a customised message, add that too.
345                 if( $row->am_customised ){
346                         $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
347                         $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
348                         if ( $formatted == '' ) {
349                                 $formatted = '&#160;';
350                         }
351                         $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
352                                 . "</tr>\n";
353                 }
354                 return $s;
355         }
356
357         function getRowAttrs( $row, $isSecond = false ){
358                 $arr = array();
359                 global $wgLang;
360                 if( $row->am_customised ){
361                         $arr['class'] = 'allmessages-customised';
362                 }
363                 if( !$isSecond ){
364                         $arr['id'] = Sanitizer::escapeId( 'msg_' . $wgLang->lcfirst( $row->am_title ) );
365                 }
366                 return $arr;
367         }
368
369         function getCellAttrs( $field, $value ){
370                 if( $this->mCurrentRow->am_customised && $field == 'am_title' ){
371                         return array( 'rowspan' => '2', 'class' => $field );
372                 } else {
373                         return array( 'class' => $field );
374                 }
375         }
376
377         // This is not actually used, as getStartBody is overridden above
378         function getFieldNames() {
379                 return array(
380                         'am_title' => wfMsg( 'allmessagesname' ),
381                         'am_default' => wfMsg( 'allmessagesdefault' )
382                 );
383         }
384         function getTitle() {
385                 return SpecialPage::getTitleFor( 'Allmessages', false );
386         }
387         function isFieldSortable( $x ){
388                 return false;
389         }
390         function getDefaultSort(){
391                 return '';
392         }
393         function getQueryInfo(){
394                 return '';
395         }
396 }
397