Class: Wiki

Inherits:
Object
  • Object
show all
Extended by:
Gitlab::Utils::Override
Includes:
Gitlab::Utils::StrongMemoize, HasRepository
Defined in:
app/models/wiki.rb

Direct Known Subclasses

ProjectWiki

Constant Summary collapse

MARKUPS =

rubocop:disable Style/MultilineIfModifier

{ # rubocop:disable Style/MultilineIfModifier
  'Markdown' => :markdown,
  'RDoc'     => :rdoc,
  'AsciiDoc' => :asciidoc,
  'Org'      => :org
}.freeze
CouldNotCreateWikiError =
Class.new(StandardError)
HOMEPAGE =
'home'
'_sidebar'
TITLE_ORDER =
'title'
CREATED_AT_ORDER =
'created_at'
DIRECTION_DESC =
'desc'
DIRECTION_ASC =
'asc'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from HasRepository

#commit, #commit_by, #commits_by, #default_branch_from_preferences, #empty_repo?, #http_url_to_repo, #reload_default_branch, #repo_exists?, #repository_exists?, #repository_size_checker, #root_ref?, #ssh_url_to_repo, #storage, #url_to_repo, #valid_repo?, #web_url

Methods included from Gitlab::ShellAdapter

#gitlab_shell

Methods included from Referable

#referable_inspect, #reference_link_text, #to_reference, #to_reference_base

Constructor Details

#initialize(container, user = nil) ⇒ Wiki

Returns a new instance of Wiki.

Raises:

  • (ArgumentError)

35
36
37
38
39
# File 'app/models/wiki.rb', line 35

def initialize(container, user = nil)
  @container = container
  @user = user
  raise ArgumentError, "user must be a User, got #{user.class}" if user && !user.is_a?(User)
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container


25
26
27
# File 'app/models/wiki.rb', line 25

def container
  @container
end

#error_messageObject (readonly)

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


29
30
31
# File 'app/models/wiki.rb', line 29

def error_message
  @error_message
end

#userObject (readonly)

Returns the value of attribute user


25
26
27
# File 'app/models/wiki.rb', line 25

def user
  @user
end

Class Method Details

.for_container(container, user = nil) ⇒ Object


31
32
33
# File 'app/models/wiki.rb', line 31

def self.for_container(container, user = nil)
  "#{container.class.name}Wiki".constantize.new(container, user)
end

Instance Method Details

#after_post_receiveObject

Callbacks for background processing after wiki changes. These will be executed after any change to the wiki repository.


223
224
# File 'app/models/wiki.rb', line 223

def after_post_receive
end

#after_wiki_activityObject

Callbacks for synchronous processing after wiki changes. These will be executed after any change made through GitLab itself (web UI and API), but not for Git pushes.


218
219
# File 'app/models/wiki.rb', line 218

def after_wiki_activity
end

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


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

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

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

  true
rescue Gitlab::Git::Wiki::DuplicatePageError => e
  @error_message = "Duplicate page: #{e.message}"
  false
end

#create_wiki_repositoryObject


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/wiki.rb', line 53

def create_wiki_repository
  repository.create_if_not_exists

  raise CouldNotCreateWikiError unless repository_exists?
rescue => err
  Gitlab::ErrorTracking.track_exception(err, wiki: {
    container_type: container.class.name,
    container_id: container.id,
    full_path: full_path,
    disk_path: disk_path
  })

  raise CouldNotCreateWikiError
end

#default_branchObject


207
208
209
# File 'app/models/wiki.rb', line 207

def default_branch
  wiki.class.default_ref
end

#delete_page(page, message = nil) ⇒ Object


153
154
155
156
157
158
159
160
# File 'app/models/wiki.rb', line 153

def delete_page(page, message = nil)
  return unless page

  wiki.delete_page(page.path, commit_details(:deleted, message, page.title))
  after_wiki_activity

  true
end

#empty?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'app/models/wiki.rb', line 72

def empty?
  list_pages(limit: 1).empty?
end

#ensure_repositoryObject


170
171
172
# File 'app/models/wiki.rb', line 170

