wikis_controller.rb 3.09 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2
require 'project_wiki'

3
class Projects::WikisController < Projects::ApplicationController
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])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
11 12
  end

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
16
    if @page
17
      render 'show'
18
    elsif file = @project_wiki.find_file(params[:id], params[:version_id])
19 20 21 22 23 24 25 26 27 28
      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
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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
43
    @page = @project_wiki.find_page(params[:id])
44

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

47
    if @page = WikiPages::UpdateService.new(@project, current_user, wiki_params).execute(@page)
48 49 50 51
      redirect_to(
        namespace_project_wiki_path(@project.namespace, @project, @page),
        notice: 'Wiki was successfully updated.'
      )
52 53 54
    else
      render 'edit'
    end
Valery Sizov's avatar
Valery Sizov committed
55 56 57
  end

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

60
    if @page.persisted?
Vinnie Okada's avatar
Vinnie Okada committed
61 62 63 64
      redirect_to(
        namespace_project_wiki_path(@project.namespace, @project, @page),
        notice: 'Wiki was successfully updated.'
      )
65 66
    else
      render action: "edit"
Valery Sizov's avatar
Valery Sizov committed
67 68
    end
  end
Valery Sizov's avatar
Valery Sizov committed
69 70

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
73
    unless @page
Vinnie Okada's avatar
Vinnie Okada committed
74 75 76 77
      redirect_to(
        namespace_project_wiki_path(@project.namespace, @project, :home),
        notice: "Page not found"
      )
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
78
    end
Valery Sizov's avatar
Valery Sizov committed
79
  end
80

Valery Sizov's avatar
Valery Sizov committed
81
  def destroy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
82 83 84
    @page = @project_wiki.find_page(params[:id])
    @page.delete if @page

Vinnie Okada's avatar
Vinnie Okada committed
85 86 87 88
    redirect_to(
      namespace_project_wiki_path(@project.namespace, @project, :home),
      notice: "Page was successfully deleted"
    )
89
  end
Valery Sizov's avatar
Valery Sizov committed
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104
  def markdown_preview
    text = params[:text]

    ext = Gitlab::ReferenceExtractor.new(@project, current_user, current_user)
    ext.analyze(text)

    render json: {
      body: view_context.markdown(text, pipeline: :wiki, project_wiki: @project_wiki),
      references: {
        users: ext.users.map(&:username)
      }
    }
  end

105
  def git_access
Valery Sizov's avatar
Valery Sizov committed
106
  end
107 108 109

  private

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

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

  def wiki_params
    params[:wiki].slice(:title, :content, :format, :message)
  end

Valery Sizov's avatar
Valery Sizov committed
125
end