Wiki2Go 1.17.5 → 1.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. data/bin/Wiki2Go_firewall_blacklist.rb +8 -0
  2. data/bin/Wiki2Go_make_cvs_repository.rb +1 -0
  3. data/bin/Wiki2Go_make_site.rb +1 -2
  4. data/bin/Wiki2Go_update_site.rb +1 -2
  5. data/lib/Web2Go/CGIRequest.rb +8 -2
  6. data/lib/Web2Go/CGIResponse.rb +17 -14
  7. data/lib/Web2Go/MockRequest.rb +12 -2
  8. data/lib/Web2Go/MockResponse.rb +10 -7
  9. data/lib/Web2Go/WebrickRequest.rb +106 -101
  10. data/lib/Web2Go/WebrickResponse.rb +4 -8
  11. data/lib/Wiki2Go/BlackList.rb +48 -25
  12. data/lib/Wiki2Go/DotGraphics.rb +1 -1
  13. data/lib/Wiki2Go/FileStorage.rb +266 -266
  14. data/lib/Wiki2Go/HTMLFormatter.rb +28 -0
  15. data/lib/Wiki2Go/Install/config/chonqed_blacklist.txt +418 -0
  16. data/lib/Wiki2Go/Install/config/passwords +1 -1
  17. data/lib/Wiki2Go/Install/config/url_blacklist.txt +3855 -0
  18. data/lib/Wiki2Go/Install/make_repository.rb +32 -26
  19. data/lib/Wiki2Go/Install/make_site.rb +197 -111
  20. data/lib/Wiki2Go/Install/site/error.html +1 -1
  21. data/lib/Wiki2Go/Install/site/robots.txt +10 -1
  22. data/lib/Wiki2Go/Install/site/style.css +129 -64
  23. data/lib/Wiki2Go/Install/templates/admin.htm +1 -1
  24. data/lib/Wiki2Go/Install/templates/admin_pages/edit.txt +2 -1
  25. data/lib/Wiki2Go/Install/templates/admin_pages/format_for_fck.txt +31 -0
  26. data/lib/Wiki2Go/Install/templates/admin_pages/passwords.txt +1 -1
  27. data/lib/Wiki2Go/Install/templates/edit.htm +62 -46
  28. data/lib/Wiki2Go/Install/templates/full_footer.htm +31 -33
  29. data/lib/Wiki2Go/Install/templates/header.htm +0 -1
  30. data/lib/Wiki2Go/Install/templates/menu.htm +29 -0
  31. data/lib/Wiki2Go/Install/templates/pagelist.htm +46 -43
  32. data/lib/Wiki2Go/Install/templates/simple_footer.htm +20 -16
  33. data/lib/Wiki2Go/Install/templates/versionlist.htm +52 -40
  34. data/lib/Wiki2Go/Install/templates/view.htm +46 -29
  35. data/lib/Wiki2Go/Install/templates/wikiedit.htm +63 -0
  36. data/lib/Wiki2Go/Install/wiki/delete.png +0 -0
  37. data/lib/Wiki2Go/Install/wiki/style.css +133 -63
  38. data/lib/Wiki2Go/LineFormatter.rb +345 -197
  39. data/lib/Wiki2Go/Page.rb +16 -3
  40. data/lib/Wiki2Go/PrivateWikiConfig.rb +5 -5
  41. data/lib/Wiki2Go/PublicWikiConfig.rb +83 -37
  42. data/lib/Wiki2Go/ReadWriteWikiConfig.rb +8 -8
  43. data/lib/Wiki2Go/Server.rb +6 -1
  44. data/lib/Wiki2Go/SpamFilter.rb +32 -41
  45. data/lib/Wiki2Go/Web.rb +10 -3
  46. data/lib/Wiki2Go/Wiki2Go.rb +301 -94
  47. data/lib/Wiki2Go/Wiki2GoConfig.rb +87 -6
  48. data/lib/Wiki2Go/Wiki2GoServlet.rb +66 -12
  49. data/lib/Wiki2Go/WikiFormatter.rb +60 -30
  50. data/lib/Wiki2Go/cgi/diff.rb +20 -0
  51. data/lib/Wiki2Go/cgi/secure/log_rss.rb +21 -0
  52. data/lib/Wiki2Go/cgi/sidebyside.rb +20 -0
  53. data/lib/Wiki2Go/cgi/wikiedit.rb +20 -0
  54. data/lib/Wiki2Go/firewall_blacklist.rb +88 -0
  55. data/test/All.rb +5 -2
  56. data/test/TestBlackList.rb +70 -14
  57. data/test/TestConfig.rb +5 -5
  58. data/test/TestDiff.rb +95 -0
  59. data/test/TestFormatter.rb +14 -16
  60. data/test/TestHTMLFormatter.rb +37 -0
  61. data/test/TestInstall.rb +70 -27
  62. data/test/TestLineFormatter.rb +71 -15
  63. data/test/TestRSS.rb +8 -7
  64. data/test/TestRepository.rb +50 -0
  65. data/test/TestServer.rb +3 -8
  66. data/test/TestSpamFilter.rb +33 -1
  67. data/test/TestStorage.rb +11 -0
  68. data/test/TestWeb.rb +2 -2
  69. data/test/TestWiki2Go.rb +766 -166
  70. data/test/TestWiki2GoServlet.rb +1122 -524
  71. data/test/UnitTestFiles.rb +7 -3
  72. data/test/Wiki2GoConfigForTest.rb +40 -6
  73. data/test/checksite.rb +17 -50
  74. data/test/test_firewall_blacklist.rb +131 -0
  75. data/test/test_page.rb +45 -0
  76. data/test/testdata/Registration.rbl.txt +7 -0
  77. data/test/testdata/config/url_blacklist.txt +38822 -0
  78. data/test/testdata/expected_changes.html +1 -2
  79. data/test/testdata/expected_edit.html +108 -59
  80. data/test/testdata/expected_full_rss.xml +2 -2
  81. data/test/testdata/expected_out.html +97 -61
  82. data/test/testdata/expected_put.html +88 -53
  83. data/test/testdata/expected_save.html +88 -52
  84. data/test/testdata/expected_savehtml.html +88 -52
  85. data/test/testdata/expected_search.html +68 -53
  86. data/test/testdata/expected_upload.html +88 -53
  87. data/test/testdata/expected_versions.html +97 -82
  88. data/test/testdata/expected_view.html +97 -61
  89. data/test/testdata/firewall/iptables.config +37 -0
  90. data/test/testdata/firewall/user_blacklist.txt +162 -0
  91. data/test/testdata/logs/wiki.log +652 -0
  92. data/test/testdata/logs/wiki.log.0 +113 -0
  93. data/test/testdata/logs/wiki.log.1 +113 -0
  94. data/test/testdata/logs/wiki.log.2 +113 -0
  95. data/test/testdata/logs/wiki.log.3 +115 -0
  96. data/test/testdata/logs/wiki.log.4 +5 -0
  97. data/test/testdata/logs/wiki.log.5 +7 -0
  98. data/test/testdata/logs/wiki.log.6 +5 -0
  99. data/test/testdata/logs/wiki.log.7 +118 -0
  100. data/test/testdata/logs/wiki.log.8 +12 -0
  101. data/test/testdata/site/html/Xpday/rss.xml +53 -103
  102. data/test/testdata/texts/Bugs/FrontPage.txt +6 -0
  103. data/test/testdata/texts/Bugs/NoHidden.spam +17850 -0
  104. data/test/testdata/texts/Bugs/PairProgrammingParties.spam +102 -0
  105. data/test/testdata/texts/Bugs/PairProgrammingParties.txt +99 -0
  106. metadata +53 -7
  107. data/test/TestMail.rb +0 -29
  108. data/test/gc.log +0 -2
