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