Module: Web::Wiki::Store

Included in:
Web::Wiki
Defined in:
lib/web/wiki.rb

Overview


Instance Method Summary collapse

Instance Method Details

#exit_handlerObject



298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/web/wiki.rb', line 298

def exit_handler
  require 'find'
  Find.find( store_dir ) { |name|
    begin
      if File.directory? name
        File.chmod( 0777, name )
      else
        File.chmod( 0666, name )
      end
    rescue Exception
      # this should be more specific to access errors
    end
  }
end

#load_page(name = ) ⇒ Object



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
# File 'lib/web/wiki.rb', line 178

def load_page( name = Web["page.name"] )
  if name.size == 0
    if (Web.path_info)
      name = Web.path_info.gsub( Regexp.new(Web.script_name), "" ).gsub(/^\/|\.html$/,"")
    end
    if name.size == 0
      name = Web::Wiki::pref( :home_page )
    end
  end
  
  page = ""
  page_file = store( name )
  if File.exists? page_file
    File.open( page_file, "r" ) { |f|
      page = YAML.load( f )
    }
  else
    page = Web::Wiki::Page.new( name )
  end
  
  {/\\r/ => "\r",
   /\\n/ => "\n",
   /\\"/ => "\"",
   /\\'/ => "'",  }.each{ |find, replace|
    page.content.gsub!( find, replace )
  }
  page
end

#more_newsObject



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/web/wiki.rb', line 211

def more_news
  if File.exists?(store_dir + "/more_news.yaml")
    contents = File.open( store_dir + "/more_news.yaml" ) { |f| f.read }
    unless contents.empty?
      YAML.load(contents)
    else
      [ ]
    end
  else
    [ ]
  end
end

#move_asset(from, filename) ⇒ Object

:nodoc:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/web/wiki.rb', line 151

def move_asset( from, filename )
  historical = File.expand_path(filename)
  i=0
  while( File.exists? historical )
    i += 1
    historical = File.dirname( historical ) + "/\#" + i.to_s + "." + File.basename( historical ).gsub( /^\#\d*\./, "" )
  end
  
  File.move( filename, historical ) if File.exists? filename

  if (from.is_a? Web::Upload)
    from.write_to_file(filename)
  else
    File.move( from, filename )
  end
end

#newsObject



207
208
209
# File 'lib/web/wiki.rb', line 207

def news
  more_news[0..4]
end

#page_listObject



168
169
170
171
172
# File 'lib/web/wiki.rb', line 168

def page_list
  Dir[store_dir + "/*.yaml"].entries.collect { |e|
    File.basename( e, ".yaml" ).gsub( /-slash-/, "/" )
  }
end

#save(page) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/web/wiki.rb', line 224

def save( page )
  # save a slim version of the page for recent changes
  rcpage = page.clone
  rcpage.content = ""
  rcpage.history = [ ]
  rc = more_news
  rc = rc.unshift(rcpage) unless rcpage.comment.empty?
  rc.pop if rc.size > Page::max_revisions
  
  yaml = rc.to_yaml
  File.open( store_dir + "/more_news.yaml", "w" ) { |f|
    f.write(yaml)
  }
  
  comment = page.comment
  # clear out comment now that we've saved more news
  page.comment = ""
  yaml = page.to_yaml
  File.open( store( page.name ), "w" ) { |f|
    f.write(yaml)
  }
end

#store(name) ⇒ Object



174
175
176
# File 'lib/web/wiki.rb', line 174

def store( name )
  File.join( store_dir, name.gsub(/\//, "-slash-") + ".yaml" )
end

#store_basedirObject



248
249
250
251
252
253
254
# File 'lib/web/wiki.rb', line 248

def store_basedir
  if ( vandal? )
    Wiki.pref( "tarpit_dir" )
  else
    Wiki.pref( "store_dir" )
  end
end

#store_dirObject



256
257
258
259
260
261
# File 'lib/web/wiki.rb', line 256

def store_dir
  unless (File.exists? store_dirname)
    File.makedirs( store_dirname)
  end
  store_dirname
end

#store_dirnameObject



267
268
269
270
271
272
273
# File 'lib/web/wiki.rb', line 267

def store_dirname
  unless ( store_basedir =~ /^(\w:)?\// )
    File.expand_path( Dir.pwd + "/" + self.store_basedir )
  else
    store_basedir
  end
end

#store_urlObject



263
264
265
# File 'lib/web/wiki.rb', line 263

def store_url
  Wiki::pref( :store_url ) || Wiki::pref( :store_dir )
end

#vandal?Boolean

Returns:

  • (Boolean)


275
276
277
278
279
280
281
282
283
284
# File 'lib/web/wiki.rb', line 275

def vandal?
  vandal = false
  vandals.each do |pattern|
    if pattern =~ ENV["REMOTE_ADDR"]
      vandal = true
      break
    end
  end
  vandal
end

#vandalsObject



286
287
288
289
290
291
292
293
294
295
296
# File 'lib/web/wiki.rb', line 286

def vandals
  @vandals ||= if File.exists?( Wiki.pref("vandals") || "" )
                 File.open( Wiki.pref("vandals") ) { |f|
                   f.to_a
                }.collect{ |line|
                   /^#{line.chomp.strip.gsub(/"/, '')}/
                }
               else
                 [ ]
               end
end