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