Class: Wiki2Go::Wiki

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Wiki

Returns a new instance of Wiki.


23
24
25
# File 'lib/Wiki2Go/Wiki2Go.rb', line 23

def initialize(config)
  @config  = config
end

Instance Method Details

#admin(request) ⇒ Object


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/Wiki2Go/Wiki2Go.rb', line 309

def admin(request)

  old_config = @config.multi_wiki 
  @config.multi_wiki = false
  wikiweb = Wiki2Go::Web.from_request(request,@config) 
  @config.multi_wiki = old_config
  
  template_name = wikiweb.current_page
  if template_name.nil? || template_name.empty?
    template_name = 'admin'
  end
  master   = @config.storage.load_template(wikiweb.name,'admin.htm')
  template = @config.storage.load_template(wikiweb.name,File.join('admin_pages',template_name+'.txt'))
  
  master = master.gsub(/\$BODY\$/,template)
  
  erb = Web2Go::ERB_Interpreter.new
  erb.add_field(:@request,request)
  erb.add_field(:@config,@config)
  erb.add_field(:@web,wikiweb)
  result = erb.execute master,0
  
end

#blacklist(user) ⇒ Object


271
272
273
274
275
276
# File 'lib/Wiki2Go/Wiki2Go.rb', line 271

def blacklist(user)
  @config.log("Adding #{user} to blacklist")
  list = @config.banned_users
  list.add(user)
  @config.storage.save_list(list)
end

#changes(web) ⇒ Object


231
232
233
234
235
236
237
238
239
# File 'lib/Wiki2Go/Wiki2Go.rb', line 231

def changes(web)
  html = @config.redirect_to_html?(web)
  formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,@config.editable?(web))
  
  topics = @config.storage.read_changes_in(web.name,@config.pages_in_recent_changes) 
  template = @config.storage.load_template(web.name,"pagelist.htm")
  
  return formatter.format_pagelist_in_template(template,topics,"","Recent Changes") 
end

#diff(web, version1, version2) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/Wiki2Go/Wiki2Go.rb', line 49

def diff(web,version1,version2)
  from,from_page,to,to_page = load_two_versions(web,version1,version2)
  
  from_lines = break_on_br(from_page.lines)
  to_lines   = break_on_br(to_page.lines)
  
  html = @config.redirect_to_html?(web)
  formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,@config.editable?(web))
  
  header = "<strong>#{to_page.name} "
  header << append_blank(formatter.diff_link(web.name,from_page.filename,'&lt;&lt;',from-1,to))
  header << formatter.view_version_link(web.name,from_page.filename,"v#{from.to_s} (by #{from_page.alias})",from)
  link = formatter.diff_link(web.name,from_page.filename,'&gt;&gt;',from+1,to)
  header << (link.length > 0 ? ' ' + link : '' )
  header << ' => '
  header << append_blank(formatter.diff_link(web.name,from_page.filename,'&lt;&lt;',from,to-1))
  header << formatter.view_version_link(web.name,to_page.filename,"v#{to.to_s} (by #{to_page.alias})",to) + ' ' 
  header << formatter.diff_link(web.name,from_page.filename,'&gt;&gt;',from,to+1)
  header << "</strong>\n<hr>\n"
  
  lines = Array.new       
  lines << header 
  sdiff = Diff::LCS.sdiff(from_lines,to_lines,nil) do |change|
    case change.action
    when '='
      lines  << "<div class=\"diff_kept\">=&nbsp;#{CGI::escapeHTML(change.old_element).gsub(' ','&nbsp;')}</div>"
    when '!'
      lines  << "<div class=\"diff_deleted\">&lt;&nbsp;#{CGI::escapeHTML(change.old_element).gsub(' ','&nbsp;')}</div>"
      lines  << "<div class=\"diff_added\">&gt;&nbsp;#{CGI::escapeHTML(change.new_element).gsub(' ','&nbsp;')}</div>"
    when '-'
      lines  << "<div class=\"diff_deleted\">&lt;&nbsp;#{CGI::escapeHTML(change.old_element).gsub(' ','&nbsp;')}</div>"
    when '+'
      lines  << "<div class=\"diff_added\">&gt;&nbsp;#{CGI::escapeHTML(change.new_element).gsub(' ','&nbsp;')}</div>"
    end 
  end
  
  to_page.lines = lines
  template = @config.storage.load_template(web.name,"view.htm")
  
  return formatter.dump_page_in_template(template,to_page,from,to) 
end

#edit(web) ⇒ Object


164
165
166
# File 'lib/Wiki2Go/Wiki2Go.rb', line 164

def edit(web)
  do_edit(web,'edit.htm')
end

#generate_html(web) ⇒ Object


282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/Wiki2Go/Wiki2Go.rb', line 282

