Class: ProjectWiki

Inherits:
Object
  • Object
show all
Includes:
Gitlab::ShellAdapter
Defined in:
app/models/project_wiki.rb

Defined Under Namespace

Classes: CouldNotCreateWikiError

Constant Summary collapse

MARKUPS =
{
  'Markdown' => :markdown,
  'RDoc'     => :rdoc,
  'AsciiDoc' => :asciidoc
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::ShellAdapter

#gitlab_shell

Constructor Details

#initialize(project, user = nil) ⇒ ProjectWiki

Returns a new instance of ProjectWiki


17
18
19
20
# File 'app/models/project_wiki.rb', line 17

def initialize(project, user = nil)
  @project = project
  @user = user
end

Instance Attribute Details

#error_messageObject (readonly)

Returns a string describing what went wrong after an operation fails.


14
15
16
# File 'app/models/project_wiki.rb', line 14

def error_message
  @error_message
end

#projectObject (readonly)

Returns the value of attribute project


15
16
17
# File 'app/models/project_wiki.rb', line 15

def project
  @project
end

Instance Method Details

#create_page(title, content, format = :markdown, message = nil) ⇒ Object


90
91
92
93
94
95
96
97
98
99
# File 'app/models/project_wiki.rb', line 90

def create_page(title, content, format = :markdown, message = nil)
  commit = commit_details(:created, message, title)

  wiki.write_page(title, format.to_sym, content, commit)

  update_project_activity
rescue Gollum::DuplicatePageError => e
  @error_message = "Duplicate page: #{e.message}"
  return false
end

#create_repo!Object


133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/project_wiki.rb', line 133

def create_repo!
  if init_repo(path_with_namespace)
    wiki = Gollum::Wiki.new(path_to_repo)
  else
    raise CouldNotCreateWikiError
  end

  repository.after_create

  wiki
end

#default_branchObject


129
130
131
# File 'app/models/project_wiki.rb', line 129

def default_branch
  wiki.class.default_ref
end

#delete_page(page, message = nil) ⇒ Object


109
110
111
112
113
# File 'app/models/project_wiki.rb', line 109

def delete_page(page, message = nil)
  wiki.delete_page(page, commit_details(:deleted, message, page.title))

  update_project_activity
end

#empty?Boolean

Returns:

  • (Boolean)

55
56
57
# File 'app/models/project_wiki.rb', line 55

def empty?
  pages.empty?
end

#find_file(name, version = nil, try_on_disk = true) ⇒ Object


81
82
83
84
85
86
87
88
# File 'app/models/project_wiki.rb', line 81

def find_file(name, version = nil, try_on_disk = true)
  version = wiki.ref if version.nil? # Gollum::Wiki#file ?
  if wiki_file = wiki.file(name, version, try_on_disk)
    wiki_file
  else
    nil
  end
end

#find_page(title, version = nil) ⇒ Object

Finds a page within the repository based on a tile or slug.

title - The human readable or parameterized title of

the page.

Returns an initialized WikiPage instance or nil


72
73
74
75
76
77
78
79
# File 'app/models/project_wiki.rb', line 72

def find_page(title, version = nil)
  page_title, page_dir = page_title_and_dir(title)
  if page = wiki.page(page_title, version, page_dir)
    WikiPage.new(self, page, true)
  else
    nil
  end
end

#http_url_to_repoObject


38
39
40
# File 'app/models/project_wiki.rb', line 38

def http_url_to_repo
  [Gitlab.config.gitlab.url, "/", path_with_namespace, ".git"].join('')
end

#page_title_and_dir(title) ⇒ Object


115
116
117
118
119
# File 'app/models/project_wiki.rb', line 115

def page_title_and_dir(title)
  title_array =  title.split("/")
  title = title_array.pop
  [title, title_array.join("/")]
end

#pagesObject

Returns an Array of Gitlab WikiPage instances or an empty Array if this Wiki has no pages.


61
62
63
# File 'app/models/project_wiki.rb', line 61

def pages
  wiki.pages.map { |page| WikiPage.new(self, page, true) }
end

#pathObject


22
23
24
# File 'app/models/project_wiki.rb', line 22

def path
  @project.path + '.wiki'
end

#path_with_namespaceObject


26
27
28
# File 'app/models/project_wiki.rb', line 26

def path_with_namespace
  @project.path_with_namespace + ".wiki"
end

#repositoryObject


125
126
127
# File 'app/models/project_wiki.rb', line 125

def repository
  @repository ||= Repository.new(path_with_namespace, @project)
end

#search_files(query) ⇒ Object


121
122
123
# File 'app/models/project_wiki.rb', line 121

def search_files(query)
  repository.search_files(query, default_branch)
end

#ssh_url_to_repoObject


34
35
36
# File 'app/models/project_wiki.rb', line 34

def ssh_url_to_repo
  url_to_repo
end

#update_page(page, content, format = :markdown, message = nil) ⇒ Object


101
102
103
104
105
106
107
# File 'app/models/project_wiki.rb', line 101

def update_page(page, content, format = :markdown, message = nil)
  commit = commit_details(:updated, message, page.title)

  wiki.update_page(page, page.name, format.to_sym, content, commit)

  update_project_activity
end

#url_to_repoObject


30
31
32
# File 'app/models/project_wiki.rb', line 30

def url_to_repo
  gitlab_shell.url_to_repo(path_with_namespace)
end

#wikiObject

Returns the Gollum::Wiki object.


47
48
49
50
51
52
53
# File 'app/models/project_wiki.rb', line 47

def wiki
  @wiki ||= begin
    Gollum::Wiki.new(path_to_repo)
  rescue Rugged::OSError
    create_repo!
  end
end

#wiki_base_pathObject


42
43
44
# File 'app/models/project_wiki.rb', line 42

def wiki_base_path
  [Gitlab.config.gitlab.url, "/", @project.path_with_namespace, "/wikis"].join('')
end