Class: RWikiBot
- Inherits:
-
Object
- Object
- RWikiBot
- 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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#all_pages(options = nil) ⇒ Object
This will return a list of all pages in a given namespace.
-
#initialize(username = 'rwikibot', password = '', api_path = 'http://www.rwikibot.net/wiki/api.php', domain = '', login = false) ⇒ RWikiBot
constructor
A new instance of RWikiBot.
-
#log_events(options = nil) ⇒ Object
This will reutrn a list of the most recent log events.
-
#login ⇒ Object
This is the method that will allow the bot to log in to the wiki.
-
#page(title = '') ⇒ Object
Use Page to create a new page object that you can then manipulate.
-
#recent_changes(options = nil) ⇒ Object
This method will return Wiki-wide recent changes, almost as if looking at the Special page Recent Changes.
-
#site_info(siprop = 'general') ⇒ Object
This is the only meta method.
-
#user_info(uiprop = nil) ⇒ Object
Get information about the current user.
-
#watchlist(options = nil) ⇒ Object
This method will get the watchlist for the bot’s MediaWiki username.
Methods included from RWBUtilities
#is_redirect?, #logged_in?, #make_request, #make_unique, #meets_version_requirement, #raw_call, #version
Constructor Details
#initialize(username = 'rwikibot', password = '', api_path = 'http://www.rwikibot.net/wiki/api.php', domain = '', login = false) ⇒ RWikiBot
Returns a new instance of RWikiBot.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rwikibot.rb', line 37 def initialize(username='rwikibot', password='', api_path='http://www.rwikibot.net/wiki/api.php', domain='', login=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 login login end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
35 36 37 |
# File 'lib/rwikibot.rb', line 35 def config @config end |
Instance Method Details
#all_pages(options = nil) ⇒ Object
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’.
101 102 103 104 105 106 107 108 |
# File 'lib/rwikibot.rb', line 101 def all_pages( = 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!() if allpages_result = make_request('query', post_me) allpages_result.fetch('allpages')['p'] end |
#log_events(options = nil) ⇒ Object
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.
133 134 135 136 137 138 |
# File 'lib/rwikibot.rb', line 133 def log_events(=nil) raise VersionTooLowError unless meets_version_requirement(1,11) post_me = {"list" => "logevents"} post_me.merge!() if make_request('query', post_me).fetch('logevents').fetch('item') end |
#login ⇒ Object
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.
60 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 |
# File 'lib/rwikibot.rb', line 60 def login 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 login_result = make_request('login', post_me) # 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 login_result['result'] == "Success" # All lg variables are directly from API and stored in config that way @config['logged_in'] = true @config['lgusername'] = login_result.fetch('lgusername') @config['lguserid'] = login_result.fetch('lguserid') @config['lgtoken'] = login_result.fetch('lgtoken') @config['_session'] = login_result.fetch('sessionid') @config['cookieprefix'] = login_result.fetch('cookieprefix') true else # puts "Error logging in. Error was: " raise LoginError, "#{login_result['result']}: #{login_result['details']}" end end |
#page(title = '') ⇒ Object
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.
93 94 95 |
# File 'lib/rwikibot.rb', line 93 def page(title='') Page.new(self, title) end |
#recent_changes(options = nil) ⇒ Object
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.
123 124 125 126 127 128 |
# File 'lib/rwikibot.rb', line 123 def recent_changes(=nil) raise VersionTooLowError unless meets_version_requirement(1,10) post_me = {"list" => "recentchanges", 'rclimit' => '5000'} post_me.merge!() if make_request('query' , post_me).fetch('recentchanges').fetch('rc') end |
#site_info(siprop = 'general') ⇒ Object
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.
142 143 144 145 146 147 148 149 |
# File 'lib/rwikibot.rb', line 142 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 |
#user_info(uiprop = nil) ⇒ Object
Get information about the current user
152 153 154 155 156 157 158 |
# File 'lib/rwikibot.rb', line 152 def user_info(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 |
#watchlist(options = nil) ⇒ Object
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.
114 115 116 117 118 119 120 |
# File 'lib/rwikibot.rb', line 114 def watchlist(=nil) raise VersionTooLowError unless meets_version_requirement(1,10) raise NotLoggedInError unless logged_in? post_me = {'list'=>'watchlist'} post_me.merge!() if make_request('query', post_me).fetch('watchlist').fetch('item') end |