def generate_html(web)  
  report = ["Generating HTML#{$/}" , "<ul>"]
  files = @config.storage.all_pages_in(web.name)
  files.each do |file|
    report << "<li>#{file}</li>#{$/}"
    pageweb = @config.static_web(web)
    pageweb.current_page = file
    save_html(pageweb)
  end
  save_changes_in_rss(web)
  report << "</ul>#{$/}"
  return report.join
end

#generate_rss(web, changes) ⇒ Object


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

def generate_rss(web,changes)
  html = @config.generate_html
  editable = false
  web = @config.static_web(web)
  template = @config.storage.load_template(web.name,"rss.xml")
  formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,editable)
  rss = formatter.generate_rss(template,changes)
  return rss
end

#generate_rss_from_log(web, errorlog) ⇒ Object


155
156
157
158
159
160
161
162
# File 'lib/Wiki2Go/Wiki2Go.rb', line 155

def generate_rss_from_log(web,errorlog)
  html = @config.generate_html
  editable = false
  web = @config.static_web(web)
  formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,editable)
  rss = formatter.generate_rss_from_log(errorlog)
  return rss
end

#log(msg) ⇒ Object


278
279
280
# File 'lib/Wiki2Go/Wiki2Go.rb', line 278

def log(msg)
  @config.log(msg)
end

#perform(web, request) ⇒ Object


333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/Wiki2Go/Wiki2Go.rb', line 333

def perform(web,request)

  if @config.allow_dynamic_pages then
    html = @config.redirect_to_html?(web)
    formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,@config.editable?(web))
    
    text = @config.storage.load_page(web.name,web.current_page)        
    
    erb = Web2Go::ERB_Interpreter.new
    erb.add_field(:@config,@config)
    erb.add_field(:@web,web)
    erb.add_field(:@request,request)
    erb.add_field(:@formatter,formatter)
    
    script = text.lines.join($/)
    output = erb.execute(script,1)
    text.lines = output.split($/)
    
    template = @config.storage.load_template(web.name,"view.htm")
    
    return formatter.format_page_in_template(template,text)
  else
    log("Perform not allowed")
    self.view(web,-1)
  end 
end

#prepare_for_fckeditor(web) ⇒ Object


296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/Wiki2Go/Wiki2Go.rb', line 296

def prepare_for_fckeditor(web)
  report = ["Generating HTML#{$/}" , "<ul>"]
  files = @config.storage.all_pages_in(web.name)
  files.each do |file|
    report << "<li>#{file}</li>#{$/}"
    pageweb = @config.static_web(web)
    pageweb.current_page = file
    generate_fckeditor_html(pageweb)
  end
  report << "</ul>#{$/}"
  return report.join
end

#remove_spam(web) ⇒ Object


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/Wiki2Go/Wiki2Go.rb', line 252

def remove_spam(web)
  any_changes = false
  files = @config.storage.all_pages_in(web.name)
  blacklist = @config.banned_users
  files.each do |topic|
    page = @config.storage.load_page(web.name,topic)
    if blacklist.contains(page.author) then
      restore_last_good_version(web,topic,blacklist)
      any_changes = true
    end  
  end
  if any_changes then
    save_changes_in_rss(web)
  end
  html = @config.redirect_to_html?(web)
  formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,@config.editable?(web))
  return formatter.changes_url
end

#save(web, new_content, checksum = "") ⇒ Object


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/Wiki2Go/Wiki2Go.rb', line 172

def save(web,new_content,checksum="")
  new_content.gsub!(/\t/,'   ')
  
  blacklist_robots(web,checksum)
  
  author   = web.user
  pagename = web.name.length > 0 ? "#{web.name}/#{web.current_page}" : web.current_page 

  changed = true
  newfile = !@config.storage.exists(web.path)
  newpage = Page.new(web.current_page,new_content,web.user,Time.now,web.title,web.alias)
  current = @config.storage.load_page(web.name,web.current_page)

  if !newfile then
    changed = (current.content != newpage.content) || newpage.name != current.name
    newpage.created_on = current.created_on
  end      
  
  if changed then
    if !@config.accept_edit?(web,current,checksum) then
      @config.errorlog("Edit by user #{author} of page '#{pagename}' clashes with edit of #{current.alias}")
    end
    if @config.accept_page?(web,new_content) then
      @config.save(web,newpage)
      save_changes_in_rss(web)
      @config.instant_commit_to_repository
      if @config.generate_html then
        if newfile then
          generate_html_for_page(web,web.current_page)  
        else
          save_html(web)
        end 
      end
    else
      @config.storage.save_spam(web.name,web.current_page,new_content,web.user)
      raise SpamException
      return web.error_page
    end
  end
  html = @config.redirect_to_html?(web)
  formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,@config.editable?(web))
  return formatter.absolute_url
