Replace a string in all posts


(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?


(Eli the Bearded) #39

Anything you can do from the command line you can do through cron. Add the task to the in-container crontab with app.yml or run it from outside the container with a docker exec.


#40

I want to delete all color tags from the posts like [color=#ff3333]. As you can imagine, hex code different in each post. How can i do that? I need pattern suggestions.


(Gerhard Schlager) #41

The following guide shows you how you can replace strings that match a regular expression.


#42

[color=#ff3333]

But hex code is not ff3333 in the all posts. It is variable. I hang out here.