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