From 097c8c6176154b3858eef2be34b8b76f54c92df7 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Thu, 24 Nov 2016 17:08:18 +0100 Subject: [PATCH] Added method to retrieve number of articles per category --- .../category/ArticleCategoryCache.class.php | 50 +++++++++++++++++++ .../category/ArticleCategoryNode.class.php | 19 +++++++ 2 files changed, 69 insertions(+) create mode 100644 wcfsetup/install/files/lib/data/article/category/ArticleCategoryCache.class.php diff --git a/wcfsetup/install/files/lib/data/article/category/ArticleCategoryCache.class.php b/wcfsetup/install/files/lib/data/article/category/ArticleCategoryCache.class.php new file mode 100644 index 0000000000..dc777ff8c5 --- /dev/null +++ b/wcfsetup/install/files/lib/data/article/category/ArticleCategoryCache.class.php @@ -0,0 +1,50 @@ + + * @package WoltLabSuite\Core\Data\Article\Category + * @since 3.0 + */ +class ArticleCategoryCache extends SingletonFactory { + /** + * number of total articles + * @var integer[] + */ + protected $articles; + + /** + * Calculates the number of articles. + */ + protected function initArticles() { + $sql = "SELECT COUNT(*) AS count, categoryID + FROM wcf" . WCF_N . "_article + WHERE publicationStatus = ? + GROUP BY categoryID"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute([Article::PUBLISHED]); + $this->articles = $statement->fetchMap('categoryID', 'count'); + } + + /** + * Returns the number of articles in the category with the given id. + * + * @param integer $categoryID + * @return integer + */ + public function getArticles($categoryID) { + if ($this->articles === null) { + $this->initArticles(); + } + + if (isset($this->articles[$categoryID])) return $this->articles[$categoryID]; + return 0; + } +} diff --git a/wcfsetup/install/files/lib/data/article/category/ArticleCategoryNode.class.php b/wcfsetup/install/files/lib/data/article/category/ArticleCategoryNode.class.php index b3c1242546..56f7c7916b 100644 --- a/wcfsetup/install/files/lib/data/article/category/ArticleCategoryNode.class.php +++ b/wcfsetup/install/files/lib/data/article/category/ArticleCategoryNode.class.php @@ -19,4 +19,23 @@ class ArticleCategoryNode extends CategoryNode { * @inheritDoc */ protected static $baseClass = ArticleCategory::class; + + /** + * number of articles in the category + * @var integer + */ + protected $articles; + + /** + * Returns number of articles in the category. + * + * @return integer + */ + public function getArticles() { + if ($this->articles === null) { + $this->articles = ArticleCategoryCache::getInstance()->getArticles($this->categoryID); + } + + return $this->articles; + } } -- 2.20.1