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