Class: Wiki2Go::FileStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/Wiki2Go/FileStorage.rb

Instance Method Summary collapse

Constructor Details

#initialize(dynamic_files, static_files) ⇒ FileStorage

Returns a new instance of FileStorage.


14
15
16
17
# File 'lib/Wiki2Go/FileStorage.rb', line 14

def initialize(dynamic_files,static_files)
    @path = dynamic_files
    @html = static_files
end

Instance Method Details

#all_pages_in(subwiki) ⇒ Object


126
127
128
129
130
131
132
133
134
# File 'lib/Wiki2Go/FileStorage.rb', line 126

def all_pages_in(subwiki) 
    textdir = text_directory(subwiki)
    pattern = File.join(textdir,"**/*.txt")
    files = Dir[pattern]
    dirlen = pattern.length - 8
    
    files = files.collect {|file| file[dirlen..-5] }.sort
    return files
end

#all_versions_of(subwiki, topic) ⇒ Object


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/Wiki2Go/FileStorage.rb', line 155

def all_versions_of(subwiki,topic)
    logfile = text_filename(subwiki,topic,"log")
    versions = Array.new
    if File.exists?(logfile) then
        pattern = /^\*\*\* Modified by (.*) \*\*$/
        mtime = File.mtime(logfile)
        lines = Array.new
        File.open(logfile,"r") do |f|
            readlines = f.read
            readlines = readlines.split($/)
            readlines.each do |line|
                if line =~ pattern then
                    add_new_version_to(versions,topic,lines,mtime)
                    lines = Array.new
                else
                    lines << line#.chomp
                end
            end
        end
        add_new_version_to(versions,topic,lines,mtime)
    end      
    
    versions << load_page(subwiki,topic)
    return versions.reverse
end

#backup_page(subwiki, pagename) ⇒ Object


49
50
51
52
53
54
# File 'lib/Wiki2Go/FileStorage.rb', line 49

def backup_page(subwiki,pagename)
    text   = text_filename(subwiki,pagename,"txt")
    backup = text_filename(subwiki,pagename,"bak")
    ensure_directory_exists(text)
    FileUtils::cp text,backup
end

#delete_spam(subwiki, page) ⇒ Object


67
68
69
70
71
72
73
# File 'lib/Wiki2Go/FileStorage.rb', line 67

def delete_spam(subwiki,page)
    text = text_filename(subwiki,page.filename,"txt")
    spam_path = text_filename(subwiki,page.filename,"spam")
    FileUtils::mv text,spam_path,:force => true
    cached = File.join(@html,subwiki,page.filename + ".html")
    FileUtils::rm cached if File.exists?(cached)
end

#exists?(path) ⇒ Boolean Also known as: exists

Returns:

  • (Boolean)

19
20
21
# File 'lib/Wiki2Go/FileStorage.rb', line 19

def exists?(path)
    return FileTest.exists?(text_path(path,"txt"))
end

#load_blacklist(type = 'user', normalize_urls = false) ⇒ Object


181
182
183
184
185
186
187
188
189
190
# File 'lib/Wiki2Go/FileStorage.rb', line 181

def load_blacklist(type = 'user',normalize_urls=false)
    filename = config_filename(type + "_blacklist","txt")
    banned = Array.new
    if File.exists?(filename) then
        File.open(filename,"r") do |f|
            banned = f.readlines
        end
    end
    return BlackList.new(filename,banned,normalize_urls)
end

#load_greylistObject


193
194
195
196
197
198
199
200
201
202
# File 'lib/Wiki2Go/FileStorage.rb', line 193

def load_greylist
    filename = config_filename("greylist","txt")
    listed = Array.new
    if File.exists?(filename) then
        File.open(filename,"r") do |f|
            listed = f.readlines
        end
    end
    return GreyList.new(filename,listed)
end

#load_page(subwiki, name) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/Wiki2Go/FileStorage.rb', line 25

def load_page(subwiki,name)
    lines = Array.new(1,"")
    modified_time = Time.now
    if exists?(File.join(subwiki,name)) then
        path = text_filename(subwiki,name,"txt") 
        File.open(path) do |f|
            lines = f.read
            lines = lines.split($/)
            modified_time = File.mtime(path)
        end
    end
    return Page.make_page(name,lines,modified_time)
end

#load_template(subwiki, name) ⇒ Object


87
88
89
90
91
92
93
94
95
# File 'lib/Wiki2Go/FileStorage.rb', line 87

def load_template(subwiki,name)
    path = template_filename(subwiki,name)
    template = ""
    File.open(path) do |f|
        template = f.gets(nil) 
    end
    template.gsub!(/\<!--\s*include=(\S*)\s*--\>/i) { load_template(subwiki,$1).rstrip }
    template
