(bug 5018) Anchors for each message in Special:Allmessages
[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, $wgAllMessagesEn, $wgRequest, $wgMessageCache, $wgTitle;
13 global $wgUseDatabaseMessages;
14
15 if(!$wgUseDatabaseMessages) {
16 $wgOut->addHTML(wfMsg('allmessagesnotsupportedDB'));
17 return;
18 }
19
20 $fname = "wfSpecialAllMessages";
21 wfProfileIn( $fname );
22
23 wfProfileIn( "$fname-setup");
24 $ot = $wgRequest->getText( 'ot' );
25
26 $navText = wfMsg( 'allmessagestext' );
27
28
29 $first = true;
30 $sortedArray = array_merge( $wgAllMessagesEn, $wgMessageCache->mExtensionMessages );
31 ksort( $sortedArray );
32 $messages = array();
33 $wgMessageCache->disableTransform();
34
35 foreach ( $sortedArray as $key => $enMsg ) {
36 $messages[$key]['enmsg'] = $enMsg;
37 $messages[$key]['statmsg'] = wfMsgNoDb( $key );
38 $messages[$key]['msg'] = wfMsg ( $key );
39 }
40
41 $wgMessageCache->enableTransform();
42 wfProfileOut( "$fname-setup" );
43
44 wfProfileIn( "$fname-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( "$fname-output" );
54
55 wfProfileOut( $fname );
56 }
57
58 /**
59 *
60 */
61 function makePhp($messages) {
62 global $wgLanguageCode;
63 $txt = "\n\n".'$wgAllMessages'.ucfirst($wgLanguageCode).' = array('."\n";
64 foreach( $messages as $key => $m ) {
65 if(strtolower($wgLanguageCode) != 'en' and $m['msg'] == $m['enmsg'] ) {
66 //if (strstr($m['msg'],"\n")) {
67 // $txt.='/* ';
68 // $comment=' */';
69 //} else {
70 // $txt .= '#';
71 // $comment = '';
72 //}
73 continue;
74 } elseif ($m['msg'] == '&lt;'.$key.'&gt;'){
75 $m['msg'] = '';
76 $comment = ' #empty';
77 } else {
78 $comment = '';
79 }
80 $txt .= "'$key' => '" . preg_replace( "/(?<!\\\\)'/", "\'", $m['msg']) . "',$comment\n";
81 }
82 $txt .= ');';
83 return $txt;
84 }
85
86 /**
87 *
88 */
89 function makeHTMLText( $messages ) {
90 global $wgLang, $wgUser, $wgLanguageCode, $wgContLanguageCode;
91 $fname = "makeHTMLText";
92 wfProfileIn( $fname );
93
94 $sk =& $wgUser->getSkin();
95 $talk = $wgLang->getNsText( NS_TALK );
96 $mwnspace = $wgLang->getNsText( NS_MEDIAWIKI );
97 $mwtalk = $wgLang->getNsText( NS_MEDIAWIKI_TALK );
98 $txt = "
99 <table border='1' cellspacing='0' width='100%' id='allmessagestable'>
100 <tr>
101 <th rowspan='2'>" . wfMsgHtml('allmessagesname') . "</th>
102 <th>" . wfMsgHtml('allmessagesdefault') . "</th>
103 </tr>
104 <tr>
105 <th>" . wfMsgHtml('allmessagescurrent') . "</th>
106 </tr>";
107
108 wfProfileIn( "$fname-check" );
109 # This is a nasty hack to avoid doing independent existence checks
110 # without sending the links and table through the slow wiki parser.
111 $pageExists = array(
112 NS_MEDIAWIKI => array(),
113 NS_MEDIAWIKI_TALK => array()
114 );
115 $dbr =& wfGetDB( DB_SLAVE );
116 $page = $dbr->tableName( 'page' );
117 $sql = "SELECT page_namespace,page_title FROM $page WHERE page_namespace IN (" . NS_MEDIAWIKI . ", " . NS_MEDIAWIKI_TALK . ")";
118 $res = $dbr->query( $sql );
119 while( $s = $dbr->fetchObject( $res ) ) {
120 $pageExists[$s->page_namespace][$s->page_title] = true;
121 }
122 $dbr->freeResult( $res );
123 wfProfileOut( "$fname-check" );
124
125 wfProfileIn( "$fname-output" );
126
127 foreach( $messages as $key => $m ) {
128
129 $title = $wgLang->ucfirst( $key );
130 if($wgLanguageCode != $wgContLanguageCode)
131 $title.="/$wgLanguageCode";
132
133 $titleObj =& Title::makeTitle( NS_MEDIAWIKI, $title );
134 $talkPage =& Title::makeTitle( NS_MEDIAWIKI_TALK, $title );
135
136 $changed = ($m['statmsg'] != $m['msg']);
137 $message = htmlspecialchars( $m['statmsg'] );
138 $mw = htmlspecialchars( $m['msg'] );
139
140 #$pageLink = $sk->makeLinkObj( $titleObj, htmlspecialchars( $key ) );
141 #$talkLink = $sk->makeLinkObj( $talkPage, htmlspecialchars( $talk ) );
142 if( isset( $pageExists[NS_MEDIAWIKI][$title] ) ) {
143 $pageLink = $sk->makeKnownLinkObj( $titleObj, htmlspecialchars( $key ) );
144 } else {
145 $pageLink = $sk->makeBrokenLinkObj( $titleObj, htmlspecialchars( $key ) );
146 }
147 if( isset( $pageExists[NS_MEDIAWIKI_TALK][$title] ) ) {
148 $talkLink = $sk->makeKnownLinkObj( $talkPage, htmlspecialchars( $talk ) );
149 } else {
150 $talkLink = $sk->makeBrokenLinkObj( $talkPage, htmlspecialchars( $talk ) );
151 }
152
153 $anchor = strtolower( $title );
154 $anchor = "<a id=\"$anchor\" name=\"$anchor\"></a>";
155
156 if($changed) {
157
158 $txt .=
159 "<tr class='orig'>
160 <td rowspan='2'>
161 $anchor$pageLink<br />$talkLink
162 </td><td>
163 $message
164 </td>
165 </tr><tr class='new'>
166 <td>
167 $mw
168 </td>
169 </tr>";
170 } else {
171
172 $txt .=
173 "<tr class='def'>
174 <td>
175 $anchor$pageLink<br />$talkLink
176 </td><td>
177 $mw
178 </td>
179 </tr>";
180
181 }
182 }
183 $txt .= "</table>";
184 wfProfileOut( "$fname-output" );
185
186 wfProfileOut( $fname );
187 return $txt;
188 }
189
190 ?>