@@ -2,41 +2,48 @@
2
2
  module Wiki2Go
3
3
 
4
4
  class BlackList
5
-
5
+
6
6
  attr_reader :name
7
-
8
- def initialize(name,items)
7
+
8
+ def initialize(name,items,normalize_urls=false)
9
9
  @name = name
10
10
  @banned = []
11
11
  items = items.collect {|line|
12
12
  line.strip!
13
- (line.empty? || line[0] == '#' ? nil : line)
13
+ (line.empty? || line[0] == '#'[0] ? nil : (normalize_urls ? normalize(line) : line ))
14
14
  }
15
- @banned = items.compact
15
+ @banned = items.compact.sort.uniq
16
16
  end
17
-
17
+
18
18
  def all
19
19
  return @banned
20
20
  end
21
-
21
+
22
+ def each(&block)
23
+ @banned.each(&block)
24
+ end
25
+
22
26
  def add(item)
23
- if item =~ /(:\/\/[^\/:]+)/ then
24
- item = $1
27
+ if item =~ /(:\/\/([^\/:]+))/ then
28
+ item = extract_domain($2)
29
+ else
30
+ item = escape_regex(item)
31
+ end
32
+ if !item.nil? then
33
+ @banned << item
34
+ @banned = @banned.sort.uniq
25
35
  end
