]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialAllmessages.php
MediaWiki 1.15.0
[autoinstallsdev/mediawiki.git] / includes / specials / SpecialAllmessages.php
1 <?php
2 /**
3  * Use this special page to get a list of the MediaWiki system messages.
4  * @file
5  * @ingroup SpecialPage
6  */
7
8 /**
9  * Constructor.
10  */
11 function wfSpecialAllmessages() {
12         global $wgOut, $wgRequest, $wgMessageCache, $wgTitle;
13         global $wgUseDatabaseMessages, $wgLang;
14
15         # The page isn't much use if the MediaWiki namespace is not being used
16         if( !$wgUseDatabaseMessages ) {
17                 $wgOut->addWikiMsg( 'allmessagesnotsupportedDB' );
18                 return;
19         }
20
21         wfProfileIn( __METHOD__ );
22
23         wfProfileIn( __METHOD__ . '-setup' );
24         $ot = $wgRequest->getText( 'ot' );
25
26         $navText = wfMsg( 'allmessagestext' );
27
28         # Make sure all extension messages are available
29
30         $wgMessageCache->loadAllMessages();
31
32         $sortedArray = array_merge( Language::getMessagesFor( 'en' ), 
33                 $wgMessageCache->getExtensionMessagesFor( 'en' ) );
34         ksort( $sortedArray );
35
36         $messages = array();
37         foreach( $sortedArray as $key => $value ) {
38                 $messages[$key]['enmsg'] = $value;
39                 $messages[$key]['statmsg'] = wfMsgReal( $key, array(), false, false, false );
40                 $messages[$key]['msg'] = wfMsgNoTrans( $key );
41                 $sortedArray[$key] = NULL; // trade bytes from $sortedArray to this
42                 
43         }
44         unset($sortedArray); // trade bytes from $sortedArray to this
45
46         wfProfileOut( __METHOD__ . '-setup' );
47
48         wfProfileIn( __METHOD__ . '-output' );
49         $wgOut->addScriptFile( 'allmessages.js' );
50         if ( $ot == 'php' ) {
51                 $navText .= wfAllMessagesMakePhp( $messages );
52                 $wgOut->addHTML( $wgLang->pipeList( array(
53                         'PHP',
54                         '<a href="' . $wgTitle->escapeLocalUrl( 'ot=html' ) . '">HTML</a>',
55                         '<a href="' . $wgTitle->escapeLocalUrl( 'ot=xml' ) . '">XML</a>' .
56                         '<pre>' . htmlspecialchars( $navText ) . '</pre>'
57                 ) ) );
58         } else if ( $ot == 'xml' ) {
59                 $wgOut->disable();
60                 header( 'Content-type: text/xml' );
61                 echo wfAllMessagesMakeXml( $messages );
62         } else {
63                 $wgOut->addHTML( $wgLang->pipeList( array(
64                         '<a href="' . $wgTitle->escapeLocalUrl( 'ot=php' ) . '">PHP</a>',
65                         'HTML',
66                         '<a href="' . $wgTitle->escapeLocalUrl( 'ot=xml' ) . '">XML</a>'
67                 ) ) );
68                 $wgOut->addWikiText( $navText );
69                 $wgOut->addHTML( wfAllMessagesMakeHTMLText( $messages ) );
70         }
71         wfProfileOut( __METHOD__ . '-output' );
72
73         wfProfileOut( __METHOD__ );
74 }
75
76 function wfAllMessagesMakeXml( &$messages ) {
77         global $wgLang;
78         $lang = $wgLang->getCode();
79         $txt = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
80         $txt .= "<messages lang=\"$lang\">\n";
81         foreach( $messages as $key => $m ) {
82                 $txt .= "\t" . Xml::element( 'message', array( 'name' => $key ), $m['msg'] ) . "\n";
83                 $messages[$key] = NULL; // trade bytes
84         }
85         $txt .= "</messages>";
86         return $txt;
87 }
88
89 /**
90  * Create the messages array, formatted in PHP to copy to language files.
91  * @param $messages Messages array.
92  * @return The PHP messages array.
93  * @todo Make suitable for language files.
94  */
95 function wfAllMessagesMakePhp( &$messages ) {
96         global $wgLang;
97         $txt = "\n\n\$messages = array(\n";
98         foreach( $messages as $key => $m ) {
99                 if( $wgLang->getCode() != 'en' && $m['msg'] == $m['enmsg'] ) {
100                         continue;
101                 } else if ( wfEmptyMsg( $key, $m['msg'] ) ) {
102                         $m['msg'] = '';
103                         $comment = ' #empty';
104                 } else {
105                         $comment = '';
106                 }
107                 $txt .= "'$key' => '" . preg_replace( '/(?<!\\\\)\'/', "\'", $m['msg']) . "',$comment\n";
108                 $messages[$key] = NULL; // trade bytes
109         }
110         $txt .= ');';
111         return $txt;
112 }
113
114 /**
115  * Create a list of messages, formatted in HTML as a list of messages and values and showing differences between the default language file message and the message in MediaWiki: namespace.
116  * @param $messages Messages array.
117  * @return The HTML list of messages.
118  */
119 function wfAllMessagesMakeHTMLText( &$messages ) {
120         global $wgLang, $wgContLang, $wgUser;
121         wfProfileIn( __METHOD__ );
122
123         $sk = $wgUser->getSkin();
124         $talk = wfMsg( 'talkpagelinktext' );
125
126         $input = Xml::element( 'input', array(
127                 'type'    => 'text',
128                 'id'      => 'allmessagesinput',
129                 'onkeyup' => 'allmessagesfilter()'
130         ), '' );
131         $checkbox = Xml::element( 'input', array(
132                 'type'    => 'button',
133                 'value'   => wfMsgHtml( 'allmessagesmodified' ),
134                 'id'      => 'allmessagescheckbox',
135                 'onclick' => 'allmessagesmodified()'
136         ), '' );
137
138         $txt = '<span id="allmessagesfilter" style="display: none;">' . wfMsgHtml( 'allmessagesfilter' ) . 
139                 " {$input}{$checkbox} " . '</span>';
140
141         $txt .= '
142 <table border="1" cellspacing="0" width="100%" id="allmessagestable">
143         <tr>
144                 <th rowspan="2">' . wfMsgHtml( 'allmessagesname' ) . '</th>
145                 <th>' . wfMsgHtml( 'allmessagesdefault' ) . '</th>
146         </tr>
147         <tr>
148                 <th>' . wfMsgHtml( 'allmessagescurrent' ) . '</th>
149         </tr>';
150
151         wfProfileIn( __METHOD__ . "-check" );
152
153         # This is a nasty hack to avoid doing independent existence checks
154         # without sending the links and table through the slow wiki parser.
155         $pageExists = array(
156                 NS_MEDIAWIKI => array(),
157                 NS_MEDIAWIKI_TALK => array()
158         );
159         $dbr = wfGetDB( DB_SLAVE );
160         $res = $dbr->select( 'page',
161                 array( 'page_namespace', 'page_title' ),
162                 array( 'page_namespace' => array(NS_MEDIAWIKI,NS_MEDIAWIKI_TALK) ),
163                 __METHOD__,
164                 array( 'USE INDEX' => 'name_title' )
165         );
166         while( $s = $dbr->fetchObject( $res ) ) {
167                 $pageExists[$s->page_namespace][$s->page_title] = 1;
168         }
169         $dbr->freeResult( $res );
170         wfProfileOut( __METHOD__ . "-check" );
171
172         wfProfileIn( __METHOD__ . "-output" );
173
174         $i = 0;
175
176         foreach( $messages as $key => $m ) {
177                 $title = $wgLang->ucfirst( $key );
178                 if( $wgLang->getCode() != $wgContLang->getCode() ) {
179                         $title .= '/' . $wgLang->getCode();
180                 }
181
182                 $titleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
183                 $talkPage = Title::makeTitle( NS_MEDIAWIKI_TALK, $title );
184
185                 $changed = ( $m['statmsg'] != $m['msg'] );
186                 $message = htmlspecialchars( $m['statmsg'] );
187                 $mw = htmlspecialchars( $m['msg'] );
188
189                 if( array_key_exists( $title, $pageExists[NS_MEDIAWIKI] ) ) {
190                         $pageLink = $sk->makeKnownLinkObj( $titleObj, "<span id=\"sp-allmessages-i-$i\">" . 
191                                 htmlspecialchars( $key ) . '</span>' );
192                 } else {
193                         $pageLink = $sk->makeBrokenLinkObj( $titleObj, "<span id=\"sp-allmessages-i-$i\">" . 
194                                 htmlspecialchars( $key ) . '</span>' );
195                 }
196                 if( array_key_exists( $title, $pageExists[NS_MEDIAWIKI_TALK] ) ) {
197                         $talkLink = $sk->makeKnownLinkObj( $talkPage, htmlspecialchars( $talk ) );
198                 } else {
199                         $talkLink = $sk->makeBrokenLinkObj( $talkPage, htmlspecialchars( $talk ) );
200                 }
201
202                 $anchor = 'msg_' . htmlspecialchars( strtolower( $title ) );
203                 $anchor = "<a id=\"$anchor\" name=\"$anchor\"></a>";
204
205                 if( $changed ) {
206                         $txt .= "
207                                 <tr class=\"orig\" id=\"sp-allmessages-r1-$i\">
208                                         <td rowspan=\"2\">
209                                                 $anchor$pageLink<br />$talkLink
210                                         </td><td>
211                                 $message
212                                         </td>
213                                 </tr><tr class=\"new\" id=\"sp-allmessages-r2-$i\">
214                                         <td>
215                                 $mw
216                                         </td>
217                                 </tr>";
218                 } else {
219                         $txt .= "
220                                 <tr class=\"def\" id=\"sp-allmessages-r1-$i\">
221                                         <td>
222                                                 $anchor$pageLink<br />$talkLink
223                                         </td><td>
224                                 $mw
225                                         </td>
226                                 </tr>";
227                 }
228                 $messages[$key] = NULL; // trade bytes
229                 $i++;
230         }
231         $txt .= '</table>';
232         wfProfileOut( __METHOD__ . '-output' );
233
234         wfProfileOut( __METHOD__ );
235         return $txt;
236 }