Class: WikiController

Inherits:
ActionControllerServlet
  • Object
show all
Defined in:
app/controllers/wiki.rb

Constant Summary collapse

EXPORT_DIRECTORY =
File.dirname(__FILE__) + "/../../storage/"

Instance Method Summary collapse

Instance Method Details

#authenticateObject



52
53
54
# File 'app/controllers/wiki.rb', line 52

def authenticate
  password_check(@params["password"]) ? redirect_show("HomePage") : redirect_action("login")
end

#authorsObject



77
78
79
# File 'app/controllers/wiki.rb', line 77

def authors
  @authors = web.select.authors
end

#cancel_editObject



221
222
223
224
225
# File 'app/controllers/wiki.rb', line 221

def cancel_edit
  @page = wiki.read_page(web_address, page_name)
  @page.unlock
  redirect_show
end

#create_systemObject



29
30
31
32
# File 'app/controllers/wiki.rb', line 29

def create_system
  wiki.setup(@params["password"], @params["web_name"], @params["web_address"]) unless wiki.setup? 
  redirect_path "/"
end

#create_webObject



34
35
36
37
38
# File 'app/controllers/wiki.rb', line 34

def create_web
  redirect_path("/") unless wiki.authenticate(@params["system_password"])
  wiki.create_web(@params["name"], @params["address"])
  redirect_show("HomePage", @params["address"])
end

#editObject



209
210
211
212
213
214
215
216
217
218
219
# File 'app/controllers/wiki.rb', line 209

def edit
  @page = wiki.read_page(web_address, page_name)

  if !@page.locked?(Time.now) || @params["break_lock"]
    @page.lock(Time.now, default_author)
    @author = default_author
    render
  else
    render "wiki/locked"
  end
end

#export_htmlObject



107
108
109
110
111
112
113
# File 'app/controllers/wiki.rb', line 107

def export_html
  file_name = "#{web.address}-html-#{web.revised_on.strftime("%Y-%m-%d-%H-%M")}.zip"
  file_path = EXPORT_DIRECTORY + file_name

  export_pages_to_zip_file(file_path) unless FileTest.exists?(file_path)
  send_export(file_name, file_path)
end

#export_markupObject



115
116
117
118
119
120
121
# File 'app/controllers/wiki.rb', line 115

def export_markup
  file_name = "#{web.address}-markup-#{web.revised_on.strftime("%Y-%m-%d-%H-%M")}.zip"
  file_path = EXPORT_DIRECTORY + file_name

  export_markup_to_zip_file(file_path) unless FileTest.exists?(file_path)
  send_export(file_name, file_path)
end

#export_pdfObject



123
124
125
126
127
128
129
130
# File 'app/controllers/wiki.rb', line 123

def export_pdf
  file_name = "#{web.address}-tex-#{web.revised_on.strftime("%Y-%m-%d-%H-%M")}"
  file_path = EXPORT_DIRECTORY + file_name

  export_web_to_tex(file_path + ".tex") unless FileTest.exists?(file_path + ".tex")
  convert_tex_to_pdf(file_path + ".tex")
  send_export(file_name + ".pdf", file_path + ".pdf")
end

#export_texObject



132
133
134
135
136
137
138
# File 'app/controllers/wiki.rb', line 132

def export_tex
  file_name = "#{web.address}-tex-#{web.revised_on.strftime("%Y-%m-%d-%H-%M")}.tex"
  file_path = EXPORT_DIRECTORY + file_name

  export_web_to_tex(file_path) unless FileTest.exists?(file_path)
  send_export(file_name, file_path)
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/wiki.rb', line 7

def index
  if web_address
    redirect_show "HomePage"
  elsif !wiki.setup?
    redirect_path "/new_system/"
  elsif wiki.webs.length == 1
    redirect_show "HomePage", wiki.webs.values.first.address
  else
    redirect_path "/web_list/"
  end
end

#listObject



100
101
102
103
104
105
# File 'app/controllers/wiki.rb', line 100

def list
  parse_category
  @pages_by_name = @pages_in_category.by_name 
  @page_names_that_are_wanted = @pages_in_category.wanted_pages
  @pages_that_are_orphaned = @pages_in_category.orphaned_pages
end

#loginObject



48
49
50
# File 'app/controllers/wiki.rb', line 48

def 
  render "wiki/login"
end

#newObject



205
206
207
# File 'app/controllers/wiki.rb', line 205

def new
  @page_name, @author = page_name, default_author
end

#new_systemObject

Administrating the Instiki setup ——————————————–



21
22
23
# File 'app/controllers/wiki.rb', line 21

def new_system
  wiki.setup? ? redirect_path("/") : render
end

#new_webObject



25
26
27
# File 'app/controllers/wiki.rb', line 25

def new_web
  redirect_path("/") if wiki.system["password"].nil?
end

#parse_categoryObject

Within a single web ———————————————————



61
62
63
64
65
66
67
68
69
# File 'app/controllers/wiki.rb', line 61

def parse_category
  @categories = web.categories
  @category = @params["category"]
  @pages_in_category = web.select { |page| page.in_category?(@category) }
  @set_name = ( @categories.include?(@category) ? "category '#{@category}'" : "the web" )
  @category_links = @categories.map do |c| 
    (@category == c ? "<span class=\"selected\">#{c}</span>" : "<a href=\"?category=#{c}\">#{c}</a>")
  end