def ensure_repository
  raise CouldNotCreateWikiError unless wiki.repository_exists?
end

#exists?Boolean

Returns:

  • (Boolean)

76
77
78
# File 'app/models/wiki.rb', line 76

def exists?
  !empty?
end

#find_file(name, version = nil) ⇒ Object


128
129
130
# File 'app/models/wiki.rb', line 128

def find_file(name, version = nil)
  wiki.file(name, version)
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


116
117
118
119
120
121
122
# File 'app/models/wiki.rb', line 116

def find_page(title, version = nil)
  page_title, page_dir = page_title_and_dir(title)

  if page = wiki.page(title: page_title, version: version, dir: page_dir)
    WikiPage.new(self, page)
  end
end

#find_sidebar(version = nil) ⇒ Object


124
125
126
# File 'app/models/wiki.rb', line 124

def find_sidebar(version = nil)
  find_page(SIDEBAR, version)
end

#full_pathObject Also known as: id, path_with_namespace


198
199
200
# File 'app/models/wiki.rb', line 198

def full_path
  container.full_path + '.wiki'
end

#has_home_page?Boolean

Returns:

  • (Boolean)

68
69
70
# File 'app/models/wiki.rb', line 68

def has_home_page?
  !!find_page(HOMEPAGE)
end

#hashed_storage?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)

193
194
195
# File 'app/models/wiki.rb', line 193

def hashed_storage?
  raise NotImplementedError
end

#hook_attrsObject


174
175
176
177
178
179
180
181
182
# File 'app/models/wiki.rb', line 174

def hook_attrs
  {
    web_url: web_url,
    git_ssh_url: ssh_url_to_repo,
    git_http_url: http_url_to_repo,
    path_with_namespace: full_path,
    default_branch: default_branch
  }
end

#list_pages(limit: 0, sort: nil, direction: DIRECTION_ASC, load_content: false) ⇒ Object

Lists wiki pages of the repository.

limit - max number of pages returned by the method. sort - criterion by which the pages are sorted. direction - order of the sorted pages. load_content - option, which specifies whether the content inside the page

will be loaded.

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


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

def list_pages(limit: 0, sort: nil, direction: DIRECTION_ASC, load_content: false)
  wiki.list_pages(
    limit: limit,
    sort: sort,
    direction_desc: direction == DIRECTION_DESC,
    load_content: load_content
  ).map do |page|
    WikiPage.new(self, page)
  end
end

#page_title_and_dir(title) ⇒ Object


162
163
164
165
166
167
168
# File 'app/models/wiki.rb', line 162

def page_title_and_dir(title)
  return unless title

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

#pathObject


41
42
43
# File 'app/models/wiki.rb', line 41

def path
  container.path + '.wiki'
end

#repositoryObject


185
186
187
# File 'app/models/wiki.rb', line 185

def repository
  @repository ||= Gitlab::GlRepository::WIKI.repository_for(container)
end

#repository_storageObject

Raises:

  • (NotImplementedError)

189
190
191
# File 'app/models/wiki.rb', line 189

def repository_storage
  raise NotImplementedError
end

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

def sidebar_entries(limit: Gitlab::WikiPages::MAX_SIDEBAR_PAGES, **options)
  pages = list_pages(**options.merge(limit: limit + 1))
  limited = pages.size > limit
  pages = pages.first(limit) if limited

  [WikiPage.group_by_directory(pages), limited]
end

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


144
145
146
147
148
149
150
151
# File 'app/models/wiki.rb', line 144

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

  wiki.update_page(page.path, title || page.name, format.to_sym, content, commit)
  after_wiki_activity

  true
end

#wikiObject

Returns the Gitlab::Git::Wiki object.


46
47
48
49
50
51
# File 'app/models/wiki.rb', line 46

def wiki
  strong_memoize(:wiki) do
    create_wiki_repository
    Gitlab::Git::Wiki.new(repository.raw)
  end
end

#wiki_base_pathObject


211
212
213
# File 'app/models/wiki.rb', line 211

def wiki_base_path
  web_url(only_path: true).sub(%r{/#{Wiki::HOMEPAGE}\z}, '')
end