Skip to content

Commit

Permalink
Merge branch 'bzr/golem' of /Users/distler/Sites/code/instiki
Browse files Browse the repository at this point in the history
  • Loading branch information
distler committed Jun 12, 2020
2 parents fb7bb30 + 068ec5d commit 6fce470
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
25 changes: 18 additions & 7 deletions lib/chunks/include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,30 @@

class Include < WikiChunk::WikiReference

INCLUDE_PATTERN = /\[\[!include\s+([^\]\s][^\]]*?)\s*\]\]/i
INCLUDE_PATTERN = /\[\[!include\s+([^\]\s:][^\]]*?:)?([^\]\s][^\]]*?)\s*\]\]/i
def self.pattern() INCLUDE_PATTERN end

def initialize(match_data, content)
super
@page_name = match_data[1].strip
@web_name = match_data[1] ? match_data[1].chop.strip : @content.web.name
@page_name = match_data[2].strip
rendering_mode = content.options[:mode] || :show
add_to_include_list
@unmask_text = get_unmask_text_avoiding_recursion_loops(rendering_mode)
@ref_web = Web.find_by_name(@web_name)
if @ref_web.password.nil? or @ref_web == @content.web
@unmask_text = get_unmask_text_avoiding_recursion_loops(rendering_mode)
else
@unmask_text = "Access to #{@web_name}:#{@page_name} forbidden."
end
end

private


# the referenced page
def refpage
@ref_web.page(@page_name)
end

def get_unmask_text_avoiding_recursion_loops(rendering_mode)
if refpage
return "<em>Recursive include detected: #{@content.page_name} " +
Expand Down Expand Up @@ -53,16 +64,16 @@ def get_unmask_text_avoiding_recursion_loops(rendering_mode)

def add_to_include_list
Thread.current[:chunk_included_by] ?
Thread.current[:chunk_included_by].push(@content.page_name) :
Thread.current[:chunk_included_by] = [@content.page_name]
Thread.current[:chunk_included_by].push([@content.web, @content.page_name]) :
Thread.current[:chunk_included_by] = [[@content.web, @content.page_name]]
end

def clear_include_list
Thread.current[:chunk_included_by] = []
end

def self_inclusion(refpage)
if Thread.current[:chunk_included_by].include?(refpage.page.name)
if Thread.current[:chunk_included_by].include?([refpage.page.web, refpage.page.name])
@content.delete_chunk(self)
clear_include_list
else
Expand Down
48 changes: 48 additions & 0 deletions test/functional/wiki_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,18 @@ def test_recursive_include_III
assert_match(/<p>Recursive-include:<\/p>\n\n<p>extra fun<\/p>\n+<p><em>Recursive include detected: Bar \342\206\222 Bar<\/em><\/p>/, r.body.as_utf8)
end

def test_recursive_include_IV
@wiki.write_page('instiki', 'Foo', "Recursive-include:\n\n[[!include wiki1:HomePage]]", Time.now,
Author.new('AnotherAuthor', '127.0.0.2'), x_test_renderer)
@wiki.write_page('wiki1', 'HomePage', "extra fun [[!include Instiki:Foo]]", Time.now,
Author.new('AnotherAuthor', '127.0.0.2'), x_test_renderer)

r = process('show', 'id' => 'HomePage', 'web' => 'wiki1')

assert_response :success
assert_match(/<p>extra fun Recursive-include:<\/p>\n\n<p><em>Recursive include detected: Foo \342\206\222 Foo<\/em><\/p>/, r.body.as_utf8)
end

def test_nonrecursive_include
@wiki.write_page('wiki1', 'Bar', "extra fun\n\n[[HomePage]]", Time.now,
Author.new('AnotherAuthor', '127.0.0.2'), x_test_renderer)
Expand All @@ -1083,6 +1095,42 @@ def test_nonrecursive_include
assert_match(/<p>Nonrecursive-include:<\/p>\n\n<p>extra fun<\/p>\n\n<p><a class='existingWikiWord' href='\/wiki1\/show\/HomePage'>HomePage<\/a><\/p>/, r.body)
end

def test_nonrecursive_include_interweb
@wiki.write_page('instiki', 'Foo', "[[!include wiki1:Bar]]\n\n[[!include wiki1:Bar]]", Time.now,
Author.new('AnotherAuthor', '127.0.0.2'), x_test_renderer)
@wiki.write_page('wiki1', 'Bar', "extra fun\n\n[[HomePage]]", Time.now,
Author.new('AnotherAuthor', '127.0.0.2'), x_test_renderer)
@wiki.write_page('wiki1', 'HomePage', "Nonrecursive-include:\n\n[[!include Instiki:Foo]]", Time.now,
Author.new('AnotherAuthor', '127.0.0.2'), x_test_renderer)

