/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/helpers/XliffHelper.php
154 строки
5 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\helpers; use common\models\memoq\Document; use common\models\memoq\TerSync; use common\models\memoq\XliffSegment; use Yii; use yii\helpers\VarDumper; /** * Вспомогательный класс для работы MemoQ * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2025 */ class XliffHelper { public static function parseXliff($document_id, $xliff_content) { $content = str_replace([ 'mq:insertedmatch', 'mq:minorversions', 'mq:historical-unit', 'mq:translatorcommitusername', 'mq:reviewer2commitusername', 'mq:reviewer1commitusername' ], [ 'mq_insertedmatch', 'mq_minorversions', 'mq_historical-unit', 'mq_translatorcommitusername', 'mq_reviewer2commitusername', 'mq_reviewer1commitusername' ], $xliff_content); $dom = new \DOMDocument(); libxml_use_internal_errors(true); $dom->loadXML($content); libxml_clear_errors(); // Convert to SimpleXML if needed $document = simplexml_import_dom($dom); /** @var \SimpleXMLElement $body */ $body = $document->file->body; $documents = []; /** @var SimpleXMLElement $row */ foreach ($body->{"trans-unit"} as $row) { $id = (string)$row->attributes()->id; // Оригинал $source = (string)$row->source; // Итог $human_final = (string)$row->target; // Провайдер $a = $row->{'mq_insertedmatch'}->attributes(); $mt_provider = $a ? (string)$a->source : null; $pre_translation = (string)$row->{'mq_insertedmatch'}->target; $hist_versions = []; foreach ($row->mq_minorversions->{'mq_historical-unit'} as $version) { $a = $version->attributes(); // Переводчик $editor_1 = $a->mq_translatorcommitusername; // Редактор $editor_2 = $a->mq_reviewer2commitusername ?: $a->mq_reviewer1commitusername; $item = [ 'editor1' => (string)$editor_1, 'editor2' => (string)$editor_2, 'target' => (string)$version->target ]; $hist_versions[] = $item; // array_unshift($hist_versions, $item); } $cnt = count($hist_versions); $editor_1_user = null; $editor_1_text = null; $editor_2_user = null; $editor_2_text = null; if ($cnt > 0) { $the = $hist_versions[0]; $editor_1_user = ArrayHelper::getValue($the, 'editor1'); if ($editor_1_user) { $editor_1_text = ArrayHelper::getValue($the, 'target', $mt_provider); } } if ($cnt > 1) { $the = $hist_versions[1]; $editor_2_user = ArrayHelper::getValue($the, 'editor2'); if ($editor_2_user) { $editor_2_text = ArrayHelper::getValue($the, 'target', $human_final); } } $wordCount = TextHelper::countWordsWithFilter($human_final); $documents[] = [ 'document_id' => $document_id, 'segment' => $id, 'source' => $source, 'pre_translation' => $pre_translation, 'ai_provider' => $mt_provider, 'editor_1_text' => $editor_1_text, 'editor_1_user' => $editor_1_user, 'editor_2_text' => $editor_2_text, 'editor_2_user' => $editor_2_user, 'human_final' => $human_final, 'word_count' => $wordCount ]; } XliffSegment::deleteAll(['document_id' => $document_id]); /** @var \yii\mongodb\Connection $mongodb */ $mongodb = Yii::$app->mongodb; $collectionName = XliffSegment::collectionName(); $command = $mongodb->createCommand(); Yii::debug(VarDumper::dumpAsString([ 'documents' => $documents ])); $segment_count = count($documents); Document::updateAll([ 'segment_count' => $segment_count ], [ 'id' => $document_id ]); $result = $command->batchInsert($collectionName, $documents); if (YII_DEBUG) { Yii::debug(VarDumper::dumpAsString([ 'result' => $result ])); } if ($segment_count > 0) { TerHelper::updateTer($document_id); } return true; } }