Added support for enclosure tags in rss feeds
authorMarcel Werk <burntime@woltlab.com>
Fri, 24 Jun 2016 17:12:19 +0000 (19:12 +0200)
committerMarcel Werk <burntime@woltlab.com>
Fri, 24 Jun 2016 17:12:25 +0000 (19:12 +0200)
com.woltlab.wcf/templates/rssFeed.tpl
wcfsetup/install/files/lib/data/IFeedEntryWithEnclosure.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/data/article/FeedArticle.class.php
wcfsetup/install/files/lib/page/AbstractFeedPage.class.php
wcfsetup/install/files/lib/page/ArticleFeedPage.class.php
wcfsetup/install/files/lib/system/feed/enclosure/FeedEnclosure.class.php [new file with mode: 0644]

index 93e8549d516bcede720938517f500e3543f55311..46a7817acfe9e01ef9f1c8e51b3b66afd7ff5753 100644 (file)
@@ -29,6 +29,7 @@
                                <category><![CDATA[{@$category|escapeCDATA}]]></category>
                        {/foreach}
                        {hascontent}<content:encoded><![CDATA[{content}{@$item->getFormattedMessage()|escapeCDATA}{/content}]]></content:encoded>{/hascontent}
+                       {if $supportsEnclosure && $item->getEnclosure()}<enclosure length="{@$item->getEnclosure()->getLength()}" type="{$item->getEnclosure()->getType()}" url="{$item->getEnclosure()->getURL()}" />{/if}
                        <slash:comments><![CDATA[{@$item->getComments()|escapeCDATA}]]></slash:comments>{*
                        *}{event name='itemFields'}
                </item>
diff --git a/wcfsetup/install/files/lib/data/IFeedEntryWithEnclosure.class.php b/wcfsetup/install/files/lib/data/IFeedEntryWithEnclosure.class.php
new file mode 100644 (file)
index 0000000..d9cccf9
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+namespace wcf\data;
+use wcf\system\feed\enclosure\FeedEnclosure;
+
+/**
+ * Every feed entry that supports enclosure tags should implement this interface.
+ * 
+ * @author     Marcel Werk
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\Data
+ */
+interface IFeedEntryWithEnclosure extends IFeedEntry {
+       /**
+        * Returns the enclosure object
+        * 
+        * @return FeedEnclosure|null
+        */
+       public function getEnclosure();
+}
index 4fef3890a18159bd818f3fab061e3d42db975f88..09d748215cbd32c01a34b1b7db206e4844cb98ea 100644 (file)
@@ -1,8 +1,8 @@
 <?php
 namespace wcf\data\article;
-use wcf\data\IFeedEntry;
+use wcf\data\IFeedEntryWithEnclosure;
 use wcf\data\TUserContent;
-use wcf\system\request\LinkHandler;
+use wcf\system\feed\enclosure\FeedEnclosure;
 use wcf\util\StringUtil;
 
 /**
@@ -14,9 +14,14 @@ use wcf\util\StringUtil;
  * @package    WoltLabSuite\Core\Data\Article
  * @since      3.0
  */
-class FeedArticle extends ViewableArticle implements IFeedEntry {
+class FeedArticle extends ViewableArticle implements IFeedEntryWithEnclosure {
        use TUserContent;
        
+       /**
+        * @var FeedEnclosure
+        */
+       protected $enclosure;
+       
        /** @noinspection PhpMissingParentCallCommonInspection */
        /**
         * @inheritDoc
@@ -90,4 +95,17 @@ class FeedArticle extends ViewableArticle implements IFeedEntry {
        public function isVisible() {
                return $this->canRead();
        }
+       
+       /**
+        * @inheritDoc
+        */
+       public function getEnclosure() {
+               if ($this->enclosure === null) {
+                       if ($this->getImage() !== null) {
+                               $this->enclosure = new FeedEnclosure($this->getImage()->getThumbnailLink('small'), $this->getImage()->smallThumbnailType, $this->getImage()->smallThumbnailSize);
+                       }
+               }
+               
+               return $this->enclosure;
+       }
 }
index 81d6a67ac573c387ccd621d93c1b202db43b98b9..ae5cc0ad0683dda2895ed5e3eee410427ff5e484 100644 (file)
@@ -54,7 +54,8 @@ abstract class AbstractFeedPage extends AbstractAuthedPage {
                
                WCF::getTPL()->assign([
                        'items' => $this->items,
-                       'title' => $this->title
+                       'title' => $this->title,
+                       'supportsEnclosure' => false
                ]);
        }
        
index 390ee7519e3cbb8e3b265ffa810d16f75f140d0c..714e93ca729157e04f4a065992abb6274c94d844 100644 (file)
@@ -65,4 +65,15 @@ class ArticleFeedPage extends AbstractFeedPage {
                        $this->title = WCF::getLanguage()->get('wcf.article.articles');
                }
        }
+       
+       /**
+        * @inheritDoc
+        */
+       public function assignVariables() {
+               parent::assignVariables();
+               
+               WCF::getTPL()->assign([
+                       'supportsEnclosure' => true
+               ]);
+       }
 }
diff --git a/wcfsetup/install/files/lib/system/feed/enclosure/FeedEnclosure.class.php b/wcfsetup/install/files/lib/system/feed/enclosure/FeedEnclosure.class.php
new file mode 100644 (file)
index 0000000..1163af3
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+namespace wcf\system\feed\enclosure;
+
+/**
+ * Represents an enclosure in a rss feed.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Feed\Enclosure
+ */
+class FeedEnclosure {
+       /**
+        * url to the enclosure
+        * @var string
+        */
+       protected $url = '';
+       
+       /**
+        * enclosure's MIME type
+        * @var string
+        */
+       protected $type = '';
+       
+       /**
+        * size of the enclosure in bytes
+        * @var integer
+        */
+       protected $length = 0;
+       
+       /**
+        * Creates a new FeedEnclosure object.
+        *
+        * @param       string          $url            url to the enclosure
+        * @param       string          $type           enclosure's MIME type
+        * @param       integer         $length         size of the enclosure in bytes
+        */
+       public function __construct($url, $type, $length) {
+               $this->url = $url;
+               $this->type = $type;
+               $this->length = $length;
+       }
+       
+       /**
+        * Returns the url to the enclosure.
+        * 
+        * @return string
+        */
+       public function getURL() {
+               return $this->url;
+       }
+       
+       /**
+        * Returns the enclosure's MIME type.
+        * 
+        * @return string
+        */
+       public function getType() {
+               return $this->type;
+       }
+       
+       /**
+        * Returns the size of the enclosure in bytes.
+        * 
+        * @return integer
+        */
+       public function getLength() {
+               return $this->length;
+       }
+}