diff --git a/library/CM/Usertext/Filter/Markdown/UserContent.php b/library/CM/Usertext/Filter/Markdown/UserContent.php deleted file mode 100644 index a437e2b70..000000000 --- a/library/CM/Usertext/Filter/Markdown/UserContent.php +++ /dev/null @@ -1,31 +0,0 @@ -setServiceManager($serviceManager); - } - - public function getCacheKey() { - return parent::getCacheKey() + array('_url' => $this->getServiceManager()->getUserContent()->getUrl('formatter')); - } - - public function transform($text, CM_Frontend_Render $render) { - $text = (string) $text; - $text = preg_replace_callback('#!\[formatter\]\(([^\)]+)\)#m', function ($matches) { - $filename = trim($matches[1]); - $file = new CM_File_UserContent('formatter', $filename, null, $this->getServiceManager()); - return '![image](' . $file->getUrl() . ')'; - }, $text); - - return $text; - } -} diff --git a/library/CM/Usertext/Usertext.php b/library/CM/Usertext/Usertext.php index db10ac561..c6110ef4c 100644 --- a/library/CM/Usertext/Usertext.php +++ b/library/CM/Usertext/Usertext.php @@ -40,6 +40,51 @@ public function setMode($mode, $maxLength = null, $isMail = null, $skipAnchors = if ($isMail) { $emoticonFixedHeight = 16; } + $this->_setMode($mode, $maxLength, $skipAnchors, $emoticonFixedHeight); + } + + /** + * @param string $text + * @return string + */ + public function transform($text) { + $cacheKey = $this->_getCacheKey($text); + $cache = CM_Cache_Local::getInstance(); + if (($result = $cache->get($cacheKey)) === false) { + $result = $text; + foreach ($this->_getFilters() as $filter) { + $result = $filter->transform($result, $this->_render); + } + $cache->set($cacheKey, $result); + } + return $result; + } + + /** + * @param string $text + * @return string + */ + protected function _getCacheKey($text) { + $cacheKey = CM_CacheConst::Usertext . '_text:' . md5($text); + $filterList = $this->_getFilters(); + if (0 !== count($filterList)) { + $cacheKeyListFilter = array_map(function (CM_Usertext_Filter_Interface $filter) { + return $filter->getCacheKey(); + }, $filterList); + $cache = CM_Cache_Local::getInstance(); + $cacheKey .= '_filter:' . $cache->key($cacheKeyListFilter); + } + return $cacheKey; + } + + /** + * @param string $mode + * @param int|null $maxLength + * @param boolean|null $skipAnchors + * @param int|null $emoticonFixedHeight + * @throws CM_Exception_Invalid + */ + protected function _setMode($mode, $maxLength = null, $skipAnchors = null, $emoticonFixedHeight = null) { $this->addFilter(new CM_Usertext_Filter_Badwords()); if ('escape' != $mode) { $this->addFilter(new CM_Usertext_Filter_Emoticon_ReplaceAdditional()); @@ -61,7 +106,6 @@ public function setMode($mode, $maxLength = null, $isMail = null, $skipAnchors = if (null !== $maxLength) { throw new CM_Exception_Invalid('MaxLength is not allowed in mode markdown.'); } - $this->addFilter(new CM_Usertext_Filter_Markdown_UserContent()); $this->addFilter(new CM_Usertext_Filter_Emoticon_EscapeMarkdown()); $this->addFilter(new CM_Usertext_Filter_Markdown_UnescapeBlockquote()); $this->addFilter(new CM_Usertext_Filter_Markdown($skipAnchors)); @@ -69,7 +113,6 @@ public function setMode($mode, $maxLength = null, $isMail = null, $skipAnchors = $this->addFilter(new CM_Usertext_Filter_Emoticon($emoticonFixedHeight)); break; case 'markdownPlain': - $this->addFilter(new CM_Usertext_Filter_Markdown_UserContent()); $this->addFilter(new CM_Usertext_Filter_Emoticon_EscapeMarkdown()); $this->addFilter(new CM_Usertext_Filter_Markdown($skipAnchors)); $this->addFilter(new CM_Usertext_Filter_Emoticon_UnescapeMarkdown()); @@ -84,40 +127,6 @@ public function setMode($mode, $maxLength = null, $isMail = null, $skipAnchors = } } - /** - * @param string $text - * @return string - */ - public function transform($text) { - $cacheKey = $this->_getCacheKey($text); - $cache = CM_Cache_Local::getInstance(); - if (($result = $cache->get($cacheKey)) === false) { - $result = $text; - foreach ($this->_getFilters() as $filter) { - $result = $filter->transform($result, $this->_render); - } - $cache->set($cacheKey, $result); - } - return $result; - } - - /** - * @param string $text - * @return string - */ - protected function _getCacheKey($text) { - $cacheKey = CM_CacheConst::Usertext . '_text:' . md5($text); - $filterList = $this->_getFilters(); - if (0 !== count($filterList)) { - $cacheKeyListFilter = array_map(function (CM_Usertext_Filter_Interface $filter) { - return $filter->getCacheKey(); - }, $filterList); - $cache = CM_Cache_Local::getInstance(); - $cacheKey .= '_filter:' . $cache->key($cacheKeyListFilter); - } - return $cacheKey; - } - private function _clearFilters() { $this->_filterList = array(); } diff --git a/tests/library/CM/Usertext/Filter/Markdown/UserContentTest.php b/tests/library/CM/Usertext/Filter/Markdown/UserContentTest.php deleted file mode 100644 index 9449bada6..000000000 --- a/tests/library/CM/Usertext/Filter/Markdown/UserContentTest.php +++ /dev/null @@ -1,62 +0,0 @@ -_serviceManager = new CM_Service_Manager(); - - $filesystemDefault = new CM_File_Filesystem(new CM_File_Filesystem_Adapter_Local()); - $this->_serviceManager->registerInstance('filesystem-usercontent-default', $filesystemDefault); - - $config = array( - 'default' => array( - 'url' => 'http://example.com/usercontent', - 'filesystem' => 'filesystem-usercontent-default', - ), - ); - $service = new CM_Service_UserContent($config); - $this->_serviceManager->registerInstance('usercontent', $service); - $service->setServiceManager($this->_serviceManager); - } - - public function tearDown() { - CMTest_TH::clearEnv(); - } - - public function testProcess() { - $text = 'test ![formatter](1.jpg) test' . - PHP_EOL . '![formatter](2.jpg)![formatter](3.jpg)nospace![formatter](4.jpg)'; - - $expected = 'test ![image](' . $this->_usercontentUrl . '1.jpg) test' - . PHP_EOL . '![image](' . $this->_usercontentUrl . '2.jpg)![image](' - . $this->_usercontentUrl . '3.jpg)nospace![image](' . $this->_usercontentUrl - . '4.jpg)'; - $filter = new CM_Usertext_Filter_Markdown_UserContent($this->_serviceManager); - $actual = $filter->transform($text, new CM_Frontend_Render()); - - $this->assertSame($expected, $actual); - } - - public function testInvalid() { - $filter = new CM_Usertext_Filter_Markdown_UserContent($this->_serviceManager); - - $text = 'test ![formatter](1.jpg'; - $actual = $filter->transform($text, new CM_Frontend_Render()); - $this->assertSame($text, $actual); - - $text = '![formatter]()'; - $actual = $filter->transform($text, new CM_Frontend_Render()); - $this->assertSame($text, $actual); - - $text = '![formatter](]1.jpg)'; - $expected = '![image](' . $this->_usercontentUrl . ']1.jpg)'; - $actual = $filter->transform($text, new CM_Frontend_Render()); - $this->assertSame($expected, $actual); - } -}