Class: Riki::Page

Inherits:
Base
  • Object
show all
Defined in:
lib/riki/page.rb

Overview

Represents a MediaWiki page. Only the latest revision of a page is considered.

Constant Summary

Constants inherited from Base

Base::DEFAULT_OPTIONS, Base::HEADERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_request, cache_key, cookieprefix, cookieprefix=, cookies, find_by_id, login, parse_response

Constructor Details

#initialize(title) ⇒ Page

Returns a new instance of Page.



66
67
68
# File 'lib/riki/page.rb', line 66

def initialize(title)
  @title = title
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



64
65
66
# File 'lib/riki/page.rb', line 64

def content
  @content
end

#idObject

Returns the value of attribute id.



64
65
66
# File 'lib/riki/page.rb', line 64

def id
  @id
end

#last_modifiedObject

Returns the value of attribute last_modified.



64
65
66
# File 'lib/riki/page.rb', line 64

def last_modified
  @last_modified
end

#namespaceObject

Returns the value of attribute namespace.



64
65
66
# File 'lib/riki/page.rb', line 64

def namespace
  @namespace
end

#titleObject

Returns the value of attribute title.



64
65
66
# File 'lib/riki/page.rb', line 64

def title
  @title
end

Class Method Details

.find_by_title(titles) ⇒ Object



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
# File 'lib/riki/page.rb', line 11

def find_by_title(titles)
  titles = [titles] unless titles.kind_of?(Array) # always treat titles as array

  results = {}

  # TODO Transform the requested title to its normalized form. Maybe double-cache or alias?
  # <normalized><n from="ISO_639-2" to="ISO 639-2" /></normalized>

  # find cached pages
  titles.each{|title|
    cached = Riki::Base.cache.read(cache_key("page_#{title}"))
    results[title] = cached if cached
  }

  # Check _in one coarse-grained API call which cached pages are still current
  if results.any?
    api_request({'action' => 'query', 'prop' => 'revisions', 'rvprop' => 'timestamp', 'titles' => results.keys.join('|')}).first.find('/m:api/m:query/m:pages/m:page').each{|page|
      last_modified = DateTime.strptime(page.find_first('m:revisions/m:rev')['timestamp'], '%Y-%m-%dT%H:%M:%S%Z')
      title = page['title']
      titles.delete(title) if last_modified <= results[title].last_modified
    }
  end

  return results.values if titles.empty? # no titles asked for or all results cached and current

  api_request({'action' => 'query', 'prop' => 'revisions', 'rvprop' => 'content|timestamp', 'titles' => titles.join('|')}).first.find('/m:api/m:query/m:pages/m:page').each{|page|
    validate!(page)
    p = Page.new(page['title'])

    p.id = page['pageid'].to_i
    p.namespace = page['ns']

    rev = page.find_first('m:revisions/m:rev')
    p.content = rev.content
    p.last_modified = DateTime.strptime(rev['timestamp'], '%Y-%m-%dT%H:%M:%S%Z')

    Riki::Base.cache.write(cache_key("page_#{p.title}"), p)

    results[p.title] = p
  }

  results.values
end

Instance Method Details

#to_htmlObject

Uses MediaWiki’s parse method to return rendered HTML



73
74
75
# File 'lib/riki/page.rb', line 73

def to_html
  CGI.unescapeHTML(Riki::Base.api_request({'action' => 'parse', 'page' => @title}).first.find('/m:api/m:parse/m:text/text()').first.to_s)
end

#to_sObject

Returns plain text



80
81
82
# File 'lib/riki/page.rb', line 80

def to_s
  @content
end