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