1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-04-20 07:42:09 +02:00

Merge pull request #4059 from JoshyPHP/ticket/14323

[ticket/14323] Added support for truncating long URLs
This commit is contained in:
Marc Alexander 2016-01-13 12:55:31 +01:00
commit 08647781c0
9 changed files with 236 additions and 16 deletions

View File

@ -33,10 +33,14 @@ services:
- '@cache.driver'
- '@dispatcher'
- '@config'
- '@text_formatter.s9e.link_helper'
- '%text_formatter.cache.dir%'
- '%text_formatter.cache.parser.key%'
- '%text_formatter.cache.renderer.key%'
text_formatter.s9e.link_helper:
class: phpbb\textformatter\s9e\link_helper
text_formatter.s9e.parser:
class: phpbb\textformatter\s9e\parser
arguments:

View File

@ -22,6 +22,11 @@ use s9e\TextFormatter\Configurator\Items\UnsafeTemplate;
*/
class factory implements \phpbb\textformatter\cache_interface
{
/**
* @var \phpbb\textformatter\s9e\link_helper
*/
protected $link_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\link_helper $link_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\link_helper $link_helper, $cache_dir, $cache_key_parser, $cache_key_renderer)
{
$this->link_helper = $link_helper;
$this->cache = $cache;
$this->cache_dir = $cache_dir;
$this->cache_key_parser = $cache_key_parser;
@ -332,8 +339,7 @@ class factory implements \phpbb\textformatter\cache_interface
}
// Load the magic links plugins. We do that after BBCodes so that they use the same tags
$configurator->plugins->load('Autoemail');
$configurator->plugins->load('Autolink', array('matchWww' => true));
$this->configure_autolink($configurator);
// Register some vars with a default value. Those should be set at runtime by whatever calls
// the parser
@ -394,6 +400,47 @@ class factory implements \phpbb\textformatter\cache_interface
return array('parser' => $parser, 'renderer' => $renderer);
}
/**
* Configure the Autolink / Autoemail plugins used to linkify text
*
* @param \s9e\TextFormatter\Configurator $configurator
* @return void
*/
protected function configure_autolink(Configurator $configurator)
{
$configurator->plugins->load('Autoemail');
$configurator->plugins->load('Autolink', array('matchWww' => true));
// 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->link_helper, 'generate_link_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('LINK_TEXT');
$tag->attributes->add('text');
$tag->template = '<xsl:value-of select="@text"/>';
$tag->filterChain
->add(array($this->link_helper, 'truncate_local_url'))
->resetParameters()
->addParameterByName('tag')
->addParameterByValue(generate_board_url() . '/');
$tag->filterChain
->add(array($this->link_helper, 'truncate_text'))
->resetParameters()
->addParameterByName('tag');
$tag->filterChain
->add(array($this->link_helper, 'cleanup_tag'))
->resetParameters()
->addParameterByName('tag')
->addParameterByName('parser');
}
/**
* Return the default BBCodes configuration
*

View File

@ -0,0 +1,118 @@
<?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 link_helper
{
/**
* Clean up and invalidate a LINK_TEXT tag if applicable
*
* Will invalidate the tag if its replacement text is the same as the original
* text and would have no visible effect
*
* @param \s9e\TextFormatter\Parser\Tag $tag LINK_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)
{
// 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 a LINK_TEXT tag inside of a link
*
* Meant to only apply to linkified URLs and [url] BBCodes without a parameter
*
* @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_link_text_tag(\s9e\TextFormatter\Parser\Tag $tag, \s9e\TextFormatter\Parser $parser)
{
// Only create a LINK_TEXT tag if the start tag is paired with an end
// tag, which is the case with tags from the Autolink plugins and with
// the [url] BBCode when its content is used for the URL
if (!$tag->getEndTag() || !$this->should_shorten($tag, $parser->getText()))
{
return true;
}
// Capture the text between the start tag and its end tag
$start = $tag->getPos() + $tag->getLen();
$end = $tag->getEndTag()->getPos();
$length = $end - $start;
$text = substr($parser->getText(), $start, $length);
// Create a tag that consumes the link's text
$parser->addSelfClosingTag('LINK_TEXT', $start, $length)->setAttribute('text', $text);
return true;
}
/**
* Test whether we should shorten this tag's text
*
* Will test whether the tag either does not use any markup or uses a single
* [url] BBCode
*
* @param \s9e\TextFormatter\Parser\Tag $tag URL tag
* @param string $text Original text
* @return bool
*/
protected function should_shorten(\s9e\TextFormatter\Parser\Tag $tag, $text)
{
return ($tag->getLen() === 0 || strtolower(substr($text, $tag->getPos(), $tag->getLen())) === '[url]');
}
/**
* Remove the board's root URL from a the start of a string
*
* @param \s9e\TextFormatter\Parser\Tag $tag LINK_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 a LINK_TEXT tag
*
* @param \s9e\TextFormatter\Parser\Tag $tag LINK_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

@ -315,7 +315,7 @@ class phpbb_test_case_helpers
public function set_s9e_services(ContainerInterface $container = null, $fixture = null, $styles_path = null)
{
static $first_run;
global $phpbb_container, $phpbb_dispatcher, $phpbb_root_path, $phpEx, $user;
global $config, $phpbb_container, $phpbb_dispatcher, $phpbb_root_path, $phpEx, $request, $user;
$cache_dir = __DIR__ . '/../tmp/';
@ -473,14 +473,18 @@ class phpbb_test_case_helpers
{
$config = $container->get('config');
}
else
elseif (!isset($config))
{
$config = new \phpbb\config\config(array());
}
$default_config = array(
'allow_nocensors' => false,
'allow_nocensors' => false,
'allowed_schemes_links' => 'http,https,ftp',
'smilies_path' => 'images/smilies',
'script_path' => '/phpbb',
'server_name' => 'localhost',
'server_port' => 80,
'server_protocol' => 'http://',
'smilies_path' => 'images/smilies',
);
foreach ($default_config as $config_name => $config_value)
{
@ -490,8 +494,14 @@ class phpbb_test_case_helpers
}
}
// Create a fake request
if (!isset($request))
{
$request = new phpbb_mock_request;
}
// 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\link_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

@ -225,6 +225,36 @@ class phpbb_textformatter_s9e_default_formatting_test extends phpbb_test_case
'... www.example.org ...',
'... <a href="http://www.example.org" class="postlink">www.example.org</a> ...'
),
array(
// From make_clickable_test.php
'www.phpbb.com/community/?',
'<a href="http://www.phpbb.com/community/" class="postlink">www.phpbb.com/community/</a>?'
),
array(
// From make_clickable_test.php
'http://www.phpbb.com/community/path/to/long/url/file.ext#section',
'<a href="http://www.phpbb.com/community/path/to/long/url/file.ext#section" class="postlink">http://www.phpbb.com/community/path/to/ ... xt#section</a>'
),
array(
'http://localhost/ http://localhost/phpbb/ http://localhost/phpbb/viewforum.php?f=1',
'<a href="http://localhost/" class="postlink">http://localhost/</a> <a href="http://localhost/phpbb/" class="postlink">http://localhost/phpbb/</a> <a href="http://localhost/phpbb/viewforum.php?f=1" class="postlink">viewforum.php?f=1</a>'
),
array(
'http://localhost/phpbb/viewforum.php?f=1#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'<a href="http://localhost/phpbb/viewforum.php?f=1#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" class="postlink">viewforum.php?f=1#xxxxxxxxxxxxxxxxxxxxx ... xxxxxxxxxx</a>'
),
array(
'[url]http://example.org/0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0[/url]',
'<a href="http://example.org/0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0" class="postlink">http://example.org/0xxxxxxxxxxxxxxxxxxx ... xxxxxxxxx0</a>'
),
array(
'[URL]http://example.org/0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0[/url]',
'<a href="http://example.org/0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0" class="postlink">http://example.org/0xxxxxxxxxxxxxxxxxxx ... xxxxxxxxx0</a>'
),
array(
'[url=http://example.org/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[/url]',
'<a href="http://example.org/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" class="postlink">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</a>'
),
array(
'[quote="[url=http://example.org]xxx[/url]"]...[/quote]',
'<blockquote><div><cite><a href="http://example.org" class="postlink">xxx</a> wrote:</cite>...</div></blockquote>'

View File

@ -35,7 +35,7 @@ class phpbb_textformatter_s9e_factory_test extends phpbb_database_test_case
public function get_factory()
{
global $phpbb_root_path;
global $config, $phpbb_root_path, $request, $user;
$this->cache = new phpbb_mock_cache;
$dal = new \phpbb\textformatter\data_access(
$this->new_dbal(),
@ -50,11 +50,22 @@ 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\link_helper,
$this->get_cache_dir(),
'_foo_parser',
'_foo_renderer'
);
// Global objects required by generate_board_url()
$config = new \phpbb\config\config(array(
'script_path' => '/phpbb',
'server_name' => 'localhost',
'server_port' => 80,
'server_protocol' => 'http://',
));
$request = new phpbb_mock_request;
$user = new phpbb_mock_user;
return $factory;
}
@ -128,14 +139,14 @@ class phpbb_textformatter_s9e_factory_test extends phpbb_database_test_case
public function test_local_url()
{
global $config, $user, $request;
$config = array(
$config = new \phpbb\config\config(array(
'force_server_vars' => true,
'server_protocol' => 'http://',
'server_name' => 'path',
'server_port' => 80,
'script_path' => '/to',
'cookie_secure' => false
);
));
$user = new phpbb_mock_user;
$request = new phpbb_mock_request;

View File

@ -1,2 +1,2 @@
<a href="http://www.tx-gaming.net/warzone/tournament.php?tourney%5Bid%5D=34&amp;action=brackets" class="postlink">http://www.tx-gaming.net/warzone/tournament.php?tourney[id]=34&amp;action=brackets</a><br>
<a href="http://www.tx-gaming.net/warzone/tournament.php?tourney%5Bid%5D=34&amp;action=brackets" class="postlink">link</a>
<a href="http://example.org/?tourney%5Bid%5D=34&amp;action=brackets" class="postlink">http://example.org/?tourney[id]=34&amp;action=brackets</a><br>
<a href="http://example.org/?tourney%5Bid%5D=34&amp;action=brackets" class="postlink">link</a>

View File

@ -1,2 +1,2 @@
[url]http://www.tx-gaming.net/warzone/tournament.php?tourney[id]=34&action=brackets[/url]
[url="http://www.tx-gaming.net/warzone/tournament.php?tourney[id]=34&action=brackets"]link[/url]
[url]http://example.org/?tourney[id]=34&action=brackets[/url]
[url="http://example.org/?tourney[id]=34&action=brackets"]link[/url]

View File

@ -1 +1 @@
<a href="http://www.phpbb.com/community/search.php?keywords=bogus&amp;terms=all&amp;author=&amp;fid%5B%5D=46&amp;sc=1&amp;sf=all&amp;sr=posts&amp;sk=t&amp;sd=d&amp;st=0&amp;ch=300&amp;t=0&amp;submit=Search" class="postlink">http://www.phpbb.com/community/search.php?keywords=bogus&amp;terms=all&amp;author=&amp;fid[]=46&amp;sc=1&amp;sf=all&amp;sr=posts&amp;sk=t&amp;sd=d&amp;st=0&amp;ch=300&amp;t=0&amp;submit=Search</a>
<a href="http://www.phpbb.com/community/search.php?keywords=bogus&amp;terms=all&amp;author=&amp;fid%5B%5D=46&amp;sc=1&amp;sf=all&amp;sr=posts&amp;sk=t&amp;sd=d&amp;st=0&amp;ch=300&amp;t=0&amp;submit=Search" class="postlink">http://www.phpbb.com/community/search.p ... mit=Search</a>