GitLab steht Mittwoch, den 23. September, zwischen 10:00 und 12:00 Uhr aufgrund von Wartungsarbeiten nicht zur Verfügung.

wikis_controller.rb 3.01 KB
Newer Older
1
class Projects::WikisController < Projects::ApplicationController
2 3
  include PreviewMarkdown

4
  before_action :authorize_read_wiki!
5
  before_action :authorize_create_wiki!, only: [:edit, :create, :history]
6 7
  before_action :authorize_admin_wiki!, only: :destroy
  before_action :load_project_wiki
8

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
9
  def pages
10
    @wiki_pages = Kaminari.paginate_array(@project_wiki.pages).page(params[:page])
11
    @wiki_entries = WikiPage.group_by_directory(@wiki_pages)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12 13
  end

Valery Sizov's avatar
Valery Sizov committed
14
  def show
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15
    @page = @project_wiki.find_page(params[:id], params[:version_id])
16

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17
    if @page
18
      render 'show'
19
    elsif file = @project_wiki.find_file(params[:id], params[:version_id])
20 21 22
      response.headers['Content-Security-Policy'] = "default-src 'none'"
      response.headers['X-Content-Security-Policy'] = "default-src 'none'"

23 24 25 26 27 28
      send_data(
        file.raw_data,
        type: file.mime_type,
        disposition: 'inline',
        filename: file.name
      )
29
    else
30
      return render('empty') unless can?(current_user, :create_wiki, @project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
31 32
      @page = WikiPage.new(@project_wiki)
      @page.title = params[:id]
33 34

      render 'edit'
Valery Sizov's avatar
Valery Sizov committed
35 36 37 38
    end
  end

  def edit
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
39
    @page = @project_wiki.find_page(params[:id])
40 41 42
  end

  def update
43
    return render('empty') unless can?(current_user, :create_wiki, @project)
44

45
    @page = @project_wiki.find_page(params[:id])
Dongqing Hu's avatar
Dongqing Hu committed
46
    @page = WikiPages::UpdateService.new(@project, current_user, wiki_params).execute(@page)
47

Dongqing Hu's avatar
Dongqing Hu committed
48
    if @page.valid?
49
      redirect_to(
50
        project_wiki_path(@project, @page),
51 52
        notice: 'Wiki was successfully updated.'
      )
53 54 55
    else
      render 'edit'
    end
56 57 58
  rescue WikiPage::PageChangedError
    @conflict = true
    render 'edit'
Valery Sizov's avatar
Valery Sizov committed
59 60 61
  end

  def create
62
    @page = WikiPages::CreateService.new(@project, current_user, wiki_params).execute
63

64
    if @page.persisted?
Vinnie Okada's avatar
Vinnie Okada committed
65
      redirect_to(
66
        project_wiki_path(@project, @page),
Vinnie Okada's avatar
Vinnie Okada committed
67 68
        notice: 'Wiki was successfully updated.'
      )
69 70
    else
      render action: "edit"
Valery Sizov's avatar
Valery Sizov committed
71 72
    end
  end
Valery Sizov's avatar
Valery Sizov committed
73 74

  def history
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
75
    @page = @project_wiki.find_page(params[:id])
76

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
77
    unless @page
Vinnie Okada's avatar
Vinnie Okada committed
78
      redirect_to(
79
        project_wiki_path(@project, :home),
Vinnie Okada's avatar
Vinnie Okada committed
80 81
        notice: "Page not found"
      )
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
82
    end
Valery Sizov's avatar
Valery Sizov committed
83
  end
84

Valery Sizov's avatar
Valery Sizov committed
85
  def destroy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
86
    @page = @project_wiki.find_page(params[:id])
87
    WikiPages::DestroyService.new(@project, current_user).execute(@page)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
88

89
    redirect_to project_wiki_path(@project, :home),
90 91
                status: 302,
                notice: "Page was successfully deleted"
92
  end
Valery Sizov's avatar
Valery Sizov committed
93

94
  def git_access
95 96
  end

97 98
  private

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
99 100
  def load_project_wiki
    @project_wiki = ProjectWiki.new(@project, current_user)
101 102

    # Call #wiki to make sure the Wiki Repo is initialized
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
103
    @project_wiki.wiki
104
    @sidebar_wiki_entries = WikiPage.group_by_directory(@project_wiki.pages.first(15))
105
  rescue ProjectWiki::CouldNotCreateWikiError
106
    flash[:notice] = "Could not create Wiki Repository at this time. Please try again later."
107
    redirect_to project_path(@project)
108 109 110 111
    return false
  end

  def wiki_params
112
    params.require(:wiki).permit(:title, :content, :format, :message, :last_commit_sha)
113
  end
Valery Sizov's avatar
Valery Sizov committed
114
end