protected function getApertiumSuggestion($serviceName, $config) { global $wgMemc; self::checkTranslationServiceFailure($serviceName); $page = $this->handle->getKey(); $code = $this->handle->getCode(); $ns = $this->handle->getTitle()->getNamespace(); $memckey = wfMemckey('translate-tmsug-pairs-' . $serviceName); $pairs = $wgMemc->get($memckey); if (!$pairs) { $pairs = array(); $json = Http::get($config['pairs'], 5); $response = FormatJson::decode($json); if ($json === false) { self::reportTranslationServiceFailure($serviceName); } elseif (!is_object($response)) { error_log(__METHOD__ . ': Unable to parse reply: ' . strval($json)); return null; } foreach ($response->responseData as $pair) { $source = $pair->sourceLanguage; $target = $pair->targetLanguage; if (!isset($pairs[$target])) { $pairs[$target] = array(); } $pairs[$target][$source] = true; } $wgMemc->set($memckey, $pairs, 60 * 60 * 24); } if (isset($config['codemap'][$code])) { $code = $config['codemap'][$code]; } $code = str_replace('-', '_', wfBCP47($code)); if (!isset($pairs[$code])) { return null; } $suggestions = array(); $codemap = array_flip($config['codemap']); foreach ($pairs[$code] as $candidate => $unused) { $mwcode = str_replace('_', '-', strtolower($candidate)); if (isset($codemap[$mwcode])) { $mwcode = $codemap[$mwcode]; } $text = TranslateUtils::getMessageContent($page, $mwcode, $ns); if ($text === null || TranslateEditAddons::hasFuzzyString($text)) { continue; } $title = Title::makeTitleSafe($ns, "{$page}/{$mwcode}"); if ($title && TranslateEditAddons::isFuzzy($title)) { continue; } $options = self::makeGoogleQueryParams($text, "{$candidate}|{$code}", $config); $options['postData']['format'] = 'html'; $json = Http::post($config['url'], $options); $response = FormatJson::decode($json); if ($json === false || !is_object($response)) { self::reportTranslationServiceFailure($serviceName); } elseif ($response->responseStatus !== 200) { error_log(__METHOD__ . " (HTTP {$response->responseStatus}) with ({$serviceName} ({$candidate}|{$code})): " . $response->responseDetails); } else { $sug = Sanitizer::decodeCharReferences($response->responseData->translatedText); $sug = trim($sug); $sug = $this->suggestionField($sug); $suggestions[] = Html::rawElement('div', array('title' => $text), self::legend("{$serviceName} ({$candidate})") . $sug . self::clear()); } } if (!count($suggestions)) { return null; } $divider = Html::element('div', array('style' => 'margin-bottom: 0.5ex')); return implode("{$divider}\n", $suggestions); }
/** * @todo Very long code block; split up. * * @param $group MessageGroup * @param $code */ public function importForm($group, $code) { $this->setSubtitle($group, $code); $formParams = array('method' => 'post', 'action' => $this->getTitle()->getFullURL(array('group' => $group->getId())), 'class' => 'mw-translate-manage'); global $wgRequest, $wgLang; if ($wgRequest->wasPosted() && $wgRequest->getBool('process', false) && $this->user->isAllowed('translate-manage') && $this->user->matchEditToken($wgRequest->getVal('token'))) { $process = true; } else { $process = false; } $this->out->addHTML(Xml::openElement('form', $formParams) . Html::hidden('title', $this->getTitle()->getPrefixedText()) . Html::hidden('token', $this->user->editToken()) . Html::hidden('group', $group->getId()) . Html::hidden('process', 1)); // BEGIN $cache = new MessageGroupCache($group, $code); if (!$cache->exists() && $code === 'en') { $cache->create(); } $collection = $group->initCollection($code); $collection->loadTranslations(); $diff = new DifferenceEngine(); $diff->showDiffStyle(); $diff->setReducedLineNumbers(); $ignoredMessages = $collection->getTags('ignored'); if (!is_array($ignoredMessages)) { $ignoredMessages = array(); } $messages = $group->load($code); $changed = array(); foreach ($messages as $key => $value) { // ignored? ignore! if (in_array($key, $ignoredMessages)) { continue; } $fuzzy = $old = false; if (isset($collection[$key])) { $old = $collection[$key]->translation(); } // No changes at all, ignore. if (str_replace(TRANSLATE_FUZZY, '', $old) === $value) { continue; } if ($old === false) { $name = wfMsgHtml('translate-manage-import-new', '<code style="font-weight:normal;">' . htmlspecialchars($key) . '</code>'); $text = TranslateUtils::convertWhiteSpaceToHTML($value); $changed[] = MessageWebImporter::makeSectionElement($name, 'new', $text); } else { if (TranslateEditAddons::hasFuzzyString($old)) { // NO-OP } else { $transTitle = MessageWebImporter::makeTranslationTitle($group, $key, $code); if (TranslateEditAddons::isFuzzy($transTitle)) { $old = TRANSLATE_FUZZY . $old; } } $diff->setText($old, $value); $text = $diff->getDiff('', ''); $type = 'changed'; if ($process) { if (!count($changed)) { $changed[] = '<ul>'; } $action = $wgRequest->getVal(MessageWebImporter::escapeNameForPHP("action-{$type}-{$key}")); if ($action === null) { $message = wfMsgExt('translate-manage-inconsistent', 'parseinline', wfEscapeWikiText("action-{$type}-{$key}")); $changed[] = "<li>{$message}</li></ul>"; $process = false; } else { // Initialise processing time counter. if (!isset($this->time)) { $this->time = wfTimestamp(); } $fuzzybot = FuzzyBot::getUser(); $message = MessageWebImporter::doAction($action, $group, $key, $code, $value, '', $fuzzybot, EDIT_FORCE_BOT); $key = array_shift($message); $params = $message; $message = wfMsgExt($key, 'parseinline', $params); $changed[] = "<li>{$message}</li>"; if ($this->checkProcessTime()) { $process = false; $duration = $wgLang->formatNum($this->processingTime); $message = wfMsgExt('translate-manage-toolong', 'parseinline', $duration); $changed[] = "<li>{$message}</li></ul>"; } continue; } } if ($code !== 'en') { $actions = array('import', 'conflict', 'ignore'); } else { $actions = array('import', 'fuzzy', 'ignore'); } $act = array(); if ($this->user->isAllowed('translate-manage')) { $defaction = $fuzzy ? 'conflict' : 'import'; foreach ($actions as $action) { $label = wfMsg("translate-manage-action-{$action}"); $name = MessageWebImporter::escapeNameForPHP("action-{$type}-{$key}"); $selected = $wgRequest->getVal($name, $defaction); $id = Sanitizer::escapeId("action-{$key}-{$action}"); $act[] = Xml::radioLabel($label, $name, $action, $id, $action === $selected); } } $name = wfMsg('translate-manage-import-diff', '<code style="font-weight:normal;">' . htmlspecialchars($key) . '</code>', implode(' ', $act)); $changed[] = MessageWebImporter::makeSectionElement($name, $type, $text); } } if (!$process) { $collection->filter('hastranslation', false); $keys = $collection->getMessageKeys(); $diff = array_diff($keys, array_keys($messages)); foreach ($diff as $s) { $name = wfMsgHtml('translate-manage-import-deleted', '<code style="font-weight:normal;">' . htmlspecialchars($s) . '</code>'); $text = TranslateUtils::convertWhiteSpaceToHTML($collection[$s]->translation()); $changed[] = MessageWebImporter::makeSectionElement($name, 'deleted', $text); } } if ($process || !count($changed) && $code !== 'en') { if (!count($changed)) { $this->out->addWikiMsg('translate-manage-nochanges-other'); } if (!count($changed) || strpos($changed[count($changed) - 1], '<li>') !== 0) { $changed[] = '<ul>'; } $cache->create(); $message = wfMsgExt('translate-manage-import-rebuild', 'parseinline'); $changed[] = "<li>{$message}</li>"; $message = wfMsgExt('translate-manage-import-done', 'parseinline'); $changed[] = "<li>{$message}</li></ul>"; $this->out->addHTML(implode("\n", $changed)); } else { // END if (count($changed)) { if ($code === 'en') { $this->out->addWikiMsg('translate-manage-intro-en'); } else { $lang = TranslateUtils::getLanguageName($code, false, $wgLang->getCode()); $this->out->addWikiMsg('translate-manage-intro-other', $lang); } $this->out->addHTML(Html::hidden('language', $code)); $this->out->addHTML(implode("\n", $changed)); if ($this->user->isAllowed('translate-manage')) { $this->out->addHTML(Xml::submitButton(wfMsg('translate-manage-submit'))); } } elseif ($this->user->isAllowed('translate-manage')) { $cache->create(); // Update timestamp $this->out->addWikiMsg('translate-manage-nochanges'); } } $this->out->addHTML('</form>'); if ($code === 'en') { $this->doModLangs($group); } else { $this->out->addHTML('<p>' . $this->skin->link($this->getTitle(), wfMsgHtml('translate-manage-return-to-group'), array(), array('group' => $group->getId())) . '</p>'); } }