Class: Web

Inherits:
Object
  • Object
show all
Defined in:
app/models/web.rb

Direct Known Subclasses

MockWeb

Constant Summary collapse

@@BLIKI_TEMPLATE =
"Try a weekly worksheet:\n\n| / | *Morning* | *Afternoon* |\n" +
"| *Mon* | - | - |\n| *Tue* | - | - |\n| *Wed* | - | - |\n" +
"| *Thu* | - | - |\n| *Fri* | - | - |\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, address, password = nil) ⇒ Web

Returns a new instance of Web.



23
24
25
26
# File 'app/models/web.rb', line 23

def initialize(name, address, password = nil)
  @name, @address, @password, @safe_mode = name, address, password, false
  @pages = {}
end

Instance Attribute Details

#additional_styleObject

Returns the value of attribute additional_style.



9
10
11
# File 'app/models/web.rb', line 9

def additional_style
  @additional_style
end

#addressObject

Returns the value of attribute address.



8
9
10
# File 'app/models/web.rb', line 8

def address
  @address
end

#brackets_onlyObject

Returns the value of attribute brackets_only.



9
10
11
# File 'app/models/web.rb', line 9

def brackets_only
  @brackets_only
end

#check_pass_on_editObject

Returns the value of attribute check_pass_on_edit.



10
11
12
# File 'app/models/web.rb', line 10

def check_pass_on_edit
  @check_pass_on_edit
end

#colorObject

Returns the value of attribute color.



9
10
11
# File 'app/models/web.rb', line 9

def color
  @color
end

#count_pagesObject

Returns the value of attribute count_pages.



9
10
11
# File 'app/models/web.rb', line 9

def count_pages
  @count_pages
end

#default_to_publishedObject

Returns the value of attribute default_to_published.



9
10
11
# File 'app/models/web.rb', line 9

def default_to_published
  @default_to_published
end

#enable_dclick_editObject

Returns the value of attribute enable_dclick_edit.



10
11
12
# File 'app/models/web.rb', line 10

def enable_dclick_edit
  @enable_dclick_edit
end

#enable_menuObject

Returns the value of attribute enable_menu.



10
11
12
# File 'app/models/web.rb', line 10

def enable_menu
  @enable_menu
end

Returns the value of attribute links_map.



10
11
12
# File 'app/models/web.rb', line 10

def links_map
  @links_map
end

#markupObject

Default values



95
96
97
# File 'app/models/web.rb', line 95

def markup
  @markup
end

Returns the value of attribute menu_category.



8
9
10
# File 'app/models/web.rb', line 8

def menu_category
  @menu_category
end

Returns the value of attribute menu_content.



8
9
10
# File 'app/models/web.rb', line 8

def menu_content
  @menu_content
end

Returns the value of attribute menu_limit.



8
9
10
# File 'app/models/web.rb', line 8

def menu_limit
  @menu_limit
end

Returns the value of attribute menu_type.



8
9
10
# File 'app/models/web.rb', line 8

def menu_type
  @menu_type
end

#mind_map_sizeObject

Returns the value of attribute mind_map_size.



10
11
12
# File 'app/models/web.rb', line 10

def mind_map_size
  @mind_map_size
end

#mm_graph_typeObject

Mind Map defaults



13
14
15
# File 'app/models/web.rb', line 13

def mm_graph_type
  @mm_graph_type
end

#mm_progObject

Mind Map defaults



13
14
15
# File 'app/models/web.rb', line 13

def mm_prog
  @mm_prog
end

#mm_selected_categoriesObject

Mind Map defaults



13
14
15
# File 'app/models/web.rb', line 13

def mm_selected_categories
  @mm_selected_categories
end

#mm_show_authorsObject

Mind Map defaults



13
14
15
# File 'app/models/web.rb', line 13

def mm_show_authors
  @mm_show_authors
end

#mm_show_leavesObject

Mind Map defaults



13
14
15
# File 'app/models/web.rb', line 13

