public function __construct($fileName = '', $guid = '')
 {
     $this->fileName = $fileName;
     $this->caption = new LexMultiText();
     $this->setReadOnlyProp('guid');
     $this->guid = Guid::makeValid($guid);
 }
 /**
  * Updates the given LexEntry in $projectId
  * @param string $projectId
  * @param array $params
  * @param string $userId
  * @param string $mergeQueuePath
  * @param string $pidFilePath
  * @param string $command
  * @return bool|array<encoded LexEntryModel> if the project is syncing (or on hold) return false (no save)FixSe
  */
 public static function updateEntry($projectId, $params, $userId, $mergeQueuePath = null, $pidFilePath = null, $command = null)
 {
     CodeGuard::checkTypeAndThrow($params, 'array');
     $project = new LexProjectModel($projectId);
     ProjectCommands::checkIfArchivedAndThrow($project);
     $now = UniversalTimestamp::now();
     if (array_key_exists('id', $params) && $params['id'] != '') {
         $entry = new LexEntryModel($project, $params['id']);
         $action = 'update';
     } else {
         $entry = new LexEntryModel($project);
         $entry->authorInfo->createdByUserRef->id = $userId;
         $entry->authorInfo->createdDate = $now;
         $entry->guid = Guid::create();
         $action = 'create';
         // TODO: Consider adding more specific activity entry: which fields were modified? 2014-09-03 RM
         // E.g., "User _____ updated entry _____ by adding a new sense with definition ______"
     }
     $entry->authorInfo->modifiedDate = $now;
     $entry->authorInfo->modifiedByUserRef->id = $userId;
     if ($project->hasSendReceive()) {
         //            $entry->dirtySR++;
         $entry->dirtySR = 0;
         $status = SendReceiveCommands::getProjectStatus($projectId);
         if ($status && $status['SRState'] != 'IDLE') {
             return false;
         }
     }
     LexEntryDecoder::decode($entry, $params);
     $entry->write();
     ActivityCommands::writeEntry($project, $userId, $entry, $action);
     //        SendReceiveCommands::queueProjectForUpdate($project, $mergeQueuePath);
     //        SendReceiveCommands::startLFMergeIfRequired($projectId, 'merge', $pidFilePath, $command);
     return JsonEncoder::encode($entry);
 }
 public function __construct($guid = '', $styleName = '')
 {
     $this->setReadOnlyProp('guid');
     $this->guid = Guid::makeValid($guid);
     $this->setPrivateProp('styleName');
     $this->initLazyProperties(['content', 'styleName'], false);
     if ($styleName) {
         $this->styleName = $styleName;
     }
 }
 public function __construct($liftId = '', $guid = '')
 {
     $this->setPrivateProp('liftId');
     $this->setReadOnlyProp('guid');
     $this->setReadOnlyProp('authorInfo');
     if ($liftId) {
         $this->liftId = $liftId;
     }
     $this->guid = Guid::makeValid($guid);
     $this->initLazyProperties(['partOfSpeech', 'semanticDomain', 'examples', 'customFields', 'authorInfo', 'definition', 'gloss', 'pictures', 'scientificName', 'anthropologyNote', 'senseBibliography', 'discourseNote', 'encyclopedicNote', 'generalNote', 'grammarNote', 'phonologyNote', 'senseRestrictions', 'semanticsNote', 'sociolinguisticsNote', 'source', 'senseImportResidue', 'usages', 'reversalEntries', 'senseType', 'academicDomains', 'sensePublishIn', 'anthropologyCategories', 'status'], false);
 }
 protected function createProperty($name)
 {
     switch ($name) {
         case 'authorInfo':
             return new LexAuthorInfo();
         case 'sentence':
         case 'translation':
         case 'reference':
             return new LexMultiText();
         case 'translationGuid':
             return Guid::create();
         case 'examplePublishIn':
             return new LexMultiValue();
         case 'customFields':
             return new MapOf('Api\\Model\\Languageforge\\Lexicon\\generateCustomField');
         default:
             return '';
     }
 }
 /**
  * Analyze a lexicon project and create Entry guids.
  * @param LexProjectModelForEntryGuidMigration $project
  * @param string $testMode
  * @internal param string $projectId
  */
 private static function analyzeProject($project, $testMode)
 {
     $entryModifiedCount = 0;
     $entryList = new LexAllEntryListModel($project);
     $entryList->read();
     foreach ($entryList->entries as $entryListItem) {
         $entry = new LexEntryModel($project, $entryListItem['id']);
         if (!isset($entry->guid) || !$entry->guid || !Guid::isValid($entry->guid)) {
             $entry->guid = Guid::create();
             $entryModifiedCount++;
             if (!$testMode) {
                 $entry->write();
             }
         }
     }
     if (!$testMode) {
         $project->hasHadEntryGuidMigrated = true;
         $project->write();
     }
     print "{$entryModifiedCount} of {$entryList->count} entries had guids created.\n";
 }
 public function testLiftDecoderGetGuid()
 {
     $guid = Guid::extract('');
     $this->assertEquals('', $guid);
     $guid = Guid::extract('does not contain guid');
     $this->assertEquals('', $guid);
     $liftGuid = Guid::create();
     $guid = Guid::extract('lexeme_' . $liftGuid);
     $this->assertEquals($liftGuid, $guid);
 }
 public function testUpdateEntry_DataPersists()
 {
     $project = self::$environ->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE);
     $projectId = $project->id->asString();
     $userId = self::$environ->createUser('john', 'john', 'john');
     $exampleGuid = Guid::create();
     $example = new LexExample($exampleGuid, $exampleGuid);
     $example->sentence->form('th', 'example1');
     $example->translation->form('en', 'trans1');
     $pictureGuid = Guid::create();
     $picture = new LexPicture('someFilename', $pictureGuid);
     $senseGuid = Guid::create();
     $sense = new LexSense($senseGuid, $senseGuid);
     $sense->definition->form('en', 'red fruit');
     $sense->gloss->form('en', 'rose fruit');
     $sense->partOfSpeech->value = 'noun';
     $sense->examples[] = $example;
     $sense->pictures[] = $picture;
     $entry = new LexEntryModel($project);
     $entry->lexeme->form('th', 'apple');
     $entry->senses[] = $sense;
     $entryId = $entry->write();
     $params = json_decode(json_encode(LexEntryCommands::readEntry($projectId, $entryId)), true);
     $params['lexeme']['th']['value'] = 'rose apple';
     LexEntryCommands::updateEntry($projectId, $params, $userId);
     $newEntry = LexEntryCommands::readEntry($projectId, $entryId);
     $this->assertEquals('rose apple', $newEntry['lexeme']['th']['value']);
     $this->assertEquals($senseGuid, $newEntry['senses'][0]['guid']);
     $this->assertArrayNotHasKey('liftId', $newEntry['senses'][0], 'sense liftId should be private');
     $this->assertEquals('red fruit', $newEntry['senses'][0]['definition']['en']['value']);
     $this->assertEquals('rose fruit', $newEntry['senses'][0]['gloss']['en']['value']);
     $this->assertEquals('noun', $newEntry['senses'][0]['partOfSpeech']['value']);
     $this->assertEquals($exampleGuid, $newEntry['senses'][0]['examples'][0]['guid']);
     $this->assertEquals($pictureGuid, $newEntry['senses'][0]['pictures'][0]['guid']);
     $this->assertArrayNotHasKey('scientificName', $newEntry['senses'][0], 'should be no empty fields');
     $this->assertArrayNotHasKey('liftId', $newEntry['senses'][0]['examples'][0], 'example liftId should be private');
     $this->assertEquals('example1', $newEntry['senses'][0]['examples'][0]['sentence']['th']['value']);
     $this->assertEquals('trans1', $newEntry['senses'][0]['examples'][0]['translation']['en']['value']);
 }
 /**
  * @param LexSense $sense
  * @param bool $entryModified
  * @param int $exampleModifiedCount
  * @param int $pictureModifiedCount
  */
 private static function createSenseGuids($sense, &$entryModified, &$exampleModifiedCount = 0, &$pictureModifiedCount = 0)
 {
     $senseModified = false;
     unset($sense->id);
     if (!$sense->guid || !Guid::isValid($sense->guid)) {
         $liftGuid = Guid::extract($sense->liftId);
         if (Guid::isValid($liftGuid)) {
             $sense->guid = $liftGuid;
         } else {
             $sense->guid = Guid::create();
         }
         $senseModified = true;
     }
     if (isset($sense->examples)) {
         /** @var LexExample $example */
         foreach ($sense->examples as $example) {
             unset($example->id);
             if (!$example->guid || !Guid::isValid($example->guid)) {
                 $liftGuid = Guid::extract($example->liftId);
                 if (Guid::isValid($liftGuid)) {
                     $example->guid = $liftGuid;
                 } else {
                     $example->guid = Guid::create();
                 }
                 $exampleModifiedCount++;
                 $senseModified = true;
             }
         }
     }
     if (isset($sense->pictures)) {
         /** @var LexPicture $picture */
         foreach ($sense->pictures as $picture) {
             if (!$picture->guid || !Guid::isValid($picture->guid)) {
                 $picture->guid = Guid::create();
                 $pictureModifiedCount++;
                 $senseModified = true;
             }
         }
     }
     if ($senseModified) {
         $entryModified = true;
     }
 }
 /**
  * @param \SimpleXMLElement $sxeNode
  * @param LexEntryModel $entry
  * @param string $mergeRule
  * @throws \Exception
  */
 public function readEntry($sxeNode, $entry, $mergeRule = LiftMergeRule::CREATE_DUPLICATES)
 {
     $this->nodeErrors = array();
     $this->nodeErrors[] = new LiftImportNodeError(LiftImportNodeError::ENTRY, (string) $sxeNode['guid']);
     /** @var \SimpleXMLElement $element */
     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 = UniversalTimestamp::fromStringTimestamp((string) $sxeNode['dateCreated']);
                     $entry->authorInfo->modifiedDate = UniversalTimestamp::fromStringTimestamp((string) $sxeNode['dateModified']);
                     $entry->lexeme = $this->readMultiText($element, $this->project->config->entry->fields[LexConfig::LEXEME]->inputSystems);
                 }
                 break;
             case 'citation':
                 $entry->citationForm = $this->readMultiText($element, $this->project->config->entry->fields[LexConfig::CITATIONFORM]->inputSystems);
                 break;
             case 'note':
                 if ($element['type'] == '') {
                     $entry->note = $this->readMultiText($element, $this->project->config->entry->fields[LexConfig::NOTE]->inputSystems);
                 } else {
                     $this->addKnownUnhandledElement('Note: ' . $element['type']);
                 }
                 break;
             case 'etymology':
                 $entry->etymology = $this->readMultiText($element, $this->project->config->entry->fields[LexConfig::ETYMOLOGY]->inputSystems, true);
                 if ($element->{'gloss'}) {
                     $this->readMultiTextGloss($element->{'gloss'}[0], $entry->etymologyGloss, $this->project->config->entry->fields[LexConfig::ETYMOLOGYGLOSS]->inputSystems);
                 }
                 foreach ($element->{'field'} as $field) {
                     if ($field['type'] == 'comment') {
                         $entry->etymologyComment = $this->readMultiText($field, $this->project->config->entry->fields[LexConfig::ETYMOLOGYCOMMENT]->inputSystems);
                     } else {
                         $this->currentNodeError()->addUnhandledField('etymology: ' . $field['type']);
                     }
                 }
                 break;
             case 'pronunciation':
                 $entry->pronunciation = $this->readMultiText($element, $this->project->config->entry->fields[LexConfig::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->project->config->entry->fields[LexConfig::LITERALMEANING]->inputSystems);
                         break;
                     case 'summary-definition':
                         $entry->summaryDefinition = $this->readMultiText($element, $this->project->config->entry->fields[LexConfig::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 LexSense('');
                             $entry->senses[] = $this->readSense($element, $sense);
                             break;
                         case LiftMergeRule::IMPORT_WINS:
                             $sense = new LexSense($liftId, Guid::extract($liftId));
                             $entry->senses[$existingSenseIndex] = $this->readSense($element, $sense);
                             break;
                         case LiftMergeRule::IMPORT_LOSES:
                             break;
                         default:
                             throw new \Exception("unknown LiftMergeRule " . $mergeRule);
                     }
                 } else {
                     $sense = new LexSense($liftId, Guid::extract($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]);
     }
 }
 public function __construct($guid = '')
 {
     $this->setReadOnlyProp('guid');
     $this->guid = Guid::makeValid($guid);
     $this->initLazyProperties(['paragraphs'], false);
 }