Updated message context for html processors
authorAlexander Ebert <ebert@woltlab.com>
Sun, 26 Jun 2016 15:23:13 +0000 (17:23 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 26 Jun 2016 15:23:13 +0000 (17:23 +0200)
wcfsetup/install/files/lib/data/article/ArticleAction.class.php
wcfsetup/install/files/lib/system/html/AbstractHtmlProcessor.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/html/input/HtmlInputProcessor.class.php
wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php
wcfsetup/install/files/lib/system/html/node/IHtmlNodeProcessor.class.php
wcfsetup/install/files/lib/system/html/output/HtmlOutputProcessor.class.php
wcfsetup/install/files/lib/system/message/embedded/object/MessageEmbeddedObjectManager.class.php

index 3236d76d7c2fbfdca30101ee2a45d672398bddcb..027349bbfcdf524572c0a165f97b83bdfb38d903 100644 (file)
@@ -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 (file)
index 0000000..acb0cc0
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+namespace wcf\system\html;
+use wcf\data\object\type\ObjectTypeCache;
+use wcf\system\exception\SystemException;
+
+/**
+ * Default implementation for html processors.
+ * 
+ * @author      Alexander Ebert
+ * @copyright   2001-2016 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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;
+       }
+}
index 4ddda621945ff3add456a0db1831c0c7c3232ba6..1c9f66f7c881aa006e27b7e695410defdbe0e723 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 namespace wcf\system\html\input;
 use wcf\system\bbcode\HtmlBBCodeParser;
-use wcf\system\html\IHtmlProcessor;
+use wcf\system\html\AbstractHtmlProcessor;
 use wcf\system\html\input\filter\IHtmlInputFilter;
 use wcf\system\html\input\filter\MessageHtmlInputFilter;
 use wcf\system\html\input\node\HtmlInputNodeProcessor;
@@ -16,7 +16,7 @@ use wcf\util\StringUtil;
  * @package     WoltLabSuite\Core\System\Html\Input
  * @since       3.0
  */
-class HtmlInputProcessor implements IHtmlProcessor {
+class HtmlInputProcessor extends AbstractHtmlProcessor {
        /**
         * list of embedded content grouped by type
         * @var array
@@ -37,6 +37,8 @@ class HtmlInputProcessor implements IHtmlProcessor {
         * @inheritDoc
         */
        public function process($html, $objectType, $objectID) {
+               $this->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();
        }
index 005759c33437e02f4fd8f449655d3ae4cd8ca80a..a8e7ac5e7a8f31efcbf88b7f4f7136ecc9fa0c0f 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\system\html\node;
 use wcf\system\exception\SystemException;
+use wcf\system\html\IHtmlProcessor;
 use wcf\util\JSON;
 
 /**
@@ -13,21 +14,18 @@ use wcf\util\JSON;
  * @since       3.0
  */
 abstract class AbstractHtmlNodeProcessor implements IHtmlNodeProcessor {
-       /**
-        * context data
-        * @var array
-        */
-       protected $context = [
-               'objectType' => '',
-               '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;
        }
        
        /**
index 6d68b357fae233eade8a480ed0007831028d46b3..c3d766b125c6ddecea82338c4d3ea99988e1b1e7 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\system\html\node;
+use wcf\system\html\IHtmlProcessor;
 
 /**
  * Default interface for html node processors.
@@ -11,13 +12,6 @@ namespace wcf\system\html\node;
  * @since       3.0
  */
 interface IHtmlNodeProcessor {
-       /**
-        * Returns an array containing `objectType` and `objectID`.
-        * 
-        * @return      array
-        */
-       public function getContext();
-       
        /**
         * Returns the currently loaded DOM document.
         * 
@@ -32,24 +26,23 @@ interface IHtmlNodeProcessor {
         */
        public function getHtml();
        
+       /**
+        * Returns the html processor instance.
+        *
+        * @return      IHtmlProcessor          html processor instance
+        */
+       public function getHtmlProcessor();
+       
        /**
         * Loads a HTML string for processing.
         * 
-        * @param       string  $html   HTML string
+        * @param       IHtmlProcessor  $htmlProcessor  html processor
+        * @param       string          $html           HTML string
         */
-       public function load($html);
+       public function load(IHtmlProcessor $htmlProcessor, $html);
        
        /**
         * Processes the HTML and transforms it depending on the output type.
         */
        public function process();
-       
-       /**
-        * Sets the context of provided HTML, `$objectID` should be
-        * `0` for objects in creation.
-        * 
-        * @param       string          $objectType     object type identifier
-        * @param       integer         $objectID       object id
-        */
-       public function setContext($objectType, $objectID);
 }
index b00cbcf821e1f05b059081e725f6ff0cc18340a0..8455f8c9a5112c5b9a1cfa2caf4d24c28a91ff34 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 namespace wcf\system\html\output;
-use wcf\system\html\IHtmlProcessor;
+use wcf\system\html\AbstractHtmlProcessor;
 use wcf\system\html\output\node\HtmlOutputNodeProcessor;
 
 /**
@@ -12,7 +12,7 @@ use wcf\system\html\output\node\HtmlOutputNodeProcessor;
  * @package     WoltLabSuite\Core\System\Html\Output
  * @since       3.0
  */
-class HtmlOutputProcessor implements IHtmlProcessor {
+class HtmlOutputProcessor extends AbstractHtmlProcessor {
        /**
         * output node processor instance
         * @var HtmlOutputNodeProcessor
@@ -23,8 +23,9 @@ class HtmlOutputProcessor implements IHtmlProcessor {
         * @inheritDoc
         */
        public function process($html, $objectType, $objectID) {
-               $this->getHtmlOutputNodeProcessor()->setContext($objectType, $objectID);
-               $this->getHtmlOutputNodeProcessor()->load($html);
+               $this->setContext($objectType, $objectID);
+               
+               $this->getHtmlOutputNodeProcessor()->load($this, $html);
                $this->getHtmlOutputNodeProcessor()->process();
        }
        
index 7d308feb51c1e74c24a1807a307ea29b620b3981..965be1835d4ea2268c1990ae41b93f527eb91dc6 100644 (file)
@@ -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)