26
- item = escape_regex(item)
27
- @banned << item
28
- @banned = @banned.sort.uniq
29
36
  end
30
-
37
+
31
38
  def length
32
39
  return @banned.length
33
40
  end
34
-
41
+
35
42
  def contains(user)
36
43
  match = @banned.find { |ban| user =~ Regexp.new(ban,Regexp::IGNORECASE) }
37
44
  ! match.nil?
38
45
  end
39
-
46
+
40
47
  def found_in(lines)
41
48
  lines = lines.join(' ') if lines.kind_of? Array
42
49
  @banned.each do | pattern |
@@ -45,21 +52,37 @@ module Wiki2Go
45
52
  end
46
53
  return false
47
54
  end
48
-
55
+
49
56
  private
50
-
57
+
51
58
  def escape_regex(item)
52
59
  item.gsub(/\./,'\.')
53
60
  end
54
-
55
- def add_line(item)
56
- if item =~ /(:\/\/[^\/:]+)/ then
57
- item = $1
61
+
62
+ def extract_domain(host)
63
+ parts = host.split('.')
64
+ if parts.length >= 3 then
65
+ if parts[-2] == 'co' || parts[-2] == 'ac' then
66
+ url = "#{parts[-3]}\\.#{parts[-2]}\\.#{parts[-1]}"
67
+ else
68
+ url = "#{parts[-2]}\\.#{parts[-1]}"
69
+ end
70
+ else
71
+ return nil if parts.length < 2
72
+ url = parts.join('\.')
58
73
  end
59
- @banned << item
60
- @banned = @banned.sort.uniq
74
+ 'https?:\/\/([^\/]*\.)?' + url
75
+ end
76
+
77
+ def normalize(item)
78
+ item = item.gsub('https?:\/\/([^\/]*\.)?','://').gsub('\.','.')
79
+ if item =~ /(:\/\/([^\/:]+))/ then
80
+ item = extract_domain($2)
81
+ else
82
+ item = escape_regex(item)
83
+ end
84
+ item
61
85
  end
62
-
63
86
  end
64
-
87
+
65
88
  end
@@ -79,7 +79,7 @@ module Wiki2Go
79
79
 
80
80
  def write(graph,format,output)
81
81
  command = @path.empty? ? 'dot' : File.join(@path,'dot')
82
- cmdline = "#{command} -T#{format} -o#{output}"
82
+ cmdline = "#{command} -T#{format} -o\"#{output}\""
83
83
  IO.popen(cmdline,"w+") do |f|
84
84
  f.puts graph
85
85
  f.close_write
