Merge pull request #6604 from marc1706/ticket/17130

[ticket/17130] Reparse magic urls in posts based on actual content
This commit is contained in:
Marc Alexander 2024-05-03 22:16:28 +02:00
commit 2267ef1ac2
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
3 changed files with 69 additions and 2 deletions

View file

@ -198,8 +198,8 @@ abstract class base implements reparser_interface
*/
protected function guess_magic_url(array $record)
{
// Look for <!-- m --> or for a URL tag that's not immediately followed by <s>
return (strpos($record['text'], '<!-- m -->') !== false || preg_match('(<URL [^>]++>(?!<s>))', $record['text']));
// Look for magic URL markers or for a URL tag that's not immediately followed by <s>
return preg_match('#<!-- ([lmwe]) -->.*?<!-- \1 -->#', $record['text']) || preg_match('(<URL [^>]++>(?!<s>))', $record['text']);
}
/**
@ -231,7 +231,10 @@ abstract class base implements reparser_interface
*/
protected function reparse_record(array $record)
{
// Guess magic URL state based on actual record content before adding fields
$record['enable_magic_url'] = $this->guess_magic_url($record);
$record = $this->add_missing_fields($record);
$flags = ($record['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0;
$flags |= ($record['enable_smilies']) ? OPTION_FLAG_SMILIES : 0;
$flags |= ($record['enable_magic_url']) ? OPTION_FLAG_LINKS : 0;

View file

@ -23,4 +23,67 @@ class phpbb_textreparser_post_text_test extends phpbb_textreparser_test_row_base
{
return new \phpbb\textreparser\plugins\post_text($this->db, POSTS_TABLE);
}
public function data_reparse_url(): array
{
return [
[ // Reparse the same
'<r><URL url="https://www.example.com">https://www.example.com</URL> and some more text test included</r>',
'<r><URL url="https://www.example.com">https://www.example.com</URL> and some more text test included</r>',
],
[ // Reparse without magic URL, shouldn't change
'https://www.example.com and some more text test included',
'<t>https://www.example.com and some more text test included</t>',
],
[ // Reparse new format without magic URL, shouldn't change
'<t>https://www.example.com and some more text test included</t>',
'<t>https://www.example.com and some more text test included</t>',
],
[ // Reparse with magic URL, should update to text formatter format
'Foo is <!-- m --><a class="postlink" href="https://symfony.com/doc/current/service_container.html">https://symfony.com/doc/current/service_container.html</a><!-- m --> good',
'<r>Foo is <URL url="https://symfony.com/doc/current/service_container.html">https://symfony.com/doc/current/service_container.html</URL> good</r>',
],
[ // Reparse new format with magic URL, shouldn't change
'<r>Foo is <URL url="https://symfony.com/doc/current/service_container.html">https://symfony.com/doc/current/service_container.html</URL> good</r>',
'<r>Foo is <URL url="https://symfony.com/doc/current/service_container.html">https://symfony.com/doc/current/service_container.html</URL> good</r>',
]
];
}
/**
* @dataProvider data_reparse_url
*/
public function test_reparse_url(string $input_text, string $expected_text)
{
foreach ([true, false] as $enable_magic_url)
{
$record = [
'enable_bbcode' => true,
'enable_smilies' => true,
'enable_magic_url' => $enable_magic_url,
'post_text' => $input_text,
'bbcode_uid' => '',
];
$sql = 'INSERT INTO ' . POSTS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $record);
$this->db->sql_query($sql);
$record['id'] = $this->db->sql_last_inserted_id();
$record['text'] = $record['post_text'];
// Call reparse_record via reflection
$reparser = $this->get_reparser();
$reparser_reflection = new \ReflectionMethod($reparser, 'reparse_record');
$reparser_reflection->setAccessible(true);
$reparser_reflection->invoke($reparser, $record);
// Retrieve reparsed post text and compare with expectec
$sql = 'SELECT post_id, post_text FROM ' . POSTS_TABLE . ' WHERE post_id = ' . (int) $record['id'];
$result = $this->db->sql_query($sql);
$actual_text = $this->db->sql_fetchfield('post_text');
$this->db->sql_freeresult($result);
$this->assertSame($expected_text, $actual_text);
}
}
}

View file

@ -15,6 +15,7 @@ require_once __DIR__ . '/../../test_framework/phpbb_database_test_case.php';
abstract class phpbb_textreparser_test_row_based_plugin extends phpbb_database_test_case
{
/** @var \phpbb\db\driver\driver_interface */
protected $db;
abstract protected function get_reparser();