Replace a string in all posts

(Arpit Jalan) #17

You’re looking at the wrong place. The source code for rake posts:remap is here:

(Jay Pfaffman) #18

A little knowledge is a dangerous thing!

Is there a way to call that from an import script,or should I do that a different way there?

(Jay Pfaffman) #19

Looks like I should just copy that code into mbox.rb. Thanks for the pointer, @techAPJ.

(Jay Pfaffman) #20

I’m mostly talking to myself here, but I think that I’ve finally gotten my code working (though it is not the first time that I have thought that and been horribly wrong).

Other people might possible care to be reminded that posts:remap is case sensitive. This is a problem replacing email addresses, which get downcased in Users, but no in the text of messages. People who accept recommendations when doing an @mention are safe; there may be some users who type @username and ignore the recommendations, which could leave some @usernames not replaced. I think.

(Anton) #21

This is a good idea (yet not so prioritized) for a built-in administration tool - a GUI with a preview and optional filtering by any criteria accepted by the “search” functionality would help to avoid making errors and mistakes.

To tackle this with minimum changes to existing GUI, an option can be added to the wrench button on the search results page which says “Search & Replace”.

Then, the resulting list will be shown in the “replace preview mode”, when what is found and replaced is highlighted.

Not sure if it is worth spending any time on this, or whether it is something that should be built-in, but I love the feature.

(Tomek) #23

Hi everyone, is it just me or rake posts:remap[“find”,“replace”] cannot replace the find string with en empty replace string? Let’s say I would like to use it to delete a word from all posts, is it possible?

This: rake posts:remap["word-to-delete",""]
Results in this: ERROR: Expecting rake posts:remap[find,replace]

(Arpit Jalan) #24

Good question, it’s not possible currently due to validation of replace argument. I will make sure that this case is supported as well.

(Tomek) #25

This would be great, thanks!

(Arpit Jalan) #26

Okay, done via:

or just rake posts:remap["word-to-delete"] :slight_smile:

(Sam Saffron) #27

Nitpicking, but I would prefer not to support that syntax cause it is super surprising. I think being explicit or adding rake posts:delete_word is probably better.

(Arpit Jalan) #28

When the user runs rake posts:remap["word-to-delete"], the task asks for confirmation, like:

Are you sure you want to remove all occurrences of ‘word-to-delete’? (Y/n)

Is that good enough, or should we still create a new task for it?

(Sam Saffron) #29

If it only takes you 10 minutes then sure, otherwise you can defer on it.

(Arpit Jalan) #30

The task already asks for confirmation when user tries to batch delete a word/string. I added the code for it when I added support for “removing all occurrences of a word”. :slight_smile:

(Arpit Jalan) #31

Done via:

Running rake posts:remap["word-to-delete",""] or rake posts:remap["word-to-delete"] will now show this helpful error message instead:

ERROR: Expecting rake posts:remap['find','replace']. Want to delete a word/string instead? Try rake posts:delete_word['word-to-delete']

Will update first post to reflect the same.

(Nordize) #32

How do I replace "[spoiler" with "[details=Click to reveal"?

It chokes if I do rake posts:remap["[spoiler","[details=Click to reveal"] (tried single quotes and backlash escaping)

EDIT: Darn, I used discourse remap "[spoiler" "[details=Click to reveal" and then also discourse remap "[/spoiler" "[/details", and while it did replace, the baked html is wrong … the text that’s supposed to be hidden appears outside of the “click to reveal line”.

What’s worse is that editing that post manually, shows a correct preview (text hidden), but saving the edit goes back to putting the text outside, even if I cahnge the text! … also did rake posts:rebake and didn’t help.

New posts work fine, I can hide stuff.

This is odd. What am I missing?

(Eli the Bearded) #33

The [details] tag is very picky about whitespace around it, I think you need a line above and below. Spoiler is less sensitive. Nested details also are not supported, I think at all, while nested spoiler works, for trivial cases of “works”, like “one click unhides all of them”.

(Nordize) #34

There wasn’t any issue with spacing. Doing the following oddity worked:

  • edit post and removed [details]
  • saved post
  • edit post and added [details] to the whole post content (instead of where it initially was)
  • saved post
  • edit post and added back [details] exactly where it initially was
  • now works fine …

Odd, sounds like a bug in the baking code?

(Christoph) #36

Will this create a new revision for all affected posts? (I suppose and hope not, but just to be sure)

(Arpit Jalan) #37

No, the rake task specified in first post will not create post revisions.

(Hosein Naseri) #38

Can I do a search and replace in a cron job? how?