@@ -9,281 +9,281 @@ require 'fileutils.rb'
9
9
 
10
10
  module Wiki2Go
11
11
 
12
- class FileStorage
13
-
14
- def initialize(dynamic_files,static_files)
15
- @path = dynamic_files
16
- @html = static_files
17
- @cached_templates = Hash.new
18
- end
19
-
20
- def exists?(path)
21
- return FileTest.exists?(text_path(path,"txt"))
22
- end
23
-
24
- alias :exists :exists?
25
-
26
- def load_page(subwiki,name)
27
- lines = Array.new(1)
28
- lines[0] = ""
29
- modified_time = Time.now
30
- if exists?(File.join(subwiki,name)) then
31
- path = text_filename(subwiki,name,"txt")
32
- File.open(path) do |f|
33
- lines = f.readlines
34
- modified_time = File.mtime(path)
12
+ class FileStorage
13
+
14
+ def initialize(dynamic_files,static_files)
15
+ @path = dynamic_files
16
+ @html = static_files
35
17
  end
36
- end
37
- return Page.make_page(name,lines,modified_time)
38
- end
39
-
40
- def save_page(subwiki,page)
41
- path = text_filename(subwiki,page.filename,"txt")
42
- logfile = text_filename(subwiki,page.filename,"log")
43
- ensure_directory_exists(path)
44
- append_current_text_to_logfile(path,logfile,page.author)
45
- File.open(path,File::CREAT|File::TRUNC|File::RDWR) do |file|
46
- page.write(file)
47
- end
48
- end
49
-
50
- def save_spam(subwiki,name,content,author)
51
- path = text_filename(subwiki,name,"spam")
52
- ensure_directory_exists(path)
53
- File.open(path,File::CREAT|File::APPEND|File::WRONLY) do |file|
54
- file.puts("=== SPAM by #{author} on #{Time.now.strftime("%d/%m/%Y %H:%M")} ===")
55
- file.puts(content)
56
- file.puts("$LASTMODIFIED:#{Time.now.to_i}$")
57
- file.puts("$AUTHOR:#{author}$")
58
- end
59
- end
60
-
61
- def delete_spam(subwiki,page)
62
- text = text_filename(subwiki,page.filename,"txt")
63
- spam_path = text_filename(subwiki,page.filename,"spam")
64
- FileUtils::mv text,spam_path,:force => true
65
- end
66
-
67
- def save_html(subwiki,page, content)
68
- path = File.join(@html,subwiki,page + ".html")
69
- ensure_directory_exists(path)
70
- File.open(path,File::CREAT|File::TRUNC|File::RDWR) do |file|
71
- file.puts content
72
- end
73
- end
74
-
75
- def store_page(subwiki,page)
76
- save_page(subwiki,page)
77
- end
78
-
79
- def load_template(subwiki,name)
80
- path = template_filename(subwiki,name)
81
- template = @cached_templates.fetch(path,nil)
82
- if template.nil? then
83
- template = ""
84
- File.open(path) do |f|
85
- template = f.gets(nil)
18
+
19
+ def exists?(path)
20
+ return FileTest.exists?(text_path(path,"txt"))
86
21
  end
87
- template.gsub!(/\<!--\s*include=(\S*)\s*--\>/i) { load_template(subwiki,$1).rstrip }
88
- # @cached_templates[path] = template
89
- end
90
- template
91
- end
92
-
93
- def store_web_file(subwiki,page,content)
94
- path = static_filename(subwiki,page)
95
- ensure_directory_exists(path)
96
- File.open(path,"wb") do |f|
97
- f.puts content
98
- end
99
- end
100
-
101
- def search(subwiki,phrase)
102
- pages = Array.new
103
- to_search_for = Regexp.new(phrase,Regexp::IGNORECASE)
104
- topics = all_pages_in(subwiki)
105
- topics.each do |topic|
106
- page = load_page(subwiki,topic)
107
- page.lines.each do |line|
108
- if line =~ to_search_for then
109
- pages << page
110
- break
111
- end
22
+
23
+ alias :exists :exists?
24
+
25
+ def load_page(subwiki,name)
26
+ lines = Array.new(1,"")
27
+ modified_time = Time.now
28
+ if exists?(File.join(subwiki,name)) then
29
+ path = text_filename(subwiki,name,"txt")
30
+ File.open(path) do |f|
31
+ lines = f.read
32
+ lines = lines.split($/)
33
+ modified_time = File.mtime(path)
34
+ end
35
+ end
36
+ return Page.make_page(name,lines,modified_time)
112
37
  end
