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

wikis_controller.rb 3.4 KB
Newer Older
1
class Projects::WikisController < Projects::ApplicationController
2
  before_action :authorize_read_wiki!
3
  before_action :authorize_create_wiki!, only: [:edit, :create, :history]
4 5
  before_action :authorize_admin_wiki!, only: :destroy
  before_action :load_project_wiki
6

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

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

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

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

      render 'edit'
Valery Sizov's avatar
Valery Sizov committed
37 38 39 40
    end
  end

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

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

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

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

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

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

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

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

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

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

96
  def git_access
97 98
  end

99
  def preview_markdown
100
    result = PreviewMarkdownService.new(@project, current_user, params).execute
101 102

    render json: {
103
      body: view_context.markdown(result[:text], pipeline: :wiki, project_wiki: @project_wiki, page_slug: params[:id]),
104
      references: {
105
        users: result[:users]
106 107 108 109
      }
    }
  end

110 111
  private

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
112 113
  def load_project_wiki
    @project_wiki = ProjectWiki.new(@project, current_user)
114 115

    # Call #wiki to make sure the Wiki Repo is initialized
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
116
    @project_wiki.wiki
117
    @sidebar_wiki_entries = WikiPage.group_by_directory(@project_wiki.pages.first(15))
118
  rescue ProjectWiki::CouldNotCreateWikiError
119
    flash[:notice] = "Could not create Wiki Repository at this time. Please try again later."
120
    redirect_to project_path(@project)
121 122 123 124
    return false
  end

  def wiki_params
125
    params.require(:wiki).permit(:title, :content, :format, :message, :last_commit_sha)
126
  end
Valery Sizov's avatar
Valery Sizov committed
127
end