public function testOnPermissionUpdateWrongType() { $event = new PermissionUpdateEvent(\stdClass::class, '1', null); $this->fileVersionMetaRepository->findByCollectionId(Argument::any())->shouldNotBeCalled(); $this->searchManager->deindex(Argument::any())->shouldNotBeCalled(); $this->permissionListener->onPermissionUpdate($event); }
public function onPermissionUpdate(PermissionUpdateEvent $permissionUpdateEvent) { if ($permissionUpdateEvent->getType() !== SecurityBehavior::class) { return; } $document = $this->documentManager->find($permissionUpdateEvent->getIdentifier()); $this->searchManager->deindex($document); }
/** * Schedules a document to be deindexed. * * @param RemoveEvent $event */ public function handlePreRemove(RemoveEvent $event) { $document = $event->getDocument(); if (!$document instanceof StructureBehavior) { return; } $this->searchManager->deindex($document); }
public function testHandlePreRemove() { $removeEvent = $this->prophesize(RemoveEvent::class); $document = $this->prophesize(StructureBehavior::class); $removeEvent->getDocument()->willReturn($document); $this->searchManager->deindex($document)->shouldBeCalled(); $this->subscriber->handlePreRemove($removeEvent->reveal()); }
/** * Removes all FileVersionMetas belonging to the collection, which just got secured. * * @param PermissionUpdateEvent $event */ public function onPermissionUpdate(PermissionUpdateEvent $event) { if ($event->getType() !== Collection::class) { return; } foreach ($this->fileVersionMetaRepository->findByCollectionId($event->getIdentifier()) as $fileVersionMeta) { $this->searchManager->deindex($fileVersionMeta); } }
/** * Deindexes the document from the search index for the website. * * @param UnpublishEvent $event */ public function deindexUnpublishedDocument(UnpublishEvent $event) { $document = $event->getDocument(); if (!$document instanceof StructureBehavior) { return; } $this->searchManager->deindex($document); }
private function purgeContentIndexes($output) { foreach ($this->mapping as $structureMapping) { $structureIndexName = $structureMapping['index']; $output->writeln('<comment>Purging index</comment>: ' . $structureIndexName); $this->searchManager->purge($structureIndexName); } }
/** * Return the category totals for the search results. * * @param Hit[] * * @return array */ private function getCategoryTotals($hits) { $categoryNames = $this->searchManager->getCategoryNames(); $categoryCount = array_combine($categoryNames, array_fill(0, count($categoryNames), 0)); foreach ($hits as $hit) { $category = $hit->getDocument()->getCategory(); ++$categoryCount[$category]; } return $categoryCount; }
/** * Returns the search results for the given query. * * @param Request $request * * @return Response */ public function queryAction(Request $request) { $query = $this->getRequestParameter($request, 'q', true); $locale = $this->requestAnalyzer->getCurrentLocalization()->getLocale(); $webspace = $this->requestAnalyzer->getWebspace(); $queryString = ''; if (strlen($query) < 3) { $queryString .= '+("' . self::escapeDoubleQuotes($query) . '") '; } else { $queryValues = explode(' ', $query); foreach ($queryValues as $queryValue) { if (strlen($queryValue) > 2) { $queryString .= '+("' . self::escapeDoubleQuotes($queryValue) . '" OR ' . preg_replace('/([^\\pL\\s\\d])/u', '?', $queryValue) . '* OR ' . preg_replace('/([^\\pL\\s\\d])/u', '', $queryValue) . '~) '; } else { $queryString .= '+("' . self::escapeDoubleQuotes($queryValue) . '") '; } } } $hits = $this->searchManager->createSearch($queryString)->locale($locale)->index('page_' . $webspace->getKey() . '_published')->execute(); return $this->engine->renderResponse($webspace->getTemplate('search'), $this->parameterResolver->resolve(['query' => $query, 'hits' => $hits], $this->requestAnalyzer)); }
/** * 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)); } }
/** * testItLogsToOutputIfAnExceptionIsThrownDuringIndexing */ public function testItLogsToOutputIfAnExceptionIsThrownDuringIndexing() { $this->container->expects($this->at(0))->method('get')->with($this->equalTo('sulu_event.event_manager'))->will($this->returnValue($this->eventManager)); $this->container->expects($this->at(1))->method('get')->with($this->equalTo('massive_search.search_manager'))->will($this->returnValue($this->searchManager)); $eventMock = $this->getMock(Event::class); $eventMock->expects($this->any())->method('getTitle')->will($this->returnValue('FooBarEvent')); $eventMocks = array($eventMock); $this->eventManager->expects($this->once())->method('findAll')->will($this->returnValue($eventMocks)); $this->searchManager->expects($this->exactly(1))->method('index')->with($eventMock)->willThrowException(new \Exception('Something went wrong!')); $this->output->expects($this->at(1))->method('writeln')->with($this->stringContains('(path: FooBarEvent: Something went wrong!')); $reindexCommand = new ReindexCommand(); $reindexCommand->setContainer($this->container); $reindexCommand->execute($this->input, $this->output); }
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()); }
/** * @return array */ private function getAllowedIndexes() { $allowedIndexNames = []; $indexNames = $this->searchManager->getIndexNames(); foreach ($indexNames as $indexName) { $indexConfiguration = $this->indexConfigurationProvider->getIndexConfiguration($indexName); if (!$indexConfiguration) { $allowedIndexNames[] = $indexName; continue; } if ($this->securityChecker->hasPermission($indexConfiguration->getSecurityContext(), 'view')) { $allowedIndexNames[] = $indexName; } } return $allowedIndexNames; }
/** * @return array */ private function getAllowedIndexes() { $allowedIndexNames = []; $indexNames = $this->searchManager->getIndexNames(); foreach ($indexNames as $indexName) { $indexConfiguration = $this->indexConfigurationProvider->getIndexConfiguration($indexName); if (!$indexConfiguration) { $allowedIndexNames[] = $indexName; continue; } $contexts = $indexConfiguration->getContexts(); if ($this->securityChecker->hasPermission($indexConfiguration->getSecurityContext(), PermissionTypes::VIEW) && (empty($contexts) || array_search('admin', $contexts) !== false)) { $allowedIndexNames[] = $indexName; } } return $allowedIndexNames; }
public function testOnPermissionUpdateNotSecured() { $event = new PermissionUpdateEvent(\stdClass::class, '1', null); $this->searchManager->deindex(Argument::any())->shouldNotBeCalled(); $this->permissionListener->onPermissionUpdate($event); }
/** * Returns page indexes. * * @return array */ private function getPageIndexes() { return array_filter($this->searchManager->getIndexNames(), function ($index) { return preg_match('/page_(.*)_published/', $index) > 0; }); }