wikis_controller.rb 2.63 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])
Marin Jankovski's avatar
Marin Jankovski committed
15 16
    gollum_wiki = @project_wiki.wiki
    file = gollum_wiki.file(params[:id], gollum_wiki.ref, true)
17

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
18
    if @page
19
      render 'show'
Marin Jankovski's avatar
Marin Jankovski committed
20
    elsif file
Marin Jankovski's avatar
Marin Jankovski committed
21
       if file.on_disk?
Marin Jankovski's avatar
Marin Jankovski committed
22
         send_file file.on_disk_path, disposition: 'inline'
Marin Jankovski's avatar
Marin Jankovski committed
23
       else
Marin Jankovski's avatar
Marin Jankovski committed
24 25 26 27 28 29
         send_data(
           file.raw_data,
           type: file.mime_type,
           disposition: 'inline',
           filename: file.name
         )
Marin Jankovski's avatar
Marin Jankovski committed
30
       end
31
    else
32
      return render('empty') unless can?(current_user, :write_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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
45
    @page = @project_wiki.find_page(params[:id])
46 47 48

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

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

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

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

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
69 70 71
    unless @page
      redirect_to(project_wiki_path(@project, :home), notice: "Page not found")
    end
Valery Sizov's avatar
Valery Sizov committed
72
  end
73

Valery Sizov's avatar
Valery Sizov committed
74
  def destroy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
75 76 77
    @page = @project_wiki.find_page(params[:id])
    @page.delete if @page

78 79
    redirect_to project_wiki_path(@project, :home), notice: "Page was successfully deleted"
  end
Valery Sizov's avatar
Valery Sizov committed
80

81
  def git_access
Valery Sizov's avatar
Valery Sizov committed
82
  end
83 84 85

  private

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
86 87
  def load_project_wiki
    @project_wiki = ProjectWiki.new(@project, current_user)
88 89

    # Call #wiki to make sure the Wiki Repo is initialized
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
90 91
    @project_wiki.wiki
  rescue ProjectWiki::CouldNotCreateWikiError => ex
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    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
112
end