public function testLiftImportMerge_LargeFile_NoException()
 {
     global $testInfo;
     $testInfo = new TestLiftImportInfo();
     $e = new LexiconMongoTestEnvironment();
     $e->clean();
     $project = $e->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE);
     //         $liftFilePath = '/home/cambell/src/Forge/TestData/Gilaki/Gilaki.lift';
     $liftFilePath = '/home/cambell/src/Forge/TestData/Webster/Webster.lift';
     $mergeRule = LiftMergeRule::IMPORT_WINS;
     $skipSameModTime = false;
     LiftImport::get()->merge($liftFilePath, $project, $mergeRule, $skipSameModTime);
     $testInfo->add('post merge');
 }
 public function testLiftImportMerge_LargeFile_NoException()
 {
     global $testInfo;
     $testInfo = new TestLiftImportInfo();
     $environ = new LexiconMongoTestEnvironment();
     $environ->clean();
     $project = $environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE);
     //         $liftFilePath = '/home/cambell/src/Forge/TestData/Gilaki/Gilaki.lift';
     //        $liftFilePath = '/home/cambell/src/Forge/TestData/Webster/Webster.lift';
     //        $liftFilePath = '/home/ira/TestData/test-langprojih-flex/test-langprojih-flex.lift';
     $liftFilePath = '/home/ira/TestData/test-rwr-flex/test-rwr-flex.lift';
     $mergeRule = LiftMergeRule::IMPORT_WINS;
     $skipSameModTime = false;
     $time1 = new DateTime();
     LiftImport::get()->merge($liftFilePath, $project, $mergeRule, $skipSameModTime);
     $time2 = new DateTime();
     $elapsed = $time2->diff($time1);
     echo $elapsed->format('%I:%S') . '<br/>';
     $testInfo->add('post merge');
 }
 /**
  * Import a LIFT file
  *
  * @param string $projectId
  * @param string $mediaType
  * @param string $tmpFilePath
  * @throws \Exception
  * @return \Api\Model\Shared\Command\UploadResponse
  */
 public static function importLiftFile($projectId, $mediaType, $tmpFilePath)
 {
     if ($mediaType != 'import-lift') {
         throw new \Exception("Unsupported upload type.");
     }
     if (!$tmpFilePath) {
         throw new \Exception("Upload controller did not move the uploaded file.");
     }
     $file = $_FILES['file'];
     $fileName = $file['name'];
     $mergeRule = $_POST['mergeRule'];
     $skipSameModTime = $_POST['skipSameModTime'];
     $deleteMatchingEntry = $_POST['deleteMatchingEntry'];
     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     $fileType = finfo_file($finfo, $tmpFilePath);
     finfo_close($finfo);
     $fileName = FileUtilities::replaceSpecialCharacters($fileName);
     $fileExt = false === ($pos = strrpos($fileName, '.')) ? '' : substr($fileName, $pos);
     $allowedTypes = array("text/xml", "application/xml");
     $response = new UploadResponse();
     if (in_array(strtolower($fileType), $allowedTypes) && in_array(strtolower($fileExt), self::$allowedLiftExtensions)) {
         // make the folders if they don't exist
         $project = new LexiconProjectModel($projectId);
         $folderPath = $project->getAssetsFolderPath();
         FileUtilities::createAllFolders($folderPath);
         $importer = LiftImport::get()->merge($tmpFilePath, $project, $mergeRule, $skipSameModTime, $deleteMatchingEntry);
         $project->write();
         $moveOk = true;
         if (!$project->liftFilePath || $mergeRule != LiftMergeRule::IMPORT_LOSES) {
             // cleanup previous files of any allowed extension
             $cleanupFiles = glob($folderPath . '/*[' . implode(', ', self::$allowedLiftExtensions) . ']');
             foreach ($cleanupFiles as $cleanupFile) {
                 @unlink($cleanupFile);
             }
             // move uploaded LIFT file from tmp location to assets
             $filePath = $folderPath . '/' . $fileName;
             $project->liftFilePath = $filePath;
             $project->write();
             $moveOk = copy($tmpFilePath, $filePath);
             @unlink($tmpFilePath);
         }
         // construct server response
         if ($moveOk && $tmpFilePath) {
             $data = new ImportResult();
             $data->path = $project->getAssetsRelativePath();
             $data->fileName = $fileName;
             $data->stats = $importer->stats;
             $data->importErrors = $importer->getReport()->toFormattedString();
             $response->result = true;
         } else {
             $data = new ErrorResult();
             $data->errorType = 'UserMessage';
             $data->errorMessage = "{$fileName} could not be saved to the right location. Contact your Site Administrator.";
             $response->result = false;
         }
     } else {
         $allowedExtensionsStr = implode(", ", self::$allowedLiftExtensions);
         $data = new ErrorResult();
         $data->errorType = 'UserMessage';
         if (count(self::$allowedLiftExtensions) < 1) {
             $data->errorMessage = "{$fileName} of type: {$fileType} is not an allowed LIFT file. No LIFT file formats are currently enabled, contact your Site Administrator.";
         } elseif (count(self::$allowedLiftExtensions) == 1) {
             $data->errorMessage = "{$fileName} of type: {$fileType} is not an allowed LIFT file. Ensure the file is a {$allowedExtensionsStr}.";
         } else {
             $data->errorMessage = "{$fileName} of type: {$fileType} is not an allowed LIFT file. Ensure the file is one of the following types: {$allowedExtensionsStr}.";
         }
         $response->result = false;
     }
     $response->data = $data;
     return $response;
 }
 public function testLiftImportMerge_MultiPara_ParagraphMarkerFound()
 {
     $liftFilePath = self::$environ->createTestLiftFile(self::liftOneEntryMultiParaV0_13, 'LiftOneEntryMultiParaV0_13.lift');
     $project = self::$environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE);
     $mergeRule = LiftMergeRule::IMPORT_WINS;
     $skipSameModTime = false;
     LiftImport::get()->merge($liftFilePath, $project, $mergeRule, $skipSameModTime);
     $entryList = new LexEntryListModel($project);
     $entryList->read();
     $entry0 = $entryList->entries[0];
     $this->assertEquals(1, $entryList->count);
     $this->assertArrayHasKey('customField_entry_Cust_MultiPara', $entry0['customFields'], 'custom field MultiPara exists');
     $this->assertEquals('First paragraph with <span lang="th">ไทย</span>', $entry0['customFields']['customField_entry_Cust_MultiPara']['paragraphs'][0]['content'], 'custom field MultiPara has paragraphs separated into paragraph 1 and native language spans removed');
     $this->assertEquals('Second Paragraph', $entry0['customFields']['customField_entry_Cust_MultiPara']['paragraphs'][1]['content'], 'custom field MultiPara has paragraphs separated into paragraph 2 and native language spans removed');
 }
 public function testLiftImportMerge_ZipFile2LiftAndOddFolder_Error()
 {
     $zipFilePath = self::$environ->copyTestUploadFile(TestPhpPath . 'common/TestLex2ProjectsOddFolder.zip');
     $project = self::$environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE);
     $importer = LiftImport::get()->importZip($zipFilePath, $project);
     $report = $importer->getReport();
     $reportStr = $report->toString();
     $this->assertEquals(true, $report->hasError(), 'should have NodeError');
     $this->assertRegExp("/unhandled LIFT file/", $reportStr);
     $this->assertRegExp("/unhandled subfolder 'OddFolder'/", $reportStr);
 }
 public function testLiftImportMerge_FlexAllFields_HasAllFields()
 {
     $liftFilePath = self::$environ->createTestLiftFile(self::liftAllFlexFields, 'LiftAllFlexFields.lift');
     $project = self::$environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE);
     $mergeRule = LiftMergeRule::IMPORT_WINS;
     $skipSameModTime = false;
     LiftImport::get()->merge($liftFilePath, $project, $mergeRule, $skipSameModTime);
     $entryList = new LexEntryListModel($project);
     $entryList->read();
     $entries = $entryList->entries;
     $this->assertEquals(2, $entryList->count);
     $entriesByGuid = self::$environ->indexItemsBy($entries, 'guid');
     $entry0 = new LexEntryModel($project, $entriesByGuid['0a18bb95-0eb2-422e-bf7e-c1fd90274670']['id']);
     new LexEntryModel($project, $entriesByGuid['dc4106ac-13fd-4ae0-a32b-b737f413d515']['id']);
     $this->assertEquals('0a18bb95-0eb2-422e-bf7e-c1fd90274670', $entry0->guid);
     $this->assertEquals('คาม', $entry0->lexeme['th']);
     $this->assertEquals('คาม', $entry0->citationForm['th']);
     $this->assertEquals('คาม', $entry0->etymology['th']);
     $this->assertEquals('A Etymology', $entry0->etymology['en']);
     $this->assertEquals('A Etymology Gloss', $entry0->etymologyGloss['en']);
     $this->assertEquals('A Etymology Comment', $entry0->etymologyComment['en']);
     $this->assertEquals('คาม', $entry0->pronunciation['th']);
     $this->assertEquals('stem', $entry0->morphologyType);
     $this->assertEquals('A Literal Meaning', $entry0->literalMeaning['en']);
     /* @var $sense00 LexSense */
     $sense00 = $entry0->senses[0];
     $this->assertEquals('Noun', $sense00->partOfSpeech->value);
     $this->assertEquals('A Word', $sense00->gloss['en']->value);
     $this->assertEquals('A Word Defn', $sense00->definition['en']->value);
     $this->assertEquals(LexMultiValue::createFromArray(['9.1.3.1']), $sense00->semanticDomain);
     $this->assertEquals(LexMultiValue::createFromArray(['901']), $sense00->anthropologyCategories);
     $this->assertEquals(LexMultiValue::createFromArray(['applied linguistics']), $sense00->academicDomains);
     $this->assertEquals(new LexValue('primary'), $sense00->senseType);
     $this->assertEquals(LexMultiValue::createFromArray(['Tentative']), $sense00->status);
     $this->assertEquals(LexMultiValue::createFromArray(['colloquial']), $sense00->usages);
     $expected = new LexPicture('Desert.jpg', $sense00->pictures[0]->guid);
     $expected->caption['th'] = 'รูป';
     $expected->caption['en'] = 'image';
     $expected->caption['fr'] = 'photo';
     $this->assertEquals($expected, $sense00->pictures[0]);
     /* @var $example000 LexExample */
     $example000 = $sense00->examples[0];
     $this->assertEquals('ใหท่ มี', $example000->sentence['th']);
     $this->assertEquals('A Translation', $example000->translation['en']->value);
 }
 public function testLiftImportMerge_MultiPara_ParagraphMarkerFound()
 {
     $liftFilePath = $this->environ->createTestLiftFile(self::liftOneEntryMultiParaV0_13, 'LiftOneEntryMultiParaV0_13.lift');
     $project = $this->environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE);
     $mergeRule = LiftMergeRule::IMPORT_WINS;
     $skipSameModTime = false;
     $importer = LiftImport::get()->merge($liftFilePath, $project, $mergeRule, $skipSameModTime);
     $entryList = new LexEntryListModel($project);
     $entryList->read();
     $entry0 = $entryList->entries[0];
     $this->assertEqual($entryList->count, 1);
     $this->assertTrue(array_key_exists('customField_entry_Cust_MultiPara', $entry0['customFields']), 'custom field MultiPara exists');
     $this->assertEqual($entry0['customFields']['customField_entry_Cust_MultiPara']['en']['value'], '<p>First paragraph with <span lang="th">ไทย</span></p><p>Second Paragraph</p>', 'custom field MultiPara has paragraph separator character U+2029 replaced by paragraph markup and native language spans removed');
 }