* Introduced a new system for localisation caching. The system is based around fast...
[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 = Language::getMessagesFor( 'en' );
33 ksort( $sortedArray );
34
35 $messages = array();
36 foreach( $sortedArray as $key => $value ) {
37 $messages[$key]['enmsg'] = $value;
38 $messages[$key]['statmsg'] = wfMsgReal( $key, array(), false, false, false );
39 $messages[$key]['msg'] = wfMsgNoTrans( $key );
40 $sortedArray[$key] = NULL; // trade bytes from $sortedArray to this
41
42 }
43 unset($sortedArray); // trade bytes from $sortedArray to this
44
45 wfProfileOut( __METHOD__ . '-setup' );
46
47 wfProfileIn( __METHOD__ . '-output' );
48 $wgOut->addScriptFile( 'allmessages.js' );
49 $title = SpecialPage::getTitleFor( 'Allmessages' );
50 if ( $ot == 'php' ) {
51 $navText .= wfAllMessagesMakePhp( $messages );
52 $wgOut->addHTML( $wgLang->pipeList( array(
53 'PHP',
54 '<a href="' . $title->escapeLocalUrl( array( 'ot' => 'html' ) ) . '">HTML</a>',
55 '<a href="' . $title->escapeLocalUrl( array( '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="' . $title->escapeLocalUrl( array( 'ot' => 'php' ) ) . '">PHP</a>',
65 'HTML',
66 '<a href="' . $title->escapeLocalUrl( array( '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 $linkText = "<span id=\"sp-allmessages-i-$i\">" . htmlspecialchars( $key ) . '</span>';
190
191 if( array_key_exists( $title, $pageExists[NS_MEDIAWIKI] ) ) {
192 // FIXME: the span should be taken care of in $customAttribs, shouldn't it?
193 $pageLink = $sk->linkKnown(
194 $titleObj,
195 $linkText
196 );
197 } else {
198 $pageLink = $sk->link(
199 $titleObj,
200 $linkText,
201 array(),
202 array(),
203 array( 'broken' )
204 );
205 }
206 if( array_key_exists( $title, $pageExists[NS_MEDIAWIKI_TALK] ) ) {
207 $talkLink = $sk->linkKnown( $talkPage, htmlspecialchars( $talk ) );
208 } else {
209 $talkLink = $sk->link(
210 $talkPage,
211 htmlspecialchars( $talk ),
212 array(),
213 array(),
214 array( 'broken' )
215 );
216 }
217
218 $anchor = 'msg_' . htmlspecialchars( strtolower( $title ) );
219 $anchor = "<a id=\"$anchor\" name=\"$anchor\"></a>";
220
221 if( $changed ) {
222 $txt .= "
223 <tr class=\"orig\" id=\"sp-allmessages-r1-$i\">
224 <td rowspan=\"2\">
225 $anchor$pageLink<br />$talkLink
226 </td><td>
227 $message
228 </td>
229 </tr><tr class=\"new\" id=\"sp-allmessages-r2-$i\">
230 <td>
231 $mw
232 </td>
233 </tr>";
234 } else {
235 $txt .= "
236 <tr class=\"def\" id=\"sp-allmessages-r1-$i\">
237 <td>
238 $anchor$pageLink<br />$talkLink
239 </td><td>
240 $mw
241 </td>
242 </tr>";
243 }
244 $messages[$key] = NULL; // trade bytes
245 $i++;
246 }
247 $txt .= '</table>';
248 wfProfileOut( __METHOD__ . '-output' );
249
250 wfProfileOut( __METHOD__ );
251 return $txt;
252 }