class Olelo::Page

Wiki page object

Constants

DIRECTORY_MIME

Mime type for directory @api public

EMPTY_MIME

Mime type for empty page @api public

PATH_PATTERN

Pattern for valid paths @api public

PATH_REGEXP

Attributes

path[R]
tree_version[R]

Public Class Methods

commit(comment) click to toggle source
# File lib/olelo/page.rb, line 59
def self.commit(comment)
  tree_version = repository.commit(comment)
  current_transaction.each {|proc| proc.call(tree_version) }
  current_transaction.clear
end
current_transaction() click to toggle source
# File lib/olelo/page.rb, line 55
def self.current_transaction
  Thread.current[:olelo_tx] || raise('No transaction running')
end
default_mime() click to toggle source
# File lib/olelo/page.rb, line 226
def self.default_mime
  mime = Config['mime'].find {|m| m.include? '/'}
  mime ? MimeMagic.new(mime) : nil
end
find(path, tree_version = nil) click to toggle source

Throws exceptions if access denied, returns nil if not found

# File lib/olelo/page.rb, line 66
def self.find(path, tree_version = nil)
  path = path.to_s.cleanpath
  check_path(path)
  tree_version = repository.get_version(tree_version) unless Version === tree_version
  if tree_version
    etag = repository.path_etag(path, tree_version)
    Page.new(path, tree_version, etag) if etag
  end
end
find!(path, tree_version = nil) click to toggle source

Throws if not found

# File lib/olelo/page.rb, line 77
def self.find!(path, tree_version = nil)
  find(path, tree_version) || raise(NotFound, path)
end
new(path, tree_version = nil, etag = nil, parent = nil) click to toggle source
# File lib/olelo/page.rb, line 42
def initialize(path, tree_version = nil, etag = nil, parent = nil)
  @path, @etag, @tree_version, @parent = path.to_s.cleanpath.freeze, etag, tree_version, parent
  Page.check_path(@path)
end
transaction(&block) click to toggle source
# File lib/olelo/page.rb, line 47
def self.transaction(&block)
  raise 'Transaction already running' if Thread.current[:olelo_tx]
  Thread.current[:olelo_tx] = []
  repository.transaction(&block)
ensure
  Thread.current[:olelo_tx] = nil
end

Private Class Methods

check_path(path) click to toggle source
# File lib/olelo/page.rb, line 246
def self.check_path(path)
  raise :invalid_path.t if !(path.blank? || path =~ PATH_REGEXP) || !valid_xml_chars?(path)
end
repository() click to toggle source
# File lib/olelo/page.rb, line 283
def self.repository
  Repository.instance
end

Public Instance Methods

attributes() click to toggle source
# File lib/olelo/page.rb, line 165
def attributes
  @attributes ||= deep_copy(saved_attributes)
end
attributes=(a) click to toggle source
# File lib/olelo/page.rb, line 173
def attributes=(a)
  a ||= {}
  if attributes != a
    @attributes = a
    @mime = nil
  end
  raise :invalid_mime_type.t if attributes['mime'] && attributes['mime'] != mime.to_s
end
children() click to toggle source
# File lib/olelo/page.rb, line 215
def children
  @children ||=
    if new?
      []
    else
      repository.get_children(path, tree_version).sort.map do |name|
        Page.new(path/name, tree_version, nil, self)
      end
    end
end
content() click to toggle source
# File lib/olelo/page.rb, line 186
def content
  @content ||= saved_content
end
content=(c) click to toggle source
# File lib/olelo/page.rb, line 190
def content=(c)
  if content != c
    @mime = nil
    @content = c
  end
end
delete() click to toggle source
# File lib/olelo/page.rb, line 135
def delete
  raise 'Page is not head' unless head?
  raise 'Page is new' if new?
  with_hooks(:delete) { repository.delete(path) }
  after_commit {|tree_version| update(path, nil) }
end
diff(from, to) click to toggle source
# File lib/olelo/page.rb, line 142
def diff(from, to)
  raise 'Page is new' if new?
  repository.diff(path, from, to)
end
editable?() click to toggle source
# File lib/olelo/page.rb, line 90
def editable?
  mime.text? || mime == EMPTY_MIME || mime == DIRECTORY_MIME
end
etag() click to toggle source
# File lib/olelo/page.rb, line 94
def etag
  unless new?
    @etag ||= repository.path_etag(path, tree_version)
    "#{Olelo::VERSION}-#{head? ? 1 : 0}-#{@etag}"
  end
