wiki.rb 10.1 KB
Newer Older
1 2 3 4
module Gitlab
  module Git
    class Wiki
      DuplicatePageError = Class.new(StandardError)
5
      OperationError = Class.new(StandardError)
6

7
      CommitDetails = Struct.new(:user_id, :username, :name, :email, :message) do
8
        def to_h
9
          { user_id: user_id, username: username, name: name, email: email, message: message }
10 11
        end
      end
12
      PageBlob = Struct.new(:name)
13

14 15
      attr_reader :repository

16 17 18 19 20 21 22 23 24 25 26 27 28 29
      def self.default_ref
        'master'
      end

      # Initialize with a Gitlab::Git::Repository instance
      def initialize(repository)
        @repository = repository
      end

      def repository_exists?
        @repository.exists?
      end

      def write_page(name, format, content, commit_details)
F
Francisco Javier López 已提交
30
        @repository.gitaly_migrate(:wiki_write_page) do |is_enabled|
31 32 33 34 35 36
          if is_enabled
            gitaly_write_page(name, format, content, commit_details)
          else
            gollum_write_page(name, format, content, commit_details)
          end
        end
37 38 39
      end

      def delete_page(page_path, commit_details)
40 41 42 43 44 45 46
        @repository.gitaly_migrate(:wiki_delete_page) do |is_enabled|
          if is_enabled
            gitaly_delete_page(page_path, commit_details)
          else
            gollum_delete_page(page_path, commit_details)
          end
        end
47 48 49
      end

      def update_page(page_path, title, format, content, commit_details)
F
Francisco Javier López 已提交
50
        @repository.gitaly_migrate(:wiki_update_page) do |is_enabled|
51 52 53 54 55 56
          if is_enabled
            gitaly_update_page(page_path, title, format, content, commit_details)
          else
            gollum_update_page(page_path, title, format, content, commit_details)
          end
        end
57 58
      end

59
      def pages(limit: nil)
60
        @repository.gitaly_migrate(:wiki_get_all_pages) do |is_enabled|
61 62 63
          if is_enabled
            gitaly_get_all_pages
          else
64
            gollum_get_all_pages(limit: limit)
65 66
          end
        end
67 68 69
      end

      def page(title:, version: nil, dir: nil)
70
        @repository.gitaly_migrate(:wiki_find_page) do |is_enabled|
71 72 73 74 75
          if is_enabled
            gitaly_find_page(title: title, version: version, dir: dir)
          else
            gollum_find_page(title: title, version: version, dir: dir)
          end
76 77 78 79
        end
      end

      def file(name, version)
80 81 82 83 84 85 86
        @repository.gitaly_migrate(:wiki_find_file) do |is_enabled|
          if is_enabled
            gitaly_find_file(name, version)
          else
            gollum_find_file(name, version)
          end
        end
87 88
      end

89 90 91 92 93
      # options:
      #  :page     - The Integer page number.
      #  :per_page - The number of items per page.
      #  :limit    - Total number of items to return.
      def page_versions(page_path, options = {})
94 95
        @repository.gitaly_migrate(:wiki_page_versions) do |is_enabled|
          if is_enabled
96 97 98 99 100 101 102
            versions = gitaly_wiki_client.page_versions(page_path, options)

            # Gitaly uses gollum-lib to get the versions. Gollum defaults to 20
            # per page, but also fetches 20 if `limit` or `per_page` < 20.
            # Slicing returns an array with the expected number of items.
            slice_bound = options[:limit] || options[:per_page] || Gollum::Page.per_page
            versions[0..slice_bound]
103 104 105 106 107 108 109 110
          else
            current_page = gollum_page_by_path(page_path)

            commits_from_page(current_page, options).map do |gitlab_git_commit|
              gollum_page = gollum_wiki.page(current_page.title, gitlab_git_commit.id)
              Gitlab::Git::WikiPageVersion.new(gitlab_git_commit, gollum_page&.format)
            end
          end
111 112 113
        end
      end

114 115 116 117
      def count_page_versions(page_path)
        @repository.count_commits(ref: 'HEAD', path: page_path)
      end

118
      def preview_slug(title, format)
119 120 121 122 123 124 125 126 127
        # Adapted from gollum gem (Gollum::Wiki#preview_page) to avoid
        # using Rugged through a Gollum::Wiki instance
        page_class = Gollum::Page
        page = page_class.new(nil)
        ext = page_class.format_to_ext(format.to_sym)
        name = page_class.cname(title) + '.' + ext
        blob = PageBlob.new(name)
        page.populate(blob)
        page.url_path
128 129
      end

130 131 132 133 134 135 136 137 138 139 140 141 142 143
      def page_formatted_data(title:, dir: nil, version: nil)
        version = version&.id

        @repository.gitaly_migrate(:wiki_page_formatted_data) do |is_enabled|
          if is_enabled
            gitaly_wiki_client.get_formatted_data(title: title, dir: dir, version: version)
          else
            # We don't use #page because if wiki_find_page feature is enabled, we would
            # get a page without formatted_data.
            gollum_find_page(title: title, dir: dir, version: version)&.formatted_data
          end
        end
      end

144 145 146 147
      def gollum_wiki
        @gollum_wiki ||= Gollum::Wiki.new(@repository.path)
      end

148 149
      private

150 151 152 153 154
      # options:
      #  :page     - The Integer page number.
      #  :per_page - The number of items per page.
      #  :limit    - Total number of items to return.
      def commits_from_page(gollum_page, options = {})
