[ticket/14323] Moved autolink-related functions to a separate helper

PHPBB3-14323
This commit is contained in:
JoshyPHP 2015-12-06 22:08:00 +01:00
parent f9d8866aee
commit ad2c032d3b
6 changed files with 150 additions and 67 deletions

View file

@ -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%'

View file

@ -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;
}
}

View file

@ -22,6 +22,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
*/
@ -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
$tag = $configurator->tags->add('AUTOLINK_TEXT');
$tag->attributes->add('text');
$tag->template = '<xsl:value-of select="@text"/>';
// Add a tag filter that replaces the text of links that were created by the Autolink plugin
// 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(__NAMESPACE__ . '\\parser::generate_autolink_text')
->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"/>';
$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');
}
/**

View file

@ -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;
}
}

View file

@ -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

View file

@ -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'