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

3
class Projects::WikisController < Projects::ApplicationController
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
4
  before_filter :authorize_read_wiki!
5 6
  before_filter :authorize_write_wiki!, only: [:edit, :create, :history]
  before_filter :authorize_admin_wiki!, only: :destroy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
7
  before_filter :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]).per(30)
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, :write_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 46

    return render('empty') unless can?(current_user, :write_wiki, @project)

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
47
    if @page.update(content, format, message)
Vinnie Okada's avatar
Vinnie Okada committed
48
      redirect_to [@project.namespace.becomes(Namespace), @project, @page], notice: 'Wiki was successfully updated.'
49 50 51
    else
      render 'edit'
    end
Valery Sizov's avatar
Valery Sizov committed
52 53 54
  end

  def create
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
55
    @page = WikiPage.new(@project_wiki)
56

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
57
    if @page.create(wiki_params)
Vinnie Okada's avatar
Vinnie Okada committed
58 59 60 61
      redirect_to(
        namespace_project_wiki_path(@project.namespace, @project, @page),
        notice: 'Wiki was successfully updated.'
      )
62 63
    else
      render action: "edit"
Valery Sizov's avatar
Valery Sizov committed
64 65
    end
  end
Valery Sizov's avatar
Valery Sizov committed
66 67

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

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

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

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

88
  def git_access
Valery Sizov's avatar
Valery Sizov committed
89
  end
90 91 92

  private

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
93 94
  def load_project_wiki
    @project_wiki = ProjectWiki.new(@project, current_user)
95 96

    # Call #wiki to make sure the Wiki Repo is initialized
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
97 98
    @project_wiki.wiki
  rescue ProjectWiki::CouldNotCreateWikiError => ex
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    flash[:notice] = "Could not create Wiki Repository at this time. Please try again later."
    redirect_to @project
    return false
  end

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

  def content
    params[:wiki][:content]
  end

  def format
    params[:wiki][:format]
  end

  def message
    params[:wiki][:message]
  end
Valery Sizov's avatar
Valery Sizov committed
119
end