Splitting a topic breaks quote links

Started by Kolya, January 16, 2021, 01:26:23 PM

Previous topic - Next topic

Kolya

SMF 2.0.17

I never noticed this before, probably because I rarely split topics. But apparently it breaks all quote links in the new split-off topic.
They still point to the old topic but cannot find the correct message there of course.

So is this normal? Is there a fix or a mod that fixes it?

Aleksi "Lex" Kilpinen

Well, the quote format in 2.0 is originally designed to point to a topic first, and a unique post second.
That can cause issues in situations like that. 2.1 fixed that, and removed the topic pointer as unnecessary.
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Kolya

I see. Since 2.1 is still RC I'm not willing to install it. But if the topic pointer is really just superfluous I guess I could remove it myself. I'll have to take a look at how 2.1 did it.
Thank you.

Aleksi "Lex" Kilpinen

#3
2.0 format:


[quote author=Kolya link=topic=576366.msg4078560#msg4078560 date=1610823674]
I see. Since 2.1 is still RC I'm not willing to install it. But if the topic pointer is really just superfluous I guess I could remove it myself. I'll have to take a look at how 2.1 did it.
Thank you.
[/quote]


2.1 format:


[quote author=Kolya link=msg=4078560 date=1610823674]
I see. Since 2.1 is still RC I'm not willing to install it. But if the topic pointer is really just superfluous I guess I could remove it myself. I'll have to take a look at how 2.1 did it.
Thank you.
[/quote]


Obviously, doesn't work here - we are still on 2.0
Still, even on 2.0 you can link to a specific post directly with https://www.simplemachines.org/community/index.php?msg=4078560
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Illori

even if you upgrade to 2.1, it will not fix the existing links for quotes that already exist, just those made in the future.

Kolya

Thanks Illori, I figured as much since the link is written into the posts.

I know where it's matched with a regex in Subs.php, and I just found where it is generated in Post.php.
Comparing it to SMF 2.1 now. Doesn't look too difficult. (His last words)

The whole thing is especially important to me, since I created a Reply-To-Post function, which is basically a quote without quote text, just the link back. Hence it uses the quote code.

Kolya

I made the changes without problems, however I'm not absolutely happy with the result.

Previously a quote link looked like this: /index.php?topic=11449.msg137830#msg137830
Now it looks like this: /index.php?msg=137622

While we gained the ability to link to posts regardless of topic, we lost the ability to just scroll up to the message when it's on the same page.
A page reload is now inevitable, since the new URL never matches the current topic URL.

Looks like the topic in the URL wasn't really unnecessary. The hashtag anchor suggests back-scrolling was achieved intentionally in the original code.

Aleksi "Lex" Kilpinen

Actually no, the ?msg=*** link should get you to the exact message, just as the old link did.
You can try it here, following this link https://www.simplemachines.org/community/index.php?msg=4078560 you should get directly to that post.

EDIT: Oh, I guess I misread you a bit - But I'm not sure if clicking on a quote link you would ever avoid a page load though.
The hashtag was there for initial page load, not for avoiding the page load as far as I know.
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Kolya

Yes, the browser eventually gets to the correct URL after following the redirect. But that still forces a page reload.

These links end up in the same place, but the first just scrolls back up, while the second page reloads:
1. https://www.simplemachines.org/community/index.php?topic=576366.msg4078558#msg4078558
2. https://www.simplemachines.org/community/index.php?msg=4078558

Example doesn't work here, because target="_blank" is set on both links...

A better solution would be to figure out the new full URL in PHP and then linked to that, instead of sending the browser to a redirect.

Aleksi "Lex" Kilpinen

No, the first one doesn't scroll back either - not by default, or at least I haven't ever seen that.
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Kolya

True, the example doesn't work here, because target="_blank" is set on both links. Otherwise the first link would just scroll up without a page reload.

You can click on quote links here to see what I mean: https://www.systemshock.org/index.php?topic=11449.0

Aleksi "Lex" Kilpinen

Even without that, it does a page load first, and only then gets you to the anchor. Because the quote or the link doesn't hold the information on if the post is actually on the same page at all.
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Kolya

If the linked URL is the same as the current URL, only with a hashtag anchor added, the browser merely scrolls to that place.
You just broke that indended function here by adding target="_blank" to all links.
I added an example in my previous post.

Aleksi "Lex" Kilpinen

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Aleksi "Lex" Kilpinen

Quote from: Kolya on January 16, 2021, 05:32:20 PM
If the linked URL is the same as the current URL, only with a hashtag anchor added, the browser merely scrolls to that place.
You merely broke that indended function here by adding target="_blank" to all links.
I added an example in my previous post.
Nope, I tried before I posted. I actually tried it on 2.1 as well, and even there it does a page load to determine what page the post is on.
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Kolya

Quote from: Aleksi "Lex" Kilpinen on January 16, 2021, 05:32:40 PM
Yeah, that's not default SMF behaviour.

Someone clearly intended it to work that way. There is no other reason to attach the hashtag anchor. And it would work here as well, if target="_blank" was not set on these links.

Anyway, I don't mean to be accusatory or anything. I merely find that the new solution has drawbacks for me.

Aleksi "Lex" Kilpinen

The anchor is there so that you can link to a specific post, and get to the post directly when you follow the link.
Both of your example links use the anchor to find it after the page load, the link format doesn't matter - they both lead to to the same URL including the anchor.
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Kolya

I created a test topic to show the difference of the old and new quote links: https://www.systemshock.org/index.php?topic=11558

The first quote links to: https://www.systemshock.org/index.php?msg=137874
The second links to: https://www.systemshock.org/index.php?topic=11558.msg137874#msg137874

The first link is a redirect and forces a page reload.
That second link is the same URL you are on when viewing the topic, only with an anchor added. Hence it scrolls back.

Oh well, I guess it's just my bad luck that this apparently undocumented feature doesn't work anymore in 2.1

Aleksi "Lex" Kilpinen

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Kolya

A link is a link. Why would it not work the same way here if the target="_blank" was removed? This is default browser behavior*. There is no scripting involved.

* https://css-tricks.com/hash-tag-links-padding/

Aleksi "Lex" Kilpinen

Try these

https://www.simplemachines.org/community/index.php?msg=4078560
and
https://www.simplemachines.org/community/index.php?topic=576366.msg4078560#msg4078560

Wait, I'll actually modify my last post on the earlier page to include these for testing purposes.
If you wish to test them, please make sure to test one, then reload the topic without the anchor in the URL before testing the other.

Quote from: Kolya on January 16, 2021, 05:59:34 PM
A link is a link. Why would it not work the same way here if the target="_blank" was removed? This is default browser behavior*. There is no scripting involved.
The link is to index.php, not directly #msg4078560 - the latter would only work if the post actually was on the same page, but would fail if it wasn't.
On your forum, there is quite probably some custom scripting involved, in one way or another, to predetermine if the anchor is already on the same page.
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Kolya

You are right Lex, I found the script. Been a few years since I wrote that. This is embarrassing. :/
I guess I won't be able to fix it, since the topic information is lost in the link, but that's my problem.

While native browser scrolling to anchors exists, if the URL is the same, that's not what's happening on my site.

Aleksi "Lex" Kilpinen

Well, we all make mistakes - No worries. :)
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

