Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / html / output / HtmlOutputProcessor.class.php
1 <?php
2
3 namespace wcf\system\html\output;
4
5 use wcf\data\object\type\ObjectTypeCache;
6 use wcf\system\html\AbstractHtmlProcessor;
7 use wcf\system\html\output\node\HtmlOutputNodeProcessor;
8 use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
9
10 /**
11 * Processes stored HTML for final display.
12 *
13 * @author Alexander Ebert
14 * @copyright 2001-2019 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package WoltLabSuite\Core\System\Html\Output
17 * @since 3.0
18 */
19 class HtmlOutputProcessor extends AbstractHtmlProcessor
20 {
21 /**
22 * Generate the table of contents, implicitly enable this for certain object types on demand.
23 * @var bool|null
24 * @since 5.2
25 */
26 public $enableToc;
27
28 /**
29 * Removes any link contained inside the message text.
30 * @var bool
31 * @since 5.2
32 */
33 public $removeLinks = false;
34
35 /**
36 * output node processor instance
37 * @var HtmlOutputNodeProcessor
38 */
39 protected $htmlOutputNodeProcessor;
40
41 /**
42 * content language id
43 * @var int
44 */
45 protected $languageID;
46
47 /**
48 * desired output type
49 * @var string
50 */
51 protected $outputType = 'text/html';
52
53 /**
54 * enables rel=ugc for external links
55 * @var bool
56 */
57 public $enableUgc = true;
58
59 /**
60 * Processes the input html string.
61 *
62 * @param string $html html string
63 * @param string $objectType object type identifier
64 * @param int $objectID object id
65 * @param bool $doKeywordHighlighting enable keyword highlighting
66 * @param int $languageID content language id
67 */
68 public function process($html, $objectType, $objectID, $doKeywordHighlighting = true, $languageID = null)
69 {
70 $this->languageID = $languageID;
71 $this->setContext($objectType, $objectID);
72
73 $this->getHtmlOutputNodeProcessor()->setOutputType($this->outputType);
74 $this->getHtmlOutputNodeProcessor()->enableKeywordHighlighting($doKeywordHighlighting);
75 $this->getHtmlOutputNodeProcessor()->load($this, $html);
76 $this->getHtmlOutputNodeProcessor()->process();
77 }
78
79 /**
80 * Sets the desired output type.
81 *
82 * @param string $outputType desired output type
83 * @throws \InvalidArgumentException
84 */
85 public function setOutputType($outputType)
86 {
87 if (!\in_array($outputType, ['text/html', 'text/simplified-html', 'text/plain'])) {
88 throw new \InvalidArgumentException(
89 "Expected 'text/html', 'text/simplified-html' or 'text/plain', but received '" . $outputType . "'"
90 );
91 }
92
93 $this->outputType = $outputType;
94 }
95
96 /**
97 * @inheritDoc
98 */
99 public function getHtml()
100 {
101 return $this->getHtmlOutputNodeProcessor()->getHtml();
102 }
103
104 /**
105 * @inheritdoc
106 * @throws \InvalidArgumentException
107 */
108 public function setContext($objectType, $objectID)
109 {
110 parent::setContext($objectType, $objectID);
111
112 MessageEmbeddedObjectManager::getInstance()->setActiveMessage($objectType, $objectID, $this->languageID);
113 $objectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.message', $objectType);
114 if ($this->enableToc === null) {
115 $this->enableToc = (!empty($objectType->additionalData['enableToc']));
116 }
117 }
118
119 /**
120 * Returns the output node processor instance.
121 *
122 * @return HtmlOutputNodeProcessor output node processor instance
123 */
124 protected function getHtmlOutputNodeProcessor()
125 {
126 if ($this->htmlOutputNodeProcessor === null) {
127 $this->htmlOutputNodeProcessor = new HtmlOutputNodeProcessor();
128 }
129
130 return $this->htmlOutputNodeProcessor;
131 }
132 }