Class: RWikiBot

Inherits:
Object
  • Object
show all
Includes:
Pages, RWBErrors, RWBUtilities
Defined in:
lib/rwikibot.rb

Overview

This is the main bot object. The goal is to represent every API method in some form here, and then write seperate, cleaner scripts in individual bot files utilizing this framework. Basically, this is an include at best.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from RWBUtilities

#is_redirect?, #logged_in?, #make_request, #make_unique, #meets_version_requirement, #raw_call, #version

Constructor Details

- (RWikiBot) initialize(username = 'rwikibot',, password = '',, api_path = 'http://www.rwikibot.net/wiki/api.php',, domain = '',, login = false)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rwikibot.rb', line 38

def initialize(username='rwikibot', password='', api_path='http://www.rwikibot.net/wiki/api.php', domain='', =false)
  @config = Hash.new

  @config = {
   'username'  => username,
   'password'  => password,
   'api_path'  => api_path,
   'domain'    => domain,
   'cookies'   => "",
   'logged_in' => false,
   'uri'       => URI.parse(api_path)
  }

  @config['api_version'] = version.to_f

  if 
    
  end
end

Instance Attribute Details

- (Object) config (readonly)

Returns the value of attribute config



36
37
38
# File 'lib/rwikibot.rb', line 36

def config
  @config
end

Instance Method Details

- (Object) all_pages(options = nil)

This will return a list of all pages in a given namespace. It returns a list of pages in with the normalized title and page ID, suitable for usage elsewhere. Accepts all parameters from the API in Hash form. Default is namespace => 0, which is just plain pages. Nothing 'special'.

Raises:



107
108
109
110
111
112
113
114
# File 'lib/rwikibot.rb', line 107

def all_pages(options = nil)
  raise VersionTooLowError unless meets_version_requirement(1,9) 
  # This will get all pages. Limits vary based on user rights of the Bot. Set to bot.
  post_me = {'list' => 'allpages', 'apnamespace' => '0', 'aplimit' => '5000'}
  post_me.merge!(options) if options
  allpages_result = make_request('query', post_me)
  allpages_result.fetch('allpages')['p']
end

- (Object) contributions(parameters)



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rwikibot.rb', line 166

def contributions(parameters)
  parameters = parameters.map do |param, value|
    param = param.to_s
    param = "uc" + param if param !~ /\Auc/
    [param, value.to_s]
  end
  parameters += [["list", "usercontribs"]]
  parameters = Hash[*parameters.flatten]
  result = make_request('query', parameters).fetch('usercontribs')
  case result['item']
  when Array
    result['item']
  when nil
    []
  else
    [result['item']]
  end
end

- (Object) log_events(options = nil)

This will reutrn a list of the most recent log events. Useful for bots who want to validate log events, or even just a notify bot that checks for events and sends them off.

Raises:



139
140
141
142
143
144
# File 'lib/rwikibot.rb', line 139

def log_events(options=nil)
  raise VersionTooLowError unless meets_version_requirement(1,11)
  post_me = {"list" => "logevents"}
  post_me.merge!(options) if options
  make_request('query', post_me).fetch('logevents').fetch('item')
end

- (Object) login

This is the method that will allow the bot to log in to the wiki. Its not always necessary, but bots need to log in to save changes or retrieve watchlists.

Raises:



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

def 
  raise VersionTooLowError unless meets_version_requirement(0,0)

  post_me = {'lgname'=>@config.fetch('username'),'lgpassword'=>@config.fetch('password')}
  if @config.has_key?('domain') && (@config.fetch('domain') != nil)
    post_me['lgdomain'] = @config.fetch('domain')
  end
   = make_request('login', post_me)

  if ['result'] == "NeedToken"
    token = ['token']
    post_me['lgtoken'] = token
     = make_request('login', post_me)
  end

  # Now we need to changed some @config stuff, specifically that we're
  # logged in and the variables of that This will also change the
  # make_request, but I'll comment there
  if ['result'] == "Success"
    # All lg variables are directly from API and stored in config that way
    @config['logged_in']    = true
    @config['lgusername']   = .fetch('lgusername')
    @config['lguserid']     = .fetch('lguserid')
    @config['lgtoken']      = .fetch('lgtoken')
    @config['_session']     = .fetch('sessionid')
    @config['cookieprefix'] = .fetch('cookieprefix')

    true
  else
    # puts "Error logging in. Error was: "
    raise LoginError, "#{['result']}: #{['details']}"
  end
end

- (Object) page(title = ''))

Use Page to create a new page object that you can then manipulate. You could create a page on it's own, but if you do, be sure to pass your bot along with the title, otherwise you won't get access to the super-fun make_request object that is pretty much required.



99
100
101
# File 'lib/rwikibot.rb', line 99

def page(title='')
  Page.new(self, title)
end

- (Object) recent_changes(options = nil)

This method will return Wiki-wide recent changes, almost as if looking at the Special page Recent Changes. But, in this format, a bot can handle it. Also we're using the API. And bots can't read.

Raises:



129
130
131
132
133
134
# File 'lib/rwikibot.rb', line 129

def recent_changes(options=nil)
  raise VersionTooLowError unless meets_version_requirement(1,10)
  post_me = {"list" => "recentchanges", 'rclimit' => '5000'}
  post_me.merge!(options) if options
  make_request('query' , post_me).fetch('recentchanges').fetch('rc')
end

- (Object) revisions(parameters)



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rwikibot.rb', line 185

def revisions(parameters)
  parameters = parameters.map do |param, value|
    param = param.to_s
    if param != "titles" and param !~ /\Arv/
      param = "rv" + param
    end
    if value.is_a? Array
      value = value.map { |v| v.to_s }.join("|")
    else
      value = value.to_s
    end
    [param, value]
  end
  parameters += [["prop", "revisions"]]
  parameters = Hash[*parameters.flatten]
  result = make_request('query', parameters)
  result
end

- (Object) site_info(siprop = 'general'))

This is the only meta method. It will return site information. I chose not to allow it to specify, and it will only return all known properties.



148
149
150
151
152
153
154
155
# File 'lib/rwikibot.rb', line 148

def site_info(siprop='general')
  #raise VersionTooLowError unless meets_version_requirement(1,9)
  post_me = {"meta" => "siteinfo" , "siprop" => siprop}
  siteinfo_result = make_request('query', post_me)
  siprop == 'general' ?
    siteinfo_result.fetch('general') :
    siteinfo_result.fetch('namespaces').fetch('ns')
end

- (Object) user_info(uiprop = nil)

Get information about the current user

Raises:



158
159
160
161
162
163
164
# File 'lib/rwikibot.rb', line 158

def (uiprop=nil)
  raise VersionTooLowError unless meets_version_requirement(1,11)
  post_me = {"meta" => "userinfo" }
  post_me['uiprop'] =  uiprop unless uiprop.nil?

  make_request('query',post_me).fetch('userinfo')
end

- (Object) watchlist(options = nil)

This method will get the watchlist for the bot's MediaWiki username. This is really onlu useful if you want the bot to watch a specific list of pages, and would require the bot maintainer to login to the wiki as the bot to set the watchlist.

Raises:



120
121
122
123
124
125
126
# File 'lib/rwikibot.rb', line 120

def watchlist(options=nil)
  raise VersionTooLowError unless meets_version_requirement(1,10)
  raise NotLoggedInError unless logged_in?
  post_me = {'list'=>'watchlist'}
  post_me.merge!(options) if options
  make_request('query', post_me).fetch('watchlist').fetch('item')
end