Escaping fixes
[lhc/web/wiklou.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;
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 $title = SpecialPage::getTitleFor( 'Allmessages' );
51 if ( $ot == 'php' ) {
52 $navText .= wfAllMessagesMakePhp( $messages );
53 $wgOut->addHTML( $wgLang->pipeList( array(
54 'PHP',
55 '<a href="' . $title->escapeLocalUrl( 'ot=html' ) . '">HTML</a>',
56 '<a href="' . $title->escapeLocalUrl( 'ot=xml' ) . '">XML</a>' .
57 '<pre>' . htmlspecialchars( $navText ) . '</pre>'
58 ) ) );
59 } else if ( $ot == 'xml' ) {
60 $wgOut->disable();
61 header( 'Content-type: text/xml' );
62 echo wfAllMessagesMakeXml( $messages );
63 } else {
64 $wgOut->addHTML( $wgLang->pipeList( array(
65 '<a href="' . $title->escapeLocalUrl( 'ot=php' ) . '">PHP</a>',
66 'HTML',
67 '<a href="' . $title->escapeLocalUrl( 'ot=xml' ) . '">XML</a>'
68 ) ) );
69 $wgOut->addWikiText( $navText );
70 $wgOut->addHTML( wfAllMessagesMakeHTMLText( $messages ) );
71 }
72 wfProfileOut( __METHOD__ . '-output' );
73
74 wfProfileOut( __METHOD__ );
75 }
76
77 function wfAllMessagesMakeXml( &$messages ) {
78 global $wgLang;
79 $lang = $wgLang->getCode();
80 $txt = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
81 $txt .= "<messages lang=\"$lang\">\n";
82 foreach( $messages as $key => $m ) {
83 $txt .= "\t" . Xml::element( 'message', array( 'name' => $key ), $m['msg'] ) . "\n";
84 $messages[$key] = NULL; // trade bytes
85 }
86 $txt .= "</messages>";
87 return $txt;
88 }
89
90 /**
91 * Create the messages array, formatted in PHP to copy to language files.
92 * @param $messages Messages array.
93 * @return The PHP messages array.
94 * @todo Make suitable for language files.
95 */
96 function wfAllMessagesMakePhp( &$messages ) {
97 global $wgLang;
98 $txt = "\n\n\$messages = array(\n";
99 foreach( $messages as $key => $m ) {
100 if( $wgLang->getCode() != 'en' && $m['msg'] == $m['enmsg'] ) {
101 continue;
102 } else if ( wfEmptyMsg( $key, $m['msg'] ) ) {
103 $m['msg'] = '';
104 $comment = ' #empty';
105 } else {
106 $comment = '';
107 }
108 $txt .= "'$key' => '" . preg_replace( '/(?<!\\\\)\'/', "\'", $m['msg']) . "',$comment\n";
109 $messages[$key] = NULL; // trade bytes
110 }
111 $txt .= ');';
112 return $txt;
113 }
114
115 /**
116 * 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.
117 * @param $messages Messages array.
118 * @return The HTML list of messages.
119 */
120 function wfAllMessagesMakeHTMLText( &$messages ) {
121 global $wgLang, $wgContLang, $wgUser;
122 wfProfileIn( __METHOD__ );
123
124 $sk = $wgUser->getSkin();
125 $talk = wfMsg( 'talkpagelinktext' );
126
127 $input = Xml::element( 'input', array(
128 'type' => 'text',
129 'id' => 'allmessagesinput',
130 'onkeyup' => 'allmessagesfilter()'
131 ), '' );
132 $checkbox = Xml::element( 'input', array(
133 'type' => 'button',
134 'value' => wfMsgHtml( 'allmessagesmodified' ),
135 'id' => 'allmessagescheckbox',
136 'onclick' => 'allmessagesmodified()'
137 ), '' );
138
139 $txt = '<span id="allmessagesfilter" style="display: none;">' . wfMsgHtml( 'allmessagesfilter' ) .
140 " {$input}{$checkbox} " . '</span>';
141
142 $txt .= '
143 <table border="1" cellspacing="0" width="100%" id="allmessagestable">
144 <tr>
145 <th rowspan="2">' . wfMsgHtml( 'allmessagesname' ) . '</th>
146 <th>' . wfMsgHtml( 'allmessagesdefault' ) . '</th>
147 </tr>
148 <tr>
149 <th>' . wfMsgHtml( 'allmessagescurrent' ) . '</th>
150 </tr>';
151
152 wfProfileIn( __METHOD__ . "-check" );
153
154 # This is a nasty hack to avoid doing independent existence checks
155 # without sending the links and table through the slow wiki parser.
156 $pageExists = array(
157 NS_MEDIAWIKI => array(),
158 NS_MEDIAWIKI_TALK => array()
159 );
160 $dbr = wfGetDB( DB_SLAVE );
161 $res = $dbr->select( 'page',
162 array( 'page_namespace', 'page_title' ),
163 array( 'page_namespace' => array(NS_MEDIAWIKI,NS_MEDIAWIKI_TALK) ),
164 __METHOD__,
165 array( 'USE INDEX' => 'name_title' )
166 );
167 while( $s = $dbr->fetchObject( $res ) ) {
168 $pageExists[$s->page_namespace][$s->page_title] = 1;
169 }
170 $dbr->freeResult( $res );
171 wfProfileOut( __METHOD__ . "-check" );
172
173 wfProfileIn( __METHOD__ . "-output" );
174
175 $i = 0;
176
177 foreach( $messages as $key => $m ) {
178 $title = $wgLang->ucfirst( $key );
179 if( $wgLang->getCode() != $wgContLang->getCode() ) {
180 $title .= '/' . $wgLang->getCode();
181 }
182
183 $titleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
184 $talkPage = Title::makeTitle( NS_MEDIAWIKI_TALK, $title );
185
186 $changed = ( $m['statmsg'] != $m['msg'] );
187 $message = htmlspecialchars( $m['statmsg'] );
188 $mw = htmlspecialchars( $m['msg'] );
189
190 if( array_key_exists( $title, $pageExists[NS_MEDIAWIKI] ) ) {
191 $pageLink = $sk->makeKnownLinkObj( $titleObj, "<span id=\"sp-allmessages-i-$i\">" .
192 htmlspecialchars( $key ) . '</span>' );
193 } else {
194 $pageLink = $sk->makeBrokenLinkObj( $titleObj, "<span id=\"sp-allmessages-i-$i\">" .
195 htmlspecialchars( $key ) . '</span>' );
196 }
197 if( array_key_exists( $title, $pageExists[NS_MEDIAWIKI_TALK] ) ) {
198 $talkLink = $sk->makeKnownLinkObj( $talkPage, htmlspecialchars( $talk ) );
199 } else {
200 $talkLink = $sk->makeBrokenLinkObj( $talkPage, htmlspecialchars( $talk ) );
201 }
202
203 $anchor = 'msg_' . htmlspecialchars( strtolower( $title ) );
204 $anchor = "<a id=\"$anchor\" name=\"$anchor\"></a>";
205
206 if( $changed ) {
207 $txt .= "
208 <tr class=\"orig\" id=\"sp-allmessages-r1-$i\">
209 <td rowspan=\"2\">
210 $anchor$pageLink<br />$talkLink
211 </td><td>
212 $message
213 </td>
214 </tr><tr class=\"new\" id=\"sp-allmessages-r2-$i\">
215 <td>
216 $mw
217 </td>
218 </tr>";
219 } else {
220 $txt .= "
221 <tr class=\"def\" id=\"sp-allmessages-r1-$i\">
222 <td>
223 $anchor$pageLink<br />$talkLink
224 </td><td>
225 $mw
226 </td>
227 </tr>";
228 }
229 $messages[$key] = NULL; // trade bytes
230 $i++;
231 }
232 $txt .= '</table>';
233 wfProfileOut( __METHOD__ . '-output' );
234
235 wfProfileOut( __METHOD__ );
236 return $txt;
237 }