Skip to content

Commit

Permalink
Shadow Elasticsearch (#2)
Browse files Browse the repository at this point in the history
* First init

* Second

* Finished

* Fixed bug

* Fixed bug

* Use correct locale

* Reload document

* Use of rehydrate

* Load correct route for shadows

* Use of originalLocale

* Reload document when indexing shadow

* Add functional controller tests

* Add ArticleIndexerTest

* Fix test
  • Loading branch information
trickreich committed Jun 14, 2018
1 parent c29d520 commit 839667e
Show file tree
Hide file tree
Showing 16 changed files with 461 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# PHPUnit
/app/config/parameters.yml
/Tests/app/config/parameters.yml
/app/phpunit.xml
/phpunit.xml

Expand Down
39 changes: 29 additions & 10 deletions Document/Index/ArticleGhostIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sulu\Bundle\ArticleBundle\Document\Index\Factory\ExcerptFactory;
use Sulu\Bundle\ArticleBundle\Document\Index\Factory\SeoFactory;
use Sulu\Bundle\ContactBundle\Entity\ContactRepository;
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
use Sulu\Bundle\SecurityBundle\UserManager\UserManager;
use Sulu\Component\Content\Document\LocalizationState;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
Expand Down Expand Up @@ -50,9 +51,10 @@ class ArticleGhostIndexer extends ArticleIndexer
* @param SeoFactory $seoFactory
* @param EventDispatcherInterface $eventDispatcher
* @param TranslatorInterface $translator
* @param DocumentManagerInterface $documentManager
* @param DocumentInspector $inspector
* @param array $typeConfiguration
* @param WebspaceManagerInterface $webspaceManager
* @param DocumentManagerInterface $documentManager
*/
public function __construct(
StructureMetadataFactoryInterface $structureMetadataFactory,
Expand All @@ -64,9 +66,10 @@ public function __construct(
SeoFactory $seoFactory,
EventDispatcherInterface $eventDispatcher,
TranslatorInterface $translator,
DocumentManagerInterface $documentManager,
DocumentInspector $inspector,
array $typeConfiguration,
WebspaceManagerInterface $webspaceManager,
DocumentManagerInterface $documentManager
WebspaceManagerInterface $webspaceManager
) {
parent::__construct(
$structureMetadataFactory,
Expand All @@ -78,6 +81,8 @@ public function __construct(
$seoFactory,
$eventDispatcher,
$translator,
$documentManager,
$inspector,
$typeConfiguration
);

Expand All @@ -90,7 +95,14 @@ public function __construct(
*/
public function index(ArticleDocument $document)
{
if ($document->isShadowLocaleEnabled()) {
$this->indexShadow($document);

return;
}

$article = $this->createOrUpdateArticle($document, $document->getLocale());
$this->createOrUpdateShadows($document);
$this->createOrUpdateGhosts($document);
$this->dispatchIndexEvent($document, $article);
$this->manager->persist($article);
Expand All @@ -109,15 +121,22 @@ private function createOrUpdateGhosts(ArticleDocument $document)
continue;
}

/** @var ArticleDocument $ghostDocument */
$ghostDocument = $this->documentManager->find(
$document->getUuid(),
$locale
);

$localizationState = $this->inspector->getLocalizationState($ghostDocument);

// Only index ghosts
if (LocalizationState::GHOST !== $localizationState) {
continue;
}

// Try index the article ghosts.
$article = $this->createOrUpdateArticle(
$this->documentManager->find(
$document->getUuid(),
$locale,
[
'load_ghost_content' => true,
]
),
$ghostDocument,
$localization->getLocale(),
LocalizationState::GHOST
);
Expand Down
67 changes: 67 additions & 0 deletions Document/Index/ArticleIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
use Sulu\Bundle\ArticleBundle\Metadata\StructureTagTrait;
use Sulu\Bundle\ContactBundle\Entity\ContactRepository;
use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
use Sulu\Bundle\SecurityBundle\UserManager\UserManager;
use Sulu\Component\Content\Document\LocalizationState;
use Sulu\Component\Content\Document\WorkflowStage;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\Content\Metadata\PropertyMetadata;
use Sulu\Component\Content\Metadata\StructureMetadata;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Exception\DocumentManagerException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;

Expand Down Expand Up @@ -91,6 +94,16 @@ class ArticleIndexer implements IndexerInterface
*/
protected $translator;

/**
* @var DocumentManagerInterface
*/
protected $documentManager;

/**
* @var DocumentInspector
*/
protected $inspector;

/**
* @var array
*/
Expand All @@ -106,6 +119,8 @@ class ArticleIndexer implements IndexerInterface
* @param SeoFactory $seoFactory
* @param EventDispatcherInterface $eventDispatcher
* @param TranslatorInterface $translator
* @param DocumentManagerInterface $documentManager
* @param DocumentInspector $inspector
* @param array $typeConfiguration
*/
public function __construct(
Expand All @@ -118,6 +133,8 @@ public function __construct(
SeoFactory $seoFactory,
EventDispatcherInterface $eventDispatcher,
TranslatorInterface $translator,
DocumentManagerInterface $documentManager,
DocumentInspector $inspector,
array $typeConfiguration
) {
$this->structureMetadataFactory = $structureMetadataFactory;
Expand All @@ -129,6 +146,8 @@ public function __construct(
$this->seoFactory = $seoFactory;
$this->eventDispatcher = $eventDispatcher;
$this->translator = $translator;
$this->documentManager = $documentManager;
$this->inspector = $inspector;
$this->typeConfiguration = $typeConfiguration;
}

Expand Down Expand Up @@ -432,9 +451,57 @@ public function setUnpublished($uuid, $locale)
*/
public function index(ArticleDocument $document)
{
if ($document->isShadowLocaleEnabled()) {
$this->indexShadow($document);

return;
}

$article = $this->createOrUpdateArticle($document, $document->getLocale());

$this->dispatchIndexEvent($document, $article);
$this->manager->persist($article);

$this->createOrUpdateShadows($document);
}

/**
* @param ArticleDocument $document
*/
protected function indexShadow(ArticleDocument $document)
{
$shadowDocument = $this->documentManager->find(
$document->getUuid(),
$document->getOriginalLocale(),
[
'rehydrate' => true,
]
);

$article = $this->createOrUpdateArticle($shadowDocument, $document->getOriginalLocale(), LocalizationState::SHADOW);

$this->dispatchIndexEvent($shadowDocument, $article);
$this->manager->persist($article);
}

/**
* @param ArticleDocument $document
*/
protected function createOrUpdateShadows(ArticleDocument $document)
{
if ($document->isShadowLocaleEnabled()) {
return;
}

foreach ($this->inspector->getShadowLocales($document) as $shadowLocale) {
try {
/** @var ArticleDocument $shadowDocument */
$shadowDocument = $this->documentManager->find($document->getUuid(), $shadowLocale);
$this->indexShadow($shadowDocument);
} catch (DocumentManagerException $documentManagerException) {
// do nothing
}
}
}

/**
Expand Down
8 changes: 6 additions & 2 deletions Document/Subscriber/RoutableSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,15 @@ public function handleHydrate(AbstractMappingEvent $event)
return;
}

$propertyName = $this->getRoutePathPropertyName($document->getStructureType(), $event->getLocale());
$propertyName = $this->getRoutePathPropertyName($document->getStructureType(), $document->getOriginalLocale());
$routePath = $event->getNode()->getPropertyValueWithDefault($propertyName, null);
$document->setRoutePath($routePath);

$route = $this->routeRepository->findByEntity($document->getClass(), $document->getUuid(), $event->getLocale());
$route = $this->routeRepository->findByEntity(
$document->getClass(),
$document->getUuid(),
$document->getOriginalLocale()
);
if ($route) {
$document->setRoute($route);
}
Expand Down
5 changes: 4 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@
<argument type="service" id="sulu_article.elastic_search.factory.seo"/>
<argument type="service" id="event_dispatcher"/>
<argument type="service" id="translator"/>
<argument type="service" id="sulu_document_manager.document_manager"/>
<argument type="service" id="sulu_document_manager.document_inspector"/>
<argument>%sulu_article.types%</argument>
<argument type="service" id="sulu_core.webspace.webspace_manager"/>
<argument type="service" id="sulu_document_manager.document_manager"/>
</service>
<service id="sulu_article.elastic_search.article_live_indexer"
class="Sulu\Bundle\ArticleBundle\Document\Index\ArticleIndexer">
Expand All @@ -64,6 +65,8 @@
<argument type="service" id="sulu_article.elastic_search.factory.seo"/>
<argument type="service" id="event_dispatcher"/>
<argument type="service" id="translator"/>
<argument type="service" id="sulu_document_manager.document_manager"/>
<argument type="service" id="sulu_document_manager.document_inspector" />
<argument>%sulu_article.types%</argument>
</service>

Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Resources/public/dist/services/list-helper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 10 additions & 21 deletions Resources/public/js/components/articles/edit/settings/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ define([
changedOnly: 'sulu_article.form.settings.changelog.changed-only',
created: 'sulu_article.form.settings.changelog.created',
createdOnly: 'sulu_article.form.settings.changelog.created-only',
shadowPage: 'sulu.content.form.settings.shadow_page',
shadowEnable: 'sulu.content.form.settings.shadow.enable',
publishedShadow: 'sulu-content.published-shadow',
shadowBaseLanguage: 'sulu.content.form.settings.shadow.base_language',
shadowArticle: 'sulu_article.shadow_article',
shadowEnable: 'sulu_article.form.settings.shadow.enable',
publishedShadow: 'sulu_article.published-shadow',
shadowBaseLanguage: 'sulu_article.form.settings.shadow.base_language',
}
},

Expand Down Expand Up @@ -171,22 +171,15 @@ define([
}
}
]);
},

updateVisibilityForShadowCheckbox: function(isInitial) {
var shadow = isShadow.call(this),
tabAction,
$shadowDescription = this.sandbox.dom.find('#shadow-container .input-description');

if (false === isInitial) {
tabAction = 'hide';
if (this.data.shadowOn) {
this.sandbox.dom.attr('#shadow_on_checkbox', 'checked', true);
}
},

if (tabAction === 'hide') {
this.sandbox.emit('husky.toolbar.header.item.disable', 'state', false);
} else {
this.sandbox.emit('husky.toolbar.header.item.enable', 'state', false);
}
updateVisibilityForShadowCheckbox: function(isInitial) {
var shadow = isShadow.call(this);
var $shadowDescription = this.sandbox.dom.find('#shadow-container .input-description');

if (!!shadow) {
this.sandbox.emit('sulu.article.show-save-items', 'shadow');
Expand All @@ -203,10 +196,6 @@ define([

rendered: function() {
this.updateChangelog(this.data);
if (this.data.shadowOn) {
this.sandbox.dom.attr('#shadow_on_checkbox', 'checked', true);
this.sandbox.emit('husky.toolbar.header.item.disable', 'state', false);
}
this.bindDomEvents();
},

Expand Down
11 changes: 11 additions & 0 deletions Resources/public/js/services/list-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ define(['underscore', 'services/husky/translator'], function(_, translator) {
published: 'public.published',
unpublished: 'public.unpublished',
publishedWithDraft: 'public.published-with-draft',
shadowArticle: 'sulu_article.shadow_article',
filterByAuthor: 'sulu_article.list.filter.by-author',
filterMe: 'sulu_article.list.filter.me',
filterByCategory: 'sulu_article.list.filter.by-category',
Expand All @@ -44,6 +45,7 @@ define(['underscore', 'services/husky/translator'], function(_, translator) {
templates = {
draftIcon: _.template('<span class="draft-icon" title="<%= title %>"/>'),
publishedIcon: _.template('<span class="published-icon" title="<%= title %>"/>'),
shadowIcon: _.template('<span class="fa-share" title="<%= title %>"></span>'),
};

return {
Expand Down Expand Up @@ -171,6 +173,15 @@ define(['underscore', 'services/husky/translator'], function(_, translator) {
return badge;
}

if (!!item.localizationState &&
item.localizationState.state === 'shadow'
) {
badge.title = templates.shadowIcon({title: translations.shadowArticle});
badge.cssClass = 'badge-none badge-color-black';

return badge;
}

return false;
}
};
Expand Down
24 changes: 24 additions & 0 deletions Resources/translations/sulu/backend.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,30 @@
<source>sulu_article.broken-template.title</source>
<target>Artikel-Template ist beschädigt</target>
</trans-unit>
<trans-unit id="shadow-01" resname="sulu_article.shadow_article">
<source>sulu_article.shadow_article</source>
<target>Shadow Artikel</target>
</trans-unit>
<trans-unit id="shadow-02" resname="sulu_article.published-shadow">
<source>sulu_article.published-shadow</source>
<target>Der Veröffentlichungsstatus wird vom Status des Hauptartikels übernommen.</target>
</trans-unit>
<trans-unit id="shadow-03" resname="sulu_article.form.settings.shadow.enable">
<source>sulu_article.form.settings.shadow.enable</source>
<target>Shadow Artikel aktivieren</target>
</trans-unit>
<trans-unit id="shadow-04" resname="sulu_article.form.settings.shadow.base_language">
<source>sulu_article.form.settings.shadow.base_language</source>
<target>Shadow Artikel Sprache</target>
</trans-unit>
<trans-unit id="shadow-05" resname="sulu_article.form.settings.shadow.no_base_language">
<source>sulu_article.form.settings.shadow.no_base_language</source>
<target>Keine Shadow Artikel Sprache vorhanden ...</target>
</trans-unit>
<trans-unit id="shadow-06" resname="sulu_article.form.settings.shadow.select_base_language">
<source>sulu_article.form.settings.shadow.select_base_language</source>
<target>Sprache auswählen ...</target>
</trans-unit>
</body>
</file>
</xliff>
Loading

0 comments on commit 839667e

Please sign in to comment.