Improved randomized sort order of related articles
authorMarcel Werk <burntime@woltlab.com>
Tue, 24 Nov 2020 17:44:34 +0000 (18:44 +0100)
committerMarcel Werk <burntime@woltlab.com>
Tue, 24 Nov 2020 17:44:34 +0000 (18:44 +0100)
wcfsetup/install/files/lib/page/AbstractArticlePage.class.php

index 8f007ad0dce63f0e84cac95996f88325ae8af5f3..f36838e12d6949a4f455ee2e5a1696883d09dd6e 100644 (file)
@@ -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';