Class: MediawikiRobot::EnhancedGateway

Inherits:
MediaWiki::Gateway
  • Object
show all
Defined in:
lib/mediawiki_robot/enhanced_gateway.rb

Instance Method Summary collapse

Instance Method Details

#exists(page_title) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mediawiki_robot/enhanced_gateway.rb', line 103

def exists(page_title)
  # used to be:
  #    return ! get_with_retry(page_title).nil?
  # but that was slower.

  form_data =
    {'action' => 'query',
    'titles'  => page_title,
    'prop'    => 'revisions'}

  res = make_api_request_with_retry(form_data)

  rev_ids = REXML::XPath.match(res, "////rev").map { |x| x.attributes["revid"] }
  return !( rev_ids.nil? or rev_ids.empty? )
end

#get_all_pages_in_category(category_title) ⇒ Object

e.g., ‘Category:Articles_with_hCards’



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mediawiki_robot/enhanced_gateway.rb', line 78

def get_all_pages_in_category(category_title) # e.g., 'Category:Articles_with_hCards'
  form_data =
    {'action' => 'query',
    'list'    => 'categorymembers',
    'cmtitle' => category_title,
    'cmlimit' => '5000'}

  res = make_api_request_with_retry(form_data)

  titles = REXML::XPath.match(res, "//cm").map { |x| x.attributes["title"] }
  return titles
end

#get_page_revision(title) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mediawiki_robot/enhanced_gateway.rb', line 91

def get_page_revision(title)
  form_data =
    {'action' => 'query',
    'titles'  => title,
    'prop'    => 'revisions'}

  res = make_api_request_with_retry(form_data)

  rev_ids = REXML::XPath.match(res, "////rev").map { |x| x.attributes["revid"] }
  return rev_ids[0]
end

#get_with_retry(title) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mediawiki_robot/enhanced_gateway.rb', line 45

def get_with_retry(title)
  res = nil
  while res.nil?
    begin
      res = get(title)
    rescue SocketError
      puts "MediaWiki::Gateway::get_with_retry -- caught SocketError, retrying..."
    rescue Errno::ETIMEDOUT
      puts "MediaWiki::Gateway::get_with_retry -- caught Errno::ETIMEDOUT, retrying..."
    rescue MediaWiki::Exception
      puts "MediaWiki::Gateway::get_with_retry -- caught MediaWiki::Exception, retrying..."
    end
  end

  return res
end

#login(user, pass) ⇒ Object

overrides the usual ‘login’ to provide retries upon timeout



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mediawiki_robot/enhanced_gateway.rb', line 12

def (user, pass)
  done = false
  while not done
    begin
      super(user, pass)
      done = true
    rescue SocketError
      puts "MediaWiki::Gateway::login -- caught SocketError, retrying..."
    rescue Errno::ETIMEDOUT
      puts "MediaWiki::Gateway::login -- caught Errno::ETIMEDOUT, retrying..."
    #rescue MediaWiki::Exception
    #  puts "MediaWiki::Gateway::login -- caught MediaWiki::Exception, retrying..."
    end
  end
end

#make_api_request_with_retry(form_data) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mediawiki_robot/enhanced_gateway.rb', line 28

def make_api_request_with_retry(form_data)
  res = nil
  while res.nil?
    begin
      res = make_api_request(form_data)
    rescue SocketError
      puts "MediaWiki::Gateway::make_api_request_with_retry -- caught SocketError, retrying..."
    rescue Errno::ETIMEDOUT
      puts "MediaWiki::Gateway::make_api_request_with_retry -- caught Errno::ETIMEDOUT, retrying..."
    rescue MediaWiki::Exception
      puts "MediaWiki::Gateway::make_api_request_with_retry -- caught MediaWiki::Exception, retrying..."
    end
  end

  return res
end

#recent_changes(num_changes, end_time) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mediawiki_robot/enhanced_gateway.rb', line 62

def recent_changes(num_changes, end_time)
  form_data =
    {'action' => 'query',
    'list' => 'recentchanges'}
  form_data['rclimit'] = num_changes if !num_changes.nil?
  form_data['rcend'] = end_time.strftime("%Y%m%d%H%M%S") if !end_time.nil?

  res = make_api_request_with_retry(form_data)

  changes = REXML::XPath.match(res, "//rc").map { |x| { :type        => x.attributes["type"], 
                                                        :title       => x.attributes["title"], 
                                                        :timestamp   => x.attributes["timestamp"], 
                                                        :revision_id => x.attributes["revid"] } }
  return changes
end