Class: Redsync::Wiki

Inherits:
Object
  • Object
show all
Defined in:
lib/redsync/wiki.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Wiki

Valid option values:

:url => This wiki's base url. Required
:cookies => Mechanize::Cookie objects that's already logged in to Redmine. Required
:data_dir => Directory to read/write to. Required.
:extension => File extensions for page files. Defaults to "txt"


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/redsync/wiki.rb', line 18

def initialize(options)
  @url = options[:url].match(/(.*?)\/?$/)[1]
  @api_key = options[:api_key]
  @data_dir = File.expand_path(options[:data_dir])
  @extension = options[:extension]

  @agent = Mechanize.new

  @pages_cache = {}
  @pages_cache_file = File.join(@data_dir, "__redsync_pages_cache.yml")

  initialize_system_files
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/redsync/wiki.rb', line 8

def api_key
  @api_key
end

#data_dirObject (readonly)

Returns the value of attribute data_dir.



8
9
10
# File 'lib/redsync/wiki.rb', line 8

def data_dir
  @data_dir
end

#extensionObject (readonly)

Returns the value of attribute extension.



8
9
10
# File 'lib/redsync/wiki.rb', line 8

def extension
  @extension
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/redsync/wiki.rb', line 8

def url
  @url
end

Instance Method Details

#downsyncObject



46
47
48
49
50
51
52
53
# File 'lib/redsync/wiki.rb', line 46

def downsync
  queue = pages_to_download
  queue.each_with_index do |page, i|
    puts "--Download (#{i+1} of #{queue.count}) #{page.name}"
    page.download
  end
  self.write_pages_cache
end

#initialize_system_filesObject



33
34
35
36
37
38
# File 'lib/redsync/wiki.rb', line 33

def initialize_system_files
  unless File.exist? @data_dir
    puts "Creating #{@data_dir}"
    FileUtils.mkdir(@data_dir) 
  end
end

#load_pages_cacheObject



139
140
141
142
143
144
145
146
147
148
# File 'lib/redsync/wiki.rb', line 139

def load_pages_cache
  return unless File.exist? @pages_cache_file
  @pages_cache = {}
  YAML.load_file(@pages_cache_file).each do |page_hash|
    wiki_page = WikiPage.new(self, page_hash[:name])
    wiki_page.remote_updated_at = page_hash[:remote_updated_at]
    wiki_page.synced_at = page_hash[:synced_at]
    @pages_cache[page_hash[:name]] = wiki_page
  end
end

#pagesObject



41
42
43
# File 'lib/redsync/wiki.rb', line 41

def pages
  @pages_cache
end

#pages_to_createObject



82
83
84
85
86
# File 'lib/redsync/wiki.rb', line 82

def pages_to_create
  list = []
  list += @pages_cache.values.select { |page| page.exists_in == :local_only }
  list
end

#pages_to_downloadObject



73
74
75
76
77
78
79
# File 'lib/redsync/wiki.rb', line 73

def pages_to_download
  list = []
  list += @pages_cache.values.select { |page| page.exists_in == :remote_only }
  list += @pages_cache.values.select { |page| page.exists_in == :both && !page.synced_at }
  list += @pages_cache.values.select { |page| page.exists_in == :both && page.synced_at && (page.remote_updated_at > page.synced_at) }
  list
end

#pages_to_uploadObject



89
90
91
92
93
# File 'lib/redsync/wiki.rb', line 89

def pages_to_upload
  list = []
  list += @pages_cache.values.select { |page| page.exists_in == :both && (page.local_updated_at > page.synced_at) }
  list
end

#scanObject



96
97
98
99
# File 'lib/redsync/wiki.rb', line 96

def scan
  scan_remote
  scan_local
end

#scan_localObject



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/redsync/wiki.rb', line 118

def scan_local
  Dir.entries(@data_dir).each do |file|
    next if File.directory? file
    next if file =~ /^__redsync_/
    page_name = file.match(/([^\/\\]+?)\.#{@extension}$/)[1]
    page_name = page_name.encode("UTF-8-MAC", "UTF-8", :invalid => :replace, :undef => :replace) if RUBY_PLATFORM =~ /darwin/
    next if pages[page_name]
    pp file
    wiki_page = WikiPage.new(self, page_name)
    pages[page_name] = wiki_page
  end
end

#scan_remoteObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/redsync/wiki.rb', line 102

def scan_remote
  webpage = @agent.get(@url + "/date_index")

  # Get remote and local update times using remote list
  webpage.search("#content h3").each do |h3|
    links = h3.next_element.search("a")
    links.each do |link|
      url = URI.parse(@url).merge(link.attr("href")).to_s
      wiki_page = WikiPage.new(self, url)
      @pages_cache[wiki_page.name] = wiki_page unless @pages_cache[wiki_page.name]
      @pages_cache[wiki_page.name].remote_updated_at = h3.text
    end
  end
end

#to_sObject



151
152
153
154
155
156
157
158
# File 'lib/redsync/wiki.rb', line 151

def to_s
  str = "#<Redsync::Wiki"
  str << " url = \"#{@url}\"\n"
  str << " data_dir = \"#{@data_dir}\"\n"
  str << " extension = \"#{@extension}\"\n"
  str << " pages = #{@pages_cache.count}\n"
  str << ">"
end

#upsyncObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/redsync/wiki.rb', line 56

def upsync
  queue = pages_to_create
  queue.each_with_index do |page, i|
    puts "--Create (#{i+1} of #{queue.count}) #{page.name}"
    page.upload
  end
  self.write_pages_cache

  queue = pages_to_upload
  queue.each_with_index do |page, i|
    puts "--Upload (#{i+1} of #{queue.count}) #{page.name}"
    page.upload
  end
  self.write_pages_cache
end

#write_pages_cacheObject



132
133
134
135
136
# File 'lib/redsync/wiki.rb', line 132

def write_pages_cache
  File.open(@pages_cache_file, "w+:UTF-8") do |f|
    f.write(self.pages.values.map{ |page| page.to_hash}.to_yaml)
  end
end