113
- end
114
- pages.sort! { |a,b| b.lastmodified <=> a.lastmodified }
115
- return pages
116
- end
117
-
118
- def all_pages_in(subwiki)
119
- textdir = text_directory(subwiki)
120
- pattern = File.join(textdir,"**/*.txt")
121
- files = Dir[pattern]
122
- dirlen = pattern.length - 8
123
-
124
- files = files.collect {|file| file[dirlen..-5] }
125
- return files
126
- end
127
-
128
- def wikis
129
- textdir = text_directory('')
130
- pattern = File.join(textdir,"*")
131
- dirs = Dir[pattern]
132
- dirs = dirs.find_all { |name|
133
- FileTest.directory?(name) && name[0] != '.' && File.basename(name) != 'CVS'
134
- }
135
- dirs = dirs.collect { |name| File.basename(name) }
136
- end
137
-
138
- def read_changes_in(subwiki,max_number)
139
- files = all_pages_in(subwiki)
140
- nbfiles = (max_number < files.length ? max_number : files.length)
141
-
142
- changed_pages = files.collect { |file| load_page(subwiki,file) }
143
- changed_pages.sort! { |a,b| b.lastmodified <=> a.lastmodified }
144
- return changed_pages[0..nbfiles-1]
145
- end
146
-
147
- def all_versions_of(subwiki,topic)
148
- logfile = text_filename(subwiki,topic,"log")
149
- versions = Array.new
150
- if File.exists?(logfile) then
151
- pattern = /^\*\*\* Modified by (.*) \*\*$/
152
- lines = Array.new
153
- File.open(logfile,"r") do |f|
154
- f.each do |line|
155
- if line =~ pattern then
156
- add_new_version_to(versions,topic,lines,File.mtime(logfile))
157
- lines = Array.new
158
- else
159
- lines << line
38
+
39
+ def save_page(subwiki,page)
40
+ path = text_filename(subwiki,page.filename,"txt")
41
+ logfile = text_filename(subwiki,page.filename,"log")
42
+ ensure_directory_exists(path)
43
+ append_current_text_to_logfile(path,logfile,page.author)
44
+ File.open(path,File::CREAT|File::TRUNC|File::RDWR) do |file|
45
+ page.write(file)
160
46
  end
161
- end
162
47
  end
163
- add_new_version_to(versions,topic,lines,File.mtime(logfile))
164
- end
165
-
166
- versions << load_page(subwiki,topic)
167
- return versions.reverse
168
- end
169
-
170
- def load_blacklist(type = 'user')
171
- filename = config_filename(type + "_blacklist","txt")
172
- banned = Array.new
173
- if File.exists?(filename) then
174
- File.open(filename,"r") do |f|
175
- banned = f.readlines
48
+
49
+ def backup_page(subwiki,pagename)
50
+ text = text_filename(subwiki,pagename,"txt")
51
+ backup = text_filename(subwiki,pagename,"bak")
52
+ ensure_directory_exists(text)
53
+ FileUtils::cp text,backup
176
54
  end