end
extension() click to toggle source
# File lib/olelo/page.rb, line 160
def extension
  i = path.index('.')
  i ? path[i+1..-1] : ''
end
head?() click to toggle source

Head version

# File lib/olelo/page.rb, line 82
def head?
  new? || tree_version.head?
end
history(skip, limit) click to toggle source
# File lib/olelo/page.rb, line 116
def history(skip, limit)
  raise 'Page is new' if new?
  repository.get_history(path, skip, limit)
end
mime() click to toggle source
# File lib/olelo/page.rb, line 211
def mime
  @mime ||= detect_mime
end
modified?() click to toggle source
# File lib/olelo/page.rb, line 197
def modified?
  content != saved_content || attributes != saved_attributes
end
move(destination) click to toggle source
# File lib/olelo/page.rb, line 125
def move(destination)
  raise 'Page is not head' unless head?
  raise 'Page is new' if new?
  destination = destination.to_s.cleanpath
  Page.check_path(destination)
  raise :already_exists.t(page: destination) if Page.find(destination)
  with_hooks(:move, destination) { repository.move(path, destination) }
  after_commit {|tree_version| update(destination, tree_version) }
end
name() click to toggle source
# File lib/olelo/page.rb, line 151
def name
  i = path.rindex('/')
  i ? path[i+1..-1] : path
end
new?() click to toggle source
# File lib/olelo/page.rb, line 147
def new?
  !tree_version
end
next_version() click to toggle source
# File lib/olelo/page.rb, line 101
def next_version
  init_versions
  @next_version
end
parent() click to toggle source
# File lib/olelo/page.rb, line 121
def parent
  @parent ||= Page.find(path/'..', tree_version) || Page.new(path/'..', tree_version) if !root?
end
previous_version() click to toggle source
# File lib/olelo/page.rb, line 106
def previous_version
  init_versions
  @previous_version
end
root?() click to toggle source
# File lib/olelo/page.rb, line 86
def root?
  path.empty?
end
save() click to toggle source
# File lib/olelo/page.rb, line 201
def save
  raise 'Page is not head' unless head?
  raise :already_exists.t(page: path) if new? && Page.find(path)
  with_hooks(:save) do
    repository.set_content(path, content)
    repository.set_attributes(path, attributes)
  end
  after_commit {|tree_version| update(path, tree_version) }
end
saved_attributes() click to toggle source
# File lib/olelo/page.rb, line 169
def saved_attributes
  @saved_attributes ||= new? ? {} : repository.get_attributes(path, tree_version)
end
saved_content() click to toggle source
# File lib/olelo/page.rb, line 182
def saved_content
  @saved_content ||= new? ? '' : repository.get_content(path, tree_version)
end
title() click to toggle source
# File lib/olelo/page.rb, line 156
def title
  attributes['title'] || (root? ? :root.t : name)
end
version() click to toggle source
# File lib/olelo/page.rb, line 111
def version
  init_versions
  @version
end

Private Instance Methods

after_commit(&block) click to toggle source
# File lib/olelo/page.rb, line 242
def after_commit(&block)
  Page.current_transaction << block
end
detect_mime() click to toggle source
# File lib/olelo/page.rb, line 250
def detect_mime
  [attributes['mime'], *Config['mime'], 'application/octet-stream'].each do |method|
    mime =
      case method
      when nil
      when 'extension'
        MimeMagic.by_extension(extension)
      when 'content', 'magic'
        unless new?
          if content.blank?
            children.empty? ? EMPTY_MIME : DIRECTORY_MIME
          else
            MimeMagic.by_magic(content)
          end
        end
      else
        MimeMagic.new(method)
      end
    return mime if mime && (!mime.text? || valid_xml_chars?(content))
  end
end
init_versions() click to toggle source
# File lib/olelo/page.rb, line 272
def init_versions
  if !@version && @tree_version
    raise 'Page is new' if new?
    @previous_version, @version, @next_version = repository.get_path_version(path, tree_version)
  end
end
repository() click to toggle source
# File lib/olelo/page.rb, line 279
def repository
  Repository.instance
end
update(path, tree_version) click to toggle source
# File lib/olelo/page.rb, line 233
def update(path, tree_version)
  @path = path.freeze
  @tree_version = tree_version
  @version = @next_version = @previous_version =
    @parent = @children = @mime =
    @attributes = @saved_attributes =
    @content = @saved_content = nil
end