shawnb61

#23
I have a utility that might help.  (It's what I do... ::))

This helps you find broken links:
https://github.com/sbulen/sjrbTools/blob/master/SMF_Topic_Link_Diag.php

And this one will actually fix them:
https://github.com/sbulen/sjrbTools/blob/master/SMF_Topic_Link_Fixer.php

We rearrange our boards & topics all the time.  I run the Fixer occasionally to address. 

As usual, experiment in test first, to get familiar with what it's doing & make sure it works alongside any customizations you may have made.

If it cannot find the original message (e.g., if it were deleted) it removes the link and just leaves the reference to the author.
Address the process rather than the outcome.  Then, the outcome becomes more likely.   - Fripp

Kolya

Thanks shawnb61, that's an interesting approach. I'm currently looking into identifying the correct topic in Subs.php through one of those costly on-the-fly generated functions.
Basically get the message ID by regex, then db_query to get its current topic. If that works out, I can ignore the topic value from quotes (if there is any) and still create a complete link, including topic.

Kolya

I had no problems writing the function in Subs.php (which I placed after the forum_time function):


function getMessageLink($msg) {
  global $smcFunc;
$request = $smcFunc['db_query']('', '
SELECT id_topic
FROM {db_prefix}messages
WHERE id_msg = {int:msg}
LIMIT 1',
array(
'msg' => (int) $msg,
));
if ($smcFunc['db_num_rows']($request) != 1) {
return;
} else {
list ($topic) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$msgLink = 'topic='.$topic.'.msg'.$msg.'#msg'.$msg;
return $msgLink;
}
}


So this function takes a message ID and gives back the path including topic that quote expects.

Surprisingly I'm having problems matching the message ID in all cases. I can get it out of new quotes but not old quotes.
new quote: msg=137874
old quote: topic=11558.msg137874#msg137874

This is what I'm doing:

'link' => array('match' => 'msg=?(\d+)', 'validate' => 'getMessageLink'),


According to my tests the regex should match both quote styles. But it only works on the new one.
If you know why, I'm interested. Else I'll figure this out tomorrow.

Kolya

I eventually fixed the regex and simplified things a bit and it's all working now. 

To summarize: I wanted quote links to always contain the correct topic ID, even after a topic split.
The solution of SMF 2.1 to remove the topic ID is legit but didn't work for me, because my scripting requires this ID in the link.
So now I compute the current topic ID by means of the message ID and produce a quote link in SMF 2.0 format at rendering time.
This happens in Subs.php.

BBC parsing:

array(
'tag' => 'quote',
'parameters' => array(
'author' => array('match' => '([^<>]{1,192}?)'),
'date' => array('match' => '(\d+)', 'validate' => 'timeformat'),
'link' => array('match' => '(?:topic=\d+\.msg\d+#?)?msg=?(\d+)', 'validate' => 'getMessageLink'),
),
'before' => '<div class="quoteheader"><a data-unused="{date}" title="View original post" href="{link}">Quote by {author}:</a></div><blockquote><div class="quote_inner">',
'after' => '</div></blockquote>',
'block_level' => true,
),


Getting the link:

function getMessageLink($msg) {
  global $smcFunc, $scripturl;
$request = $smcFunc['db_query']('', '
SELECT id_topic
FROM {db_prefix}messages
WHERE id_msg = {int:msg}
LIMIT 1',
array(
'msg' => (int) $msg,
));
if ($smcFunc['db_num_rows']($request) != 1) {
return;
} else {
list ($topic) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$msgLink = $scripturl.'?topic='.$topic.'.msg'.$msg.'#msg'.$msg;
return $msgLink;
}
}

shawnb61

Address the process rather than the outcome.  Then, the outcome becomes more likely.   - Fripp

Advertisement: