def execute
time_started = Time.now.utc
statistics_info_element = REXML::Document.new(REXML::Source.new(open(STATISTICS_INFO_FILE_NAME).read())).root
if @config == nil then
@config = Config.new(statistics_info_element.
get_elements('//' + STATISTICS_INFO_WORKING_DIRECTORY_ELEMENT_NAME).first.text)
end
log_file_name = @config.working_directory + '/log.txt'
logger = Logger.new([$stderr, File.new(log_file_name, 'a')])
logger.log('統計ページの更新')
date_time_format = DateTimeFormat.new()
logger.log('開始しました。 ' + date_time_format.format(time_started) )
resource = Resource.new(logger)
text = ''
text += @config.edit_header
new_page_file_name = @config.working_directory + '/' + NewStatisticsPage::FILE_NAME
File.new(new_page_file_name, 'r').readlines.each do |line|
text += line
end
bot = WikiBot.new(@config)
update_summary = statistics_info_element.
get_elements('//' + STATISTICS_INFO_UPDATE_SUMMARY_ELEMENT_NAME).first.text
logger.log('更新対象ページ: ' + @config.edit_target_page)
logger.log('更新用ウィキテキスト: ' + new_page_file_name)
logger.log('データ取得時刻: ' + statistics_info_element.
get_elements('//' + STATISTICS_INFO_FETCH_END_DATE_TIME_ELEMENT_NAME).first.text)
logger.log('言語数: ' + statistics_info_element.
get_elements('//' + STATISTICS_INFO_NUMBER_OF_LANGUAGES_IN_SITE_MATRIX_ELEMENT_NAME).first.text)
logger.log('要約欄: "' + update_summary + '"')
if ARGV.length > 0 and ( ARGV[0].eql?('-f') or ARGV[0].eql?('--force') ) then
else
print(Iconv.conv(Config.new.console_encoding, 'UTF-8', 'ウェブページに対する更新を実行しますか? (yes/no) [no] '))
response = gets().strip()
if not response.slice(0, 1).downcase.eql?('y') then
logger.log('更新を中断しました。')
FileUtils.copy(log_file_name, '.')
return
end
end
logger.log('更新します。')
begin
bot.login
rescue MediaWikiApiError => error
logger.log(error.to_s)
@status = 1
return
end
bot.obtain_edit_token(@config.edit_target_page)
bot.edit(@config.edit_target_page, text, update_summary)
logger.log('更新しました。')
bot.logout
time_ended = Time.now.utc
logger.log('終了しました。 ' + date_time_format.format(time_ended))
logger.log('要した時間: ' + (time_ended - time_started).to_s + ' 秒')
FileUtils.copy(log_file_name, '.')
end