From 8b0d07ead954ec40ae7b0cc7c8644f1d134f407f Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Tue, 24 Nov 2020 18:44:34 +0100 Subject: [PATCH] Improved randomized sort order of related articles --- .../install/files/lib/page/AbstractArticlePage.class.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/wcfsetup/install/files/lib/page/AbstractArticlePage.class.php b/wcfsetup/install/files/lib/page/AbstractArticlePage.class.php index 8f007ad0dc..f36838e12d 100644 --- a/wcfsetup/install/files/lib/page/AbstractArticlePage.class.php +++ b/wcfsetup/install/files/lib/page/AbstractArticlePage.class.php @@ -150,12 +150,17 @@ abstract class AbstractArticlePage extends AbstractPage { " . $conditionBuilder . " GROUP BY tag_to_object.objectID HAVING COUNT(*) >= " . round(count($this->tags) * ARTICLE_RELATED_ARTICLES_MATCH_THRESHOLD / 100) . " - ORDER BY count DESC, RAND()"; - $statement = WCF::getDB()->prepareStatement($sql, ARTICLE_RELATED_ARTICLES); + ORDER BY count DESC, MAX(article.time) DESC"; + $statement = WCF::getDB()->prepareStatement($sql, ARTICLE_RELATED_ARTICLES * 4); $statement->execute($conditionBuilder->getParameters()); $articleIDs = $statement->fetchAll(\PDO::FETCH_COLUMN); if (!empty($articleIDs)) { + if (count($articleIDs) > ARTICLE_RELATED_ARTICLES) { + shuffle($articleIDs); + $articleIDs = array_slice($articleIDs, 0, ARTICLE_RELATED_ARTICLES); + } + $this->relatedArticles = new AccessibleArticleList(); $this->relatedArticles->getConditionBuilder()->add('article.articleID IN (?)', [$articleIDs]); $this->relatedArticles->sqlOrderBy = 'article.time'; -- 2.20.1