public function preFillFromSourceLanguage($useGoogleTranslateData = true) { $path = APPPATH . "resources/languageforge/semdomtrans/GoogleTranslateHarvester/semdom-google-translate-{$this->languageIsoCode}.txt.gz"; $googleTranslateData = []; if ($useGoogleTranslateData && file_exists($path)) { $lines = gzfile($path); foreach ($lines as $line) { $splitLine = explode("|", $line); if (count($splitLine) == 2) { $googleTranslateData[$splitLine[0]] = $splitLine[1]; } } } // cjh review: we may actually want to only prefill from English, if in the future we allow creating projects from incomplete source projects $sourceProject = new SemDomTransProjectModel($this->sourceLanguageProjectId->asString()); $this->_copyXmlToAssets($sourceProject->xmlFilePath); $sourceItems = new SemDomTransItemListModel($sourceProject); $sourceItems->read(); foreach ($sourceItems->entries as $item) { $newItem = new SemDomTransItemModel($this); // if Google translation exists for given name exists, use it if (array_key_exists($item['name']['translation'], $googleTranslateData)) { $newItem->name->translation = $googleTranslateData[$item['name']['translation']]; $newItem->name->status = SemDomTransStatus::Suggested; } // if Google translation exists for given description exists, use it if (array_key_exists($item['description']['translation'], $googleTranslateData)) { $newItem->description->translation = $googleTranslateData[$item['description']['translation']]; $newItem->description->status = SemDomTransStatus::Suggested; } $newItem->key = $item['key']; for ($x = 0; $x < count($item['questions']); $x++) { $q = new SemDomTransQuestion(); // if Google translation exists for given question, use it if (array_key_exists($item['questions'][$x]['question']['translation'], $googleTranslateData)) { $q->question->translation = $googleTranslateData[$item['questions'][$x]['question']['translation']]; $q->question->status = SemDomTransStatus::Suggested; } // if Google translation exists for given question term, use it if (array_key_exists($item['questions'][$x]['terms']['translation'], $googleTranslateData)) { $q->terms->translation = $googleTranslateData[$item['questions'][$x]['terms']['translation']]; $q->terms->status = SemDomTransStatus::Suggested; } $newItem->questions[] = $q; } for ($x = 0; $x < count($item['searchKeys']); $x++) { $sk = new SemDomTransTranslatedForm(); // if Google translation exists for given search key, use it if (array_key_exists($item['searchKeys'][$x]['translation'], $googleTranslateData)) { $sk->translation = $googleTranslateData[$item['searchKeys'][$x]['translation']]; $sk->status = SemDomTransStatus::Suggested; } $newItem->searchKeys[] = $sk; } $newItem->xmlGuid = $item['xmlGuid']; $newItem->write(); } }
function testCleanupFiles_4Files2Allowed_2Removed() { $environ = new MongoTestEnvironment(); $environ->clean(); $project = $environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE); $projectId = $project->write(); $text = new TextModel($project); $textId = $text->write(); $fakeId = new Id(); $fakeTextId = $fakeId->asString(); $folderPath = $project->getAssetsFolderPath(); FileUtilities::createAllFolders($folderPath); $allowedExtensions = array(".mp2", ".mp3"); // put a copy of the test files in the folderPath $fileName1 = 'TestAudio1.mp1'; $filePath1 = SfchecksUploadCommands::mediaFilePath($folderPath, $textId, $fileName1); copy(TestPath . 'common/TestAudio.mp3', $filePath1); $fileName2 = 'TestAudio2.mp2'; $filePath2 = SfchecksUploadCommands::mediaFilePath($folderPath, $textId, $fileName2); copy(TestPath . 'common/TestAudio.mp3', $filePath2); $fileName3 = 'TestAudio3.mp3'; $filePath3 = SfchecksUploadCommands::mediaFilePath($folderPath, $textId, $fileName3); copy(TestPath . 'common/TestAudio.mp3', $filePath3); $fileName4 = 'TestAudio4.mp3'; $filePath4 = SfchecksUploadCommands::mediaFilePath($folderPath, $fakeTextId, $fileName4); copy(TestPath . 'common/TestAudio.mp3', $filePath4); $this->assertTrue(file_exists($filePath1), 'File should exist before cleanup'); $this->assertTrue(file_exists($filePath2), 'File should exist before cleanup'); $this->assertTrue(file_exists($filePath3), 'File should exist before cleanup'); $this->assertTrue(file_exists($filePath4), 'File should exist before cleanup'); SfchecksUploadCommands::cleanupFiles($folderPath, $textId, $allowedExtensions); $this->assertTrue(file_exists($filePath1), 'File should exist after cleanup'); $this->assertFalse(file_exists($filePath2), 'File should not exist after cleanup'); $this->assertFalse(file_exists($filePath3), 'File should not exist after cleanup'); $this->assertTrue(file_exists($filePath4), 'File should exist after cleanup'); $environ->cleanupTestFiles($folderPath); }
/** * * @param SimpleXMLElement $sxeNode * @param LexEntryModel $entry * @param LiftMergeRule $mergeRule * @throws \Exception */ public function readEntry($sxeNode, $entry, $mergeRule = LiftMergeRule::CREATE_DUPLICATES) { $this->nodeErrors = array(); $this->nodeErrors[] = new LiftImportNodeError(LiftImportNodeError::ENTRY, (string) $sxeNode['guid']); foreach ($sxeNode as $element) { switch ($element->getName()) { case 'lexical-unit': if ($mergeRule != LiftMergeRule::IMPORT_LOSES || Id::isEmpty($entry->id)) { $entry->guid = (string) $sxeNode['guid']; $entry->authorInfo->createdDate = new \DateTime((string) $sxeNode['dateCreated']); $entry->authorInfo->modifiedDate = new \DateTime((string) $sxeNode['dateModified']); $entry->lexeme = $this->readMultiText($element, $this->projectModel->config->entry->fields[LexiconConfigObj::LEXEME]->inputSystems); } break; case 'citation': $entry->citationForm = $this->readMultiText($element, $this->projectModel->config->entry->fields[LexiconConfigObj::CITATIONFORM]->inputSystems); break; case 'note': if ($element['type'] == '') { $entry->note = $this->readMultiText($element, $this->projectModel->config->entry->fields[LexiconConfigObj::NOTE]->inputSystems); } else { $this->addKnownUnhandledElement('Note: ' . $element['type']); } break; case 'etymology': $entry->etymology = $this->readMultiText($element, $this->projectModel->config->entry->fields[LexiconConfigObj::ETYMOLOGY]->inputSystems, true); if ($element->{'gloss'}) { $this->readMultiTextGloss($element->gloss, $entry->etymologyGloss, $this->projectModel->config->entry->fields[LexiconConfigObj::ETYMOLOGYGLOSS]->inputSystems); } foreach ($element->{'field'} as $field) { if ($field['type'] == 'comment') { $entry->etymologyComment = $this->readMultiText($field, $this->projectModel->config->entry->fields[LexiconConfigObj::ETYMOLOGYCOMMENT]->inputSystems); } else { $this->currentNodeError()->addUnhandledField($field['type'], 'etymology'); } } break; case 'pronunciation': $entry->pronunciation = $this->readMultiText($element, $this->projectModel->config->entry->fields[LexiconConfigObj::PRONUNCIATION]->inputSystems, true); if ($element->{'media'}) { $this->addKnownUnhandledElement('pronunciation: media'); } break; case 'field': switch ($element['type']) { case 'literal-meaning': $entry->literalMeaning = $this->readMultiText($element, $this->projectModel->config->entry->fields[LexiconConfigObj::LITERALMEANING]->inputSystems); break; case 'summary-definition': $entry->summaryDefinition = $this->readMultiText($element, $this->projectModel->config->entry->fields[LexiconConfigObj::SUMMARYDEFINITION]->inputSystems); break; case 'import-residue': // Currently ignored in LanguageForge break; default: if ($this->isEntryCustomField($element['type'])) { $this->addEntryCustomField($element, $element['type'], $entry); } else { $this->currentNodeError()->addUnhandledField($element['type']); } } break; case 'trait': switch ($element['name']) { case 'morph-type': $entry->morphologyType = (string) $element['value']; break; case 'do-not-publish-in': case 'DoNotUseForParsing': $this->addKnownUnhandledElement('trait: ' . $element['name']); break; default: if ($this->isEntryCustomField($element['name'])) { $this->addEntryCustomField($element, $element['name'], $entry); } else { $this->currentNodeError()->addUnhandledTrait($element['name']); } } break; case 'sense': $liftId = ''; if (isset($element['id'])) { $liftId = (string) $element['id']; } $existingSenseIndex = $entry->searchSensesFor('liftId', $liftId); if ($existingSenseIndex >= 0) { switch ($mergeRule) { case LiftMergeRule::CREATE_DUPLICATES: $sense = new Sense(''); $entry->senses[] = $this->readSense($element, $sense); break; case LiftMergeRule::IMPORT_WINS: $sense = new Sense($liftId); $entry->senses[$existingSenseIndex] = $this->readSense($element, $sense); break; case LiftMergeRule::IMPORT_LOSES: break; default: throw new \Exception("unknown LiftMergeRule " . $mergeRule); } } else { $sense = new Sense($liftId); $entry->senses[] = $this->readSense($element, $sense); } break; case 'variant': case 'relation': $this->addKnownUnhandledElement('Element: ' . $element->getName()); break; default: $this->currentNodeError()->addUnhandledElement($element->getName()); } } if (!$this->currentNodeError()->hasErrors()) { unset($this->nodeErrors[count($this->nodeErrors) - 1]); } }
/** * Writes the model to the mongo collection * @return string The unique id of the object written * @see MongoMapper::write() */ public function write() { CodeGuard::checkTypeAndThrow($this->id, 'Api\\Model\\Mapper\\Id'); $this->dateModified = new \DateTime(); if (Id::isEmpty($this->id)) { $this->dateCreated = new \DateTime(); } $this->id->id = $this->_mapper->write($this, $this->id->id); return $this->id->id; }
/** * Determines if project with given langauge code exists * @param string $languageCode * @return boolean */ public static function checkProjectExists($languageCode) { $project = new SemDomTransProjectModel(); $project->readByCode($languageCode); if (Id::isEmpty($project->id)) { return true; } else { return false; } }
public function cleanPreviousProject($languageCode) { $p = new SemDomTransProjectModel(); $p->readByCode($languageCode, self::TESTVERSION); if (!Id::isEmpty($p->id)) { $this->cleanProjectEnvironment($p); } else { // create the project and then clean the project environment $p = new SemDomTransProjectModel(); $p->projectCode = SemDomTransProjectModel::projectCode($languageCode, self::TESTVERSION); $p->write(); $this->cleanProjectEnvironment($p); } $p->remove(); }