Skip to content

Commit

Permalink
pkp/pkp-lib#8710 Updated Recommend by Similarity plugin to use the su…
Browse files Browse the repository at this point in the history
…bmission collector
  • Loading branch information
jonasraoni committed Apr 12, 2023
1 parent 525a002 commit 8d743ee
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 82 deletions.
140 changes: 77 additions & 63 deletions plugins/generic/recommendBySimilarity/RecommendBySimilarityPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,104 +8,118 @@
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class RecommendBySimilarityPlugin
*
* @brief Plugin to recommend similar articles.
*/

namespace APP\plugins\generic\recommendBySimilarity;

use APP\core\Application;
use APP\facades\Repo;
use APP\handler\Handler;
use APP\search\ArticleSearch;
use APP\submission\Collector;
use APP\submission\Submission;
use APP\template\TemplateManager;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;

define('RECOMMEND_BY_SIMILARITY_PLUGIN_COUNT', 10);

class RecommendBySimilarityPlugin extends GenericPlugin
{
//
// Implement template methods from Plugin.
//
private const DEFAULT_RECOMMENDATION_COUNT = 10;

/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
public function register($category, $path, $mainContextId = null): bool
{
$success = parent::register($category, $path, $mainContextId);
if (Application::isUnderMaintenance()) {
return $success;
if (!parent::register($category, $path, $mainContextId)) {
return false;
}

if ($success && $this->getEnabled($mainContextId)) {
Hook::add('Templates::Article::Footer::PageFooter', [$this, 'callbackTemplateArticlePageFooter']);
if (!Application::isUnderMaintenance() && $this->getEnabled($mainContextId)) {
Hook::add('Templates::Article::Footer::PageFooter', function (string $hookName, array $params): bool {
$output = & $params[2];
$output .= $this->buildTemplate();
return Hook::CONTINUE;
});
}
return $success;
return true;
}

/**
* @see Plugin::getDisplayName()
* Hook handler which is responsible to add content to the article footer
*
* @see templates/article/footer.tpl
*/
public function getDisplayName()
private function buildTemplate(): ?string
{
return __('plugins.generic.recommendBySimilarity.displayName');
$templateManager = TemplateManager::getManager();
$submissionId = $templateManager->getTemplateVars('article')->getId();

// If there's no keywords, quit
if (!strlen($searchPhrase = implode(' ', (new ArticleSearch())->getSimilarityTerms($submissionId)))) {
return null;
}

$request = Application::get()->getRequest();
$router = $request->getRouter();
$context = $router->getContext($request);

$rangeInfo = Handler::getRangeInfo($request, 'articlesBySimilarity');
$rangeInfo->setCount(static::DEFAULT_RECOMMENDATION_COUNT);

$collector = Repo::submission()
->getCollector()
->excludeIds([$submissionId])
->filterByContextIds([$context->getId()])
->filterByStatus([Submission::STATUS_PUBLISHED])
->searchPhrase($searchPhrase);

$submissionCount = $collector->getCount();
$submissions = $collector
->limit($rangeInfo->getCount())
->offset($rangeInfo->getOffset())
->orderBy(Collector::ORDERBY_SEARCH_RANKING)
->getMany();
$issues = Repo::issue()->getCollector()
->filterByContextIds([$context->getId()])
->filterByIssueIds(
$submissions->map(fn (Submission $submission) => $submission->getCurrentPublication()->getData('issueId'))
->unique()
->toArray()
)
->getMany();

$templateManager->assign('articlesBySimilarity', (object) [
'submissions' => $submissions,
'query' => $searchPhrase,
'issues' => $issues,
'start' => $rangeInfo->getOffset() + 1,
'end' => $rangeInfo->getOffset() + $submissions->count(),
'total' => $submissionCount,
'nextUrl' => $rangeInfo->getPage() * $rangeInfo->getCount() < $submissionCount ? $request->url(params: ['articlesBySimilarity' => $rangeInfo->getPage() + 1]) : null,
'previousUrl' => $rangeInfo->getPage() > 1 ? $request->url(params: ['articlesBySimilarity' => $rangeInfo->getPage() - 1]) : null
]);

return $templateManager->fetch($this->getTemplateResource('articleFooter.tpl'));
}

/**
* @see Plugin::getDescription()
* @copydoc Plugin::getDisplayName()
*/
public function getDescription()
public function getDisplayName(): string
{
return __('plugins.generic.recommendBySimilarity.description');
return __('plugins.generic.recommendBySimilarity.displayName');
}


//
// View level hook implementations.
//
/**
* @see templates/article/footer.tpl
* @copydoc Plugin::getDescription()
*/
public function callbackTemplateArticlePageFooter($hookName, $params)
public function getDescription(): string
{
$smarty = & $params[1];
$output = & $params[2];

// Identify similarity terms for the given article.
$displayedArticle = $smarty->getTemplateVars('article');
$articleId = $displayedArticle->getId();
$articleSearch = new ArticleSearch();
$searchTerms = $articleSearch->getSimilarityTerms($articleId);
if (empty($searchTerms)) {
return false;
}

// If we got similarity terms then execute a search with...
// ... request, journal and error messages, ...
$request = Application::get()->getRequest();
$router = $request->getRouter();
$journal = $router->getContext($request);
$error = null;
// ... search keywords ...
$query = implode(' ', $searchTerms);
$keywords = [null => $query];
// ... and pagination.
$rangeInfo = Handler::getRangeInfo($request, 'articlesBySimilarity');
$rangeInfo->setCount(RECOMMEND_BY_SIMILARITY_PLUGIN_COUNT);
$smarty->assign([
'articlesBySimilarity' => $articleSearch->retrieveResults(
$request,
$journal,
$keywords,
$error,
null,
null,
$rangeInfo,
[$articleId]
),
'articlesBySimilarityQuery' => $query,
]);
$output .= $smarty->fetch($this->getTemplateResource('articleFooter.tpl'));
return false;
return __('plugins.generic.recommendBySimilarity.description');
}
}
47 changes: 28 additions & 19 deletions plugins/generic/recommendBySimilarity/templates/articleFooter.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,47 @@
* A template to be included via Templates::Article::Footer::PageFooter hook.
*}
<div id="articlesBySimilarityList">
{if !$articlesBySimilarity->wasEmpty()}
<h3>{translate key="plugins.generic.recommendBySimilarity.heading"}</h3>

{if !$articlesBySimilarity->submissions->isEmpty()}
<h3>
<a name="articlesBySimilarity">{translate key="plugins.generic.recommendBySimilarity.heading"}</a>
</h3>
<ul>
{iterate from=articlesBySimilarity item=articleBySimilarity}
{assign var=submission value=$articleBySimilarity.publishedSubmission}
{assign var=article value=$articleBySimilarity.article}
{assign var=issue value=$articleBySimilarity.issue}
{assign var=journal value=$articleBySimilarity.journal}
{foreach from=$articlesBySimilarity->submissions item=submission}
{assign var=publication value=$submission->getCurrentPublication()}
{assign var=issue value=$articlesBySimilarity->issues->get($publication->getData('issueId'))}

<li>
{foreach from=$article->getCurrentPublication()->getData('authors') item=author}
{foreach from=$publication->getData('authors') item=author}
{$author->getFullName()|escape},
{/foreach}
<a href="{url journal=$journal->getPath() page="article" op="view" path=$submission->getBestId()}">
{$article->getLocalizedTitle()|strip_unsafe_html}
</a>,
<a href="{url journal=$journal->getPath() page="issue" op="view" path=$issue->getBestIssueId()}">
{$journal->getLocalizedName()|escape}: {$issue->getIssueIdentification()|escape}
<a href="{url journal=$currentContext->getPath() page="article" op="view" path=$submission->getBestId()}">
{$submission->getLocalizedTitle()|strip_unsafe_html}
</a>
{if $issue},
<a href="{url journal=$currentContext->getPath() page="issue" op="view" path=$issue->getBestIssueId()}">
{$currentContext->getLocalizedName()|escape}: {$issue->getIssueIdentification()|escape}
</a>
{/if}
</li>
{/iterate}
{/foreach}
</ul>
<p id="articlesBySimilarityPages">
{page_links anchor="articlesBySimilarity" iterator=$articlesBySimilarity name="articlesBySimilarity"}
{include
file="frontend/components/pagination.tpl"
prevUrl=$articlesBySimilarity->previousUrl
nextUrl=$articlesBySimilarity->nextUrl
showingStart=$articlesBySimilarity->start
showingEnd=$articlesBySimilarity->end
total=$articlesBySimilarity->total
}
</p>
<p id="articlesBySimilaritySearch">
{capture assign="advancedSearchLink"}{strip}
<a href="{url page="search" query=$articlesBySimilarityQuery}">
{capture assign="articlesBySimilaritySearchLink"}{strip}
<a href="{url page="search" op="search" query=$articlesBySimilarity->query}">
{translate key="plugins.generic.recommendBySimilarity.advancedSearch"}
</a>
{/strip}{/capture}
{translate key="plugins.generic.recommendBySimilarity.advancedSearchIntro" advancedSearchLink=$advancedSearchLink}
{translate key="plugins.generic.recommendBySimilarity.advancedSearchIntro" advancedSearchLink=$articlesBySimilaritySearchLink}
</p>
{/if}
</div>

0 comments on commit 8d743ee

Please sign in to comment.