Commit 26ec74c446e2242fe76bfe9ce84c8f86e9c46f3d

Authored by Dmitriy Zaporozhets
1 parent b27c42be

Refactor wiki model

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
app/models/project_wiki.rb 0 → 100644
... ... @@ -0,0 +1,120 @@
  1 +class ProjectWiki
  2 + include Gitlab::ShellAdapter
  3 +
  4 + MARKUPS = {
  5 + "Markdown" => :markdown,
  6 + "RDoc" => :rdoc
  7 + }
  8 +
  9 + class CouldNotCreateWikiError < StandardError; end
  10 +
  11 + # Returns a string describing what went wrong after
  12 + # an operation fails.
  13 + attr_reader :error_message
  14 +
  15 + def initialize(project, user = nil)
  16 + @project = project
  17 + @user = user
  18 + end
  19 +
  20 + def path
  21 + @project.path + '.wiki'
  22 + end
  23 +
  24 + def path_with_namespace
  25 + @project.path_with_namespace + ".wiki"
  26 + end
  27 +
  28 + def url_to_repo
  29 + gitlab_shell.url_to_repo(path_with_namespace)
  30 + end
  31 +
  32 + def ssh_url_to_repo
  33 + url_to_repo
  34 + end
  35 +
  36 + def http_url_to_repo
  37 + [Gitlab.config.gitlab.url, "/", path_with_namespace, ".git"].join('')
  38 + end
  39 +
  40 + # Returns the Gollum::Wiki object.
  41 + def wiki
  42 + @wiki ||= begin
  43 + Gollum::Wiki.new(path_to_repo)
  44 + rescue Gollum::NoSuchPathError
  45 + create_repo!
  46 + end
  47 + end
  48 +
  49 + def empty?
  50 + pages.empty?
  51 + end
  52 +
  53 + # Returns an Array of Gitlab WikiPage instances or an
  54 + # empty Array if this Wiki has no pages.
  55 + def pages
  56 + wiki.pages.map { |page| WikiPage.new(self, page, true) }
  57 + end
  58 +
  59 + # Finds a page within the repository based on a tile
  60 + # or slug.
  61 + #
  62 + # title - The human readable or parameterized title of
  63 + # the page.
  64 + #
  65 + # Returns an initialized WikiPage instance or nil
  66 + def find_page(title, version = nil)
  67 + if page = wiki.page(title, version)
  68 + WikiPage.new(self, page, true)
  69 + else
  70 + nil
  71 + end
  72 + end
  73 +
  74 + def create_page(title, content, format = :markdown, message = nil)
  75 + commit = commit_details(:created, message, title)
  76 +
  77 + wiki.write_page(title, format, content, commit)
  78 + rescue Gollum::DuplicatePageError => e
  79 + @error_message = "Duplicate page: #{e.message}"
  80 + return false
  81 + end
  82 +
  83 + def update_page(page, content, format = :markdown, message = nil)
  84 + commit = commit_details(:updated, message, page.title)
  85 +
  86 + wiki.update_page(page, page.name, format, content, commit)
  87 + end
  88 +
  89 + def delete_page(page, message = nil)
  90 + wiki.delete_page(page, commit_details(:deleted, message, page.title))
  91 + end
  92 +
  93 + private
  94 +
  95 + def create_repo!
  96 + if init_repo(path_with_namespace)
  97 + Gollum::Wiki.new(path_to_repo)
  98 + else
  99 + raise CouldNotCreateWikiError
  100 + end
  101 + end
  102 +
  103 + def init_repo(path_with_namespace)
  104 + gitlab_shell.add_repository(path_with_namespace)
  105 + end
  106 +
  107 + def commit_details(action, message = nil, title = nil)
  108 + commit_message = message || default_message(action, title)
  109 +
  110 + {email: @user.email, name: @user.name, message: commit_message}
  111 + end
  112 +
  113 + def default_message(action, title)
  114 + "#{@user.username} #{action} page: #{title}"
  115 + end
  116 +
  117 + def path_to_repo
  118 + @path_to_repo ||= File.join(Gitlab.config.gitlab_shell.repos_path, "#{path_with_namespace}.git")
  119 + end
  120 +end
... ...
app/models/wiki_page.rb
... ... @@ -19,7 +19,7 @@ class WikiPage
19 19 validates :title, presence: true
20 20 validates :content, presence: true
21 21  
22   - # The Gitlab GollumWiki instance.
  22 + # The Gitlab ProjectWiki instance.
23 23 attr_reader :wiki
24 24  
25 25 # The raw Gollum::Page instance.
... ... @@ -118,7 +118,7 @@ class WikiPage
118 118 # :content - The raw markup content.
119 119 # :format - Optional symbol representing the
120 120 # content format. Can be any type
121   - # listed in the GollumWiki::MARKUPS
  121 + # listed in the ProjectWiki::MARKUPS
122 122 # Hash.
123 123 # :message - Optional commit message to set on
124 124 # the new page.
... ... @@ -135,7 +135,7 @@ class WikiPage
135 135 #
136 136 # new_content - The raw markup content to replace the existing.
137 137 # format - Optional symbol representing the content format.
138   - # See GollumWiki::MARKUPS Hash for available formats.
  138 + # See ProjectWiki::MARKUPS Hash for available formats.
139 139 # message - Optional commit message to set on the new version.
140 140 #
141 141 # Returns the String SHA1 of the newly created page
... ... @@ -181,5 +181,4 @@ class WikiPage
181 181 end
182 182 @persisted
183 183 end
184   -
185 184 end
... ...