Advertisement
Guest User

Untitled

a guest
Dec 24th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.53 KB | None | 0 0
  1. # coding: utf-8
  2. require 'sunflower'
  3.  
  4. settings = File.readlines('Settings-core.php').map{|ln| ln.chomp }
  5. settings = settings[ settings.index("$settings = array(")..settings.index(");") ]
  6. settings = settings.reject{|ln| ln =~ /^\s*(#|\/\/)/ }.select{|ln| ln =~ /^\s*'(wg\w+)' => ['"]\w+['"],$/i }
  7. settings = settings.map{|ln| ln.match(/^\s*'(wg\w+)' => ['"]\w+['"],$/i)[1] }
  8.  
  9. summaries = File.readlines('Settings.i18n.php').map{|ln| ln.chomp }
  10. summaries = summaries[ summaries.index("$messages['en'] = array(")..summaries.index(");") ]
  11. summaries = summaries.reject{|ln| ln =~ /^\s*(#|\/\/)/ }.select{|ln| ln =~ /^\s*'configure-setting-(wg\w+)' => ['"](.+)['"],$/i }
  12. summaries = Hash[ summaries.map{|ln| ln.match(/^\s*'configure-setting-(wg\w+)' => ['"](.+)['"],$/i).values_at(1,2) } ]
  13.  
  14. # puts settings.length
  15. # puts summaries.length
  16.  
  17. s = Sunflower.new('www.mediawiki.org')
  18. pages = s.make_list('pages', settings.map{|s| "Manual:$#{s}" } ).pages_preloaded
  19.  
  20. output = File.read 'Settings.i18n.php'
  21.  
  22. # note on escaping: \' has special meaning in regex replacement, we need to double-escape
  23. pages.each_with_index do |page, idx|
  24.     setting = page.title[/^Manual:\$(.+)$/, 1]
  25.    
  26.     page_summ = page.text[/\|\s*summary\s*=\s*(.+?\s*\.?)\s*\|?$/, 1]
  27.     local_summ = summaries[setting]
  28.    
  29.     page_summ = nil if page_summ and page_summ.length<=5
  30.    
  31.     page_summ_orig = page_summ.dup
  32.    
  33.     page_summ.sub!(/\s*\.?\s*$/, '') if page_summ
  34.     page_summ.gsub!(/\[\[([^\|]+)\]\]/, '\1') if page_summ
  35.     page_summ.gsub!(/\[\[([^\|]+)\|([^\]]+)\]\]/, '\2') if page_summ
  36.    
  37.     local_summ.strip! if local_summ
  38.     local_summ = local_summ.sub('\\\\', '\\').sub('\\\'', '\\\'') if local_summ
  39.    
  40.     print_contents = true
  41.    
  42.     print "[#{idx+1}/#{pages.length}] "
  43.     if !page_summ
  44.         puts "#{setting}: Missing remote summary"
  45.     elsif !local_summ
  46.         puts "#{setting}: Missing local summary"
  47.     elsif page_summ == local_summ
  48.         puts "#{setting}: All is well"
  49.         print_contents = false
  50.     else
  51.         puts "#{setting}: Conflict"
  52.         print_contents = true
  53.         need_action = true
  54.     end
  55.    
  56.     if print_contents
  57.         puts "  Remote:", "  #{page_summ}" if page_summ
  58.         puts "  Local:", "  #{local_summ}" if local_summ
  59.     end
  60.    
  61.     if need_action
  62.         # this is awful. awful.
  63.         catch :loop_break do
  64.         while true
  65.         catch :loop_retry do
  66.             print "What now? [rlx?] "
  67.             action = gets.strip
  68.             case action
  69.             when 'l'
  70.                 unless s.logged_in?
  71.                     require 'io/console'
  72.                     begin
  73.                         print "User: "; user = gets.strip
  74.                         print "Password: "; pass = $stdin.noecho{ gets.strip }
  75.                         puts ''
  76.                         s.login user, pass
  77.                         s.summary = "updating summary based on data in [[Extension:Configure]]"
  78.                     rescue Sunflower::Error
  79.                         puts "Login failed."
  80.                         throw :loop_retry
  81.                     end
  82.                 end
  83.                
  84.                 page.text.sub! page_summ_orig, local_summ + '.'
  85.                 page.save
  86.                
  87.                 throw :loop_break
  88.                
  89.             when 'r'
  90.                 # oh god why
  91.                 for_output = page_summ =~ /['\\]/ ? page_summ.inspect.gsub('$', '\\$') : "'#{page_summ}'"
  92.                
  93.                 output.sub!(
  94.                     /^(\s*)'configure-setting-#{setting}'.+$/,
  95.                     "\\1'configure-setting-#{setting}' => #{for_output},"
  96.                 )
  97.                
  98.                 File.write 'Settings.i18n.php', output
  99.                
  100.                 throw :loop_break
  101.             when 'x'
  102.                 throw :loop_break
  103.             when '?'
  104.                 puts <<-EOF.gsub(/^\s+/, '  ')
  105.                     l - upload local to remote wiki
  106.                     r - download remote to local messages
  107.                     x - ignore and continue
  108.                 EOF
  109.                 throw :loop_retry
  110.             else
  111.                 # nuffin'
  112.             end
  113.         end
  114.         end
  115.         end
  116.     end
  117.     # output_qqq << "'configure-setting-#{setting}' => '{{config-wg|#{setting.sub 'wg', ''}}}',"
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement