From bfb52dd93507900ac5de926ea7b3eefa49076356 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sun, 26 Jun 2016 17:23:13 +0200 Subject: [PATCH] Updated message context for html processors --- .../lib/data/article/ArticleAction.class.php | 4 +- .../html/AbstractHtmlProcessor.class.php | 54 +++++++++++++++++++ .../html/input/HtmlInputProcessor.class.php | 10 ++-- .../node/AbstractHtmlNodeProcessor.class.php | 36 ++++--------- .../html/node/IHtmlNodeProcessor.class.php | 29 ++++------ .../html/output/HtmlOutputProcessor.class.php | 9 ++-- .../MessageEmbeddedObjectManager.class.php | 19 +++---- 7 files changed, 98 insertions(+), 63 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/html/AbstractHtmlProcessor.class.php diff --git a/wcfsetup/install/files/lib/data/article/ArticleAction.class.php b/wcfsetup/install/files/lib/data/article/ArticleAction.class.php index 3236d76d7c..027349bbfc 100644 --- a/wcfsetup/install/files/lib/data/article/ArticleAction.class.php +++ b/wcfsetup/install/files/lib/data/article/ArticleAction.class.php @@ -85,7 +85,7 @@ class ArticleAction extends AbstractDatabaseObjectAction { // save embedded objects if (!empty($content['htmlInputProcessor'])) { - if (MessageEmbeddedObjectManager::getInstance()->registerObjects($content['htmlInputProcessor'], 'com.woltlab.wcf.article.content', $articleContent->articleContentID)) { + if (MessageEmbeddedObjectManager::getInstance()->registerObjects($content['htmlInputProcessor'])) { $articleContentEditor->update(['hasEmbeddedObjects' => 1]); } } @@ -151,7 +151,7 @@ class ArticleAction extends AbstractDatabaseObjectAction { // save embedded objects if (!empty($content['htmlInputProcessor'])) { - if ($articleContent->hasEmbeddedObjects != MessageEmbeddedObjectManager::getInstance()->registerObjects($content['htmlInputProcessor'], 'com.woltlab.wcf.article.content', $articleContent->articleContentID)) { + if ($articleContent->hasEmbeddedObjects != MessageEmbeddedObjectManager::getInstance()->registerObjects($content['htmlInputProcessor'])) { $articleContentEditor->update(['hasEmbeddedObjects' => ($articleContent->hasEmbeddedObjects ? 0 : 1)]); } } diff --git a/wcfsetup/install/files/lib/system/html/AbstractHtmlProcessor.class.php b/wcfsetup/install/files/lib/system/html/AbstractHtmlProcessor.class.php new file mode 100644 index 0000000000..acb0cc09aa --- /dev/null +++ b/wcfsetup/install/files/lib/system/html/AbstractHtmlProcessor.class.php @@ -0,0 +1,54 @@ + + * @package WoltLabSuite\Core\System\Html + * @since 3.0 + */ +abstract class AbstractHtmlProcessor implements IHtmlProcessor { + /** + * message context data + * @var array + */ + protected $context = [ + 'objectType' => '', + 'objectTypeID' => 0, + 'objectID' => 0 + ]; + + /** + * Sets the message context data. + * + * @param string $objectType object type identifier + * @param integer $objectID object id + * @throws SystemException + */ + public function setContext($objectType, $objectID) { + $objectTypeID = ObjectTypeCache::getInstance()->getObjectTypeIDByName('com.woltlab.wcf.message', $objectType); + if ($objectTypeID === null) { + throw new SystemException("Invalid object type '" . $objectType . "' for definition 'com.woltlab.wcf.message'."); + } + + $this->context = [ + 'objectType' => $objectType, + 'objectTypeID' => $objectTypeID, + 'objectID' => $objectID + ]; + } + + /** + * Returns the message context data. + * + * @return array message context data + */ + public function getContext() { + return $this->context; + } +} diff --git a/wcfsetup/install/files/lib/system/html/input/HtmlInputProcessor.class.php b/wcfsetup/install/files/lib/system/html/input/HtmlInputProcessor.class.php index 4ddda62194..1c9f66f7c8 100644 --- a/wcfsetup/install/files/lib/system/html/input/HtmlInputProcessor.class.php +++ b/wcfsetup/install/files/lib/system/html/input/HtmlInputProcessor.class.php @@ -1,7 +1,7 @@ setContext($objectType, $objectID); + // enforce consistent newlines $html = StringUtil::unifyNewlines($html); @@ -46,10 +48,8 @@ class HtmlInputProcessor implements IHtmlProcessor { // filter HTML $html = $this->getHtmlInputFilter()->apply($html); - $this->getHtmlInputNodeProcessor()->setContext($objectType, $objectID); - // pre-parse HTML - $this->getHtmlInputNodeProcessor()->load($html); + $this->getHtmlInputNodeProcessor()->load($this, $html); $this->getHtmlInputNodeProcessor()->process(); $this->embeddedContent = $this->getHtmlInputNodeProcessor()->getEmbeddedContent(); } diff --git a/wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php b/wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php index 005759c334..a8e7ac5e7a 100644 --- a/wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php +++ b/wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php @@ -1,6 +1,7 @@ '', - 'objectID' => 0 - ]; - /** * active DOM document * @var \DOMDocument */ protected $document; + /** + * html processor instance + * @var IHtmlProcessor + */ + protected $htmlProcessor; + /** * storage for node replacements * @var array @@ -43,10 +41,8 @@ abstract class AbstractHtmlNodeProcessor implements IHtmlNodeProcessor { /** * @inheritDOc */ - public function load($html) { - if (empty($this->context['objectType'])) { - throw new SystemException('Missing object type, please set the context before attempting to call `load()`.'); - } + public function load(IHtmlProcessor $htmlProcessor, $html) { + $this->htmlProcessor = $htmlProcessor; $this->document = new \DOMDocument('1.0', 'UTF-8'); $this->xpath = null; @@ -185,18 +181,8 @@ abstract class AbstractHtmlNodeProcessor implements IHtmlNodeProcessor { /** * @inheritDoc */ - public function setContext($objectType, $objectID) { - $this->context = [ - 'objectType' => $objectType, - 'objectID' => $objectID - ]; - } - - /** - * @inheritDoc - */ - public function getContext() { - return $this->context; + public function getHtmlProcessor() { + return $this->htmlProcessor; } /** diff --git a/wcfsetup/install/files/lib/system/html/node/IHtmlNodeProcessor.class.php b/wcfsetup/install/files/lib/system/html/node/IHtmlNodeProcessor.class.php index 6d68b357fa..c3d766b125 100644 --- a/wcfsetup/install/files/lib/system/html/node/IHtmlNodeProcessor.class.php +++ b/wcfsetup/install/files/lib/system/html/node/IHtmlNodeProcessor.class.php @@ -1,5 +1,6 @@ getHtmlOutputNodeProcessor()->setContext($objectType, $objectID); - $this->getHtmlOutputNodeProcessor()->load($html); + $this->setContext($objectType, $objectID); + + $this->getHtmlOutputNodeProcessor()->load($this, $html); $this->getHtmlOutputNodeProcessor()->process(); } diff --git a/wcfsetup/install/files/lib/system/message/embedded/object/MessageEmbeddedObjectManager.class.php b/wcfsetup/install/files/lib/system/message/embedded/object/MessageEmbeddedObjectManager.class.php index 7d308feb51..965be1835d 100644 --- a/wcfsetup/install/files/lib/system/message/embedded/object/MessageEmbeddedObjectManager.class.php +++ b/wcfsetup/install/files/lib/system/message/embedded/object/MessageEmbeddedObjectManager.class.php @@ -32,35 +32,36 @@ class MessageEmbeddedObjectManager extends SingletonFactory { * object type of the active message * @var integer */ - protected $activeMessageObjectTypeID = null; + protected $activeMessageObjectTypeID; /** * id of the active message * @var integer */ - protected $activeMessageID = null; + protected $activeMessageID; /** * list of embedded object handlers * @var array */ - protected $embeddedObjectHandlers = null; + protected $embeddedObjectHandlers; /** * Registers the embedded objects found in given message. * * @param HtmlInputProcessor $htmlInputProcessor html input processor instance holding embedded object data - * @param string $messageObjectType message object type - * @param integer $messageID message id * @return boolean true if at least one embedded object was found */ - public function registerObjects(HtmlInputProcessor $htmlInputProcessor, $messageObjectType, $messageID) { + public function registerObjects(HtmlInputProcessor $htmlInputProcessor) { + $context = $htmlInputProcessor->getContext(); + + $messageObjectType = $context['objectType']; + $messageObjectTypeID = $context['objectTypeID']; + $messageID = $context['objectID']; + // delete existing assignments $this->removeObjects($messageObjectType, [$messageID]); - // get object type id - $messageObjectTypeID = ObjectTypeCache::getInstance()->getObjectTypeIDByName('com.woltlab.wcf.message', $messageObjectType); - // prepare statement $sql = "INSERT INTO wcf".WCF_N."_message_embedded_object (messageObjectTypeID, messageID, embeddedObjectTypeID, embeddedObjectID) -- 2.20.1