diff options
author | JoshyPHP <s9e.dev@gmail.com> | 2015-12-06 22:08:00 +0100 |
---|---|---|
committer | JoshyPHP <s9e.dev@gmail.com> | 2015-12-26 14:20:39 +0100 |
commit | ad2c032d3b748d177403fd495685977f7964f8d4 (patch) | |
tree | 6f7e4d53f9aa1e8389849bf0434f98620e2b0fd1 | |
parent | f9d8866aee9bca746801ef0360a5ab350d04061d (diff) | |
download | forums-ad2c032d3b748d177403fd495685977f7964f8d4.tar forums-ad2c032d3b748d177403fd495685977f7964f8d4.tar.gz forums-ad2c032d3b748d177403fd495685977f7964f8d4.tar.bz2 forums-ad2c032d3b748d177403fd495685977f7964f8d4.tar.xz forums-ad2c032d3b748d177403fd495685977f7964f8d4.zip |
[ticket/14323] Moved autolink-related functions to a separate helper
PHPBB3-14323
-rw-r--r-- | phpBB/config/default/container/services_text_formatter.yml | 4 | ||||
-rw-r--r-- | phpBB/phpbb/textformatter/s9e/autolink_helper.php | 105 | ||||
-rw-r--r-- | phpBB/phpbb/textformatter/s9e/factory.php | 37 | ||||
-rw-r--r-- | phpBB/phpbb/textformatter/s9e/parser.php | 47 | ||||
-rw-r--r-- | tests/test_framework/phpbb_test_case_helpers.php | 17 | ||||
-rw-r--r-- | tests/text_formatter/s9e/factory_test.php | 1 |
6 files changed, 147 insertions, 64 deletions
diff --git a/phpBB/config/default/container/services_text_formatter.yml b/phpBB/config/default/container/services_text_formatter.yml index f86d30658e..01a833aef3 100644 --- a/phpBB/config/default/container/services_text_formatter.yml +++ b/phpBB/config/default/container/services_text_formatter.yml @@ -26,6 +26,9 @@ services: text_formatter.utils: alias: text_formatter.s9e.utils + text_formatter.s9e.autolink_helper: + class: phpbb\textformatter\s9e\autolink_helper + text_formatter.s9e.factory: class: phpbb\textformatter\s9e\factory arguments: @@ -33,6 +36,7 @@ services: - '@cache.driver' - '@dispatcher' - '@config' + - '@text_formatter.s9e.autolink_helper' - '%text_formatter.cache.dir%' - '%text_formatter.cache.parser.key%' - '%text_formatter.cache.renderer.key%' diff --git a/phpBB/phpbb/textformatter/s9e/autolink_helper.php b/phpBB/phpbb/textformatter/s9e/autolink_helper.php new file mode 100644 index 0000000000..8fb171f413 --- /dev/null +++ b/phpBB/phpbb/textformatter/s9e/autolink_helper.php @@ -0,0 +1,105 @@ +<?php +/** +* +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\textformatter\s9e; + +class autolink_helper +{ + /** + * Clean up and invalidate an AUTOLINK_TEXT tag if applicable + * + * @param \s9e\TextFormatter\Parser\Tag $tag AUTOLINK_TEXT tag + * @param \s9e\TextFormatter\Parser $parser Parser + * @return bool Whether the tag is valid + */ + public function cleanup_tag(\s9e\TextFormatter\Parser\Tag $tag, \s9e\TextFormatter\Parser $parser) + { + // Remove the url attribute because it's not needed. + $tag->removeAttribute('url'); + + // Invalidate if the content of the tag matches the text attribute + $text = substr($parser->getText(), $tag->getPos(), $tag->getLen()); + + return ($text !== $tag->getAttribute('text')); + } + + /** + * Create an AUTOLINK_TEXT tag inside of a link created by the Autolink plugin + * + * Will only apply to URL tags that do not use any markup (e.g. not "[url]") + * on the assumption that those tags were created by the Autolink plugin to + * linkify URLs found in plain text + * + * @param \s9e\TextFormatter\Parser\Tag $tag URL tag (start tag) + * @param \s9e\TextFormatter\Parser $parser Parser + * @return bool Always true to indicate that the tag is valid + */ + public function generate_autolink_text_tag(\s9e\TextFormatter\Parser\Tag $tag, \s9e\TextFormatter\Parser $parser) + { + // If the tag consumes any text then we ignore it because it's not a + // linkified URL. Same if it's not paired with an end tag that doesn't + // consume any text either + if ($tag->getLen() > 0 || !$tag->getEndTag()) + { + return true; + } + + // Capture the text between the start tag and its end tag + $start = $tag->getPos(); + $end = $tag->getEndTag()->getPos(); + $length = $end - $start; + $text = substr($parser->getText(), $start, $length); + + // Create a tag that consumes the link's text + $parser->addSelfClosingTag('AUTOLINK_TEXT', $start, $length)->setAttribute('text', $text); + + return true; + } + + /** + * Remove the board's root URL from a the start of a string + * + * @param \s9e\TextFormatter\Parser\Tag $tag AUTOLINK_TEXT tag + * @param string $board_url Forum's root URL (with trailing slash) + * @return bool Always true to indicate that the tag is valid + */ + public function truncate_local_url(\s9e\TextFormatter\Parser\Tag $tag, $board_url) + { + $text = $tag->getAttribute('text'); + if (stripos($text, $board_url) === 0 && strlen($text) > strlen($board_url)) + { + $tag->setAttribute('text', substr($text, strlen($board_url))); + } + + return true; + } + + /** + * Truncate the replacement text set in an AUTOLINK_TEXT tag + * + * @param \s9e\TextFormatter\Parser\Tag $tag AUTOLINK_TEXT tag + * @return bool Always true to indicate that the tag is valid + */ + public function truncate_text(\s9e\TextFormatter\Parser\Tag $tag) + { + $text = $tag->getAttribute('text'); + if (utf8_strlen($text) > 55) + { + $text = utf8_substr($text, 0, 39) . ' ... ' . utf8_substr($text, -10); + } + + $tag->setAttribute('text', $text); + + return true; + } +} diff --git a/phpBB/phpbb/textformatter/s9e/factory.php b/phpBB/phpbb/textformatter/s9e/factory.php index 63e49b6dd4..736f9a9f8a 100644 --- a/phpBB/phpbb/textformatter/s9e/factory.php +++ b/phpBB/phpbb/textformatter/s9e/factory.php @@ -23,6 +23,11 @@ use s9e\TextFormatter\Configurator\Items\UnsafeTemplate; class factory implements \phpbb\textformatter\cache_interface { /** + * @var \phpbb\textformatter\s9e\autolink_helper + */ + protected $autolink_helper; + + /** * @var \phpbb\cache\driver\driver_interface */ protected $cache; @@ -133,12 +138,14 @@ class factory implements \phpbb\textformatter\cache_interface * @param \phpbb\cache\driver\driver_interface $cache * @param \phpbb\event\dispatcher_interface $dispatcher * @param \phpbb\config\config $config + * @param \phpbb\textformatter\s9e\autolink_helper $autolink_helper * @param string $cache_dir Path to the cache dir * @param string $cache_key_parser Cache key used for the parser * @param string $cache_key_renderer Cache key used for the renderer */ - public function __construct(\phpbb\textformatter\data_access $data_access, \phpbb\cache\driver\driver_interface $cache, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\config\config $config, $cache_dir, $cache_key_parser, $cache_key_renderer) + public function __construct(\phpbb\textformatter\data_access $data_access, \phpbb\cache\driver\driver_interface $cache, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\config\config $config, \phpbb\textformatter\s9e\autolink_helper $autolink_helper, $cache_dir, $cache_key_parser, $cache_key_renderer) { + $this->autolink_helper = $autolink_helper; $this->cache = $cache; $this->cache_dir = $cache_dir; $this->cache_key_parser = $cache_key_parser; @@ -404,19 +411,35 @@ class factory implements \phpbb\textformatter\cache_interface $configurator->plugins->load('Autoemail'); $configurator->plugins->load('Autolink', array('matchWww' => true)); - // Create a tag that will be used to display the truncated text by replacing the original - // content with the content of the @text attribute + // Add a tag filter that creates a tag that stores and replace the + // content of a link created by the Autolink plugin + $configurator->Autolink->getTag()->filterChain + ->add(array($this->autolink_helper, 'generate_autolink_text_tag')) + ->resetParameters() + ->addParameterByName('tag') + ->addParameterByName('parser'); + + // Create a tag that will be used to display the truncated text by + // replacing the original content with the content of the @text attribute $tag = $configurator->tags->add('AUTOLINK_TEXT'); $tag->attributes->add('text'); + $tag->attributes->add('url', array('required' => false))->filterChain->add('#url'); $tag->template = '<xsl:value-of select="@text"/>'; - // Add a tag filter that replaces the text of links that were created by the Autolink plugin - $configurator->Autolink->getTag()->filterChain - ->add(__NAMESPACE__ . '\\parser::generate_autolink_text') + $tag->filterChain + ->add(array($this->autolink_helper, 'truncate_local_url')) ->resetParameters() ->addParameterByName('tag') - ->addParameterByName('parser') ->addParameterByValue(generate_board_url() . '/'); + $tag->filterChain + ->add(array($this->autolink_helper, 'truncate_text')) + ->resetParameters() + ->addParameterByName('tag'); + $tag->filterChain + ->add(array($this->autolink_helper, 'cleanup_tag')) + ->resetParameters() + ->addParameterByName('tag') + ->addParameterByName('parser'); } /** diff --git a/phpBB/phpbb/textformatter/s9e/parser.php b/phpBB/phpbb/textformatter/s9e/parser.php index beb737c816..ffaffbc63c 100644 --- a/phpBB/phpbb/textformatter/s9e/parser.php +++ b/phpBB/phpbb/textformatter/s9e/parser.php @@ -393,51 +393,4 @@ class parser implements \phpbb\textformatter\parser_interface return $url; } - - /** - * Replace the content displayed inside of a URL tag - * - * Will only apply to URL tags that do not use any markup (e.g. not "[url]") - * on the assumption that those tags were created by the Autolink plugin to - * linkify URLs found in plain text - * - * @param \s9e\TextFormatter\Parser\Tag $url_tag URL tag (start tag) - * @param \s9e\TextFormatter\Parser $parser Parser - * @param string $board_url Forum's root URL (with trailing slash) - * @return bool Always true to indicate that the tag is valid - */ - public static function generate_autolink_text(\s9e\TextFormatter\Parser\Tag $url_tag, \s9e\TextFormatter\Parser $parser, $board_url) - { - // If the tag consumes any text then we ignore it because it's not a - // linkified URL. Same if it's not paired with an end tag that doesn't - // consume any text either - if ($url_tag->getLen() > 0 || !$url_tag->getEndTag()) - { - return true; - } - - // Capture the text between the start tag and its end tag - $start = $url_tag->getPos(); - $end = $url_tag->getEndTag()->getPos(); - $length = $end - $start; - $text = substr($parser->getText(), $start, $length); - - // Remove the board's root URL from the link if applicable - if (stripos($text, $board_url) === 0 && strlen($text) > strlen($board_url)) - { - $text = substr($text, strlen($board_url)); - } - - // Truncate the text if it's longer than 55 characters - if (utf8_strlen($text) > 55) - { - $text = utf8_substr($text, 0, 39) . ' ... ' . utf8_substr($text, -10); - } - - // Create a tag that consumes the link's text - $tag = $parser->addSelfClosingTag('AUTOLINK_TEXT', $start, $length); - $tag->setAttribute('text', $text); - - return true; - } } diff --git a/tests/test_framework/phpbb_test_case_helpers.php b/tests/test_framework/phpbb_test_case_helpers.php index aa58b3974b..dacf9d923f 100644 --- a/tests/test_framework/phpbb_test_case_helpers.php +++ b/tests/test_framework/phpbb_test_case_helpers.php @@ -469,16 +469,13 @@ class phpbb_test_case_helpers } // Set up the a minimum config - if (!isset($config)) + if ($container->has('config')) { - if ($container->has('config')) - { - $config = $container->get('config'); - } - else - { - $config = new \phpbb\config\config(array()); - } + $config = $container->get('config'); + } + elseif (!isset($config)) + { + $config = new \phpbb\config\config(array()); } $default_config = array( 'allow_nocensors' => false, @@ -504,7 +501,7 @@ class phpbb_test_case_helpers } // Create and register the text_formatter.s9e.factory service - $factory = new \phpbb\textformatter\s9e\factory($dal, $cache, $dispatcher, $config, $cache_dir, $cache_key_parser, $cache_key_renderer); + $factory = new \phpbb\textformatter\s9e\factory($dal, $cache, $dispatcher, $config, new \phpbb\textformatter\s9e\autolink_helper, $cache_dir, $cache_key_parser, $cache_key_renderer); $container->set('text_formatter.s9e.factory', $factory); // Create a user if none was provided, and add the common lang strings diff --git a/tests/text_formatter/s9e/factory_test.php b/tests/text_formatter/s9e/factory_test.php index 77f27d6547..dbf28b1d38 100644 --- a/tests/text_formatter/s9e/factory_test.php +++ b/tests/text_formatter/s9e/factory_test.php @@ -50,6 +50,7 @@ class phpbb_textformatter_s9e_factory_test extends phpbb_database_test_case $this->cache, $this->dispatcher, new \phpbb\config\config(array('allowed_schemes_links' => 'http,https,ftp')), + new \phpbb\textformatter\s9e\autolink_helper, $this->get_cache_dir(), '_foo_parser', '_foo_renderer' |