Merge remote-tracking branch 'refs/remotes/origin/2.1' into 3.0
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / page / AbstractArticlePage.class.php
1 <?php
2 namespace wcf\page;
3 use wcf\data\article\category\ArticleCategory;
4 use wcf\data\article\content\ViewableArticleContent;
5 use wcf\data\article\AccessibleArticleList;
6 use wcf\data\article\ArticleEditor;
7 use wcf\data\article\ViewableArticle;
8 use wcf\data\tag\Tag;
9 use wcf\system\database\util\PreparedStatementConditionBuilder;
10 use wcf\system\exception\IllegalLinkException;
11 use wcf\system\exception\PermissionDeniedException;
12 use wcf\system\language\LanguageFactory;
13 use wcf\system\page\PageLocationManager;
14 use wcf\system\tagging\TagEngine;
15 use wcf\system\WCF;
16
17 /**
18 * Abstract implementation of the article page.
19 *
20 * @author Marcel Werk
21 * @copyright 2001-2017 WoltLab GmbH
22 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
23 * @package WoltLabSuite\Core\Page
24 * @since 3.0
25 */
26 abstract class AbstractArticlePage extends AbstractPage {
27 /**
28 * @inheritDoc
29 */
30 public $neededModules = ['MODULE_ARTICLE'];
31
32 /**
33 * article content id
34 * @var integer
35 */
36 public $articleContentID = 0;
37
38 /**
39 * article content object
40 * @var ViewableArticleContent
41 */
42 public $articleContent;
43
44 /**
45 * article object
46 * @var ViewableArticle
47 */
48 public $article;
49
50 /**
51 * list of tags
52 * @var Tag[]
53 */
54 public $tags = [];
55
56 /**
57 * category object
58 * @var ArticleCategory
59 */
60 public $category;
61
62 /**
63 * list of related articles
64 * @var AccessibleArticleList
65 */
66 public $relatedArticles;
67
68 /**
69 * @inheritDoc
70 */
71 public function readParameters() {
72 parent::readParameters();
73
74 if (isset($_REQUEST['id'])) $this->articleContentID = intval($_REQUEST['id']);
75 $this->articleContent = ViewableArticleContent::getArticleContent($this->articleContentID);
76 if ($this->articleContent === null) {
77 throw new IllegalLinkException();
78 }
79 $this->article = ViewableArticle::getArticle($this->articleContent->articleID, false);
80 $this->category = $this->article->getCategory();
81
82 // update interface language
83 if (!WCF::getUser()->userID && $this->article->isMultilingual && $this->articleContent->languageID != WCF::getLanguage()->languageID) {
84 WCF::setLanguage($this->articleContent->languageID);
85 }
86 }
87
88 /**
89 * @inheritDoc
90 */
91 public function checkPermissions() {
92 parent::checkPermissions();
93
94 if (!$this->article->canRead()) {
95 throw new PermissionDeniedException();
96 }
97 }
98
99 /**
100 * @inheritDoc
101 */
102 public function readData() {
103 parent::readData();
104
105 // update view count
106 $articleEditor = new ArticleEditor($this->article->getDecoratedObject());
107 $articleEditor->updateCounters([
108 'views' => 1
109 ]);
110
111 // get tags
112 if (MODULE_TAGGING && WCF::getSession()->getPermission('user.tag.canViewTag')) {
113 $this->tags = TagEngine::getInstance()->getObjectTags(
114 'com.woltlab.wcf.article',
115 $this->articleContent->articleContentID,
116 [$this->articleContent->languageID ?: LanguageFactory::getInstance()->getDefaultLanguageID()]
117 );
118 }
119
120 // get related articles
121 if (MODULE_TAGGING && ARTICLE_RELATED_ARTICLES) {
122 if (!empty($this->tags)) {
123 $conditionBuilder = new PreparedStatementConditionBuilder();
124 $conditionBuilder->add('objectTypeID = ?', [TagEngine::getInstance()->getObjectTypeID('com.woltlab.wcf.article')]);
125 $conditionBuilder->add('tagID IN (?)', [array_keys($this->tags)]);
126 $conditionBuilder->add('objectID <> ?', [$this->articleContentID]);
127 $sql = "SELECT objectID, COUNT(*) AS count
128 FROM wcf" . WCF_N . "_tag_to_object
129 " . $conditionBuilder . "
130 GROUP BY objectID
131 HAVING COUNT(*) >= " . round(count($this->tags) * ARTICLE_RELATED_ARTICLES_MATCH_THRESHOLD / 100) . "
132 ORDER BY count DESC";
133 $statement = WCF::getDB()->prepareStatement($sql, ARTICLE_RELATED_ARTICLES);
134 $statement->execute($conditionBuilder->getParameters());
135 $articleContentIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
136
137 if (!empty($articleContentIDs)) {
138 $conditionBuilder = new PreparedStatementConditionBuilder();
139 $conditionBuilder->add('articleContentID IN (?)', [$articleContentIDs]);
140 $sql = "SELECT articleID
141 FROM wcf" . WCF_N . "_article_content
142 " . $conditionBuilder;
143 $statement = WCF::getDB()->prepareStatement($sql);
144 $statement->execute($conditionBuilder->getParameters());
145 $articleIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
146
147 $this->relatedArticles = new AccessibleArticleList();
148 $this->relatedArticles->getConditionBuilder()->add('article.articleID IN (?)', [$articleIDs]);
149 $this->relatedArticles->sqlOrderBy = 'article.time';
150 $this->relatedArticles->readObjects();
151 }
152 }
153 }
154
155 // set location
156 PageLocationManager::getInstance()->addParentLocation('com.woltlab.wcf.CategoryArticleList', $this->article->categoryID, $this->article->getCategory());
157 foreach (array_reverse($this->article->getCategory()->getParentCategories()) as $parentCategory) {
158 PageLocationManager::getInstance()->addParentLocation('com.woltlab.wcf.CategoryArticleList', $parentCategory->categoryID, $parentCategory);
159 }
160 }
161
162 /**
163 * @inheritDoc
164 */
165 public function assignVariables() {
166 parent::assignVariables();
167
168 WCF::getTPL()->assign([
169 'articleContentID' => $this->articleContentID,
170 'articleContent' => $this->articleContent,
171 'article' => $this->article,
172 'category' => $this->category,
173 'relatedArticles' => $this->relatedArticles,
174 'tags' => $this->tags
175 ]);
176 }
177 }