end

#search(web, searchtopic) ⇒ Object


241
242
243
244
245
246
247
248
249
250
# File 'lib/Wiki2Go/Wiki2Go.rb', line 241

def search(web,searchtopic)
  searchtopic ||= ''
  html = @config.redirect_to_html?(web)
  formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,@config.editable?(web))
  
  topics = @config.storage.search(web.name,searchtopic) 
  template = @config.storage.load_template(web.name,"pagelist.htm")
  
  return formatter.format_pagelist_in_template(template,topics,searchtopic,"Search for '#{searchtopic}'") 
end

#sidebyside(web, version1, version2) ⇒ Object


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/Wiki2Go/Wiki2Go.rb', line 91

def sidebyside(web,version1,version2)

  from,from_page,to,to_page = load_two_versions(web,version1,version2)
  
  template = @config.storage.load_template(web.name,"view.htm")
  
  html = @config.redirect_to_html?(web)
  formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,@config.editable?(web))
  
  left = formatter.format_page(from_page.content)
  right = formatter.format_page(to_page.content)
  
  
  leftheader = append_blank(formatter.sidebyside_link(web.name,from_page.filename,'&lt;&lt;',from-1,to))
  leftheader << formatter.view_version_link(web.name,from_page.filename,"#{to_page.name} v#{from.to_s} (by #{from_page.alias})",from)
  leftheader << formatter.sidebyside_link(web.name,from_page.filename,'&gt;&gt;',from+1,to)
  
  rightheader =  append_blank(formatter.sidebyside_link(web.name,from_page.filename,'&lt;&lt;',from,to-1))
  rightheader << formatter.view_version_link(web.name,to_page.filename,"#{to_page.name} v#{to.to_s} (by #{to_page.alias})",to) + ' ' 
  rightheader << formatter.sidebyside_link(web.name,from_page.filename,'&gt;&gt;',from,to+1)
  
  
  content = "<table class=\"wikitable\" width=\"100%\"><tr>"
  content += "<th>#{leftheader}</th><th>#{rightheader}</th></tr>\n"
  content += "<tr><td width='50%' valign='top'>#{left}</td><td width='50%' valign='top'>#{right}</td></tr></table>"
  to_page.lines = content.split($/)
  
  
  return formatter.dump_page_in_template(template,to_page,from,to) 
end

#upload(web, name, content) ⇒ Object


216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/Wiki2Go/Wiki2Go.rb', line 216

def upload(web,name,content)
  if name.empty? || content.nil? then
    html = @config.redirect_to_html?(web)
    formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,@config.editable?(web))
    return formatter.absolute_url
  else
    @config.storage.store_web_file(web.name,name,content) 
    page = @config.storage.load_page(web.name,web.current_page)
    web.title = page.name
    text = page.content 
    text += "\n---\n{#{name}}"
    return save(web,text)
  end
end

#versions(web) ⇒ Object


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

def versions(web)
  topics = all_good_versions(web)
  template = @config.storage.load_template(web.name,"versionlist.htm")
  
  html = @config.redirect_to_html?(web)
  formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,@config.editable?(web))
  
  return formatter.format_pagelist_in_template(template,topics,"","Versions of '#{web.current_page}'") 
end

#view(web, version) ⇒ Object


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

def view(web,version)
  text = load_page_version(web,version)
  
  template = @config.storage.load_template(web.name,"view.htm")
  
  html = @config.redirect_to_html?(web)
  formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,@config.editable?(web))
  
  result = formatter.format_page_in_template(template,text,version)
  return result 
end

#view_dynamic(web) ⇒ Object


122
123
124
125
126
127
128
129
130
131
# File 'lib/Wiki2Go/Wiki2Go.rb', line 122

def view_dynamic(web)
  html = false
  editable = @config.editable?(web)
  formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,editable)
  
  text = @config.storage.load_page(web.name,web.current_page)        
  template = @config.storage.load_template(web.name,"view.htm")
  
  return formatter.format_page_in_template(template,text) 
end

#view_html(web) ⇒ Object


133
134
135
136
137
138
139
140
141
142
143
# File 'lib/Wiki2Go/Wiki2Go.rb', line 133

def view_html(web)
  html = true
  editable = @config.editable?(web)
  web = @config.static_web(web)
  formatter = Wiki2Go::Formatter.new(web,@config.storage,@config,html,editable)
  
  text = @config.storage.load_page(web.name,web.current_page)        
  template = @config.storage.load_template(web.name,"view.htm")
  
  return formatter.format_page_in_template(template,text) 
end

#wikiedit(web) ⇒ Object


168
169
170
# File 'lib/Wiki2Go/Wiki2Go.rb', line 168

def wikiedit(web)
  do_edit(web,'wikiedit.htm')
end