-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented twig methods sulu_article_load_recent
sulu_article_load_similar
#193
Merged
wachterjohannes
merged 15 commits into
sulu:develop
from
trickreich:feature/similar-recent-articles
Jun 14, 2017
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
95587e9
Implemented twig methods `sulu_article_load_recent` `sulu_article_loa…
02b2592
Code clean up
84a84a0
Merge branch 'develop' of github.com:sulu/SuluArticleBundle into feat…
b3e9203
Code clean up
d4b3e33
Implemented tests
f5d8f5e
StyleCI fix
5d6ac14
Code clean up
d51cb00
Fixed bugs
197bfb5
Added authored
5f9dfaf
Code clean up
751fbaf
Fixed StyleCI
840334c
Code clean up
faa6ab3
Fixed comment
1656f5f
Fixed tests
9e2af12
Fixed sort order
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Content; | ||
|
||
use ProxyManager\Factory\LazyLoadingValueHolderFactory; | ||
use ProxyManager\Proxy\LazyLoadingInterface; | ||
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument; | ||
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface; | ||
use Sulu\Component\DocumentManager\DocumentManagerInterface; | ||
|
||
/** | ||
* Creates article resource items for given article view document. | ||
*/ | ||
class ArticleResourceItemFactory | ||
{ | ||
/** | ||
* @var DocumentManagerInterface | ||
*/ | ||
protected $documentManager; | ||
|
||
/** | ||
* @var LazyLoadingValueHolderFactory | ||
*/ | ||
protected $proxyFactory; | ||
|
||
/** | ||
* @param DocumentManagerInterface $documentManager | ||
* @param LazyLoadingValueHolderFactory $proxyFactory | ||
*/ | ||
public function __construct( | ||
DocumentManagerInterface $documentManager, | ||
LazyLoadingValueHolderFactory $proxyFactory | ||
) { | ||
$this->documentManager = $documentManager; | ||
$this->proxyFactory = $proxyFactory; | ||
} | ||
|
||
/** | ||
* Creates and returns article source item with proxy document. | ||
* | ||
* @param ArticleViewDocumentInterface $articleViewDocument | ||
* | ||
* @return ArticleResourceItem | ||
*/ | ||
public function createResourceItem(ArticleViewDocumentInterface $articleViewDocument) | ||
{ | ||
return new ArticleResourceItem( | ||
$articleViewDocument, | ||
$this->getResource($articleViewDocument->getUuid(), $articleViewDocument->getLocale()) | ||
); | ||
} | ||
|
||
/** | ||
* Returns Proxy document for uuid. | ||
* | ||
* @param string $uuid | ||
* @param string $locale | ||
* | ||
* @return object | ||
*/ | ||
private function getResource($uuid, $locale) | ||
{ | ||
return $this->proxyFactory->createProxy( | ||
ArticleDocument::class, | ||
function ( | ||
&$wrappedObject, | ||
LazyLoadingInterface $proxy, | ||
$method, | ||
array $parameters, | ||
&$initializer | ||
) use ($uuid, $locale) { | ||
$initializer = null; | ||
$wrappedObject = $this->documentManager->find($uuid, $locale); | ||
|
||
return true; | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Document\Repository; | ||
|
||
use ONGR\ElasticsearchBundle\Result\DocumentIterator; | ||
use ONGR\ElasticsearchBundle\Service\Manager; | ||
use ONGR\ElasticsearchBundle\Service\Repository; | ||
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery; | ||
use ONGR\ElasticsearchDSL\Query\Specialized\MoreLikeThisQuery; | ||
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; | ||
use ONGR\ElasticsearchDSL\Search; | ||
use ONGR\ElasticsearchDSL\Sort\FieldSort; | ||
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait; | ||
use Sulu\Component\DocumentManager\DocumentManagerInterface; | ||
|
||
/** | ||
* Find article view documents in elasticsearch index. | ||
*/ | ||
class ArticleViewDocumentRepository | ||
{ | ||
use ArticleViewDocumentIdTrait; | ||
|
||
const DEFAULT_LIMIT = 5; | ||
|
||
/** | ||
* @var Manager | ||
*/ | ||
protected $searchManager; | ||
|
||
/** | ||
* @var DocumentManagerInterface | ||
*/ | ||
protected $documentManager; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $articleDocumentClass; | ||
|
||
/** | ||
* @var Repository | ||
*/ | ||
protected $repository; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $searchFields; | ||
|
||
/** | ||
* @param Manager $searchManager | ||
* @param string $articleDocumentClass | ||
* @param array $searchFields | ||
*/ | ||
public function __construct( | ||
Manager $searchManager, | ||
$articleDocumentClass, | ||
array $searchFields | ||
) { | ||
$this->searchManager = $searchManager; | ||
$this->articleDocumentClass = $articleDocumentClass; | ||
$this->repository = $this->searchManager->getRepository($this->articleDocumentClass); | ||
$this->searchFields = $searchFields; | ||
} | ||
|
||
/** | ||
* Finds recent articles for given parameters sorted by field `authored`. | ||
* | ||
* @param null|string $excludeUuid | ||
* @param int $limit | ||
* @param null|array $types | ||
* @param null|string $locale | ||
* | ||
* @return DocumentIterator | ||
*/ | ||
public function findRecent($excludeUuid = null, $limit = self::DEFAULT_LIMIT, array $types = null, $locale = null) | ||
{ | ||
$search = $this->createSearch($limit, $types, $locale); | ||
|
||
if ($excludeUuid) { | ||
$search->addQuery(new TermQuery('uuid', $excludeUuid), BoolQuery::MUST_NOT); | ||
} | ||
|
||
$search->addSort(new FieldSort('authored', FieldSort::DESC)); | ||
|
||
return $this->repository->findDocuments($search); | ||
} | ||
|
||
/** | ||
* Finds similar articles for given `uuid` with given parameters. | ||
* | ||
* @param string $uuid | ||
* @param int $limit | ||
* @param null|array $types | ||
* @param null|string $locale | ||
* | ||
* @return DocumentIterator | ||
*/ | ||
public function findSimilar($uuid, $limit = self::DEFAULT_LIMIT, array $types = null, $locale = null) | ||
{ | ||
$search = $this->createSearch($limit, $types, $locale); | ||
|
||
$search->addQuery( | ||
new MoreLikeThisQuery( | ||
null, | ||
[ | ||
'fields' => $this->searchFields, | ||
'min_term_freq' => 1, | ||
'min_doc_freq' => 2, | ||
'ids' => [$this->getViewDocumentId($uuid, $locale)], | ||
] | ||
) | ||
); | ||
|
||
return $this->repository->findDocuments($search); | ||
} | ||
|
||
/** | ||
* Creates search with default queries (size, locale, types). | ||
* | ||
* @param int $limit | ||
* @param null|array $types | ||
* @param null|string $locale | ||
* | ||
* @return Search | ||
*/ | ||
private function createSearch($limit, array $types = null, $locale = null) | ||
{ | ||
$search = $this->repository->createSearch(); | ||
|
||
// set size | ||
$search->setSize($limit); | ||
|
||
// filter by locale if provided | ||
if ($locale) { | ||
$search->addQuery(new TermQuery('locale', $locale), BoolQuery::FILTER); | ||
} | ||
|
||
// filter by types if provided | ||
if ($types) { | ||
$typesQuery = new BoolQuery(); | ||
foreach ($types as $type) { | ||
$typesQuery->add(new TermQuery('type', $type), BoolQuery::SHOULD); | ||
} | ||
$search->addQuery($typesQuery); | ||
} | ||
|
||
return $search; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Exception; | ||
|
||
/** | ||
* Thrown when no article-page is found in request. | ||
*/ | ||
class ArticleInRequestNotFoundException extends \Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('No article in request found'); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update config:dump in
installation.md