XF 2.0 Converting Twitter links from old forum into proper format

orthopug

Member
I just moved over to Xenforo from Mybb. Right now my forum has a lot of Twitter links in this format:

Code:
https://twitter.com/capitalweather/status/1255596050162860037

It only displays a clickable link. If I click Edit on a post with such a link and then hit save, Xenforo adds the media tags to the Twitter link:

Code:
[MEDIA=twitter]1255596050162860037[/MEDIA]

The tweet then shows correctly. Is it possible to convert all existing Twitter links on my forum into the proper media format? I tried to Rebuild Threads but that didn't do it.
 
How big is your database?

You're probably going to have to use a regex replacement to rewrite them.

2.1 version: https://xenforo.com/community/resources/post-content-find-replace.5748/
Agreed, it’s easy enough to search and replace the Twitter url with opening media tag, but adding the closing tag not so straightforward.
 
You also need to handle the URL bbcodes too I think. Those pasted twitter links have a URL bbcode around them? If so, you need to match those too.

This is a first draft. Untested.

It only looks for twitter links wrapped in URL bbcode.

regex:
Code:
/\[url\]https?:\/\/twitter\.com\/(?:\#!\/)?(\w+)\/status(es)?\/(\d+)(\?s=\d+)?\[\/url\]/mi

replace
Code:
[MEDIA=twitter]\3[/MEDIA]

Use at your own risk!

Here's what it matches: https://regex101.com/r/Bk0yLv/1

arn
 
You also need to handle the URL bbcodes too I think. Those pasted twitter links have a URL bbcode around them? If so, you need to match those too.

This is a first draft. Untested.

It only looks for twitter links wrapped in URL bbcode.

regex:
Code:
/\[url\]https?:\/\/twitter\.com\/(?:\#!\/)?(\w+)\/status(es)?\/(\d+)(\?s=\d+)?\[\/url\]/mi

replace
Code:
[MEDIA=twitter]\3[/MEDIA]

Use at your own risk!

Here's what it matches: https://regex101.com/r/Bk0yLv/1

arn

Is there a way to do this find/replace in command line? My database is too big so the Find/Replace add-on crashes.

I tried dumping my xf_post.sql table and running this:

Code:
cat xf_post.sql | sed -e "s/\[url\]/\[MEDIA=twitter\]/" -e "s/\[\/url\]/\[\/MEDIA\]/" | sed -E "s/(\[MEDIA=twitter\]).*status.(.+\[\/MEDIA\])/\1\2/" | sed -E "s/(\[MEDIA=twitter\])*[[:space:]|\/]*(\[\/MEDIA\])/\1\2/" > new_xf_post.sql

But it didn't work and ended up deleting about 10% of the database.
 
Top Bottom