BigW Consortium Gitlab

wiki_page.rb 4.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
class WikiPage
  include ActiveModel::Validations
  include ActiveModel::Conversion
  include StaticModel
  extend ActiveModel::Naming

  def self.primary_key
    'slug'
  end

  def self.model_name
    ActiveModel::Name.new(self, nil, 'wiki')
  end

  def to_key
    [:slug]
  end

  validates :title, presence: true
  validates :content, presence: true

22
  # The Gitlab ProjectWiki instance.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  attr_reader :wiki

  # The raw Gollum::Page instance.
  attr_reader :page

  # The attributes Hash used for storing and validating
  # new Page values before writing to the Gollum repository.
  attr_accessor :attributes

  def initialize(wiki, page = nil, persisted = false)
    @wiki       = wiki
    @page       = page
    @persisted  = persisted
    @attributes = {}.with_indifferent_access

    set_attributes if persisted?
  end

  # The escaped URL path of this page.
  def slug
    @attributes[:slug]
  end

46
  alias_method :to_param, :slug
47 48 49

  # The formatted title of this page.
  def title
50 51 52 53 54
    if @attributes[:title]
      @attributes[:title].gsub(/-+/, ' ')
    else
      ""
    end
55 56 57 58 59 60 61 62 63
  end

  # Sets the title of this page.
  def title=(new_title)
    @attributes[:title] = new_title
  end

  # The raw content of this page.
  def content
64 65 66
    @attributes[:content] ||= if @page
                                @page.raw_data
                              end
67 68 69 70
  end

  # The processed/formatted content of this page.
  def formatted_content
71 72 73
    @attributes[:formatted_content] ||= if @page
                                          @page.formatted_data
                                        end
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  end

  # The markup format for the page.
  def format
    @attributes[:format] || :markdown
  end

  # The commit message for this page version.
  def message
    version.try(:message)
  end

  # The Gitlab Commit instance for this page.
  def version
    return nil unless persisted?

Dmitriy Zaporozhets committed
90
    @version ||= @page.version
91 92 93 94 95 96
  end

  # Returns an array of Gitlab Commit instances.
  def versions
    return [] unless persisted?

Dmitriy Zaporozhets committed
97
    @page.versions
98 99
  end

100 101 102 103
  def commit
    versions.first
  end

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  # Returns the Date that this latest version was
  # created on.
  def created_at
    @page.version.date
  end

  # Returns boolean True or False if this instance
  # is an old version of the page.
  def historical?
    @page.historical?
  end

  # Returns boolean True or False if this instance
  # has been fully saved to disk or not.
  def persisted?
    @persisted == true
  end

  # Creates a new Wiki Page.
  #
  # attr - Hash of attributes to set on the new page.
  #       :title   - The title for the new page.
  #       :content - The raw markup content.
  #       :format  - Optional symbol representing the
  #                  content format. Can be any type
129
  #                  listed in the ProjectWiki::MARKUPS
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  #                  Hash.
  #       :message - Optional commit message to set on
  #                  the new page.
  #
  # Returns the String SHA1 of the newly created page
  # or False if the save was unsuccessful.
  def create(attr = {})
    @attributes.merge!(attr)

    save :create_page, title, content, format, message
  end

  # Updates an existing Wiki Page, creating a new version.
  #
  # new_content - The raw markup content to replace the existing.
  # format      - Optional symbol representing the content format.
146
  #               See ProjectWiki::MARKUPS Hash for available formats.
147 148 149 150 151 152 153 154 155 156 157
  # message     - Optional commit message to set on the new version.
  #
  # Returns the String SHA1 of the newly created page
  # or False if the save was unsuccessful.
  def update(new_content = "", format = :markdown, message = nil)
    @attributes[:content] = new_content
    @attributes[:format] = format

    save :update_page, @page, content, format, message
  end

Johannes Schleifenbaum committed
158
  # Destroys the Wiki Page.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  #
  # Returns boolean True or False.
  def delete
    if wiki.delete_page(@page)
      true
    else
      false
    end
  end

  private

  def set_attributes
    attributes[:slug] = @page.escaped_url_path
    attributes[:title] = @page.title
    attributes[:format] = @page.format
  end

  def save(method, *args)
178 179
    project_wiki = wiki
    if valid? && project_wiki.send(method, *args)
180 181 182 183 184 185 186

      page_details = if method == :update_page
                       @page.path
                     else
                       title
                     end

187 188 189
      page_title, page_dir = project_wiki.page_title_and_dir(page_details)
      gollum_wiki = project_wiki.wiki
      @page = gollum_wiki.paged(page_title, page_dir)
190 191 192 193 194

      set_attributes

      @persisted = true
    else
195
      errors.add(:base, project_wiki.error_message) if project_wiki.error_message
196 197 198 199 200
      @persisted = false
    end
    @persisted
  end
end