177
- end
178
- return BlackList.new(filename,banned)
179
- end
180
-
181
- def load_whitelist
182
- filename = config_filename("whitelist","txt")
183
- allowed = Array.new
184
- if File.exists?(filename) then
185
- File.open(filename,"r") do |f|
186
- allowed = f.readlines
55
+
56
+ def save_spam(subwiki,name,content,author)
57
+ path = text_filename(subwiki,name,"spam")
58
+ ensure_directory_exists(path)
59
+ File.open(path,File::CREAT|File::APPEND|File::WRONLY) do |file|
60
+ file.puts("=== SPAM by #{author} on #{Time.now.strftime("%d/%m/%Y %H:%M")} ===")
61
+ file.puts(content)
62
+ file.puts("$LASTMODIFIED:#{Time.now.to_i}$")
63
+ file.puts("$AUTHOR:#{author}$")
64
+ end
187
65
  end
188
- end
189
- return WhiteList.new(allowed)
190
- end
191
-
192
- def load_greylist
193
- filename = config_filename("greylist","txt")
194
- listed = Array.new
195
- if File.exists?(filename) then
196
- File.open(filename,"r") do |f|
197
- listed = f.readlines
66
+
67
+ def delete_spam(subwiki,page)
68
+ text = text_filename(subwiki,page.filename,"txt")
69
+ spam_path = text_filename(subwiki,page.filename,"spam")
70
+ FileUtils::mv text,spam_path,:force => true
71
+ cached = File.join(@html,subwiki,page.filename + ".html")
72
+ FileUtils::rm cached if File.exists?(cached)
198
73
  end
199
- end
200
- return GreyList.new(filename,listed)
201
- end
202
-
203
- def save_list(list)
204
- filename = list.name
205
- ensure_directory_exists(filename)
206
- File.open(filename,File::CREAT|File::TRUNC|File::RDWR) do |f|
207
- f.puts list.all
208
- end
209
- end
210
-
211
- def update_chongqed_spamlist(content)
212
- filename = config_filename('chonqed_blacklist','txt')
213
- ensure_directory_exists(filename)
214
- File.open(filename,File::CREAT|File::TRUNC|File::RDWR) do |f|
215
- f.puts content
216
- end
217
- end
218
-
219
- private
220
-
221
- def add_new_version_to(versions,topic,lines,modified_time)
222
- if !lines.empty? then
223
- page = Page.make_page(topic,lines,modified_time)
224
- versions << page
225
- end
226
- end
227
-
228
- def append_current_text_to_logfile(file,logfile,author)
229
- if File.exists?(file) then
230
- File.open(file,"r") do |f|
231
- text = f.read
232
- File.open(logfile,"a") do |log|
233
- log.puts "*** Modified by #{author} **"
234
- log.puts text
235
- end
74
+
75
+ def save_html(subwiki,page, content)
76
+ path = File.join(@html,subwiki,page + ".html")
77
+ ensure_directory_exists(path)
78
+ File.open(path,File::CREAT|File::TRUNC|File::RDWR) do |file|
79
+ file.puts content
80
+ end
236
81
  end
237
- end
238
- end
239
-
240
- def ensure_directory_exists(path)
241
- dir,filename = File.split(path)
242
- FileUtils.mkdir_p(dir,{ :mode => 0775}) unless File.directory?(dir)
243
- end
244
-
245
- def text_path(path,ext)
246
- dir = File.dirname(path)
247
- file = File.basename(path) + '.' + ext
248
- File.join(text_directory(dir),file)
249
- end
250
-
251
- def text_filename(subwiki,page,ext)
252
- return File.join(text_directory(subwiki),"#{page}.#{ext}")
253
- end
254
-
255
- def text_directory(subwiki)
256
- dir = File.join(@path,"texts",subwiki)
257
- ensure_directory_exists(dir)
258
- return dir
259
- end
260
-
261
- def config_filename(page,ext)
262
- return File.join(config_directory,"#{page}.#{ext}")
263
- end
264
-
265
- def config_directory
266
- dir = File.join(@path,"config")
267
- ensure_directory_exists(dir)
268
- return dir
269
- end
270
-
271
- def template_filename(subwiki,name)
272
- template_dir = File.join(@path,"templates")
273
- path = File.join(template_dir,subwiki,name)
274
- if !File.exists?(path) then
275
- path = File.join(template_dir,name)
276
- if !File.exists?(path) then
277
- path = File.join(File.dirname(__FILE__),"Install","templates",name)
82
+
83
+ def store_page(subwiki,page)
84
+ save_page(subwiki,page)
278
85
  end