155 156 157 158
        unless options[:limit]
          options[:offset] = ([1, options.delete(:page).to_i].max - 1) * Gollum::Page.per_page
          options[:limit] = (options.delete(:per_page) || Gollum::Page.per_page).to_i
        end
159 160 161

        @repository.log(ref: gollum_page.last_version.id,
                        path: gollum_page.path,
162 163
                        limit: options[:limit],
                        offset: options[:offset])
164 165
      end

166 167 168 169 170 171 172 173 174 175 176 177
      def gollum_page_by_path(page_path)
        page_name = Gollum::Page.canonicalize_filename(page_path)
        page_dir = File.split(page_path).first

        gollum_wiki.paged(page_name, page_dir)
      end

      def new_page(gollum_page)
        Gitlab::Git::WikiPage.new(gollum_page, new_version(gollum_page, gollum_page.version.id))
      end

      def new_version(gollum_page, commit_id)
178 179 180 181 182 183 184 185 186 187 188
        Gitlab::Git::WikiPageVersion.new(version(commit_id), gollum_page&.format)
      end

      def version(commit_id)
        commit_find_proc = -> { Gitlab::Git::Commit.find(@repository, commit_id) }

        if RequestStore.active?
          RequestStore.fetch([:wiki_version_commit, commit_id]) { commit_find_proc.call }
        else
          commit_find_proc.call
        end
189 190 191 192 193 194 195
      end

      def assert_type!(object, klass)
        unless object.is_a?(klass)
          raise ArgumentError, "expected a #{klass}, got #{object.inspect}"
        end
      end
196 197 198 199 200 201 202 203 204

      def gitaly_wiki_client
        @gitaly_wiki_client ||= Gitlab::GitalyClient::WikiService.new(@repository)
      end

      def gollum_write_page(name, format, content, commit_details)
        assert_type!(format, Symbol)
        assert_type!(commit_details, CommitDetails)

205 206 207
        with_committer_with_hooks(commit_details) do |committer|
          filename = File.basename(name)
          dir = (tmp_dir = File.dirname(name)) == '.' ? '' : tmp_dir
208

209 210
          gollum_wiki.write_page(filename, format, content, { committer: committer }, dir)
        end
211 212 213 214
      rescue Gollum::DuplicatePageError => e
        raise Gitlab::Git::Wiki::DuplicatePageError, e.message
      end

215 216 217
      def gollum_delete_page(page_path, commit_details)
        assert_type!(commit_details, CommitDetails)

218 219 220
        with_committer_with_hooks(commit_details) do |committer|
          gollum_wiki.delete_page(gollum_page_by_path(page_path), committer: committer)
        end
221 222
      end

223 224 225 226
      def gollum_update_page(page_path, title, format, content, commit_details)
        assert_type!(format, Symbol)
        assert_type!(commit_details, CommitDetails)

227 228 229 230 231 232 233 234
        with_committer_with_hooks(commit_details) do |committer|
          page = gollum_page_by_path(page_path)
          # Instead of performing two renames if the title has changed,
          # the update_page will only update the format and content and
          # the rename_page will do anything related to moving/renaming
          gollum_wiki.update_page(page, page.name, format, content, committer: committer)
          gollum_wiki.rename_page(page, title, committer: committer)
        end
235 236
      end

237 238 239 240 241 242 243 244 245 246 247
      def gollum_find_page(title:, version: nil, dir: nil)
        if version
          version = Gitlab::Git::Commit.find(@repository, version).id
        end

        gollum_page = gollum_wiki.page(title, version, dir)
        return unless gollum_page

        new_page(gollum_page)
      end

248 249 250 251 252 253 254 255
      def gollum_find_file(name, version)
        version ||= self.class.default_ref
        gollum_file = gollum_wiki.file(name, version)
        return unless gollum_file

        Gitlab::Git::WikiFile.new(gollum_file)
      end

256 257
      def gollum_get_all_pages(limit: nil)
        gollum_wiki.pages(limit: limit).map { |gollum_page| new_page(gollum_page) }
258 259
      end

260 261 262
      def gitaly_write_page(name, format, content, commit_details)
        gitaly_wiki_client.write_page(name, format, content, commit_details)
      end
263

264 265 266 267
      def gitaly_update_page(page_path, title, format, content, commit_details)
        gitaly_wiki_client.update_page(page_path, title, format, content, commit_details)
      end

268 269 270
      def gitaly_delete_page(page_path, commit_details)
        gitaly_wiki_client.delete_page(page_path, commit_details)
      end
271 272 273 274 275 276 277

      def gitaly_find_page(title:, version: nil, dir: nil)
        wiki_page, version = gitaly_wiki_client.find_page(title: title, version: version, dir: dir)
        return unless wiki_page

        Gitlab::Git::WikiPage.new(wiki_page, version)
      end
278 279 280 281 282 283 284

      def gitaly_find_file(name, version)
        wiki_file = gitaly_wiki_client.find_file(name, version)
        return unless wiki_file

        Gitlab::Git::WikiFile.new(wiki_file)
      end
285 286 287 288 289 290

      def gitaly_get_all_pages
        gitaly_wiki_client.get_all_pages.map do |wiki_page, version|
          Gitlab::Git::WikiPage.new(wiki_page, version)
        end
      end
291 292

      def committer_with_hooks(commit_details)
293
        Gitlab::Git::CommitterWithHooks.new(self, commit_details.to_h)
294 295 296 297 298 299 300 301 302 303 304
      end

      def with_committer_with_hooks(commit_details, &block)
        committer = committer_with_hooks(commit_details)

        yield committer

        committer.commit

        nil
      end
305 306 307
    end
  end
end