Class: Rmwiki

Inherits:
Object
  • Object
show all
Defined in:
lib/rmwiki.rb

Defined Under Namespace

Classes: ExtendedWikiPage, SimpleWikiPage

Instance Method Summary collapse

Constructor Details

#initialize(wiki_root, username, password) ⇒ Rmwiki

ここで使われたユーザーはRedMineの方の操作ログに名前が残るよ



9
10
11
12
13
# File 'lib/rmwiki.rb', line 9

def initialize(wiki_root, username, password)
  @wiki_root = wiki_root
  @http_client = HTTPClient.new
   username, password
end

Instance Method Details

#all_pagesObject



39
40
41
42
43
44
45
# File 'lib/rmwiki.rb', line 39

def all_pages
  response = @http_client.get(File.join(@wiki_root, 'index.json'))
  check_status_code response
  JSON.parse(response.content)['wiki_pages'].map { |raw_obj|
    SimpleWikiPage.new(raw_obj)
  }
end

#exist?(page_title) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/rmwiki.rb', line 75

def exist? page_title
  self.page(page_title) != nil
end

#fetch_login_url_from_wiki_rootObject



125
126
127
128
129
130
# File 'lib/rmwiki.rb', line 125

def 
  url = URI.parse(@wiki_root)
  doc = Nokogiri::HTML(@http_client.get_content(@wiki_root))
  url.path = doc.css('.login').first.attributes['href'].value
  url.to_s
end

#get_default_parent_id(nokogiri_doc) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/rmwiki.rb', line 89

def get_default_parent_id nokogiri_doc
  elem = nokogiri_doc.css('#wiki_page_parent_id option[selected="selected"]').first
  if elem
    elem.attributes['value'].value.to_i
  else
    ''
  end
end

#get_page_title_id_map(nokogiri_doc) ⇒ Object



82
83
84
85
86
87
# File 'lib/rmwiki.rb', line 82

def get_page_title_id_map nokogiri_doc
  page_id_and_anme = nokogiri_doc.css('#wiki_page_parent_id option').
    map { |i| i.text =~ /(?:.*» )?(.+)/; [$1, i.attributes['value'].value.to_i] }.
    select { |page_title, id| page_title }
  Hash[page_id_and_anme]
end

#page(page_title) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/rmwiki.rb', line 65

def page page_title
  response = @http_client.get(File.join(@wiki_root, URI::escape(page_title) + '.json'))

  if response.header.status_code == 200
    ExtendedWikiPage.new(JSON.parse(response.content)['wiki_page'])
  else
    nil
  end
end

#rename(before_title, after_title, parent_title = :default) ⇒ Object

スペースの入ったページ名などダメなページ名があるので返り値として移動後のページ名を返す



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
# File 'lib/rmwiki.rb', line 81

def rename before_title, after_title, parent_title = :default
  def get_page_title_id_map nokogiri_doc
    page_id_and_anme = nokogiri_doc.css('#wiki_page_parent_id option').
      map { |i| i.text =~ /(?:.*» )?(.+)/; [$1, i.attributes['value'].value.to_i] }.
      select { |page_title, id| page_title }
    Hash[page_id_and_anme]
  end

  def get_default_parent_id nokogiri_doc
    elem = nokogiri_doc.css('#wiki_page_parent_id option[selected="selected"]').first
    if elem
      elem.attributes['value'].value.to_i
    else
      ''
    end
  end

  rename_form_url = File.join(@wiki_root, before_title, '/rename')
  doc = Nokogiri::HTML(@http_client.get_content(rename_form_url))
  authenticity_token = get_authenticity_token(doc)
  parent_id = if parent_title == :default
                get_default_parent_id(doc)
              elsif parent_title == nil
                ''
              else
                page_title_to_id = get_page_title_id_map(doc)
                unless page_title_to_id.has_key? parent_title
                  raise '指定された親ページが存在しません'
                end
                page_title_to_id[parent_title]
              end

  res = @http_client.post(rename_form_url, {
    'authenticity_token'                 => authenticity_token,
    'wiki_page[title]'                   => after_title,
    'wiki_page[redirect_existing_links]' => 0,
    'wiki_page[parent_id]'               => parent_id
  })
  check_status_code res, 302
  File.basename(res.header['Location'].first)
end

#sub_page_tree(all_pages, page_title) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rmwiki.rb', line 48

def sub_page_tree all_pages, page_title
  def all_pages.select_child_pages parent_title
    self.select { |page| page.parent_title == parent_title }
  end
  pages = all_pages.select_child_pages(page_title).map { |page|
    page.instance_variable_set(:@children, sub_page_tree(all_pages, page.title))
    def page.children
      @children
    end
    page
  }
  Hash[pages.map { |page| [page.title, page] }]
end

#treeObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rmwiki.rb', line 47

def tree
  def sub_page_tree all_pages, page_title
    def all_pages.select_child_pages parent_title
      self.select { |page| page.parent_title == parent_title }
    end
    pages = all_pages.select_child_pages(page_title).map { |page|
      page.instance_variable_set(:@children, sub_page_tree(all_pages, page.title))
      def page.children
        @children
      end
      page
    }
    Hash[pages.map { |page| [page.title, page] }]
  end

  sub_page_tree(self.all_pages, nil)
end