def mm_show_leaves
  @mm_show_leaves
end

#mm_show_missingObject

Mind Map defaults



13
14
15
# File 'app/models/web.rb', line 13

def mm_show_missing
  @mm_show_missing
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'app/models/web.rb', line 8

def name
  @name
end

#pagesObject

Returns the value of attribute pages.



8
9
10
# File 'app/models/web.rb', line 8

def pages
  @pages
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'app/models/web.rb', line 8

def password
  @password
end

#publishedObject

Returns the value of attribute published.



9
10
11
# File 'app/models/web.rb', line 9

def published
  @published
end

#rendered_menuObject

Returns the value of attribute rendered_menu.



8
9
10
# File 'app/models/web.rb', line 8

def rendered_menu
  @rendered_menu
end

#safe_modeObject

Returns the value of attribute safe_mode.



9
10
11
# File 'app/models/web.rb', line 9

def safe_mode
  @safe_mode
end

#symbols_mapObject

Returns the value of attribute symbols_map.



10
11
12
# File 'app/models/web.rb', line 10

def symbols_map
  @symbols_map
end

Instance Method Details

#add_bliki_entry(page) ⇒ Object

Bliki methods



277
278
279
# File 'app/models/web.rb', line 277

def add_bliki_entry(page)
  bliki[page.name] = page
end

#add_page(page) ⇒ Object



28
29
30
# File 'app/models/web.rb', line 28

def add_page(page)
  @pages[page.name] = page
end

#authorsObject



52
53
54
# File 'app/models/web.rb', line 52

def authors 
  select.authors 
end

#blikiObject



19
20
21
# File 'app/models/web.rb', line 19

def bliki
  @bliki ||= Hash.new
end

#bliki_entries_authored_by(author) ⇒ Object



297
298
299
# File 'app/models/web.rb', line 297

def bliki_entries_authored_by(author)
  bliki.values.select { |page| page.authors.include?(author) }
end

#bliki_entries_by_dateObject



281
282
283
# File 'app/models/web.rb', line 281

def bliki_entries_by_date
  bliki.values.sort_by { |page| page.revisions.first.created_at }.reverse
end

#bliki_entries_by_nameObject



285
286
287
# File 'app/models/web.rb', line 285

def bliki_entries_by_name
  pages.values.sort_by { |page| [page.name] }
end

#bliki_entries_that_match(regexp) ⇒ Object



289
290
291
# File 'app/models/web.rb', line 289

def bliki_entries_that_match(regexp)
  bliki.values.select { |page| page.content =~ /#{regexp}/i }
end

#bliki_entries_that_reference(page_name) ⇒ Object



293
294
295
# File 'app/models/web.rb', line 293

def bliki_entries_that_reference(page_name)
  bliki.values.select { |page| page.wiki_words.include?(page_name) }
end

#call_graphviz(prog, dotFile, mapFile, pngFile) ⇒ Object

}}}



270
271
272
273
# File 'app/models/web.rb', line 270

def call_graphviz(prog, dotFile, mapFile, pngFile)
  system("#{prog} -Tcmap \"#{dotFile}\" -o \"#{mapFile}\"")
  system("#{prog} -Tpng  \"#{dotFile}\" -o \"#{pngFile}\"")
end

#categoriesObject



56
57
58
# File 'app/models/web.rb', line 56

def categories
  select.map { |page| page.categories }.flatten.uniq.sort
end

#create_author_graph(prog, selected_categories, mm_size) ⇒ Object



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
# File 'app/models/web.rb', line 175

