Added MetaTagHandler (replaces OpenGraphProtocolHandler)
authorAlexander Ebert <ebert@woltlab.com>
Mon, 4 Mar 2013 17:15:32 +0000 (18:15 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 4 Mar 2013 17:15:32 +0000 (18:15 +0100)
com.woltlab.wcf/coreObject.xml
com.woltlab.wcf/template/headInclude.tpl
wcfsetup/install/files/lib/system/MetaTagHandler.class.php [new file with mode: 0644]

index 212aece5ac400ade3dd28a90ffba82008956b894..9d10bf19284915d5429dfef41e92c167e332df1a 100644 (file)
@@ -19,5 +19,8 @@
                <coreobject>
                        <objectname><![CDATA[wcf\system\user\authentication\UserAuthenticationFactory]]></objectname>
                </coreobject>
+               <coreobject>
+                       <objectname><![CDATA[wcf\system\MetaTagHandler]]></objectname>
+               </coreobject>
        </import>
 </data>
index 535f55c60af087c42776981ce6e284b01663d2ee..877918365c564e45824c813fa0399614d7b0b4eb 100644 (file)
@@ -2,7 +2,9 @@
 <meta charset="utf-8" />
 <meta name="description" content="{META_DESCRIPTION}" />
 <meta name="keywords" content="{META_KEYWORDS}" />
-{event name='metaTags'}
+{foreach from=$__wcf->getMetaTagHandler() item=__metaTag}
+       {@$__metaTag}
+{/foreach}
 
 <script type="text/javascript">
        //<![CDATA[
diff --git a/wcfsetup/install/files/lib/system/MetaTagHandler.class.php b/wcfsetup/install/files/lib/system/MetaTagHandler.class.php
new file mode 100644 (file)
index 0000000..c013734
--- /dev/null
@@ -0,0 +1,129 @@
+<?php
+namespace wcf\system;
+use wcf\util\StringUtil;
+
+/**
+ * Handles meta tags.
+ * 
+ * @author     Alexander Ebert
+ * @copyright  2001-2013 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf.message
+ * @subpackage system.message
+ * @category   Community Framework
+ */
+class MetaTagHandler extends SingletonFactory implements \Countable, \Iterator {
+       /**
+        * current iterator index
+        * @var integer
+        */
+       protected $index = 0;
+       
+       /**
+        * list of index to object relation
+        * @var array<integer>
+        */
+       protected $indexToObject = null;
+       
+       /**
+        * regex object
+        * @var wcf\system\Regex;
+        */
+       protected $regex = null;
+       
+       /**
+        * list of meta tags
+        * @var array
+        */
+       protected $objects = array();
+       
+       /**
+        * @see wcf\system\SingletonFactory::init()
+        */
+       protected function init() {
+               $this->regex = new Regex('^https?://');
+               
+               // set default tags
+               $this->addTag('description', 'description', WCF::getLanguage()->get(META_DESCRIPTION));
+               $this->addTag('keywords', 'keywords', WCF::getLanguage()->get(META_KEYWORDS));
+               $this->addTag('og:site_name', 'og:site_name', WCF::getLanguage()->get(PAGE_TITLE));
+       }
+       
+       /**
+        * Adds or replaces a meta tag.
+        * 
+        * @param       string          $identifier
+        * @param       string          $name
+        * @param       string          $value
+        * @param       boolean         $isProperty
+        */
+       public function addTag($identifier, $name, $value, $isProperty = false) {
+               if (!$this->regex->match($value)) {
+                       $value = StringUtil::encodeHTML($value);
+               }
+               
+               $this->objects[$identifier] = array(
+                       'isProperty' => $isProperty,
+                       'name' => $name,
+                       'value' => $value
+               );
+       }
+       
+       /**
+        * Removes a meta tag.
+        * 
+        * @param       string          $identifier
+        */
+       public function removeTag($identifier) {
+               if (isset($this->objects[$identifier])) {
+                       unset($this->objects[$identifier]);
+               }
+       }
+       
+       /**
+        * @see \Countable::count()
+        */
+       public function count() {
+               return count($this->objects);
+       }
+       
+       /**
+        * @see \Iterator::current()
+        */
+       public function current() {
+               $tag = $this->objects[$this->indexToObject[$this->index]];
+               
+               return '<meta ' . ($tag['isProperty'] ? 'property' : 'name') . '="' . $tag['name'] . '" content="' + $tag['value'] + '" />';
+       }
+       
+       /**
+        * CAUTION: This methods does not return the current iterator index,
+        * rather than the object key which maps to that index.
+        *
+        * @see \Iterator::key()
+        */
+       public function key() {
+               return $this->indexToObject[$this->index];
+       }
+       
+       /**
+        * @see \Iterator::next()
+        */
+       public function next() {
+               ++$this->index;
+       }
+       
+       /**
+        * @see \Iterator::rewind()
+        */
+       public function rewind() {
+               $this->index = 0;
+       }
+       
+       /**
+        * @see \Iterator::valid()
+        */
+       public function valid() {
+               return isset($this->indexToObject[$this->index]);
+       }
+}