أحاول تحديث مجموعة من المنشورات في سكريبت باستخدام Rails. يبدو أن هناك سلوكًا غريبًا يتعلق بالانحراف بمقدار واحد (off-by-one)، حيث يتم تحديث منشور واحد بالقيمة الخاصة بالمنشور السابق.
لقد نقلت جميع هذه التحديثات لتنفيذ post.raw.gsub() مباشرةً لـ “حل” هذه المشكلة للمنشورات داخل الحلقة الداخلية (التي كانت تحصل سابقًا على القيمة من منشور الموضوع)، ولكن الآن يتم تحديث منشور الموضوع رقم N بالقيمة الخام (raw) للمنشور النهائي السابق من الموضوع N-1.
أنا مشوش بشكل مضاعف الآن، حيث يتم تحديث المنشور الأول (أي منشور الموضوع) باستخدام tpost.raw.gsub! بالقيمة الناتجة عن post.raw.gsub!(preg,"#{$2}") من التكرار السابق.
تعديل: حسنًا، لم أستطع أبدًا معرفة ما كان خاطئًا في النسخة الأخيرة، ولكن إليك نسخة من سكريبت لتحديث بعض المنشورات يقوم بإنشاء مراجعة:
def fix_slack_posts
opts = { bypass_rate_limiter: true, bypass_bump: true, skip_validations: true }
reg=/(\*\*)(This topic was automatically generated from Slack. You can find the original thread \[here\].+?\))(\*\*\.)?\s*?([a-zA-Z, ()]* : )(.*)/m
preg = /([a-zA-Z, ()]+? : )(.*)/m
topic_posts = Post.where("raw like '**This topic was automatically%'")
#topic_posts = Post.where(topic_id: [23934, 23935], post_number: 1)
topic_posts.each do |tpost|
begin
puts "Fixing topic post #{tpost.id} with #{tpost.raw[0..30]}"
puts tpost.raw.gsub(reg,"1 #{$1}\n2 #{$2}\n3 #{$3}\n4 #{$4}\n 5 #{$5}")
raw = tpost.raw.gsub(reg,"#{$5}\n\n#{$2}.")
puts "Fixing topic post #{tpost.id} with #{tpost.raw[0..30]}\n--->#{raw}"
user = User.find(tpost.user_id)
if raw.length > 10
tpost.revise(user,{raw: raw,edit_reason: 'fix slack link location'}, opts)
end
rescue
puts "Can't update topic post #{tpost.raw}"
end
posts = Post.where(topic_id: tpost.topic_id).where("post_number > 1")
posts.each do |post|
if post.raw.gsub(preg,"#{$2}").length>=10
begin
puts "Fixing post #{post.id} with #{post.raw[0..30]}"
raw = post.raw.gsub(preg,"#{$2}")
user = User.find(post.user_id)
post.revise(user,{raw: raw,edit_reason: 'remove user name from text'}, opts)
rescue
puts "#{post.id}--cannot save #{post.raw}. "
end
end
end
end
end