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