/** * Prefix url of document with current resourcelocator prefix. * * @param IndexRebuildEvent $event */ public function onIndexRebuild(IndexRebuildEvent $event) { $output = $event->getOutput(); $filter = $event->getFilter(); $output->writeln('<info>Rebuilding content index</info>'); $typeMap = $this->baseMetadataFactory->getPhpcrTypeMap(); $phpcrTypes = []; foreach ($typeMap as $type) { $phpcrType = $type['phpcr_type']; if ($phpcrType !== 'sulu:path') { $phpcrTypes[] = sprintf('[jcr:mixinTypes] = "%s"', $phpcrType); } } $condition = implode(' or ', $phpcrTypes); // TODO: We cannot select all contents via. the parent type, see: https://github.com/jackalope/jackalope-doctrine-dbal/issues/217 $query = $this->documentManager->createQuery('SELECT * FROM [nt:unstructured] AS a WHERE ' . $condition); $count = []; $documents = $query->execute(); $progress = new ProgressHelper(); $progress->start($output, count($documents)); foreach ($documents as $document) { if ($document instanceof SecurityBehavior && !empty($document->getPermissions())) { $progress->advance(); continue; } $locales = $this->inspector->getLocales($document); foreach ($locales as $locale) { try { $this->documentManager->find($document->getUuid(), $locale); $documentClass = get_class($document); if ($filter && !preg_match('{' . $filter . '}', $documentClass)) { continue; } $this->searchManager->index($document, $locale); if (!isset($count[$documentClass])) { $count[$documentClass] = 0; } ++$count[$documentClass]; } catch (\Exception $e) { $output->writeln(sprintf('<error>Error indexing or de-indexing page (path: %s locale: %s)</error>: %s', $this->inspector->getPath($document), $locale, $e->getMessage())); } } $progress->advance(); } $output->writeln(''); foreach ($count as $className => $count) { if ($count == 0) { continue; } $output->writeln(sprintf('<comment>Content</comment>: %s <info>%s</info> indexed', $className, $count)); } }
/** * Updates the route for the given document after a move or copy. * * @param object $document * @param bool $generateRoutes If set to true a route in the routing tree will also be created */ private function updateRoute($document, $generateRoutes) { $locales = $this->documentInspector->getLocales($document); $webspaceKey = $this->documentInspector->getWebspace($document); $uuid = $this->documentInspector->getUuid($document); $path = $this->documentInspector->getPath($document); $parentUuid = $this->documentInspector->getUuid($this->documentInspector->getParent($document)); $defaultNode = $this->defaultSession->getNode($path); $liveNode = $this->liveSession->getNode($path); $resourceLocatorStrategy = $this->resourceLocatorStrategyPool->getStrategyByWebspaceKey($webspaceKey); foreach ($locales as $locale) { $localizedDocument = $this->documentManager->find($uuid, $locale); if ($localizedDocument->getRedirectType() !== RedirectType::NONE) { continue; } $resourceSegmentPropertyName = $this->encoder->localizedSystemName($this->getResourceSegmentProperty($localizedDocument)->getName(), $locale); $this->updateResourceSegmentProperty($defaultNode, $resourceSegmentPropertyName, $parentUuid, $webspaceKey, $locale); if ($liveNode->hasProperty($resourceSegmentPropertyName)) { $this->updateResourceSegmentProperty($liveNode, $resourceSegmentPropertyName, $parentUuid, $webspaceKey, $locale); // if the method is called with the generateRoutes flag it will create a new route // this happens on a move, but not on copy, because copy results in a draft page without url if ($generateRoutes) { $localizedDocument->setResourceSegment($liveNode->getPropertyValue($resourceSegmentPropertyName)); $resourceLocatorStrategy->save($localizedDocument, null); $localizedDocument->setResourceSegment($defaultNode->getPropertyValue($resourceSegmentPropertyName)); } } } }
/** * Prefix url of document with current resourcelocator prefix. * * @param IndexRebuildEvent $event */ public function onIndexRebuild(IndexRebuildEvent $event) { $output = $event->getOutput(); $purge = $event->getPurge(); $filter = $event->getFilter(); $output->writeln('<info>Rebuilding content index</info>'); // TODO: We cannot select all contents via. the parent type, see: https://github.com/jackalope/jackalope-doctrine-dbal/issues/217 $query = $this->documentManager->createQuery('SELECT * FROM [nt:unstructured] AS a WHERE [jcr:mixinTypes] = "sulu:page" or [jcr:mixinTypes] = "sulu:snippet"'); $count = []; if ($purge) { $this->purgeContentIndexes($output); } $documents = $query->execute(); $progress = new ProgressHelper(); $progress->start($output, count($documents)); foreach ($documents as $document) { $locales = $this->inspector->getLocales($document); foreach ($locales as $locale) { try { $this->documentManager->find($document->getUuid(), $locale); $documentClass = get_class($document); if ($filter && !preg_match('{' . $filter . '}', $documentClass)) { continue; } $this->searchManager->index($document, $locale); if (!isset($count[$documentClass])) { $count[$documentClass] = 0; } ++$count[$documentClass]; } catch (\Exception $e) { $output->writeln(sprintf('<error>Error indexing or de-indexing page (path: %s locale: %s)</error>: %s', $this->inspector->getPath($document), $locale, $e->getMessage())); } } $progress->advance(); } $output->writeln(''); foreach ($count as $className => $count) { if ($count == 0) { continue; } $output->writeln(sprintf('<comment>Content</comment>: %s <info>%s</info> indexed', $className, $count)); } }
/** * Upgrades the node to new URL representation. * * @param NodeInterface $node The node to be upgraded * @param string $locale The locale of the node to be upgraded * @param array $properties The properties which are or contain URL fields * @param bool $addScheme Adds the scheme to URLs if true, removes the scheme otherwise */ private function upgradeNode(NodeInterface $node, $locale, $properties, $addScheme) { /** @var BasePageDocument $document */ $document = $this->documentManager->find($node->getIdentifier(), $locale); $documentLocales = $this->documentInspector->getLocales($document); if (!in_array($locale, $documentLocales)) { return; } foreach ($properties as $property) { $this->upgradeProperty($document->getStructure()->getProperty($property), $addScheme); } $this->documentManager->persist($document, $locale); }
/** * It should return any localizations if neither parent nor children. */ public function testWebspaceAnyLocalization() { $this->inspector->getWebspace($this->document->reveal())->willReturn(self::FIX_WEBSPACE); $this->inspector->getLocales($this->document->reveal())->willReturn(['de']); $this->webspace->getLocalization(self::FIX_LOCALE)->willReturn($this->localization1->reveal()); $this->localization1->getLocalization()->willReturn('en'); $this->localization2->getLocalization()->willReturn('de'); $this->hydrateEvent->getOption('load_ghost_content', true)->willReturn(true); $this->localization1->getParent()->willReturn(null); $this->localization1->getChildren()->willReturn([]); $this->webspace->getLocalizations()->willReturn([$this->localization2->reveal()]); $this->registry->updateLocale($this->document->reveal(), 'de', 'en')->shouldBeCalled(); $this->hydrateEvent->setLocale('de')->shouldBeCalled(); $this->subscriber->handleHydrate($this->hydrateEvent->reveal()); }
public function testOnIndexRebuild() { $event = $this->prophesize(IndexRebuildEvent::class); $query = $this->prophesize(Query::class); $document = $this->prophesize(StructureBehavior::class); $document->willImplement(UuidBehavior::class); $securableDocument = $this->prophesize(StructureBehavior::class); $securableDocument->willImplement(SecurityBehavior::class); $securableDocument->willImplement(UuidBehavior::class); $securableDocument->getPermissions()->willReturn([]); $securedDocument = $this->prophesize(StructureBehavior::class); $securedDocument->willImplement(SecurityBehavior::class); $securedDocument->willImplement(UuidBehavior::class); $securedDocument->getPermissions()->willReturn(['some' => 'permissions']); $typemap = [['phpcr_type' => 'page'], ['phpcr_type' => 'home'], ['phpcr_type' => 'snippet']]; $output = $this->prophesize(OutputInterface::class); $event->getOutput()->willReturn($output->reveal()); $event->getFilter()->shouldBeCalled(); $this->baseMetadataFactory->getPhpcrTypeMap()->shouldBeCalled()->willReturn($typemap); $this->documentManager->createQuery('SELECT * FROM [nt:unstructured] AS a WHERE [jcr:mixinTypes] = "page" or [jcr:mixinTypes] = "home" or [jcr:mixinTypes] = "snippet"')->shouldBeCalled()->willReturn($query->reveal()); $query->execute()->shouldBeCalled()->willReturn([$document->reveal(), $securableDocument->reveal(), $securedDocument->reveal()]); $this->inspector->getLocales($document->reveal())->shouldBeCalled()->willReturn(['de', 'en']); $document->getUuid()->shouldBeCalled()->willReturn('1'); $this->documentManager->find('1', 'en')->shouldBeCalled(); $this->searchManager->index($document->reveal(), 'en')->shouldBeCalled(); $this->documentManager->find('1', 'de')->shouldBeCalled(); $this->searchManager->index($document->reveal(), 'de')->shouldBeCalled(); $this->inspector->getLocales($securableDocument->reveal())->shouldBeCalled()->willReturn(['de']); $securableDocument->getUuid()->willReturn('2'); $this->documentManager->find('2', 'de')->shouldBeCalled(); $this->searchManager->index($securableDocument->reveal(), 'de')->shouldBeCalled(); $securedDocument->getUuid()->willReturn('3'); $this->documentManager->find('3', 'de')->shouldNotBeCalled(); $this->searchManager->index($securedDocument->reveal(), 'de')->shouldNotBeCalled(); $this->reindexListener->onIndexRebuild($event->reveal()); }
/** * Upgrades the node to new date representation. * * @param NodeInterface $node The node to be upgraded * @param string $locale The locale of the node to be upgraded * @param array $properties The properties which are or contain date fields * @param bool $up */ private function upgradeNode(NodeInterface $node, $locale, array $properties, $up) { /** @var BasePageDocument $document */ $document = $this->documentManager->find($node->getIdentifier(), $locale); $documentLocales = $this->documentInspector->getLocales($document); if (!in_array($locale, $documentLocales)) { return; } foreach ($properties as $property) { if ($property['property'] instanceof BlockMetadata) { $this->upgradeBlockProperty($property['property'], $property['components'], $node, $locale, $up); } else { $this->upgradeProperty($property['property'], $node, $locale, $up); } } $this->documentManager->persist($document, $locale, ['auto_name' => false]); }
/** * {@inheritdoc} */ public function toArray($complete = true) { $document = $this->getDocument(); $result = ['id' => $this->inspector->getUuid($document), 'path' => $this->inspector->getContentPath($document), 'nodeType' => $this->getNodeType(), 'nodeState' => $this->getNodeState(), 'internal' => false, 'concreteLanguages' => $this->inspector->getLocales($document), 'hasSub' => $this->getHasChildren(), 'title' => $document->getTitle()]; if ($document instanceof OrderBehavior) { $result['order'] = $document->getSuluOrder(); } if ($document instanceof RedirectTypeBehavior) { $redirectType = $document->getRedirectType(); $result['linked'] = null; if ($redirectType == RedirectType::INTERNAL && $document->getRedirectTarget() !== null) { $result['linked'] = 'internal'; $result['internal_link'] = $document->getRedirectTarget()->getUuid(); } elseif ($redirectType == RedirectType::EXTERNAL) { $result['linked'] = 'external'; $result['external'] = $document->getRedirectExternal(); } } if ($document instanceof WorkflowStageBehavior) { $result['publishedState'] = $document->getWorkflowStage() === WorkflowStage::PUBLISHED; $result['published'] = $document->getPublished(); } $result['navContexts'] = []; if ($document instanceof NavigationContextBehavior) { $result['navContexts'] = $document->getNavigationContexts(); } if (null !== $this->getType()) { $result['type'] = $this->getType()->toArray(); } if ($complete) { if ($document instanceof ShadowLocaleBehavior) { $result = array_merge($result, ['enabledShadowLanguages' => $this->inspector->getShadowLocales($document), 'shadowOn' => $document->isShadowLocaleEnabled(), 'shadowBaseLanguage' => $document->getShadowLocale() ?: false]); } $result = array_merge($result, ['template' => $this->structure->getName(), 'originTemplate' => $this->structure->getName(), 'creator' => $document->getCreator(), 'changer' => $document->getChanger(), 'created' => $document->getCreated(), 'changed' => $document->getChanged(), 'title' => $document->getTitle(), 'url' => null]); if ($document instanceof ResourceSegmentBehavior) { $result['url'] = $document->getResourceSegment(); } if ($document instanceof ExtensionBehavior) { $result['ext'] = $document->getExtensionsData(); } $result = array_merge($this->getDocument()->getStructure()->toArray(), $result); return $result; } return $result; }
/** * Upgrades the node to new URL representation. * * @param NodeInterface $node The node to be upgraded * @param string $locale The locale of the node to be upgraded * @param array $properties The properties which are or contain URL fields * @param bool $addScheme Adds the scheme to URLs if true, removes the scheme otherwise */ private function upgradeNode(NodeInterface $node, $locale, array $properties, $addScheme) { /** @var BasePageDocument $document */ $document = $this->documentManager->find($node->getIdentifier(), $locale); $documentLocales = $this->documentInspector->getLocales($document); if (!in_array($locale, $documentLocales)) { return; } foreach ($properties as $property) { $propertyValue = $document->getStructure()->getProperty($property['property']->getName()); if ($property['property'] instanceof BlockMetadata) { $this->upgradeBlockProperty($property['property'], $property['components'], $propertyValue, $addScheme); } else { $this->upgradeProperty($propertyValue, $addScheme); } } $this->documentManager->persist($document, $locale); }
/** * Return available localizations. * * @param StructureBehavior $document * @param string $locale * * @return string */ public function getAvailableLocalization(StructureBehavior $document, $locale) { $availableLocales = $this->inspector->getLocales($document); if (in_array($locale, $availableLocales)) { return $locale; } $fallbackLocale = null; if ($document instanceof WebspaceBehavior) { $fallbackLocale = $this->localizationFinder->findAvailableLocale($this->inspector->getWebspace($document), $availableLocales, $locale); } if (!$fallbackLocale) { $fallbackLocale = reset($availableLocales); } if (!$fallbackLocale) { $fallbackLocale = $this->documentRegistry->getDefaultLocale(); } return $fallbackLocale; }
/** * {@inheritdoc} */ public function getLocalesForObject($object) { return $this->inspector->getLocales($object); }