forked from rahul-rakesh/xenforo-migration-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_to_grid.rb
More file actions
79 lines (63 loc) · 2.36 KB
/
convert_to_grid.rb
File metadata and controls
79 lines (63 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true
#################################
# ------ CONFIGURATION ------ #
#################################
# 1. For a final check, run with DRY_RUN = true and LIMIT = nil to see all affected posts.
# 2. To run a small batch, set DRY_RUN = false and LIMIT = 50.
# 3. For the final bulk update, set DRY_RUN = false and LIMIT = nil.
DRY_RUN = false
# Set to nil to run on all qualifying posts.
TARGET_POST_ID = nil
# Set to a number (e.g., 50) for a batch, or nil for all posts.
LIMIT = nil
# The minimum number of consecutive images to trigger the grid wrap.
MIN_IMAGES = 3
#################################
# ------ SCRIPT LOGIC (v2 - Production) ------ #
#################################
puts "Starting image grid wrapper script (v2)..."
puts "---"
puts "MODE: #{DRY_RUN ? 'DRY RUN' : 'LIVE RUN'}"
puts "Limit: #{LIMIT || 'All Posts'}"
puts "---"
# Regex to find an OPTIONAL legacy "Attachments" header and a required block of images.
regex = /(?:<hr>\s*<strong>Attachments:<\/strong>\s*)?((?:!\[[^\]]*?\]\(upload:\/\/[^\)]+?\)\s*){#{MIN_IMAGES},})/m
# Find posts that have uploads but do not already have a [grid] tag.
posts = Post.where("raw ILIKE '%upload://%' AND raw NOT ILIKE '%[grid]%'")
posts = posts.where(id: TARGET_POST_ID) if TARGET_POST_ID
posts = posts.limit(LIMIT) if LIMIT
processed_count = 0
changed_count = 0
posts.find_each do |post|
processed_count += 1
original_raw = post.raw.dup
modified_raw = original_raw.gsub(regex) do
# $1 contains the matched block of images.
image_block = $1
"\n[grid]\n#{image_block.strip}\n[/grid]\n"
end
if original_raw != modified_raw
changed_count += 1
puts "✅ Change required in Post ID: #{post.id} (Topic: #{post.topic_id})"
if DRY_RUN
puts " (Dry Run - No changes made)"
else
begin
revisor = PostRevisor.new(post)
revisor.revise!(
Discourse.system_user,
{ raw: modified_raw },
{ bypass_bump: true }
)
puts " ✅ Post ID: #{post.id} successfully updated."
rescue => e
puts " ❌ ERROR updating Post ID: #{post.id}. Reason: #{e.message}"
end
end
end
end
puts "---"
puts "Script finished."
puts "Total posts scanned: #{processed_count}"
puts "Posts requiring changes: #{changed_count}"
puts "Mode: #{DRY_RUN ? 'DRY RUN - No changes were saved.' : 'LIVE RUN - Changes were saved.'}"