PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/LiveTranslate/LiveTranslate.hooks.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 352 lines | 195 code | 45 blank | 112 comment | 23 complexity | d1124b482784e95e6bb1d19c8b8e9c25 MD5 | raw file
  1. <?php
  2. /**
  3. * Static class for hooks handled by the Live Translate extension.
  4. *
  5. * @since 0.1
  6. *
  7. * @file LiveTranslate.hooks.php
  8. * @ingroup LiveTranslate
  9. *
  10. * @author Jeroen De Dauw
  11. */
  12. final class LiveTranslateHooks {
  13. /**
  14. * Hook to insert things into article headers.
  15. *
  16. * @since 0.1
  17. *
  18. * @param Article &$article
  19. * @param boolean $outputDone
  20. * @param boolean $useParserCache
  21. *
  22. * @return true
  23. */
  24. public static function onArticleViewHeader( Article &$article, &$outputDone, &$useParserCache ) {
  25. global $egLiveTranslateLanguages;
  26. $egLiveTranslateLanguages = array_unique( $egLiveTranslateLanguages );
  27. $title = $article->getTitle();
  28. $currentLang = LiveTranslateFunctions::getCurrentLang( $title );
  29. if ( in_array( $title->getFullText(), LiveTranslateFunctions::getLocalMemoryNames() ) ) {
  30. self::displayDictionaryPage( $article, $title );
  31. $outputDone = true; // The translations themselves should not be shown.
  32. }
  33. elseif (
  34. LiveTranslateFunctions::hasTranslationService()
  35. && $article->exists()
  36. && ( count( $egLiveTranslateLanguages ) > 1 || ( count( $egLiveTranslateLanguages ) == 1 && $egLiveTranslateLanguages[0] != $currentLang ) ) ) {
  37. global $wgParser;
  38. $po = $wgParser->getOutput();
  39. $magicWords = isset( $po->mLTMagicWords ) ? $po->mLTMagicWords : array();
  40. if ( !in_array( 'LT_NOTRANSLATIONCONTROL', $magicWords ) ) {
  41. global $egLTNSWithTranslationControl, $egLTUnknownNSShowControl;
  42. $ns = $title->getNamespace();
  43. if ( in_array( 'LT_SHOWTRANSLATIONCONTROL', $magicWords )
  44. || ( array_key_exists( $ns, $egLTNSWithTranslationControl ) && $egLTNSWithTranslationControl[$ns] )
  45. || ( !array_key_exists( $ns, $egLTNSWithTranslationControl ) && $egLTUnknownNSShowControl )
  46. ) {
  47. self::displayTranslationControl( $currentLang );
  48. }
  49. }
  50. }
  51. return true;
  52. }
  53. /**
  54. * Displays some shorts statistics about the dictionary page.
  55. *
  56. * @since 0.4
  57. *
  58. * @param Article $article
  59. * @param Title $title
  60. */
  61. protected static function displayDictionaryPage( Article &$article, Title $title ) {
  62. global $wgOut, $wgLang, $wgUser, $egLiveTranslateLanguages;
  63. $dbr = wfGetDb( DB_SLAVE );
  64. $res = $dbr->select(
  65. 'live_translate_memories',
  66. array(
  67. 'memory_lang_count',
  68. 'memory_tu_count'
  69. ),
  70. array( 'memory_location' => $title->getFullText() ),
  71. array( 'LIMIT' => 1 )
  72. );
  73. foreach ( $res as $tm ) {
  74. break;
  75. }
  76. if ( $tm->memory_tu_count == 0 ) {
  77. $wgOut->addWikiMsg( 'livetranslate-dictionary-empty' );
  78. }
  79. else {
  80. $wgOut->addWikiMsg(
  81. 'livetranslate-dictionary-count',
  82. $wgLang->formatNum( $tm->memory_tu_count ) ,
  83. $wgLang->formatNum( $tm->memory_lang_count )
  84. );
  85. /*
  86. $notAllowedLanguages = array();
  87. foreach ( $tus[0]->getVariants() as $languageCode => $translations ) {
  88. $languageCode = strtolower( $languageCode );
  89. $mappings = LiveTranslateFunctions::getInputLangMapping();
  90. if ( array_key_exists( $languageCode, $mappings ) ) {
  91. $languageCode = $mappings[$languageCode];
  92. }
  93. if ( !in_array( $languageCode, $egLiveTranslateLanguages ) ) {
  94. $notAllowedLanguages[] = $languageCode;
  95. }
  96. }
  97. if ( count( $notAllowedLanguages ) > 0 ) {
  98. $languages = Language::getLanguageNames( false );
  99. foreach ( $notAllowedLanguages as &$notAllowedLang ) {
  100. if ( array_key_exists( $notAllowedLang, $languages ) ) {
  101. $notAllowedLang = $languages[$notAllowedLang];
  102. }
  103. }
  104. $wgOut->addHTML(
  105. Html::element(
  106. 'span',
  107. array( 'style' => 'color:darkred' ),
  108. wfMsgExt( 'livetranslate-dictionary-unallowed-langs', 'parsemag', $wgLang->listToText( $notAllowedLanguages ), count( $notAllowedLanguages ) )
  109. )
  110. );
  111. }
  112. */
  113. }
  114. if ( $wgUser->isAllowed( 'managetms' ) ) {
  115. $wgOut->addHTML(
  116. Html::element(
  117. 'a',
  118. array( 'href' => Title::newFromText( 'Special:LiveTranslate' )->getInternalURL() ),
  119. wfMsg( 'livetranslate-dictionary-goto-edit' )
  120. )
  121. );
  122. }
  123. }
  124. /**
  125. * Outputs the Live Translate translation control.
  126. *
  127. * @since 0.4
  128. *
  129. * @param string $currentLang
  130. */
  131. protected static function displayTranslationControl( $currentLang ) {
  132. global $wgOut;
  133. $langs = array();
  134. foreach ( LiveTranslateFunctions::getLanguages( $currentLang ) as $label => $code ) {
  135. $langs[] = "$code|$label";
  136. }
  137. $wgOut->addHTML(
  138. Html::rawElement(
  139. 'div',
  140. array(
  141. 'id' => 'livetranslatediv',
  142. 'sourcelang' => $currentLang,
  143. 'languages' => implode( '||', $langs )
  144. )
  145. )
  146. );
  147. LiveTranslateFunctions::loadJs();
  148. }
  149. /**
  150. * Schema update to set up the needed database tables.
  151. *
  152. * @since 0.1
  153. *
  154. * @param DatabaseUpdater $updater
  155. *
  156. * @return true
  157. */
  158. public static function onSchemaUpdate( /* DatabaseUpdater */ $updater = null ) {
  159. global $wgDBtype, $egLiveTranslateIP;
  160. if ( $wgDBtype == 'mysql' ) {
  161. // Set up the current schema.
  162. if ( $updater === null ) {
  163. global $wgExtNewTables, $wgExtNewIndexes, $wgExtNewFields;
  164. $wgExtNewTables[] = array(
  165. 'live_translate',
  166. $egLiveTranslateIP . '/LiveTranslate.sql',
  167. true
  168. );
  169. $wgExtNewTables[] = array(
  170. 'live_translate_memories',
  171. $egLiveTranslateIP . '/LiveTranslate.sql',
  172. true
  173. );
  174. $wgExtNewIndexes[] = array(
  175. 'live_translate',
  176. 'word_translation',
  177. $egLiveTranslateIP . '/sql/LT_IndexWordTranslation.sql',
  178. true
  179. );
  180. $wgExtNewFields[] = array(
  181. 'live_translate',
  182. 'memory_id',
  183. $egLiveTranslateIP . '/sql/LT_addTMField.sql'
  184. );
  185. $wgExtNewFields[] = array(
  186. 'live_translate_memories',
  187. 'memory_version_hash',
  188. $egLiveTranslateIP . '/sql/LT_addTMHashField.sql'
  189. );
  190. }
  191. else {
  192. $updater->addExtensionUpdate( array(
  193. 'addTable',
  194. 'live_translate',
  195. $egLiveTranslateIP . '/LiveTranslate.sql',
  196. true
  197. ) );
  198. $updater->addExtensionUpdate( array(
  199. 'addTable',
  200. 'live_translate_memories',
  201. $egLiveTranslateIP . '/LiveTranslate.sql',
  202. true
  203. ) );
  204. $updater->addExtensionUpdate( array(
  205. 'addIndex',
  206. 'live_translate',
  207. 'word_translation',
  208. $egLiveTranslateIP . '/sql/LT_IndexWordTranslation.sql',
  209. true
  210. ) );
  211. $updater->addExtensionUpdate( array(
  212. 'addField',
  213. 'live_translate',
  214. 'memory_id',
  215. $egLiveTranslateIP . '/sql/LT_addTMField.sql',
  216. true
  217. ) );
  218. $updater->addExtensionUpdate( array(
  219. 'addField',
  220. 'live_translate_memories',
  221. 'memory_version_hash',
  222. $egLiveTranslateIP . '/sql/LT_addTMHashField.sql',
  223. true
  224. ) );
  225. }
  226. }
  227. return true;
  228. }
  229. /**
  230. * Handles edits to the dictionary page to save the translations into the db.
  231. *
  232. * @since 0.1
  233. *
  234. * @return true
  235. */
  236. public static function onArticleSaveComplete( &$article, &$user, $text, $summary,
  237. $minoredit, $watchthis, $sectionanchor, &$flags, $revision, &$status, $baseRevId, &$redirect = null ) {
  238. $title = $article->getTitle();
  239. // FIXME: Hitting the db on every page save should be avoided
  240. if ( in_array( $title->getFullText(), LiveTranslateFunctions::getLocalMemoryNames() ) ) {
  241. $requestData = array(
  242. 'action' => 'importtms',
  243. 'format' => 'json',
  244. 'source' => $title->getFullText(),
  245. 'type' => LiveTranslateFunctions::getMemoryType( $title->getFullText() ),
  246. 'local' => 1
  247. );
  248. $api = new ApiMain( new FauxRequest( $requestData, true ), true );
  249. $api->execute();
  250. $response = $api->getResultData();
  251. }
  252. return true;
  253. }
  254. /**
  255. * Registers the magic words to show and hide the translation control.
  256. *
  257. * TODO: apparently there is some new way of doing this for quite a while,
  258. * which is not linked in the docs. If someone cares to explain the new stuff,
  259. * I'll be happy to update this.
  260. *
  261. * @since 0.6
  262. *
  263. * @param array &$magicWords
  264. * @param string $langCode
  265. *
  266. * @return true
  267. */
  268. public static function addMagicWords( array &$magicWords, $langCode ) {
  269. $magicWords['LT_NOTRANSLATIONCONTROL'] = array( 0, '__NOTRANSLATIONCONTROL__' );
  270. $magicWords['LT_SHOWTRANSLATIONCONTROL'] =array( 0, '__SHOWTRANSLATIONCONTROL__' );
  271. return true;
  272. }
  273. /**
  274. * Strips the magic words added by Live Translate from the page text.
  275. *
  276. * @since 0.6
  277. *
  278. * @param Parser &$parser
  279. * @param string &$text
  280. *
  281. * @return true
  282. */
  283. public static function stripMagicWords( Parser &$parser, &$text ) {
  284. global $egLiveTranslateMagicWords;
  285. $mw = MagicWord::get( 'LT_NOTRANSLATIONCONTROL' );
  286. if ( $mw->matchAndRemove( $text ) ) {
  287. $egLiveTranslateMagicWords[] = 'LT_NOTRANSLATIONCONTROL';
  288. }
  289. $mw = MagicWord::get( 'LT_SHOWTRANSLATIONCONTROL' );
  290. if ( $mw->matchAndRemove( $text ) ) {
  291. $egLiveTranslateMagicWords[] = 'LT_SHOWTRANSLATIONCONTROL';
  292. }
  293. $po = $parser->getOutput();
  294. $po->mLTMagicWords = $egLiveTranslateMagicWords;
  295. return true;
  296. }
  297. public static function onOutputPageParserOutput( $outputpage, $parseroutput ) {
  298. $magicWords = isset( $parseroutput->mLTMagicWords ) ? $parseroutput->mLTMagicWords : array();
  299. return true;
  300. }
  301. }