SpecialAllmessages.php 6.8 KB

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