Guest User

Untitled

a guest
Oct 14th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'awesome_print'
  3. require 'pg'
  4. require 'json'
  5.  
  6. con = PG.connect host: 'localhost', user: 'postgres', password: 'postgres', dbname: 'privatebin'
  7.  
  8. files = Dir['**/*.php'].sort
  9.  
  10. con.exec 'TRUNCATE TABLE paste'
  11. pastes = files.reject { |f| f.include? '.discussion/' }
  12. pastes.each do |paste|
  13. id = File.basename paste, '.php'
  14. paste = File.read paste
  15. #paste = paste.split("\n")[1..-1].join "\n"
  16. paste = paste.split("\n")[1]
  17. paste = JSON.parse paste
  18.  
  19. meta = paste['meta']
  20. postdate = meta.delete 'postdate'
  21. expiredate = meta.delete 'expire_date'
  22. opendiscussion = meta.delete 'opendiscussion' == true
  23. burnafterreading = meta.delete 'burnafterreading' == true
  24. attachment = meta.delete 'attachment'
  25. attachmentname = meta.delete 'attachmentname'
  26. meta = JSON.dump meta
  27. data = paste['data']
  28.  
  29. puts "Migrate paste #{id}"
  30. con.exec_params 'INSERT INTO paste(dataid, data, postdate, expiredate, opendiscussion, burnafterreading, meta, attachment, attachmentname) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)', [id, data, postdate, expiredate, opendiscussion, burnafterreading, meta, attachment, attachmentname]
  31. rescue => e
  32. ap e
  33. end
  34.  
  35. con.exec 'TRUNCATE TABLE comment'
  36. comments = files.select { |f| f.include? '.discussion/' }
  37. comments.each do |comment|
  38. id = File.basename comment, '.php'
  39. id = id.split('.')[1]
  40. comment = File.read comment
  41. #comment = comment.split("\n")[1..-1].join "\n"
  42. comment = comment.split("\n")[1]
  43. comment = JSON.parse comment
  44.  
  45. meta = comment['meta']
  46. pasteid = meta.delete 'pasteid'
  47. parentid = meta.delete 'parentid'
  48. nickname = meta.delete 'nickname'
  49. vizhash = meta.delete 'vizhash'
  50. postdate = meta.delete 'postdate'
  51. meta = JSON.dump meta
  52. data = comment['data']
  53.  
  54. puts "Migrate comment #{id}"
  55. con.exec_params 'INSERT INTO comment(dataid, pasteid, parentid, data, nickname, vizhash, postdate) VALUES($1, $2, $3, $4, $5, $6, $7)', [id, pasteid, parentid, data, nickname, vizhash, postdate]
  56. rescue => e
  57. ap e
  58. end
  59.  
  60. con.close
Add Comment
Please, Sign In to add comment