BigW Consortium Gitlab

project_wiki.rb 4.46 KB
Newer Older
1 2
class ProjectWiki
  include Gitlab::ShellAdapter
3
  include Storage::LegacyProjectWiki
4 5

  MARKUPS = {
6
    'Markdown' => :markdown,
7 8
    'RDoc'     => :rdoc,
    'AsciiDoc' => :asciidoc
9
  }.freeze unless defined?(MARKUPS)
10

11
  CouldNotCreateWikiError = Class.new(StandardError)
12 13 14 15

  # Returns a string describing what went wrong after
  # an operation fails.
  attr_reader :error_message
16
  attr_reader :project
17 18 19 20 21 22

  def initialize(project, user = nil)
    @project = project
    @user = user
  end

Douwe Maan committed
23 24 25
  delegate :empty?, to: :pages
  delegate :repository_storage_path, to: :project

26 27 28 29
  def path
    @project.path + '.wiki'
  end

30
  def full_path
31
    @project.full_path + '.wiki'
32 33
  end

34 35 36
  # @deprecated use full_path when you need it for an URL route or disk_path when you want to point to the filesystem
  alias_method :path_with_namespace, :full_path

37
  def web_url
38
    Gitlab::Routing.url_helpers.project_wiki_url(@project, :home)
39 40
  end

41
  def url_to_repo
42
    gitlab_shell.url_to_repo(full_path)
43 44 45 46 47 48
  end

  def ssh_url_to_repo
    url_to_repo
  end

49
  def http_url_to_repo
50
    "#{Gitlab.config.gitlab.url}/#{full_path}.git"
51 52
  end

53
  def wiki_base_path
54
    [Gitlab.config.gitlab.relative_url_root, '/', @project.full_path, '/wikis'].join('')
55 56
  end

57 58 59 60
  # Returns the Gollum::Wiki object.
  def wiki
    @wiki ||= begin
      Gollum::Wiki.new(path_to_repo)
61
    rescue Rugged::OSError
62 63 64 65
      create_repo!
    end
  end

66 67 68
  def repository_exists?
    !!repository.exists?
  end
69 70 71 72

  def has_home_page?
    !!find_page('home')
  end
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87
  # Returns an Array of Gitlab WikiPage instances or an
  # empty Array if this Wiki has no pages.
  def pages
    wiki.pages.map { |page| WikiPage.new(self, page, true) }
  end

  # Finds a page within the repository based on a tile
  # or slug.
  #
  # title - The human readable or parameterized title of
  #         the page.
  #
  # Returns an initialized WikiPage instance or nil
  def find_page(title, version = nil)
88 89
    page_title, page_dir = page_title_and_dir(title)
    if page = wiki.page(page_title, version, page_dir)
90 91 92 93 94 95
      WikiPage.new(self, page, true)
    else
      nil
    end
  end

96 97 98 99 100 101 102 103 104
  def find_file(name, version = nil, try_on_disk = true)
    version = wiki.ref if version.nil? # Gollum::Wiki#file ?
    if wiki_file = wiki.file(name, version, try_on_disk)
      wiki_file
    else
      nil
    end
  end

105 106 107
  def create_page(title, content, format = :markdown, message = nil)
    commit = commit_details(:created, message, title)

108
    wiki.write_page(title, format.to_sym, content, commit)
109 110

    update_project_activity
111 112 113 114 115
  rescue Gollum::DuplicatePageError => e
    @error_message = "Duplicate page: #{e.message}"
    return false
  end

116
  def update_page(page, content:, title: nil, format: :markdown, message: nil)
117 118
    commit = commit_details(:updated, message, page.title)

119
    wiki.update_page(page, title || page.name, format.to_sym, content, commit)
120 121

    update_project_activity
122 123 124 125
  end

  def delete_page(page, message = nil)
    wiki.delete_page(page, commit_details(:deleted, message, page.title))
126 127

    update_project_activity
128 129
  end

130
  def page_title_and_dir(title)
131
    title_array = title.split("/")
132
    title = title_array.pop
133
    [title, title_array.join("/")]
134 135
  end

Dmitriy Zaporozhets committed
136
  def search_files(query)
137
    repository.search_files_by_content(query, default_branch)
Dmitriy Zaporozhets committed
138 139 140
  end

  def repository
141
    @repository ||= Repository.new(full_path, @project, disk_path: disk_path)
Dmitriy Zaporozhets committed
142 143 144 145 146 147
  end

  def default_branch
    wiki.class.default_ref
  end

148
  def create_repo!
149
    if init_repo(disk_path)
150
      wiki = Gollum::Wiki.new(path_to_repo)
151 152 153
    else
      raise CouldNotCreateWikiError
    end
154 155 156 157

    repository.after_create

    wiki
158
  end
Gabriel Mazetto committed
159

160 161 162 163
  def ensure_repository
    create_repo! unless repository_exists?
  end

164 165 166 167 168
  def hook_attrs
    {
      web_url: web_url,
      git_ssh_url: ssh_url_to_repo,
      git_http_url: http_url_to_repo,
169
      path_with_namespace: full_path,
Gabriel Mazetto committed
170
      default_branch: default_branch
171 172
    }
  end
173

174 175
  private

176 177
  def init_repo(disk_path)
    gitlab_shell.add_repository(project.repository_storage_path, disk_path)
178 179 180 181 182
  end

  def commit_details(action, message = nil, title = nil)
    commit_message = message || default_message(action, title)

183
    { email: @user.email, name: @user.name, message: commit_message }
184 185 186 187 188 189 190
  end

  def default_message(action, title)
    "#{@user.username} #{action} page: #{title}"
  end

  def path_to_repo
191
    @path_to_repo ||= File.join(project.repository_storage_path, "#{disk_path}.git")
192
  end
193 194

  def update_project_activity
195
    @project.touch(:last_activity_at, :last_repository_updated_at)
196
  end
197
end