Batch processing to search and replace in all posts

Is there a simple Ruby script somewhere that can be easily tweaked to do a search and replace in all posts?

I’ve migrated from MyBB and there are some tiny formatting tweaks that need to be made, e.g. replace [hr] with ----------, or convert the quote lines. I’m very new to Ruby (basically learned enough last night to submit that quoteline conversion PR), so I’m looking for a skeleton script that loops through existing posts.

1 Like

you can use script/discourse remap it may do the trick.

remap does a search and replace on text and char columns across all tables - a little rough of a tool. Also, how would the raw -> cooked conversion be handled?

Is there a higher-level way of altering and rebaking posts, that would take into account things like re-computing excerpts, quoted posts and reply relationships? The latter are especially important when fixing the quote line after the import.

You have a good point it might be nice to have a variant of this task that operates on just post bodies, @sam. It would be much safer too.

2 Likes

Authors of import plugins (e.g. @Sander78’s Facebook import) should be able to crank/extract one out relatively quickly.

Alternatively, I’ve released a Node.js script that uses the API to do search&replace:

3 Likes

@techapj I would like a rake task that does string replacements scoped to just posts, if you can add that to your todo list. It can be a variant of the rake task that already exists, just a bit more specific in what it affects…

2 Likes

Okay, we have one now:

5 Likes