end

#read_changes_in(subwiki, max_number) ⇒ Object


146
147
148
149
150
151
152
153
# File 'lib/Wiki2Go/FileStorage.rb', line 146

def read_changes_in(subwiki,max_number) 
    files = all_pages_in(subwiki)
    nbfiles = (max_number < files.length ? max_number : files.length)
    
    changed_pages = files.collect { |file| load_page(subwiki,file) }
    changed_pages.sort! { |a,b| b.lastmodified <=> a.lastmodified }
    return changed_pages[0..nbfiles-1] 
end

#save_html(subwiki, page, content) ⇒ Object


75
76
77
78
79
80
81
# File 'lib/Wiki2Go/FileStorage.rb', line 75

def save_html(subwiki,page, content)
    path = File.join(@html,subwiki,page + ".html")
    ensure_directory_exists(path)
    File.open(path,File::CREAT|File::TRUNC|File::RDWR) do |file|
        file.puts content
    end
end

#save_list(list) ⇒ Object


204
205
206
207
208
209
210
# File 'lib/Wiki2Go/FileStorage.rb', line 204

def save_list(list)
    filename = list.name
    ensure_directory_exists(filename)
    File.open(filename,File::CREAT|File::TRUNC|File::RDWR) do |f|
        f.puts list.all
    end
end

#save_page(subwiki, page) ⇒ Object


39
40
41
42
43
44
45
46
47
# File 'lib/Wiki2Go/FileStorage.rb', line 39

def save_page(subwiki,page)
    path    = text_filename(subwiki,page.filename,"txt")
    logfile = text_filename(subwiki,page.filename,"log")
    ensure_directory_exists(path)
    append_current_text_to_logfile(path,logfile,page.author)
    File.open(path,File::CREAT|File::TRUNC|File::RDWR) do |file|
        page.write(file)
    end
end

#save_spam(subwiki, name, content, author) ⇒ Object


56
57
58
59
60
61
62
63
64
65
# File 'lib/Wiki2Go/FileStorage.rb', line 56

def save_spam(subwiki,name,content,author)
    path = text_filename(subwiki,name,"spam")
    ensure_directory_exists(path)
    File.open(path,File::CREAT|File::APPEND|File::WRONLY) do |file|
        file.puts("=== SPAM by #{author} on #{Time.now.strftime("%d/%m/%Y %H:%M")} ===")
        file.puts(content)
        file.puts("$LASTMODIFIED:#{Time.now.to_i}$")
        file.puts("$AUTHOR:#{author}$")
    end
end

#search(subwiki, phrase) ⇒ Object


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/Wiki2Go/FileStorage.rb', line 105

def search(subwiki,phrase)
    pages = Array.new
    to_search_for = Regexp.new(Regexp.escape(phrase),Regexp::IGNORECASE)
    topics = all_pages_in(subwiki)
    topics.each do |topic|
        page = load_page(subwiki,topic)
        if page.author =~ to_search_for || page.name =~ to_search_for || page.alias =~ to_search_for then
            pages << page
        else
            page.lines.each do |line|
                if line =~ to_search_for then
                    pages << page
                    break 
                end
            end
        end
    end
    pages.sort! { |a,b| b.lastmodified <=> a.lastmodified }
    return pages
end

#store_page(subwiki, page) ⇒ Object


83
84
85
# File 'lib/Wiki2Go/FileStorage.rb', line 83

def store_page(subwiki,page)
    save_page(subwiki,page)
end

#store_web_file(subwiki, page, content) ⇒ Object


97
98
99
100
101
102
103
# File 'lib/Wiki2Go/FileStorage.rb', line 97

def store_web_file(subwiki,page,content)
    path = static_filename(subwiki,page)
    ensure_directory_exists(path)
    File.open(path,"wb") do |f|
        f.puts content
    end
end

#update_chongqed_spamlist(content) ⇒ Object


212
213
214
215
216
217
218
# File 'lib/Wiki2Go/FileStorage.rb', line 212

def update_chongqed_spamlist(content)
    filename = config_filename('chonqed_blacklist','txt')
    ensure_directory_exists(filename)
    File.open(filename,File::CREAT|File::TRUNC|File::RDWR) do |f|
        f.puts content
    end
end

#wikisObject


136
137
138
139
140
141
142
143
144
# File 'lib/Wiki2Go/FileStorage.rb', line 136

def wikis
    textdir = text_directory('')
    pattern = File.join(textdir,"*")
    dirs = Dir[pattern]
    dirs = dirs.find_all { |name| 
        FileTest.directory?(name) && name[0] != '.' && File.basename(name) != 'CVS' 
    }
    dirs = dirs.collect { |name| File.basename(name) }
end