announcement-test/temp_file/config/do_on_startup_helper.rb

508 lines
18 KiB
Ruby

def fix_email_job
crontab_email=%x[crontab -l|grep 'bundle exec rake email:deliver_all'|grep `pwd`]
crontab_email_all = crontab_email.split("\n")
if crontab_email_all.count >= 1
crontab_jobs=%x[crontab -l|grep -v "cd `pwd`.*bundle exec rake email:deliver_all"].gsub(/^\n$/,'').gsub(/'/,"'\"'\"'")
system("echo '#{crontab_jobs}' | crontab -")
end
CronWorker.cron_every_minute(Email.method(:deliver_all))
end
def fix_site_root_url(site)
nginx_exe = %x[ps -o args -C nginx| grep -E 'daemon|master'| awk '{print $4}'].split("\n")[0]
if !nginx_exe.nil?
nginx_config = %x[#{nginx_exe} -V 2>&1 | grep -o '\\-\\-conf-path=\\(.*conf\\)' | cut -d '=' -f2].sub("\n",'')
orbit_sites_config = %x[grep orbit_sites #{nginx_config}].scan(/^[\t ]*include(.*);/)[0][0].gsub(/ /,'')
current_site_config = %x[grep -H `pwd` #{orbit_sites_config}|grep -E ':\s\+root'].split(':')[0]
current_site_config_content = current_site_config.present? ? %x[cat #{current_site_config}] : ""
config_domain = site.site_settings['domain'] rescue nil
server_names = current_site_config_content.scan(/^[\t ]*server_name[\t ]*(.*);/).flatten.collect{|v| v.to_s.split(/[\t ]+/)}.flatten.sort_by{|server_name| server_name.include?('orbit') ? 100 : server_name.starts_with?('www') ? 0 : server_name==config_domain ? 1 : server_name.split('.').count}
ip = UDPSocket.open {|s| s.connect("8.8.8.8", 1); s.addr.last} rescue nil
new_server_names = server_names.uniq.select{|server_name| ip == (IPSocket.getaddress(server_name) rescue nil)}
server_name = new_server_names.select{|server_name| ip != server_name}.first
if server_name.nil?
if new_server_names.count != 0
server_name = new_server_names.first
else
server_name = server_names.first
if server_name.nil?
server_name = ip || (`ip route get 8.8.8.8|xargs|awk '{print $7}'`.strip)
end
end
end
ports = current_site_config_content.scan(/^[\t ]*listen[\t ]*(.*);/).flatten.collect{|v| v.split(/[\t ]+/)}.flatten.select{|v| v.to_i.to_s == v}
port = ports.blank? ? '80' : (ports.select{|v| v=='443'}.blank? ? ports.sort_by{|v| v.to_i==80 ? -1 : v.to_i}[0] : '443')
root_url = (port=='443' ? "https://#{server_name}" : (port=='80' ? "http://#{server_name}" : "http://#{server_name}:#{port}"))
site.update_attributes(root_url: root_url)
end
end
def gsub_block(c,rg,block)
return c.scan(rg) if !block
result = c.gsub(rg) do |str|
block.call(str)
end
end
def get_js_include_name(c,name,&block)
gsub_block(c,/^[ \t]*(?:<script|<%=[ \t]*javascript_include_tag)(?:(?!<script|<%=|<%#).)*[\"\/\']+#{name}[-\d\.]*(?:\.js|\.min|\")(?:(?!<script|<%=|<%#|<\/script>).)*(?:<\/script>|%>)[ \t\r]*(?:\n)*/m,block)
end
def get_css_include_name(c,name,&block)
gsub_block(c,/^[ \t]*(?:<link|<%=[ \t]*stylesheet_link_tag)(?:(?!<link|<%=|<%#).)*[\"\/\']+#{name}[-\d\.]*(?:\.js|\.min|\")(?:(?!<link|<%=|<%#).)*(?:>|%>)[ \t\r]*(?:\n)*/m,block)
end
def fix_css_file
all_css_files = Dir["app/templates/**/*.css","app/templates/**/*.scss"]
contents = all_css_files.collect do |css_file|
[css_file,File.open(css_file, "r") { |f| f.read }]
end
contents.each do |f,c|
c_tp = c
flag = false
c_tp = c_tp.gsub(/(^[ \t]*background(?:(?!\R).)*:[ \t]*linear-gradient(?:(?!\R|;).)*;[ \r]*(?:\/\/(?:(?!\R|;).)*)*)[ \r\t]*[\n]*([\t ]*(?:(?!\R|;).)*(?:$|;))/m) do |m|
var1 = $1
var2 = $2
if !var2.to_s.match(/^[ \t]*-pie-background/)
get_indent = var1.scan(/([ \t]*)background/).flatten[0].to_s
t = var1.sub(/background.*:/,'-pie-background:')
m = "#{var1}\n#{t}\n#{get_indent}behavior: url(\"/assets/ie_support/PIE2/PIE.htc\");\n#{var2}"
flag = true
end
m
end
c_tp = c_tp.gsub(/^([ \t]*border-radius:[ \t]*\d+(?:em|px|%|cm|pt|rem|vw|vh)(?:(?!\R|;).)*;[ \r]*(?:\/\/(?:(?!\R|;).)*)*)[ \r\t]*[\n]*([\t ]*(?:(?!\R|;).)*(?:$|;))/m) do |m|
var1 = $1
var2 = $2
if !var2.to_s.match(/^[ \t]*behavior: url\(\"\/assets\/ie_support\/PIE2\/PIE.htc\"\);/)
get_indent = var1.scan(/([ \t]*)border-radius/).flatten[0].to_s
m = "#{var1}\n#{get_indent}behavior: url(\"/assets/ie_support/PIE2/PIE.htc\");\n#{var2}"
flag = true
end
m
end
if flag
File.open(f, "w") { |f| f.write c_tp }
end
end
end
def fix_template_header_file
all_html_files = Dir['app/templates/**/*.html','app/templates/**/*.erb']
all_html_contents = all_html_files.collect{|file| [file,File.open(file,'r'){|f| f.read}]}
all_html_contents.each do |f,c|
flag = false
c_tp = c.clone
c_tp = get_js_include_name(c_tp,"(?:html5shiv|jquery|jquery\_prefix|response|ie9|cssParser|polycalc|jquery\.cycle2|jquery\.cycle2\.carousel|jquery\.cycle2\.scrollVert|jquery\.cycle2\.tile|jquery\.dataTables|dataTables\.responsive)") do |m|
flag = true
m = ''
end
c_tp = get_css_include_name(c_tp,"(?:responsive\.dataTables)") do |m|
flag = true
m = ''
end
if flag
File.open(f, "w") { |f| f.write c_tp }
end
end
all_head_files = Dir['app/templates/*/partial/_head.html.erb','app/templates/mobile/orbit_mobile/partial/_head.html.erb']
all_head_files.each do |f|
c = File.open(f, "r") { |f| f.read }
flag = false
if c.scan(/<%= render 'shared\/ie_html5_fix' %>/).length == 0
c = "<%= render 'shared/ie_html5_fix' %>\n#{c}"
flag = true
end
c = get_css_include_name(c,/\/\/(?:(?!\n).)*bootstrap/) do |m|
flag = true
v_match = m.scan(/"(.*)"/).flatten[0]
v = v_match.start_with?('http') ? v_match : ("http:"+v_match)
save_path = File.expand_path('../../assets/stylesheets/bootstrap/bootstrap.min.css',f)
system("wget #{v} -O #{save_path}")
m.sub(v_match,"bootstrap/bootstrap.min")
end
if flag
File.open(f, "w") { |f| f.write c }
end
end
end
def fix_datatable_width_issue
#autofix DataTable width 0 issue
bs = "[ \t\r\n]*"
Dir['app/templates/**/*.erb'].each do |v|
c = File.open(v,'r'){|f| f.read}
flag = false
c = c.gsub(/(<table(?:(?!>).)*class=\"(?:(?!>).)*) nowrap((?:(?!>).)*\"(?:(?!>).)*>)/m) do |m|
flag = true
"#{$1}#{$2}"
end
c = c.gsub(/\$\('\.i-member span a'\)/) do |m|
flag = true
"$('.i-member span.member-data-value-name a')"
end
c = c.gsub(/^([ \t]*)(\$\((?:(?!\n).)*\))\.(DataTable\({#{bs}searching:#{bs}false,#{bs}paging:#{bs}false,#{bs}ordering:#{bs}false,#{bs}info:#{bs}false(?:(?!,|\n).)*(?:(?!autoWidth|}).)*)[\r\n]+^[ \t]*}\)/m) do |m|
flag = true
indent = $1
new_text = "#{indent}#{$2}.each(function(){\n#{indent} if($(this).find('thead').length!=0 && $(this).find('td').length!=0 && !$(this).hasClass('dataTable')){\n#{indent} $(this).#{$3.gsub(/^ /,' ')},\n#{indent} autoWidth: false\n#{indent} });\n#{indent} }\n#{indent}});"
new_text
end
if flag
File.open(v,'w'){|f| f.write c}
end
end
end
def fix_member_info_cache(site)
if ((site.tmp_flags.include?("uf1_v2")) rescue true)
return
end
MemberProfile.where({:tmp_name=>nil}).each do |member|
member_update_key = {}
user_update_key = {}
member.name_translations.each do |k, v|
member_update_key["tmp_name.#{k}"] = v
user_update_key["member_name.#{k}"] = v
end
MemberProfile.where(id: member.id).update_all(member_update_key)
User.where(member_profile_id: member.id).update_all(user_update_key)
#user.fix_member_name
end
approved_member_profile_ids = MemberProfile.where(:is_approved=>true).pluck(:id)
User.where(:member_profile_id.in => approved_member_profile_ids).update_all(approved: true)
approved_member_ids = User.where(approved: true).pluck(:member_profile_id)
MemberProfile.where(:id.in => approved_member_ids).update_all(is_approved: true)
MemberProfile.where(:id.nin => approved_member_ids).update_all(is_approved: false)
User.where(:user_name=>'rulingcom').update_all(beta_tester: true)
users = User.where(beta_tester: true)
users.each do |user|
user.member_profile.update(:is_hidden=>true) if user.member_profile
end
Site.update_all("$push"=>{"tmp_flags"=>'uf1_v2'})
end
def fix_member_page_cache(site)
if ((site.tmp_flags.include?("uf10")) rescue true)
return
end
Page.where(:module=>'member').each do |page|
page.custom_array_field = page.custom_array_field.to_a.sort_by{|a| a['sort_order'].to_i}
page.custom_array_field_for_show = page.custom_array_field_for_show.to_a.sort_by{|a| a['sort_order'].to_i}
page.save
end
MemberProfile.cache_member_page
Site.update_all("$push"=>{"tmp_flags"=>'uf10'})
end
def fix_module_app(site)
if (!(site.tmp_flags.include?("uf11")) rescue false)
ModuleApp.all.each do |module_app|
module_app.update_tmp_sort_number
end
Site.update_all("$push"=>{"tmp_flags"=>'uf11'})
end
if (!(site.tmp_flags.include?("uf12")) rescue false)
Tag.where(:module_app_ids.in=>[[], nil]).each do |tag|
tag.module_app_ids = ModuleApp.where(:tag_ids=>tag.id).pluck(:id)
tag.save
end
Site.update_all("$push"=>{"tmp_flags"=>'uf12'})
end
end
def fix_page_part
admin_message = ShowForAdminMessage.first
save_flag = false
message_tmp = ''
PagePart.all.group_by{|v| [v.part_id,v.page_id]}.each do |k,v|
next if k[1].nil? #For popup window
if v.count > 1
page = Page.find(k[1])
all_sub_parts = []
new_v = []
v.each do |v1|
tmp_sub_parts = v1.sub_parts
if tmp_sub_parts.count == 0
v1.destroy
else
all_sub_parts += tmp_sub_parts
new_v << v1
end
end
if new_v.count > 1
if message_tmp.exclude? ("<a href=\"#{page.url}\">#{page.name}</a>")
message_tmp += "<a href=\"#{page.url}\">#{page.name}</a><br>"
end
save_flag = true
new_v.each_with_index do |p1,i1|
if i1 == 0
p1.sub_parts = all_sub_parts
p1.save!
else
p1.sub_parts = []
p1.destroy
end
end
end
end
end
if save_flag
admin_message_message_tp = admin_message.message
admin_message_message_tp << [I18n.t('please_check_for_repeate_data'),message_tmp,false]
admin_message.update_attributes(message: admin_message_message_tp)
end
end
def fix_broken_page
broken_pages = Page.all.select{|v| v.child_page_ids.include?(v.id)}
if broken_pages.length>0
page_root = Page.root
broken_pages.each do |p|
p.child_page_ids -= [p.id]
p.save
end
end
broken_pages = Page.all.select{|v| v.id==v.parent_page_id}
if broken_pages.length>0
page_root = Page.root
broken_pages.each do |p|
p.parent_page_id = page_root.id
p.save
end
end
end
def fix_db_index
ModuleApp.create_indexes
Tag.create_indexes
Category.create_indexes
Page.create_indexes
RoleStatus.create_indexes
MemberProfile.create_indexes
Role.create_indexes
User.create_indexes
AttributeValue.create_indexes
Impression.index(impressionable_type: -1,impressionable_id: -1)
Impression.index(created_at: -1)
Impression.create_indexes
PageCacheModel.create_indexes
['bulletin', 'page_context', 'web_link'].each do |app|
app_class = app.classify.constantize rescue nil
if !app_class.nil?
[{updated_at: -1}, {view_count: -1}].map{|v| app_class.index(v)}
app_class.create_indexes
end
end
#remove duplicated index
AssetTag.collection.indexes.drop_one({asset_wrap_id: 1}) rescue nil
AssetTag.collection.indexes.drop_one({asset_tag_id: -1, created_at: -1}) rescue nil
AssetTag.collection.indexes.drop_one({is_public: 1, is_admin: -1}) rescue nil
Asset.collection.indexes.drop_one({_fts: 'text', _ftsx: 1}) rescue nil
AssetTag.create_indexes
SubPart.create_indexes
PagePart.create_indexes
Asset.create_indexes
ImpressionCount.create_indexes
ChecklistSubSetting.create_indexes
ChecklistField.create_indexes
ChecklistRecord.create_indexes
ChecklistValue.create_indexes
end
def fix_asset_data(site)
if ((site.tmp_flags.include?("asf1")) rescue true)
return
end
image_regex = Asset::ImageRegex
Asset.where("data" => image_regex).update_all(is_image: true)
Asset.where(:data.not => image_regex).update_all(is_image: false)
Asset.where(size: nil).each{|asset| asset.save}
AssetTag.all.update_all(assets_count: 0)
Asset.collection.aggregate([{"$match"=>{"user_id"=>{"$ne"=>nil}}},{"$group"=>{"_id"=>"$user_id","count"=>{"$sum"=> 1}}}]).to_a.each do |gp|
User.where(id: gp['_id']).update_all(assets_count: gp['count'])
end
Asset.collection.aggregate([{"$match"=>{"asset_tag_id"=>{"$ne"=>nil}}},{"$group"=>{"_id"=>{"tag_id" => "$asset_tag_id","parent_ids" => "$root_asset_tag_ids"},"count"=>{"$sum"=> 1}}}]).to_a.each do |gp|
AssetTag.where(:id => gp['_id']['tag_id']).update_all(assets_count: gp['count'])
end
Asset.where(:title=>nil).each do |a|
a.title_translations = I18n.available_locales.map{|l| [l.to_s, a[:data]]}.to_h
a.description_translations = I18n.available_locales.map{|l| [l.to_s, ""]}.to_h
a.save
end
Site.update_all("$push"=>{"tmp_flags"=>'asf1'})
end
def fix_empty_user
member_profile_ids = MemberProfile.all.pluck(:id).to_set
empty_user_ids = []
User.all.pluck(:id, :member_profile_id).each do |u|
if !member_profile_ids.include?(u[1])
empty_user_ids << u[0]
end
end
if !empty_user_ids.empty?
User.where(:id.in => empty_user_ids).delete_all
end
end
def fix_user_password_notify(site)
if ((site.tmp_flags.include?("uf2")) rescue true)
return
end
flag = !User::PasswordValidTime.nil?
User.all.each do |v|
if flag
send_date = v.password_updated_at.nil? ? Time.now : (v.password_updated_at+User::PasswordValidTime)
v.send_password_reset_email(send_date: send_date,period_flag: true,email: m.email)
end
end
Site.update_all("$push"=>{"tmp_flags"=>'uf2'})
end
def fix_site_map(site)
if ((site.tmp_flags.include?("sm1")) rescue true)
return
end
Page.where(:page_id=>'sitemap').update_all("name.zh_tw"=>"網站導覽")
Site.update_all("$push"=>{"tmp_flags"=>'sm1'})
end
def fix_login_page(site)
if ((site.tmp_flags.include?("sm2")) rescue true)
return
end
Page.where(:module => "login_page").each do |p|
p.instance_variable_set(:@skip_callback, true)
p.save
end
Site.update_all("$push"=>{"tmp_flags"=>'sm2'})
end
def fix_sort_number(site)
if ((site.tmp_flags.include?("sort1")) rescue true)
return
end
Category.where(:sort_number=>nil).each do |c|
c.sort_number = 0
c.save
end
Tag.where(:sort_number=>nil).each do |t|
t.sort_number = 0
t.save
end
Site.update_all("$push"=>{"tmp_flags"=>'sort1'})
end
def fix_thumb(site)
force_update_thumb = !(site.tmp_flags.include?('uf4'))
if `which fc-list`.present? && `fc-list |grep 'MingLiU'`.blank?
puts "Installing fonts for system to fix fetching template bug ..."
system("wget -q http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/fonts.tgz?inline=false -O fonts.tgz")
system("mkdir ~/.local/share/fonts -p")
system("tar -zxf fonts.tgz -C ~/.local/share && fc-cache -r")
puts "Finish installing fonts!"
force_update_thumb = true
end
Multithread.where(:key=>"fetch_design_thumbs").destroy
require File.expand_path('../fetch_design_thumb', __FILE__)
puts 'fetch_design_thumb start'
current_locale = I18n.locale
fetch_design_thumb(site,current_locale,nil,force_update_thumb)
puts 'fetch_design_thumb finish'
Site.update_all("$push"=>{"tmp_flags"=>'uf4'}) if force_update_thumb
end
def fix_selection_option(site)
if ((site.tmp_flags.include?("uf5")) rescue true)
return
end
SelectOption.all.to_a.group_by{|o| [o.field_name, o.sub_part_id]}.each do |k, select_options|
select_options[1..-1].each{|o| o.destroy}
end
Site.update_all("$push"=>{"tmp_flags"=>'uf5'})
end
def fix_member_profile_field(site)
if ((site.tmp_flags.include?("uf6")) rescue true)
return
end
MemberProfileField.where(:option_list.in=>[{},nil]).each do |m|
m.check_option_list
m.save
end
Site.update_all("$push"=>{"tmp_flags"=>'uf6'})
end
def fix_script_permission
if File.exist?('start_site.sh') && File.stat('start_site.sh').mode < 0100755
tmp = File.read('start_site.sh')
File.open('start_site.sh', 'w+'){|f| f.write(tmp.gsub("\r", ""))}
File.chmod(0755, 'start_site.sh')
end
if File.exist?('close_site.sh') && File.stat('close_site.sh').mode < 0100755
tmp = File.read('close_site.sh')
File.open('close_site.sh', 'w+'){|f| f.write(tmp.gsub("\r", ""))}
File.chmod(0755, 'close_site.sh')
end
end
def fix_impression_count(site)
if (!(site.tmp_flags.include?("imc1")) rescue false)
ImpressionCount.migrate_all
site.tmp_flags << "imc1"
site.tmp_flags << "imc2"
Site.update_all("$push"=>{"tmp_flags"=>{"$each"=>['imc1', 'imc2']}})
elsif (!(site.tmp_flags.include?("imc2")) rescue false)
if Rails.env == 'production'
rails_root = Rails.root
cpu_cores = %x(cat /proc/cpuinfo | grep processor | wc -l).sub("\n",'').to_i * 3 / 4 rescue 2
default_cpu_cores = cpu_cores
if File.exist?("#{rails_root}/cpu_cores.txt")
cpu_cores = File.read("#{rails_root}/cpu_cores.txt").force_encoding('utf-8').sub("\n",'').to_i rescue default_cpu_cores
else
begin
cpu_cores = File.read("#{rails_root}/../cpu_cores.txt").force_encoding('utf-8').sub("\n",'').to_i if File.exist?("#{rails_root}/../cpu_cores.txt")
rescue => e
cpu_cores = default_cpu_cores
end
end
cpu_cores = 1 if (cpu_cores < 1)
else
cpu_cores = 1
end
last_date = ImpressionCount.order_by(:date=>-1)[cpu_cores*2].date rescue nil
if last_date.nil?
ImpressionCount.migrate_all
else
mongoid_version = Mongoid.default_client.command(buildInfo: 1).first[:version].to_f rescue 2.6
if mongoid_version >= 3.6
last_date = DateTime.parse(last_date.to_s+" 00:00:00"+Time.zone.formatted_offset)
else #mongoid < 3.6 only support UTC
last_date = DateTime.parse(last_date.to_s+" 00:00:00+00:00")
end
ImpressionCount.migrate_all({"$match"=>{"created_at"=>{"$gte"=>last_date}}})
end
Site.update_all("$push"=>{"tmp_flags"=>'imc2'})
end
end
def fix_old_content_unit(site)
if !AutoConvertUnitHelper::AutoConvertFlag || ((site.tmp_flags.include?("poc1")) rescue true)
return
end
AutoConvertUnitHelper.process_old_content
Site.update_all("$push"=>{"tmp_flags"=>'poc1'})
end