end

#pdfObject



194
195
196
197
198
199
200
201
202
203
# File 'app/controllers/wiki.rb', line 194

def pdf
  page = wiki.read_page(web_address, page_name)
  safe_page_name = page.name.gsub(/\W/, "")
  file_name = "#{safe_page_name}-#{web.address}-#{page.created_at.strftime("%Y-%m-%d-%H-%M")}"
  file_path = EXPORT_DIRECTORY + file_name

  export_page_to_tex(file_path + ".tex") unless FileTest.exists?(file_path + ".tex")
  convert_tex_to_pdf(file_path + ".tex")
  send_export(file_name + ".pdf", file_path + ".pdf")
end


185
186
187
# File 'app/controllers/wiki.rb', line 185

def print
  @page = wiki.read_page(web_address, page_name)
end

#publishedObject



181
182
183
# File 'app/controllers/wiki.rb', line 181

def published
  if web.published then @page = wiki.read_page(web_address, page_name || "HomePage") else redirect_show("HomePage") end
end

#recently_revisedObject



81
82
83
84
# File 'app/controllers/wiki.rb', line 81

def recently_revised
  parse_category
  @pages_by_revision = @pages_in_category.by_revision
end

#remove_orphaned_pagesObject



157
158
159
160
161
162
163
164
# File 'app/controllers/wiki.rb', line 157

def remove_orphaned_pages
  if wiki.authenticate(@params["system_password"])
    wiki.remove_orphaned_pages(web_address)
    redirect_action "list/"
  else
    redirect_show "HomePage"
  end
end

#revisionObject



246
247
248
249
# File 'app/controllers/wiki.rb', line 246

def revision
  @page = wiki.read_page(web_address, page_name)
  @revision = @page.revisions[@params["rev"].to_i]
end

#rollbackObject



251
252
253
254
# File 'app/controllers/wiki.rb', line 251

def rollback
  @page = wiki.read_page(web_address, page_name)
  @revision = @page.revisions[@params["rev"].to_i]  
end

#rss_with_contentObject



86
87
88
89
90
91
92
93
# File 'app/controllers/wiki.rb', line 86

def rss_with_content
  @pages_by_revision = web.select.by_revision.first(15)
  @uri = @req.request_uri
  host = @req.meta_vars["HTTP_X_FORWARDED_HOST"] || "#{@uri.host}:#{@uri.port.to_s}"
  @web_url = "#{@uri.scheme}://#{host}/#{@web.address}"
  @res["Content-Type"] = "text/xml"
  render "wiki/rss_feed"
end

#rss_with_headlinesObject



95
96
97
98
# File 'app/controllers/wiki.rb', line 95

def rss_with_headlines
  @hide_description = true
  rss_with_content
end

#saveObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'app/controllers/wiki.rb', line 227

def save
  if web.pages[page_name]
    page = wiki.revise_page(
      web_address, page_name, @params["content"], Time.now, 
      Author.new(@params["author"], remote_ip)
    )

    page.unlock
  else
    page = wiki.write_page(
      web_address, page_name, @params["content"], Time.now, 
      Author.new(@params["author"], remote_ip)
    )
  end

  write_cookie("author", @params["author"], true)
  redirect_show(page_name)
end

#searchObject



71
72
73
74
75
# File 'app/controllers/wiki.rb', line 71

def search
  @query   = @params["query"]
  @results = web.select { |page| page.content =~ /#{@query}/i }
  @results.length == 1 ? redirect_show(@results.first.name) : render
end

#showObject

Within a single page ——————————————————–



168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/controllers/wiki.rb', line 168

def show
  if @page = wiki.read_page(web_address, page_name)
    begin
      render_action "page"
    rescue => e
      $stderr << e.backtrace.join("\n")
      redirect_action "edit/#{CGI.escape(page_name)}?msg=#{CGI.escape(e.message)}"
    end
  else
    redirect_action "new/#{CGI.escape(page_name)}"
  end
end

#static_style_sheetObject



56
# File 'app/controllers/wiki.rb', line 56

def static_style_sheet() render "static_style_sheet" end

#texObject



189
190
191
192
# File 'app/controllers/wiki.rb', line 189

def tex
  @page = wiki.read_page(web_address, page_name)
  @tex_content = RedClothForTex.new(@page.content).to_tex
end

#update_webObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/controllers/wiki.rb', line 140

def update_web
  redirect_show("HomePage") unless wiki.authenticate(@params["system_password"])

  wiki.update_web(
    web.address, @params["address"], @params["name"], 
    @params["markup"].intern, 
    @params["color"], @params["additional_style"], 
    @params["safe_mode"] ? true : false, 
    @params["password"].empty? ? nil : @params["password"],
    @params["published"] ? true : false, 
    @params["brackets_only"] ? true : false,
    @params["count_pages"] ? true : false
  )

  redirect_show("HomePage", @params["address"])
end

#web_listObject

Outside a single web ——————————————————–



43
44
45
46
# File 'app/controllers/wiki.rb', line 43

def web_list
  @system, @webs = wiki.system, wiki.webs.values
  render "wiki/web_list"
end