def create_author_graph(prog, selected_categories, mm_size)
  dotFile = File.expand_path("#{WikiService.storage_path}/graph.dot")
  mapFile = File.expand_path("#{WikiService.storage_path}/graph.map")
  pngFile = File.expand_path("#{WikiService.storage_path}/map.png")
  
  File.open(dotFile, "w") do |file|
  
    # Graph properties:
    output_graph_header_to file, mm_size
  
    # Links and node properties:
    auths = authors # avoid repeated selects
    auths.each do |auth|
      file.puts %{"#{auth}" [style=filled,color=grey,URL="../show/#{auth}"];}
    end

    nodes = pages.values.reject { |entry| auths.include? entry.name }
    nodes = filter_categories(nodes, selected_categories)
    nodes.each do |page|
      file.puts %{"#{page.plain_name}" [URL="../show/#{page.name}"];}
      page.authors.each do |auth|
        file.puts %{"#{auth}" -> "#{page.plain_name}";}
      end
    end

    output_graph_footer_to file
  end

  call_graphviz(prog, dotFile, mapFile, pngFile)
  
  [pngFile, mapFile]
end

#create_category_graph(prog, show_authors, selected_categories, mm_size) ⇒ Object

{{{



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'app/models/web.rb', line 208

def create_category_graph(prog, show_authors, selected_categories, mm_size) #{{{
  dotFile = File.expand_path("#{WikiService.storage_path}/graph.dot")
  mapFile = File.expand_path("#{WikiService.storage_path}/graph.map")
  pngFile = File.expand_path("#{WikiService.storage_path}/map.png")
  
  File.open(dotFile, "w") do |file|
    # Graph properties:
    output_graph_header_to file, mm_size
  
    # Page Special nodes properties:
    categs = selected_categories.empty? ? categories : selected_categories
    categs.each do |category|
      file.puts %{"#{category}" [fontsize=20,style=filled,color=grey,comment="#{category}",URL="../list/?category=#{category}"];}
    end
  
    # Links and node properties:
    nodes = filter_categories(pages.values, selected_categories)
    auths = authors # avoid repeated selects
    unless show_authors
      nodes.delete_if { |entry|
        auths.include? entry.name
      }
    end
    nodes.each do |page|
      file.puts %{"#{page.plain_name}" [URL=\"../show/#{page.name}\"];}
      page.categories.each do |category|
        file.puts %{"#{category}" -> "#{page.plain_name}";}
      end
    end

    output_graph_footer_to file
  end

  call_graphviz(prog, dotFile, mapFile, pngFile)
  
  [pngFile, mapFile]
end

#create_mind_map(prog, missing, show_authors, show_leaves, selected_categories, mm_size) ⇒ Object

create a Mind Map graph and return the PNG and HTML map files generated



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/web.rb', line 113

def create_mind_map(prog, missing, show_authors, show_leaves, selected_categories, mm_size)
  dotFile = File.expand_path("#{WikiService.storage_path}/graph.dot")
  mapFile = File.expand_path("#{WikiService.storage_path}/graph.map")
  pngFile = File.expand_path("#{WikiService.storage_path}/map.png")
  
  File.open(dotFile, "w") do |file|
  
    # Graph properties:
    output_graph_header_to file, mm_size
  
    # Links and node properties:
    nodes = filter_categories(pages.values, selected_categories)
    auths = authors # avoid repeated selects
    unless show_authors
      nodes.delete_if { |entry|
        auths.include? entry.name
      }
    end
    unless show_leaves
      nodes.delete_if { |page|
        (page.wiki_words - [missing].flatten).size == 0
      }
    end

    # Page Special nodes properties:
    file.puts %{"Home Page" [color=\"##{color}\",style=bold];} if nodes.map{ |p| p.name }.include? "HomePage"

    nodes.each do |page|
      file.puts %{"#{page.plain_name}" [URL=\"../show/#{page.name}\"];}
      page.references.each do |referer|
        unless page.name == referer.name or not nodes.include? referer
          unless !show_authors and auths.include? referer.name
            file.puts %{"#{referer.plain_name}" -> "#{page.plain_name}";}
          end
        end
      end
    end
  
    # find missing pages:
    if missing
      shown_missing = []
      nodes.each do |page|
        missing.each do |wanted|
          if page.content =~ /#{wanted}/
            file.puts %{"#{page.plain_name}" -> "#{WikiWords.separate wanted}";}
            shown_missing << wanted
          end
        end
      end
      shown_missing.each do |wanted|
        file.puts %{"#{WikiWords.separate wanted}" [URL="/#{@address}/show/#{wanted}", fontsize=10,style=filled,color=grey];}
      end
    end

    output_graph_footer_to file
  end

  call_graphviz(prog, dotFile, mapFile, pngFile)
  
  [pngFile, mapFile]
end

#filter_categories(pages, selected_categories) ⇒ Object

{{{



259
260
261
262
263
264
265
266
267
268
# File 'app/models/web.rb', line 259

def filter_categories(pages, selected_categories) #{{{
  nodes = pages
  unless selected_categories.empty?
    nodes = pages.reject { |page| (page.categories & selected_categories).empty? }
    if selected_categories.include? 'none'
      nodes += pages.select { |page| page.categories.empty? }
    end
  end
  nodes
end

Create a link for the given page name and link text based on the render mode in options and whether the page exists in the this web.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/web.rb', line 63

def make_link(name, text = nil, options = {})
  page = pages[name]
  text = text || WikiWords.separate(name)
  link = CGI.escape(name)
  
  case options[:mode]
    when :export
      if page then "<a class=\"existingWikiWord\" href=\"#{link}.html\">#{text}</a>"
      else "<span class=\"newWikiWord\">#{text}</span>" end
    when :publish
      if page then "<a class=\"existingWikiWord\" href=\"../published/#{link}\">#{text}</a>"
      else "<span class=\"newWikiWord\">#{text}</span>" end
    else
      if page then "<a class=\"existingWikiWord\" href=\"../show/#{link}\">#{text}</a>"
      else "<span class=\"newWikiWord\">#{text}<a href=\"../show/#{link}\">?</a></span>" end
  end
end


255
256
257
# File 'app/models/web.rb', line 255

def output_graph_footer_to file
  file.puts "}"
end

#output_graph_header_to(file, mm_size) ⇒ Object

}}}



246
247
248
249
250
251
252
253
# File 'app/models/web.rb', line 246

def output_graph_header_to file, mm_size
  file.puts "digraph G {"
  file.puts "size=\"#{mm_size}\";" if mm_size
  file.puts 'ratio=fill;'
  file.puts 'concentrate=true;'
  file.puts 'node [fontsize=10,fontname="Tahoma"];'
  file.puts 'edge [len=1.5];'
end

#refresh_pages_with_references(page_name) ⇒ Object

Clears the display cache for all the pages with references to



83
84
85
86
87
# File 'app/models/web.rb', line 83

def refresh_pages_with_references(page_name)
  select.pages_that_reference(page_name).each { |page| 
    page.revisions.each { |revision| revision.clear_display_cache }
  }
end

#refresh_revisionsObject



89
90
91
92
# File 'app/models/web.rb', line 89

def refresh_revisions
  select.each { |page| page.revisions.each { |revision| revision.clear_display_cache } }
  select_bliki.each { |page| page.revisions.each { |revision| revision.clear_display_cache } }
end

#remove_pages(pages_to_be_removed) ⇒ Object



32
33
34
# File 'app/models/web.rb', line 32

def remove_pages(pages_to_be_removed)
  pages.delete_if { |page_name, page| pages_to_be_removed.include?(page) }
end

#revised_onObject



36
37
38
# File 'app/models/web.rb', line 36

def revised_on
  pages.values.sort_by { |page| [page.created_at] }.reverse.first.created_at
end

#select(&accept) ⇒ Object



40
41
42
# File 'app/models/web.rb', line 40

def select(&accept)
  PageSet.new(self, @pages.values, accept)
end

#select_bliki(&accept) ⇒ Object



44
45
46
# File 'app/models/web.rb', line 44

def select_bliki(&accept)
  PageSet.new(self, bliki.values, accept)
end