Module: WikiActions

Extended by:
ActiveSupport::Concern
Includes:
DiffHelper, Gitlab::Utils::StrongMemoize, PreviewMarkdown, ProductAnalyticsTracking, SafeFormatHelper, SendsBlob, StrongPaginationParams
Included in:
Projects::WikisController
Defined in:
app/controllers/concerns/wiki_actions.rb

Constant Summary collapse

RESCUE_GIT_TIMEOUTS_IN =
%w[show raw edit history diff pages templates].freeze

Constants included from StrongPaginationParams

StrongPaginationParams::PAGINATION_PARAMS

Instance Method Summary collapse

Methods included from StrongPaginationParams

#pagination_params

Methods included from SafeFormatHelper

#safe_format, #tag_pair

Methods included from Gitlab::Tracking::Helpers

#dnt_enabled?, #trackable_html_request?

Methods included from SendsBlob

#send_blob

Methods included from PreviewMarkdown

#preview_markdown

Methods included from DiffHelper

#apply_diff_view_cookie!, #collapsed_diff_url, #conflicts, #conflicts_with_types, #diff_file_blob_raw_path, #diff_file_blob_raw_url, #diff_file_old_blob_raw_path, #diff_file_old_blob_raw_url, #diff_file_stats_data, #diff_line_content, #diff_link_number, #diff_match_line, #diff_nomappinginraw_line, #diff_options, #diff_view, #diffs_expanded?, #editable_diff?, #file_heading_id, #hide_whitespace?, #inline_diff_btn, #mark_inline_diffs, #parallel_diff_btn, #parallel_diff_discussions, #params_with_whitespace, #render_fork_suggestion, #render_overflow_warning?, #show_only_context_commits?, #submodule_diff_compare_data, #submodule_diff_compare_link, #submodule_link, #with_custom_diff_options

Instance Method Details

#createObject

rubocop:disable Gitlab/ModuleWithInstanceVariables



244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'app/controllers/concerns/wiki_actions.rb', line 244

def create
  response = WikiPages::CreateService.new(container: container, current_user: current_user,
    params: wiki_params).execute
  @page = response.payload[:page]

  if response.success?
    handle_action_success :created, @page
  else
    @templates = templates_list

    render 'shared/wikis/edit'
  end
end

#destroyObject

rubocop:disable Gitlab/ModuleWithInstanceVariables



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'app/controllers/concerns/wiki_actions.rb', line 291

def destroy
  return render_404 unless page

  response = WikiPages::DestroyService.new(container: container, current_user: current_user).execute(page)

  if response.success?
    flash[:toast] = _("Wiki page was successfully deleted.")

    redirect_to wiki_path(wiki), status: :found
  else
    @error = response.message
    @templates = templates_list

    render 'shared/wikis/edit'
  end
end

#diffObject

rubocop:disable Gitlab/ModuleWithInstanceVariables



278
279
280
281
282
283
284
285
286
287
# File 'app/controllers/concerns/wiki_actions.rb', line 278

def diff
  return render_404 unless page

  apply_diff_view_cookie!

  @diffs = page.diffs(diff_options)
  @diff_notes_disabled = true

  render 'shared/wikis/diff'
end

#editObject



216
217
218
219
220
# File 'app/controllers/concerns/wiki_actions.rb', line 216

def edit
  @templates = templates_list

  render 'shared/wikis/edit'
end

#git_accessObject

rubocop:enable Gitlab/ModuleWithInstanceVariables



309
310
311
# File 'app/controllers/concerns/wiki_actions.rb', line 309

def git_access
  render 'shared/wikis/git_access'
end

#handle_create_formObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/controllers/concerns/wiki_actions.rb', line 186

def handle_create_form
  title = params[:id]
  if params[:redirected_from] # override the notice if redirected
    redirected_link = helpers.link_to('', "#{wiki_page_path(wiki, params[:redirected_from])}?no_redirect=true")
    flash[:notice] = safe_format(
      s_('Wiki|The page at %{code_start}%{redirected_from}%{code_end} tried to redirect to ' \
        '%{code_start}%{redirected_to}%{code_end}, but it does not exist. You are now ' \
        'editing the page at %{code_start}%{redirected_to}%{code_end}. %{link_start}Edit ' \
        'page at %{code_start}%{redirected_from}%{code_end} instead.%{link_end}'
        ),
      tag_pair(helpers.(:code), :code_start, :code_end),
      tag_pair(redirected_link, :link_start, :link_end),
      redirected_from: params[:redirected_from],
      redirected_to: params[:id]
    )
  end

  @page = build_page(title: title)
  @templates = templates_list

  render 'shared/wikis/edit'

  flash[:notice] = nil if params[:redirected_from]
end

#handle_redirectionObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/controllers/concerns/wiki_actions.rb', line 149