279
- end
280
- return path
281
- end
282
-
283
- def static_filename(subwiki,name)
284
- return File.join(@html,"html",subwiki,name)
86
+
87
+ def load_template(subwiki,name)
88
+ path = template_filename(subwiki,name)
89
+ template = ""
90
+ File.open(path) do |f|
91
+ template = f.gets(nil)
92
+ end
93
+ template.gsub!(/\<!--\s*include=(\S*)\s*--\>/i) { load_template(subwiki,$1).rstrip }
94
+ template
95
+ end
96
+
97
+ def store_web_file(subwiki,page,content)
98
+ path = static_filename(subwiki,page)
99
+ ensure_directory_exists(path)
100
+ File.open(path,"wb") do |f|
101
+ f.puts content
102
+ end
103
+ end
104
+
105
+ def search(subwiki,phrase)
106
+ pages = Array.new
107
+ to_search_for = Regexp.new(Regexp.escape(phrase),Regexp::IGNORECASE)
108
+ topics = all_pages_in(subwiki)
109
+ topics.each do |topic|
110
+ page = load_page(subwiki,topic)
111
+ if page.author =~ to_search_for || page.name =~ to_search_for || page.alias =~ to_search_for then
112
+ pages << page
113
+ else
114
+ page.lines.each do |line|
115
+ if line =~ to_search_for then
116
+ pages << page
117
+ break
118
+ end
119
+ end
120
+ end
121
+ end
122
+ pages.sort! { |a,b| b.lastmodified <=> a.lastmodified }
123
+ return pages
124
+ end
125
+
126
+ def all_pages_in(subwiki)
127
+ textdir = text_directory(subwiki)
128
+ pattern = File.join(textdir,"**/*.txt")
129
+ files = Dir[pattern]
130
+ dirlen = pattern.length - 8
131
+
132
+ files = files.collect {|file| file[dirlen..-5] }.sort
133
+ return files
134
+ end
135
+
136
+ def wikis
137
+ textdir = text_directory('')
138
+ pattern = File.join(textdir,"*")
139
+ dirs = Dir[pattern]
140
+ dirs = dirs.find_all { |name|
141
+ FileTest.directory?(name) && name[0] != '.' && File.basename(name) != 'CVS'
142
+ }
143
+ dirs = dirs.collect { |name| File.basename(name) }
144
+ end
145
+
146
+ def read_changes_in(subwiki,max_number)
147
+ files = all_pages_in(subwiki)
148
+ nbfiles = (max_number < files.length ? max_number : files.length)
149
+
150
+ changed_pages = files.collect { |file| load_page(subwiki,file) }
151
+ changed_pages.sort! { |a,b| b.lastmodified <=> a.lastmodified }
152
+ return changed_pages[0..nbfiles-1]
153
+ end
154
+
155
+ def all_versions_of(subwiki,topic)
156
+ logfile = text_filename(subwiki,topic,"log")
157
+ versions = Array.new
158
+ if File.exists?(logfile) then
159
+ pattern = /^\*\*\* Modified by (.*) \*\*$/
160
+ mtime = File.mtime(logfile)
161
+ lines = Array.new
162
+ File.open(logfile,"r") do |f|
163
+ readlines = f.read
164
+ readlines = readlines.split($/)
165
+ readlines.each do |line|
166
+ if line =~ pattern then
167
+ add_new_version_to(versions,topic,lines,mtime)
168
+ lines = Array.new
169
+ else
170
+ lines << line#.chomp
171
+ end
172
+ end
173
+ end
174
+ add_new_version_to(versions,topic,lines,mtime)
175
+ end
176
+
177
+ versions << load_page(subwiki,topic)
178
+ return versions.reverse
179
+ end
180
+
181
+ def load_blacklist(type = 'user',normalize_urls=false)
182
+ filename = config_filename(type + "_blacklist","txt")
183
+ banned = Array.new
184
+ if File.exists?(filename) then
185
+ File.open(filename,"r") do |f|
186
+ banned = f.readlines
187
+ end
188
+ end
189
+ return BlackList.new(filename,banned,normalize_urls)
190
+ end
191
+
192
+
193
+ def load_greylist
194
+ filename = config_filename("greylist","txt")
195
+ listed = Array.new
196
+ if File.exists?(filename) then
197
+ File.open(filename,"r") do |f|
198
+ listed = f.readlines
199
+ end
200
+ end
201
+ return GreyList.new(filename,listed)
202
+ end
203
+
204
+ def save_list(list)
205
+ filename = list.name
206
+ ensure_directory_exists(filename)
207
+ File.open(filename,File::CREAT|File::TRUNC|File::RDWR) do |f|
208
+ f.puts list.all
209
+ end
210
+ end
211
+
212
+ def update_chongqed_spamlist(content)
213
+ filename = config_filename('chonqed_blacklist','txt')
214
+ ensure_directory_exists(filename)
215
+ File.open(filename,File::CREAT|File::TRUNC|File::RDWR) do |f|
216
+ f.puts content
217
+ end
218
+ end
219
+
220
+ private
221
+
222
+ def add_new_version_to(versions,topic,lines,modified_time)
223
+ if !lines.empty? then
224
+ page = Page.make_page(topic,lines,modified_time)
225
+ versions << page
226
+ end
227
+ end
228
+
229
+ def append_current_text_to_logfile(file,logfile,author)
230
+ if File.exists?(file) then
231
+ File.open(file,"r") do |f|
232
+ text = f.read
233
+ File.open(logfile,"a") do |log|
234
+ log.puts "*** Modified by #{author} **"
235
+ log.puts text
236
+ end
237
+ end
238
+ end
239
+ end
240
+
241
+ def ensure_directory_exists(path)
242
+ dir,filename = File.split(path)
243
+ FileUtils.mkdir_p(dir,{ :mode => 0775}) unless File.directory?(dir)
244
+ end
245
+
246
+ def text_path(path,ext)
247
+ dir = File.dirname(path)
248
+ file = File.basename(path) + '.' + ext
249
+ File.join(text_directory(dir),file)
250
+ end
251
+
252
+ def text_filename(subwiki,page,ext)
253
+ return File.join(text_directory(subwiki),"#{page}.#{ext}")
254
+ end
255
+
256
+ def text_directory(subwiki)
257
+ dir = File.join(@path,"texts",subwiki)
258
+ return dir
259
+ end
260
+
261
+ def config_filename(page,ext)
262
+ return File.join(config_directory,"#{page}.#{ext}")
263
+ end
264
+
265
+ def config_directory
266
+ dir = File.join(@path,"config")
267
+ ensure_directory_exists(dir)
268
+ return dir
269
+ end
270
+
271
+ def template_filename(subwiki,name)
272
+ template_dir = File.join(@path,"templates")
273
+ path = File.join(template_dir,subwiki,name)
274
+ if !File.exists?(path) then
275
+ path = File.join(template_dir,name)
276
+ if !File.exists?(path) then
277
+ path = File.join(File.dirname(__FILE__),"Install","templates",name)
278
+ end
279
+ end
280
+ return path
281
+ end
282
+
283
+ def static_filename(subwiki,name)
284
+ return File.join(@html,"html",subwiki,name)
285
+ end
286
+
285
287
  end
286
-
287
- end
288
-
288
+
289
289
  end