-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathwiki_controller.rb
454 lines (416 loc) · 15.8 KB
/
wiki_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
require 'rss'
class WikiController < ApplicationController
before_action :require_user, only: %i(new create edit update delete replace)
def subdomain
url = "//#{request.host}/wiki/"
case request.subdomain
when 'new-york-city',
'gulf-coast',
'boston',
'espana' then
redirect_to url + request.subdomain
when 'nyc'
redirect_to url + 'new-york-city'
else
redirect_to url
end
end
def show
@node = if params[:lang]
Node.find_wiki(params[:lang] + '/' + params[:id])
else
Node.find_wiki(params[:id])
end
if @node&.has_power_tag('redirect') && Node.where(nid: @node.power_tag('redirect')).exists?
if current_user.nil? || !current_user.can_moderate?
redirect_to Node.find(@node.power_tag('redirect')).path
return
elsif current_user.can_moderate?
flash.now[:warning] = "Only moderators and admins see this page, as it is redirected to <a href='#{Node.find(@node.power_tag('redirect')).path}'>#{Node.find(@node.power_tag('redirect')).title}</a>.
To remove the redirect, delete the tag beginning with 'redirect:'"
end
elsif @node&.has_power_tag('redirect') && Node.where(slug: @node.power_tag('redirect')).exists?
if current_user.nil? || !current_user.can_moderate?
redirect_to Node.find_by(slug: @node.power_tag('redirect')).path
return
elsif current_user.can_moderate?
flash.now[:warning] = "Only moderators and admins see this page, as it is redirected to <a href='#{Node.find_by(slug: @node.power_tag('redirect')).path}'>#{Node.find_by(slug: @node.power_tag('redirect')).title}</a>.
To remove the redirect, delete the tag beginning with 'redirect:'"
end
if @node&.has_power_tag('abtest') && !Node.where(nid: @node.power_tag('abtest')).empty?
if current_user.nil? || !current_user.can_moderate?
if Random.rand(2) == 0
redirect_to Node.find(@node.power_tag('abtest')).path
return
end
elsif current_user.can_moderate?
flash.now[:warning] = "Only moderators and admins see this page, as it is redirected to #{Node.find(@node.power_tag('abtest')).title} roughly around 50% of the time.
To remove this behavior, delete the tag beginning with 'abtest:'"
end
end
end
# if request.path != @node.path && request.path != '/wiki/' + @node.nid.to_s
# return redirect_to @node.path, :status => :moved_permanently
# end
return if check_and_redirect_node(@node)
if [email protected]? # it's a place page!
@tags = @node.tags
@tags += [Tag.find_by(name: params[:id])] if Tag.find_by(name: params[:id])
else # it's a new wiki page!
@title = I18n.t('wiki_controller.new_wiki_page')
if current_user
new
else
flash[:warning] = I18n.t('wiki_controller.pages_does_not_exist')
redirect_to '/login'
end
end
unless @title # the page exists
if @node.status == 0
flash[:warning] = I18n.t('wiki_controller.page_moderated_as_spam')
redirect_to '/wiki'
end
@tagnames = @tags.collect(&:name)
set_sidebar :tags, @tagnames, videos: true
@wikis = Tag.find_pages(@node.slug_from_path, 30) if @node.has_tag('chapter') || @node.has_tag('tabbed:wikis')
impressionist(@node, 'show', unique: [:ip_address])
@revision = @node.latest # needed for template, so it can be used for past revisions too in the "revision" action
@title = @revision.title
end
@unpaginated = true
end
# display a revision, raw
def raw
response.headers['Content-Type'] = 'text/plain; charset=utf-8'
render plain: Revision.find(params[:id]).body
end
def edit
@node = if params[:lang]
Node.find_wiki(params[:lang] + '/' + params[:id])
else
Node.find_wiki(params[:id])
end
if @node.has_tag('locked') && !current_user.can_moderate?
flash[:warning] = "This page is <a href='/wiki/power-tags#Locking'>locked</a>, and only <a href='/wiki/moderators'>moderators</a> can edit it."
redirect_to @node.path
end
if ((Time.now.to_i - @node.latest.timestamp) < 5.minutes.to_i) && @node.latest.author.uid != current_user.uid
flash.now[:warning] = I18n.t('wiki_controller.someone_clicked_edit_5_minutes_ago')
end
# we could do this...
# @node.locked = true
# @node.save
@title = I18n.t('wiki_controller.editing', title: @node.title).html_safe
@tags = @node.tags
end
def new
@node = Node.new
if params[:n] && !params[:body] # use another node body as a template
node = Node.find(params[:n])
params[:body] = node.latest.body if node&.latest
end
@tags = []
if params[:id]
flash.now[:notice] = I18n.t('wiki_controller.page_does_not_exist_create')
title = params[:id].tr('-', ' ')
@related = Node.limit(10)
.order('node.nid DESC')
.where('type = "page" AND node.status = 1 AND (node.title LIKE ? OR node_revisions.body LIKE ?)', '%' + title + '%', '%' + title + '%')
.includes(:revision)
.references(:node_revisions)
tag = Tag.find_by(name: params[:id]) # add page name as a tag, too
@tags << tag if tag
@related += Tag.find_nodes_by_type(@tags.collect(&:name), 'page', 10)
end
render template: 'wiki/edit'
end
def create
if current_user.drupal_user.status == 1
# we no longer allow custom urls, just titles which are parameterized automatically into urls
# slug = params[:title].parameterize
# slug = params[:id].parameterize if params[:id] != "" && !params[:id].nil?
# slug = params[:url].parameterize if params[:url] != "" && !params[:url].nil?
saved, @node, @revision = Node.new_wiki(uid: current_user.uid,
title: params[:title],
body: params[:body])
if saved
flash[:notice] = I18n.t('wiki_controller.wiki_page_created')
if params[:main_image] && params[:main_image] != ''
img = Image.find params[:main_image]
img.nid = @node.id
img.save
end
redirect_to @node.path
else
if params[:main_image] && Image.find_by(id: params[:main_image])
@main_image = Image.find_by(id: params[:main_image]).path
end
if params[:n] && !params[:body] # use another node body as a template
node = Node.find(params[:n])
params[:body] = node.body if node
end
flash[:error] = "Please enter both body and title"
render template: 'editor/wikiRich'
end
else
flash.keep[:error] = I18n.t('wiki_controller.you_have_been_banned').html_safe
redirect_to '/logout'
end
end
def update
@node = Node.find(params[:id])
@revision = @node.new_revision(uid: current_user.uid,
title: params[:title],
body: params[:body])
if @node.has_tag('locked') && !current_user.can_moderate?
flash[:warning] = "This page is <a href='/wiki/power-tags#Locking'>locked</a>, and only <a href='/wiki/moderators'>moderators</a> can update it."
redirect_to @node.path
elsif @revision.valid?
ActiveRecord::Base.transaction do
@revision.save
@node.vid = @revision.vid
# update vid (version id) of main image
if @node.drupal_main_image && params[:main_image].nil?
i = @node.drupal_main_image
i.vid = @revision.vid
i.save
end
@node.title = @revision.title
# save main image
if params[:main_image] && params[:main_image] != ''
begin
img = Image.find params[:main_image]
unless img.nil?
img.nid = @node.id
@node.main_image_id = img.id
img.save
end
rescue
end
end
@node.save
end
flash[:notice] = I18n.t('wiki_controller.edits_saved')
redirect_to @node.path
else
flash[:error] = I18n.t('wiki_controller.edit_could_not_be_saved')
render action: :edit
# redirect_to "/wiki/edit/"[email protected]
end
end
def delete
@node = Node.find(params[:id])
if current_user && current_user.admin?
@node.destroy
flash[:notice] = I18n.t('wiki_controller.wiki_page_deleted')
redirect_to '/dashboard'
else
flash[:error] = I18n.t('wiki_controller.only_admins_delete_pages')
redirect_to @node.path
end
end
def revert
revision = Revision.find params[:id]
node = revision.parent
if current_user && current_user.can_moderate?
new_rev = revision.dup
new_rev.timestamp = DateTime.now.to_i
if new_rev.save!
flash[:notice] = I18n.t('wiki_controller.wiki_page_reverted')
else
flash[:error] = I18n.t('wiki_controller.problem_reverting')
end
else
flash[:error] = I18n.t('wiki_controller.moderators_admin_delete_pages')
end
redirect_to node.path
end
# wiki pages which have a root URL, like /about
# also just redirect anything else matching /____ to /wiki/____
def root
@node = Node.find_by_path(params[:id])
return if check_and_redirect_node(@node)
if @node
@revision = @node.latest
@title = @revision.title
@tags = @node.tags
@tagnames = @tags.collect(&:name)
render template: 'wiki/show'
else
# redirects any uncaught requests to example.com/______ to /wiki/____
redirect_to '/wiki/' + params[:id]
end
end
def revisions
@node = Node.find_wiki(params[:id])
if @node
@revisions = @node.revisions
@revisions = @revisions.where(status: 1).page(params[:page]).per_page(20) unless current_user && current_user.can_moderate?
@title = I18n.t('wiki_controller.revisions_for', title: @node.title).html_safe
@tags = @node.tags
@paginated = true unless current_user && current_user.can_moderate?
else
flash[:error] = I18n.t('wiki_controller.invalid_wiki_page')
end
end
def revision
@node = Node.find_wiki(params[:id])
@tags = @node.tags
@tagnames = @tags.collect(&:name)
@unpaginated = true
@is_revision = true
set_sidebar :tags, @tagnames, videos: true
@revision = Revision.find_by_nid_and_vid(@node.id, params[:vid])
if @revision.nil?
flash[:error] = I18n.t('wiki_controller.revision_not_found')
redirect_to action: 'revisions'
elsif @revision.status == 1 || current_user && current_user.can_moderate?
@title = I18n.t('wiki_controller.revisions_for', title: @revision.title).html_safe
render template: 'wiki/show'
else
flash[:error] = I18n.t('wiki_controller.revision_has_been_moderated').html_safe
redirect_to @node.path
end
end
def diff
@a = Revision.find_by(vid: params[:a])
@b = Revision.find_by(vid: params[:b])
if @a.body == @b.body
render plain: I18n.t('wiki_controller.lead_image_or_title_change').html_safe
else
render partial: 'wiki/diff'
end
end
def index
@title = I18n.t('wiki_controller.wiki')
sort_param = params[:sort]
order_string = 'node_revisions.timestamp DESC'
if sort_param == 'title'
order_string = 'node_revisions.title ASC'
elsif sort_param == 'last_edited'
order_string = 'node_revisions.timestamp DESC'
elsif sort_param == 'edits'
order_string = 'drupal_node_revisions_count DESC'
elsif sort_param == 'page_views'
order_string = 'views DESC'
elsif sort_param == 'likes'
order_string = 'cached_likes DESC'
end
@wikis = Node.includes(:revision)
.references(:node_revisions)
.group('node_revisions.nid')
.order(order_string)
.where("node_revisions.status = 1 AND node.status = 1 AND (type = 'page' OR type = 'tool' OR type = 'place')")
.page(params[:page])
@paginated = true
end
def stale
@title = I18n.t('wiki_controller.wiki')
@wikis = Node.includes(:revision)
.references(:node_revisions)
.group('node_revisions.nid')
.order('node_revisions.timestamp ASC')
.where("node_revisions.status = 1 AND node.status = 1 AND (type = 'page' OR type = 'tool' OR type = 'place')")
.page(params[:page])
@paginated = true
render template: 'wiki/index'
end
def popular
@title = I18n.t('wiki_controller.popular_wiki_pages')
@wikis = Node.limit(40)
.joins(:revision)
.group('node_revisions.nid')
.order('node_revisions.timestamp DESC')
.where("node.status = 1 AND node_revisions.status = 1 AND node.nid != 259 AND (type = 'page' OR type = 'tool' OR type = 'place')")
.sort_by(&:totalviews).reverse
render template: 'wiki/index'
end
def liked
@title = I18n.t('wiki_controller.well_liked_wiki_pages')
@wikis = Node.limit(40)
.order('node.cached_likes DESC')
.where("status = 1 AND nid != 259 AND (type = 'page' OR type = 'tool' OR type = 'place') AND cached_likes >= 0")
render template: 'wiki/index'
end
# replace subsection of wiki body
def replace
@node = Node.find(params[:id])
if params[:before] && params[:after]
# during round trip, strings are getting "\r\n" newlines converted to "\n",
# so we're ensuring they remain "\r\n"; this may vary based on platform, unfortunately
before = params[:before].gsub("\n", "\r\n")
after = params[:after]#.gsub( "\n", "\r\n")
if output = @node.replace(before, after, current_user)
flash[:notice] = 'New revision created with your additions.' unless request.xhr?
else
flash[:error] = 'There was a problem replacing that text.' unless request.xhr?
end
else
flash[:error] = "You must specify 'before' and 'after' terms to replace content in a wiki page."
end
if request.xhr?
render json: output
else
redirect_to @node.path
end
end
def techniques
redirect_to '/methods', status: 302
end
def methods
@nodes = Node.where(status: 1, type: ['page'])
.where('term_data.name = ?', 'method')
.includes(:revision, :tag)
.references(:node_revision)
.order('node_revisions.timestamp DESC')
# deprecating the following in favor of javascript implementation in /app/assets/javascripts/methods.js
if params[:topic]
nids = @nodes.collect(&:nid) || []
@notes = Node.where(status: 1, type: ['page'])
.where('node.nid IN (?)', nids)
.where('(type = "note" OR type = "page" OR type = "map") AND node.status = 1 AND (node.title LIKE ? OR node_revisions.title LIKE ? OR node_revisions.body LIKE ? OR term_data.name = ?)',
'%' + params[:topic] + '%',
'%' + params[:topic] + '%',
'%' + params[:topic] + '%',
params[:topic])
.includes(:revision, :tag)
.references(:node_revision, :term_data)
.order('node_revisions.timestamp DESC')
end
if params[:topic]
nids = @nodes.collect(&:nid) || []
@nodes = Node.where(status: 1, type: ['page'])
.where('node.nid IN (?)', nids)
.where('(type = "note" OR type = "page" OR type = "map") AND node.status = 1 AND (node.title LIKE ? OR node_revisions.title LIKE ? OR node_revisions.body LIKE ? OR term_data.name = ?)',
'%' + params[:topic] + '%',
'%' + params[:topic] + '%',
'%' + params[:topic] + '%',
params[:topic])
.includes(:revision, :tag)
.references(:node_revision, :term_data)
.order('node_revisions.timestamp DESC')
end
@unpaginated = true
@topics = [
'agriculture',
'drinking-water',
'fracking',
'indoor-air',
'chemicals',
'industry',
'land-use',
'land-change',
'mining',
'oil-and-gas',
'transportation',
'urban-planning',
'sensors',
'community-organizing'
]
render template: 'wiki/methods'
end
def comments
show
render :show
end
end