# This shouldn't be here. Detritus from another test?
File.delete(File.join(RAILS_ROOT, 'tmp', 'cache', "wiki1_HomePage.cache"))
r = process('show', 'id' => 'HomePage', 'web' => 'wiki1')

assert_response :success
assert_match(/<p>Nonrecursive-include:<\/p>\n\n<p>extra fun<\/p>\n\n<p><a class='existingWikiWord' href='\/wiki1\/show\/HomePage'>HomePage<\/a><\/p>/, r.body)
end

def test_nonrecursive_include_interweb_forbidden
set_web_property :password, 'pswd'
@wiki.write_page('instiki', 'Bar', "extra fun\n\n[[HomePage]]", Time.now,
Author.new('AnotherAuthor', '127.0.0.2'), x_test_renderer)
@wiki.write_page('wiki1', 'Bar', "Lopsided", Time.now,
Author.new('AnotherAuthor', '127.0.0.2'), x_test_renderer)
@wiki.write_page('instiki', 'Foo', "[[!include wiki1:Bar]]\n\n[[!include Instiki:Bar]]", Time.now,
Author.new('AnotherAuthor', '127.0.0.2'), x_test_renderer)
@wiki.write_page('instiki', 'HomePage', "Nonrecursive-include:\n\n[[!include Instiki:Foo]]", Time.now,
Author.new('AnotherAuthor', '127.0.0.2'), x_test_renderer)

r = process('show', 'id' => 'HomePage', 'web' => 'instiki')

assert_response :success
assert_match(/<p>Nonrecursive-include:<\/p>\n\n<p>Access to wiki1:Bar forbidden.<\/p>\n\n<p>extra fun<\/p>/, r.body)
set_web_property :password, nil

end


def test_divref
@wiki.write_page('wiki1', 'Bar', "+-- \{: .num_lemma #Leftcosetsdisjoint\}\n###### Lem" +
"ma\nLet $H$ be a subgroup of a group $G$, and let $x$ and $y$ be elements\n of $G$" +
Expand Down
22 changes: 19 additions & 3 deletions test/unit/chunks/wiki_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,29 @@ def test_include_strip_spaces
content = "This is a [[!include Sperberg-McQueen \t ]] link with trailing spaces to strip. " +
"This is a [[!include \t Gross-Mende]] link with leading spaces to strip." +
"This is a [[!include Milo Miles ]] link with spaces around it to strip"
recognized_includes = content.scan(Include.pattern).collect { |m| m[0] }
assert_equal ['Sperberg-McQueen', 'Gross-Mende', 'Milo Miles'], recognized_includes
recognized_includes = content.scan(Include.pattern).collect { |m| [m[0], m[1]] }
assert_equal [[nil, 'Sperberg-McQueen'], [nil, 'Gross-Mende'], [nil, 'Milo Miles']], recognized_includes
end

def test_include_strip_spaces_with_a_web
content = "This is a [[!include Boof:Sperberg-McQueen \t ]] link with trailing spaces to strip. " +
"This is a [[!include \t Boof:Gross-Mende]] link with leading spaces to strip." +
"This is a [[!include Boof:Milo Miles ]] link with spaces around it to strip"
recognized_includes = content.scan(Include.pattern).collect { |m| [m[0].chop.strip, m[1]] }
assert_equal [['Boof', 'Sperberg-McQueen'], ['Boof','Gross-Mende'], ['Boof', 'Milo Miles']], recognized_includes
end

def test_include_strip_spaces_with_a_web_with_spaces
content = "This is a [[!include Wild West:Sperberg-McQueen \t ]] link with trailing spaces to strip. " +
"This is a [[!include \t Wild West:Gross-Mende]] link with leading spaces to strip." +
"This is a [[!include Wild West:Milo Miles ]] link with spaces around it to strip"
recognized_includes = content.scan(Include.pattern).collect { |m| [m[0].chop.strip, m[1]] }
assert_equal [['Wild West', 'Sperberg-McQueen'], ['Wild West','Gross-Mende'], ['Wild West', 'Milo Miles']], recognized_includes
end

def test_include_chunk_pattern
content = 'This is a [[!include pagename]] and [[!include WikiWord]] and [[!include x]]but [[blah]]'
recognized_includes = content.scan(Include.pattern).collect { |m| m[0] }
recognized_includes = content.scan(Include.pattern).collect { |m| m[1] }
assert_equal %w(pagename WikiWord x), recognized_includes
end

Expand Down

0 comments on commit 6fce470

Please sign in to comment.