That won’t let Discourse download those images so you’ll have them when they are no longer available from photobucket, but it will display them.
So then you’d want to go into rails, get all the posts that have ^https://hosting.photobucket.com, and do a gsub to replace those bare URLs with ![](the url).
Wait. When I wrote this the first time, the top URL didn’t render, but then later it looks like Discourse did manage to pull it down! (And it worked on my test server too!)
I need to run a rails command to find all of those posts and then use gsub to replace the [URL…][/URL] with just the actual url so that it will render in a one-box
DiscourseHelper:
Searching
Searching for: How do I use a Rails command or console code to find all posts containing a specific string format like [URL] tags and then replace those tags with just the contained URL in Discourse?
To find all instances of [URL]...[/URL] and replace them with only the contained URL in your Discourse posts, you can accomplish this using Rails in the Discourse console. Here’s how you can do it:
Steps:
Access the Rails Console:
You need SSH access to your server. Run the following command to access the Discourse Rails console:
cd /var/discourse
./launcher enter app
rails c
Run a Find and Replace Command:
Use the following code snippet to locate posts containing [URL]...[/URL] and replace them.
Post.where("cooked LIKE ?", "%[URL=%[/URL]%").find_each do |post|
updated_cooked = post.cooked.gsub(/\[URL=.*?\](.*?)\[\/URL\]/, '\1')
if updated_cooked != post.cooked
post.update!(cooked: updated_cooked)
PostRevisor.new(post, Discourse.system_user).revise!(
post.user,
{ cooked: updated_cooked },
revised_at: Time.now
)
puts "Updated post id: #{post.id}"
end
end
Test the Script:
Before replacing the content in bulk, run the gsub logic on a single post to ensure it behaves as expected.
test_post = Post.first # Replace with an ID for better testing
puts test_post.cooked.gsub(/\[URL=.*?\](.*?)\[\/URL\]/, '\1')
Run the Script and Monitor:
Execute the full script, monitor the output, and correct any errors if necessary.
Notes:
This script uses Regex (\[URL=.*?\](.*?)\[\/URL\]) to match and replace the specified pattern. Ensure the pattern matches your needs exactly.
It uses PostRevisor to ensure the changes are recorded in the post’s edit history.
Be sure to test thoroughly and back up your database before executing any bulk changes.
For additional guidance, check related documentation on meta.discourse.org. If you encounter issues, feel free to share details here!
pfaffman:
It needs to replace those in raw, not cooked
DiscourseHelper:
Thank you for clarifying! Since the changes need to be made to the raw (original post content), we can modify the script to work with the raw field instead. Here’s how you can achieve this:
Updated Rails Command for raw Field
Modify and Run the Script:
Below is the updated script to process and replace the [URL]...[/URL] tags in the raw field of posts:
# Find all posts where the raw includes the [URL] tag
Post.where("raw LIKE ?", "%[URL=%[/URL]%").find_each do |post|
updated_raw = post.raw.gsub(/\[URL=.*?\](.*?)\[\/URL\]/, '\1')
if updated_raw != post.raw
# Update the raw content
post.update!(raw: updated_raw, cooked: nil)
# Trigger rebake to regenerate cooked content
post.rebake!
puts "Updated post id: #{post.id}"
end
end
Explanation of Changes:
raw instead of cooked: The raw field contains the original post content, so the gsub operation is applied to the raw text.
Reset cooked: Setting cooked to nil ensures that the post will be rebaked (processed) correctly.
rebake!: This triggers the rebaking process, which regenerates the cooked (HTML-rendered) version of the post from the updated raw content.
Test the Gsub for Raw Content:
Test the regular expression substitution on a single post to ensure it behaves correctly:
test_post = Post.first # Replace with an appropriate post ID for testing
puts test_post.raw.gsub(/\[URL=.*?\](.*?)\[\/URL\]/, '\1')
Run the Bulk Update:
Once you confirm it works as intended, you can run the script to process all posts containing the [URL] tags.
Monitor and Verify:
Monitor the script’s output. For every updated post, its id will be printed to the console. After completion, check a few posts on the site to ensure the changes appear correctly.
Backup Reminder:
Before running the script, ensure you have a full database backup, especially when working with bulk updates.
If you have additional questions or encounter issues, feel free to ask!
And I like that it uses PostRevisor so that if something goes wrong, you’ll have a record of the edit.
You definitely want to test it on a few posts before turning it loose–and also see how many posts it’s going to fix and who’s going to get notifications on all of those (there is something like bypass_bump or similar that skips the notifications, I think).