def handle_redirection
  redir = find_redirection(params[:id]) unless params[:redirect_limit_reached] || params[:no_redirect]
  if redir.is_a?(Hash) && redir[:error]
    message = safe_format(
      s_('Wiki|The page at %{code_start}%{redirected_from}%{code_end} redirected too many times. ' \
        'You are now editing the page at %{code_start}%{redirected_from}%{code_end}.'),
      tag_pair(helpers.(:code), :code_start, :code_end),
      redirected_from: params[:id]
    )
    redirect_to(
      "#{wiki_page_path(wiki, params[:id])}?redirect_limit_reached=true",
      status: :found,
      notice: message
    )
  elsif redir
    redirected_from = params[:redirected_from] || params[:id]
    message = safe_format(
      s_('Wiki|The page at %{code_start}%{redirected_from}%{code_end} ' \
        'has been moved to %{code_start}%{redirected_to}%{code_end}.'),
      tag_pair(helpers.(:code), :code_start, :code_end),
      redirected_from: redirected_from,
      redirected_to: redir
    )
    redirect_to(
      "#{wiki_page_path(wiki, redir)}?redirected_from=#{redirected_from}",
      status: :found,
      notice: message
    )
  elsif show_create_form?
    handle_create_form
  elsif wiki.exists?
    render 'shared/wikis/404', status: :not_found
  else
    render 'shared/wikis/empty'
  end
end

#historyObject

rubocop:disable Gitlab/ModuleWithInstanceVariables



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'app/controllers/concerns/wiki_actions.rb', line 260

def history
  if page
    @commits_count = page.count_versions
    @commits = Kaminari.paginate_array(page.versions(page: pagination_params[:page].to_i),
      total_count: page.count_versions)
      .page(pagination_params[:page])

    render 'shared/wikis/history'
  else
    redirect_to(
      wiki_path(wiki),
      notice: _("Page not found")
    )
  end
end

#newObject



74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/concerns/wiki_actions.rb', line 74

def new
  selected_template_slug = request.query_parameters[:selected_template_slug]
  redirect_to wiki_page_path(
    wiki,
    SecureRandom.uuid,
    random_title: true,
    view: 'create',
    selected_template_slug: selected_template_slug
  )
end

#pagesObject

rubocop:disable Gitlab/ModuleWithInstanceVariables



86
87
88
89
90
# File 'app/controllers/concerns/wiki_actions.rb', line 86

def pages
  @wiki_entries = WikiDirectory.group_pages(pages_list)

  render 'shared/wikis/pages'
end

#pages_listObject



92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/concerns/wiki_actions.rb', line 92

def pages_list
  strong_memoize(:pages_list) do
    Kaminari.paginate_array(
      # only include pages not starting with 'templates/'
      wiki
        .list_pages(direction: params[:direction])
        .reject { |page| page.slug.start_with?('templates/', 'uploads/') }
    ).page(pagination_params[:page])
  end
end

#rawObject



211
212
213
214
# File 'app/controllers/concerns/wiki_actions.rb', line 211

def raw
  response.headers['Content-Type'] = 'text/plain'
  render plain: page.raw_content
end

#showObject

#show handles a number of scenarios:

  • If id matches a WikiPage, then show the wiki page.
  • If id is a file in the wiki repository, then send the file.
  • If we know the user wants to create a new page with the given id, then display a create form.
  • Otherwise show the empty wiki page and invite the user to create a page.

rubocop:disable Gitlab/ModuleWithInstanceVariables



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/concerns/wiki_actions.rb', line 131

def show
  if page
    set_encoding_error unless valid_encoding?

    # Assign vars expected by MarkupHelper
    @ref = params[:version_id]
    @path = page.path
    @templates = templates_list

    render 'shared/wikis/show'
  elsif file_blob
    # This is needed by [GitLab JH](https://gitlab.com/gitlab-jh/gitlab/-/issues/247)
    send_wiki_file_blob(wiki, file_blob)
  else
    handle_redirection
  end
end

#templatesObject



114
115
116
117
118
119
# File 'app/controllers/concerns/wiki_actions.rb', line 114

def templates
  @wiki_entries_count = templates_list.total_count
  @wiki_entries = WikiDirectory.group_pages(templates_list, templates: true)

  render 'shared/wikis/templates'
end

#templates_listObject



103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/concerns/wiki_actions.rb', line 103

def templates_list
  strong_memoize(:templates_list) do
    Kaminari.paginate_array(
      # only include pages starting with 'templates/'
      wiki
        .list_pages(direction: params[:direction])
        .select { |page| page.slug.start_with?('templates/') }
    ).page(pagination_params[:page])
  end
end

#updateObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'app/controllers/concerns/wiki_actions.rb', line 222

def update
  return render('shared/wikis/empty') unless can?(current_user, :create_wiki, container)

  response = WikiPages::UpdateService.new(
    container: container,
    current_user: current_user,
    params: wiki_params
  ).execute(page)
  @page = response.payload[:page]

  if response.success?
    handle_action_success :updated, @page
  else
    @error = response.message
    @templates = templates_list

    render 'shared/wikis/edit'
  end
end