public function mapFields(Content $content) { $versionInfo = $content->versionInfo; $contentInfo = $content->versionInfo->contentInfo; // UserGroups and Users are Content, but permissions cascade is achieved through // Locations hierarchy. We index all ancestor Location Content ids of all // Locations of an owner. $ancestorLocationsContentIds = $this->getAncestorLocationsContentIds($contentInfo->ownerId); // Add owner user id as it can also be considered as user group. $ancestorLocationsContentIds[] = $contentInfo->ownerId; $section = $this->sectionHandler->load($contentInfo->sectionId); return [new Field('content_id', $contentInfo->id, new FieldType\IdentifierField()), new Field('content_type_id', $contentInfo->contentTypeId, new FieldType\IdentifierField()), new Field('content_version_no', $versionInfo->versionNo, new FieldType\IntegerField()), new Field('content_version_status', $versionInfo->status, new FieldType\IdentifierField()), new Field('content_name', $contentInfo->name, new FieldType\StringField()), new Field('content_version_creator_user_id', $versionInfo->creatorId, new FieldType\IdentifierField()), new Field('content_owner_user_id', $contentInfo->ownerId, new FieldType\IdentifierField()), new Field('content_section_id', $contentInfo->sectionId, new FieldType\IdentifierField()), new Field('content_remote_id', $contentInfo->remoteId, new FieldType\IdentifierField()), new Field('content_modification_date', $contentInfo->modificationDate, new FieldType\DateField()), new Field('content_publication_date', $contentInfo->publicationDate, new FieldType\DateField()), new Field('content_language_codes', array_keys($versionInfo->names), new FieldType\MultipleStringField()), new Field('content_main_language_code', $contentInfo->mainLanguageCode, new FieldType\StringField()), new Field('content_always_available', $contentInfo->alwaysAvailable, new FieldType\BooleanField()), new Field('content_owner_user_group_ids', $ancestorLocationsContentIds, new FieldType\MultipleIdentifierField()), new Field('content_section_identifier', $section->identifier, new FieldType\IdentifierField()), new Field('content_section_name', $section->name, new FieldType\StringField()), new Field('content_type_group_ids', $this->contentTypeHandler->load($contentInfo->contentTypeId)->groupIds, new FieldType\MultipleIdentifierField()), new Field('content_object_state_ids', $this->getObjectStateIds($contentInfo->id), new FieldType\MultipleIdentifierField())]; }
/** * Map content to document. * * A document is an array of fields * * @param \eZ\Publish\SPI\Persistence\Content $content * * @return array */ protected function mapContent(Content $content) { $locations = $this->locationHandler->loadLocationsByContent($content->versionInfo->contentInfo->id); $mainLocation = null; foreach ($locations as $location) { if ($location->id == $content->versionInfo->contentInfo->mainLocationId) { $mainLocation = $location; } } $section = $this->sectionHandler->load($content->versionInfo->contentInfo->sectionId); // UserGroups and Users are Content, but permissions cascade is achieved through // Locations hierarchy. We index all ancestor Location Content ids of all // Locations of an owner. $ancestorLocationsContentIds = $this->getAncestorLocationsContentIds($content->versionInfo->contentInfo->ownerId); // Add owner user id as it can also be considered as user group. $ancestorLocationsContentIds[] = $content->versionInfo->contentInfo->ownerId; $document = array(new Field('id', $content->versionInfo->contentInfo->id, new FieldType\IdentifierField()), new Field('type', $content->versionInfo->contentInfo->contentTypeId, new FieldType\IdentifierField()), new Field('version', $content->versionInfo->versionNo, new FieldType\IdentifierField()), new Field('status', $content->versionInfo->status, new FieldType\IdentifierField()), new Field('name', $content->versionInfo->contentInfo->name, new FieldType\StringField()), new Field('creator', $content->versionInfo->creatorId, new FieldType\IdentifierField()), new Field('owner', $content->versionInfo->contentInfo->ownerId, new FieldType\IdentifierField()), new Field('owner_user_group', $ancestorLocationsContentIds, new FieldType\MultipleIdentifierField()), new Field('section', $content->versionInfo->contentInfo->sectionId, new FieldType\IdentifierField()), new Field('section_identifier', $section->identifier, new FieldType\IdentifierField()), new Field('section_name', $section->name, new FieldType\StringField()), new Field('remote_id', $content->versionInfo->contentInfo->remoteId, new FieldType\IdentifierField()), new Field('modified', $content->versionInfo->contentInfo->modificationDate, new FieldType\DateField()), new Field('published', $content->versionInfo->contentInfo->publicationDate, new FieldType\DateField()), new Field('path', array_map(function ($location) { return $location->pathString; }, $locations), new FieldType\MultipleIdentifierField()), new Field('location', array_map(function ($location) { return $location->id; }, $locations), new FieldType\MultipleIdentifierField()), new Field('depth', array_map(function ($location) { return $location->depth; }, $locations), new FieldType\IntegerField()), new Field('priority', array_map(function ($location) { return $location->priority; }, $locations), new FieldType\IntegerField()), new Field('location_parent', array_map(function ($location) { return $location->parentId; }, $locations), new FieldType\MultipleIdentifierField()), new Field('location_remote_id', array_map(function ($location) { return $location->remoteId; }, $locations), new FieldType\MultipleIdentifierField()), new Field('language_code', array_keys($content->versionInfo->names), new FieldType\MultipleStringField()), new Field('main_language_code', $content->versionInfo->contentInfo->mainLanguageCode, new FieldType\StringField()), new Field('invisible', array_map(function ($location) { return $location->invisible; }, $locations), new FieldType\MultipleBooleanField()), new Field('always_available', $content->versionInfo->contentInfo->alwaysAvailable, new FieldType\BooleanField())); if ($mainLocation !== null) { $document[] = new Field('main_location', $mainLocation->id, new FieldType\IdentifierField()); $document[] = new Field('main_location_parent', $mainLocation->parentId, new FieldType\IdentifierField()); $document[] = new Field('main_location_remote_id', $mainLocation->remoteId, new FieldType\IdentifierField()); $document[] = new Field('main_path', $mainLocation->pathString, new FieldType\IdentifierField()); $document[] = new Field('main_depth', $mainLocation->depth, new FieldType\IntegerField()); $document[] = new Field('main_priority', $mainLocation->priority, new FieldType\IntegerField()); } $contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId); $document[] = new Field('group', $contentType->groupIds, new FieldType\MultipleIdentifierField()); foreach ($content->fields as $field) { foreach ($contentType->fieldDefinitions as $fieldDefinition) { if ($fieldDefinition->id !== $field->fieldDefinitionId) { continue; } $fieldType = $this->fieldRegistry->getType($field->type); foreach ($fieldType->getIndexData($field) as $indexField) { $document[] = new Field($this->fieldNameGenerator->getName($indexField->name, $fieldDefinition->identifier, $contentType->identifier), $indexField->value, $indexField->type); } } } $objectStateIds = array(); foreach ($this->objectStateHandler->loadAllGroups() as $objectStateGroup) { $objectStateIds[] = $this->objectStateHandler->getContentState($content->versionInfo->contentInfo->id, $objectStateGroup->id)->id; } $document[] = new Field('object_state', $objectStateIds, new FieldType\MultipleIdentifierField()); return $document; }
/** * @dataProvider providerForTestValidateError * * @param \eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation $limitation * @param int $errorCount */ public function testValidateError(SectionLimitation $limitation, $errorCount) { if (!empty($limitation->limitationValues)) { $this->getPersistenceMock()->expects($this->any())->method('sectionHandler')->will($this->returnValue($this->sectionHandlerMock)); foreach ($limitation->limitationValues as $key => $value) { $this->sectionHandlerMock->expects($this->at($key))->method('load')->with($value)->will($this->throwException(new NotFoundException('Section', $value))); } } else { $this->getPersistenceMock()->expects($this->never())->method($this->anything()); } // Need to create inline instead of depending on testConstruct() to get correct mock instance $limitationType = $this->testConstruct(); $validationErrors = $limitationType->validate($limitation); self::assertCount($errorCount, $validationErrors); }
/** * Deletes $section from content repository * * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified section is not found * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to delete a section * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If section can not be deleted * because it is still assigned to some contents. * * @param \eZ\Publish\API\Repository\Values\Content\Section $section */ public function deleteSection(Section $section) { $loadedSection = $this->loadSection($section->id); if ($this->repository->canUser('section', 'edit', $loadedSection) !== true) { throw new UnauthorizedException('section', 'edit', array('name' => $loadedSection->name)); } if ($this->countAssignedContents($loadedSection) > 0) { throw new BadStateException("section", 'section is still assigned to content'); } $this->repository->beginTransaction(); try { $this->sectionHandler->delete($loadedSection->id); $this->repository->commit(); } catch (Exception $e) { $this->repository->rollback(); throw $e; } }
public function mapLocation(Location $location) { $contentInfo = $this->contentHandler->loadContentInfo($location->contentId); $content = $this->contentHandler->load($location->contentId, $contentInfo->currentVersionNo); $section = $this->sectionHandler->load($content->versionInfo->contentInfo->sectionId); $fields = array(new Field('location', $location->id, new FieldType\IdentifierField()), new Field('document_type', 'location', new FieldType\IdentifierField()), new Field('priority', $location->priority, new FieldType\IntegerField()), new Field('hidden', $location->hidden, new FieldType\BooleanField()), new Field('invisible', $location->invisible, new FieldType\BooleanField()), new Field('remote_id', $location->remoteId, new FieldType\IdentifierField()), new Field('parent_id', $location->parentId, new FieldType\IdentifierField()), new Field('path_string', $location->pathString, new FieldType\IdentifierField()), new Field('depth', $location->depth, new FieldType\IntegerField()), new Field('sort_field', $location->sortField, new FieldType\IdentifierField()), new Field('sort_order', $location->sortOrder, new FieldType\IdentifierField()), new Field('is_main_location', $location->id == $content->versionInfo->contentInfo->mainLocationId, new FieldType\BooleanField())); // UserGroups and Users are Content, but permissions cascade is achieved through // Locations hierarchy. We index all ancestor Location Content ids of all // Locations of an owner. $ancestorLocationsContentIds = $this->getAncestorLocationsContentIds($contentInfo->ownerId); // Add owner user id as it can also be considered as user group. $ancestorLocationsContentIds[] = $contentInfo->ownerId; $fields[] = new Field('content_owner_user_group', $ancestorLocationsContentIds, new FieldType\MultipleIdentifierField()); $fields[] = new Field('content_id', $content->versionInfo->contentInfo->id, new FieldType\IdentifierField()); $fields[] = new Field('content_type', $content->versionInfo->contentInfo->contentTypeId, new FieldType\IdentifierField()); $fields[] = new Field('content_version', $content->versionInfo->versionNo, new FieldType\IdentifierField()); $fields[] = new Field('content_status', $content->versionInfo->status, new FieldType\IdentifierField()); $fields[] = new Field('content_name', $content->versionInfo->contentInfo->name, new FieldType\StringField()); $fields[] = new Field('content_creator', $content->versionInfo->creatorId, new FieldType\IdentifierField()); $fields[] = new Field('content_owner', $content->versionInfo->contentInfo->ownerId, new FieldType\IdentifierField()); $fields[] = new Field('content_section', $content->versionInfo->contentInfo->sectionId, new FieldType\IdentifierField()); $fields[] = new Field('content_section_identifier', $section->identifier, new FieldType\IdentifierField()); $fields[] = new Field('content_section_name', $section->name, new FieldType\StringField()); $fields[] = new Field('content_remote_id', $content->versionInfo->contentInfo->remoteId, new FieldType\IdentifierField()); $fields[] = new Field('content_modified', $content->versionInfo->contentInfo->modificationDate, new FieldType\DateField()); $fields[] = new Field('content_published', $content->versionInfo->contentInfo->publicationDate, new FieldType\DateField()); $fields[] = new Field('language_code', array_keys($content->versionInfo->names), new FieldType\MultipleStringField()); $fields[] = new Field('content_always_available', $content->versionInfo->contentInfo->alwaysAvailable, new FieldType\BooleanField()); $fields[] = new Field('content_group', $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId)->groupIds, new FieldType\MultipleIdentifierField()); $fields[] = new Field('content_object_state', $this->getObjectStateIds($content->versionInfo->contentInfo->id), new FieldType\MultipleIdentifierField()); $contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId); $fieldSets = $this->mapContentFields($content, $contentType, false); $documents = array(); foreach ($fieldSets as $languageCode => $translationFields) { $translationFields[] = new Field('meta_indexed_language_code', $languageCode, new FieldType\StringField()); $translationFields[] = new Field('meta_indexed_is_main_translation', $languageCode === $content->versionInfo->contentInfo->mainLanguageCode, new FieldType\BooleanField()); $translationFields[] = new Field('meta_indexed_is_main_translation_and_always_available', $languageCode === $content->versionInfo->contentInfo->mainLanguageCode && $content->versionInfo->contentInfo->alwaysAvailable, new FieldType\BooleanField()); $isMainTranslation = $content->versionInfo->contentInfo->mainLanguageCode === $languageCode; $alwaysAvailable = $isMainTranslation && $content->versionInfo->contentInfo->alwaysAvailable; $documents[] = new Document(array("id" => $this->generateLocationDocumentId($location, $languageCode), "languageCode" => $languageCode, "alwaysAvailable" => $alwaysAvailable, "isMainTranslation" => $isMainTranslation, "fields" => array_merge($fields, $translationFields))); } return $documents; }
/** * Deletes $section from content repository. * * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified section is not found * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to delete a section * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If section can not be deleted * because it is still assigned to some contents, * or because it is still being used in policy limitations. * * @param \eZ\Publish\API\Repository\Values\Content\Section $section */ public function deleteSection(Section $section) { $loadedSection = $this->loadSection($section->id); if ($this->repository->canUser('section', 'edit', $loadedSection) !== true) { throw new UnauthorizedException('section', 'edit', array('sectionId' => $loadedSection->id)); } if ($this->sectionHandler->assignmentsCount($loadedSection->id) > 0) { throw new BadStateException('section', 'section is still assigned to content'); } if ($this->sectionHandler->policiesCount($loadedSection->id) > 0) { throw new BadStateException('section', 'section is still being used in policy limitations'); } $this->repository->beginTransaction(); try { $this->sectionHandler->delete($loadedSection->id); $this->repository->commit(); } catch (Exception $e) { $this->repository->rollback(); throw $e; } }
/** * Maps given Location to a Document. * * Returned Document represents a "parent" Location document searchable with Location Search. * * @param \eZ\Publish\SPI\Persistence\Content\Location $location * * @return \eZ\Publish\Core\Search\Elasticsearch\Content\Document */ public function mapLocation(Location $location) { $contentInfo = $this->contentHandler->loadContentInfo($location->contentId); $content = $this->contentHandler->load($location->contentId, $contentInfo->currentVersionNo); $section = $this->sectionHandler->load($content->versionInfo->contentInfo->sectionId); $document = $this->mapContentLocation($location, $content); $document->id = $location->id; $document->type = "location"; $document->fields[] = new Field('is_main_location', $location->id == $content->versionInfo->contentInfo->mainLocationId, new FieldType\BooleanField()); // UserGroups and Users are Content, but permissions cascade is achieved through // Locations hierarchy. We index all ancestor Location Content ids of all // Locations of an owner. $ancestorLocationsContentIds = $this->getAncestorLocationsContentIds($contentInfo->ownerId); // Add owner user id as it can also be considered as user group. $ancestorLocationsContentIds[] = $contentInfo->ownerId; $document->fields[] = new Field('content_owner_user_group', $ancestorLocationsContentIds, new FieldType\MultipleIdentifierField()); $document->fields[] = new Field('content_id', $content->versionInfo->contentInfo->id, new FieldType\IdentifierField()); $document->fields[] = new Field('content_type', $content->versionInfo->contentInfo->contentTypeId, new FieldType\IdentifierField()); $document->fields[] = new Field('content_version', $content->versionInfo->versionNo, new FieldType\IdentifierField()); $document->fields[] = new Field('content_status', $content->versionInfo->status, new FieldType\IdentifierField()); $document->fields[] = new Field('content_name', $content->versionInfo->contentInfo->name, new FieldType\StringField()); $document->fields[] = new Field('content_creator', $content->versionInfo->creatorId, new FieldType\IdentifierField()); $document->fields[] = new Field('content_owner', $content->versionInfo->contentInfo->ownerId, new FieldType\IdentifierField()); $document->fields[] = new Field('content_section', $content->versionInfo->contentInfo->sectionId, new FieldType\IdentifierField()); $document->fields[] = new Field('content_section_identifier', $section->identifier, new FieldType\IdentifierField()); $document->fields[] = new Field('content_section_name', $section->name, new FieldType\StringField()); $document->fields[] = new Field('content_remote_id', $content->versionInfo->contentInfo->remoteId, new FieldType\IdentifierField()); $document->fields[] = new Field('content_modified', $content->versionInfo->contentInfo->modificationDate, new FieldType\DateField()); $document->fields[] = new Field('content_published', $content->versionInfo->contentInfo->publicationDate, new FieldType\DateField()); $document->fields[] = new Field('content_language_code', array_keys($content->versionInfo->names), new FieldType\MultipleStringField()); $document->fields[] = new Field('content_always_available', $content->versionInfo->contentInfo->alwaysAvailable, new FieldType\BooleanField()); $contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId); $document->fields[] = new Field('content_group', $contentType->groupIds, new FieldType\MultipleIdentifierField()); $document->fields[] = new Field('content_object_state', $this->getObjectStateIds($content->versionInfo->contentInfo->id), new FieldType\MultipleIdentifierField()); return $document; }
/** * Maps given Content to a Document. * * @param \eZ\Publish\SPI\Persistence\Content $content * * @return \eZ\Publish\SPI\Search\Document[] */ public function mapContentBlock(Content $content) { $locations = $this->locationHandler->loadLocationsByContent($content->versionInfo->contentInfo->id); $section = $this->sectionHandler->load($content->versionInfo->contentInfo->sectionId); $mainLocation = null; $isSomeLocationVisible = false; $locationData = array(); $locationFields = array(); foreach ($locations as $location) { $locationFields[$location->id] = $this->mapLocationFields($location, $content, $section); $locationData['ids'][] = $location->id; $locationData['parent_ids'][] = $location->parentId; $locationData['remote_ids'][] = $location->remoteId; $locationData['path_strings'][] = $location->pathString; if ($location->id == $content->versionInfo->contentInfo->mainLocationId) { $mainLocation = $location; } if (!$location->hidden && !$location->invisible) { $isSomeLocationVisible = true; } } // UserGroups and Users are Content, but permissions cascade is achieved through // Locations hierarchy. We index all ancestor Location Content ids of all // Locations of an owner. $ancestorLocationsContentIds = $this->getAncestorLocationsContentIds($content->versionInfo->contentInfo->ownerId); // Add owner user id as it can also be considered as user group. $ancestorLocationsContentIds[] = $content->versionInfo->contentInfo->ownerId; $fields = array(new Field('content', $content->versionInfo->contentInfo->id, new FieldType\IdentifierField()), new Field('document_type', self::DOCUMENT_TYPE_IDENTIFIER_CONTENT, new FieldType\IdentifierField()), new Field('type', $content->versionInfo->contentInfo->contentTypeId, new FieldType\IdentifierField()), new Field('version_no', $content->versionInfo->versionNo, new FieldType\IntegerField()), new Field('status', $content->versionInfo->status, new FieldType\IdentifierField()), new Field('name', $content->versionInfo->contentInfo->name, new FieldType\StringField()), new Field('creator', $content->versionInfo->creatorId, new FieldType\IdentifierField()), new Field('owner', $content->versionInfo->contentInfo->ownerId, new FieldType\IdentifierField()), new Field('owner_user_group', $ancestorLocationsContentIds, new FieldType\MultipleIdentifierField()), new Field('section', $content->versionInfo->contentInfo->sectionId, new FieldType\IdentifierField()), new Field('section_identifier', $section->identifier, new FieldType\IdentifierField()), new Field('section_name', $section->name, new FieldType\StringField()), new Field('remote_id', $content->versionInfo->contentInfo->remoteId, new FieldType\IdentifierField()), new Field('modified', $content->versionInfo->contentInfo->modificationDate, new FieldType\DateField()), new Field('published', $content->versionInfo->contentInfo->publicationDate, new FieldType\DateField()), new Field('language_code', array_keys($content->versionInfo->names), new FieldType\MultipleStringField()), new Field('main_language_code', $content->versionInfo->contentInfo->mainLanguageCode, new FieldType\StringField()), new Field('always_available', $content->versionInfo->contentInfo->alwaysAvailable, new FieldType\BooleanField()), new Field('location_visible', $isSomeLocationVisible, new FieldType\BooleanField())); if (!empty($locationData)) { $fields[] = new Field('location_id', $locationData['ids'], new FieldType\MultipleIdentifierField()); $fields[] = new Field('location_parent_id', $locationData['parent_ids'], new FieldType\MultipleIdentifierField()); $fields[] = new Field('location_remote_id', $locationData['remote_ids'], new FieldType\MultipleIdentifierField()); $fields[] = new Field('location_path_string', $locationData['path_strings'], new FieldType\MultipleIdentifierField()); } if ($mainLocation !== null) { $fields[] = new Field('main_location', $mainLocation->id, new FieldType\IdentifierField()); $fields[] = new Field('main_location_parent', $mainLocation->parentId, new FieldType\IdentifierField()); $fields[] = new Field('main_location_remote_id', $mainLocation->remoteId, new FieldType\IdentifierField()); $fields[] = new Field('main_location_visible', !$mainLocation->hidden && !$mainLocation->invisible, new FieldType\BooleanField()); $fields[] = new Field('main_location_path', $mainLocation->pathString, new FieldType\IdentifierField()); $fields[] = new Field('main_location_depth', $mainLocation->depth, new FieldType\IntegerField()); $fields[] = new Field('main_location_priority', $mainLocation->priority, new FieldType\IntegerField()); } $contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId); $fields[] = new Field('group', $contentType->groupIds, new FieldType\MultipleIdentifierField()); $fields[] = new Field('object_state', $this->getObjectStateIds($content->versionInfo->contentInfo->id), new FieldType\MultipleIdentifierField()); $fieldSets = $this->mapContentFields($content, $contentType); $documents = array(); foreach ($fieldSets as $languageCode => $translationFields) { $metaFields = array(); $metaFields[] = new Field('meta_indexed_language_code', $languageCode, new FieldType\StringField()); $metaFields[] = new Field('meta_indexed_is_main_translation', $languageCode === $content->versionInfo->contentInfo->mainLanguageCode, new FieldType\BooleanField()); $metaFields[] = new Field('meta_indexed_is_main_translation_and_always_available', $languageCode === $content->versionInfo->contentInfo->mainLanguageCode && $content->versionInfo->contentInfo->alwaysAvailable, new FieldType\BooleanField()); $translationLocationDocuments = array(); foreach ($locations as $location) { $translationLocationDocuments[] = new Document(array('id' => $this->generateLocationDocumentId($location->id, $languageCode), 'fields' => array_merge($locationFields[$location->id], $translationFields['regular'], $metaFields))); } $isMainTranslation = $content->versionInfo->contentInfo->mainLanguageCode === $languageCode; $alwaysAvailable = $isMainTranslation && $content->versionInfo->contentInfo->alwaysAvailable; $documents[] = new Document(array('id' => $this->generateContentDocumentId($content->versionInfo->contentInfo->id, $languageCode), 'languageCode' => $languageCode, 'alwaysAvailable' => $alwaysAvailable, 'isMainTranslation' => $isMainTranslation, 'fields' => array_merge($fields, $translationFields['regular'], $translationFields['fulltext'], $metaFields), 'documents' => $translationLocationDocuments)); } return $documents; }