Class: Pages::Page

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

Overview

This class defines a MediaWiki page, which allows you to perform actions related to a page, such as to retrieve an existing page or to create a new one.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Page) initialize(bot, title = ''))

Creates a new Page object. It expects an RWikiBot instance and a title.



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

def initialize(bot, title='')
  @bot = bot
  #puts @bot.config
  
  info = info(title)
  @title      = info['title']
  @namespace  = info['ns'].to_i
  @new        = info.has_key?('new')
  @length     = info['length'].to_i
  @counter    = info ['counter'].to_i
  @lastrevid  = info['lastrevid'].to_i
  @missing    = info.has_key?('missing')
end

Instance Attribute Details

- (Object) counter (readonly)

Returns the value of attribute counter



11
12
13
# File 'lib/pages.rb', line 11

def counter
  @counter
end

- (Object) lastrevid (readonly)

Returns the value of attribute lastrevid



11
12
13
# File 'lib/pages.rb', line 11

def lastrevid
  @lastrevid
end

- (Object) length (readonly)

Returns the value of attribute length



11
12
13
# File 'lib/pages.rb', line 11

def length
  @length
end

- (Object) missing (readonly)

Returns the value of attribute missing



11
12
13
# File 'lib/pages.rb', line 11

def missing
  @missing
end

- (Object) namespace (readonly)

Returns the value of attribute namespace



11
12
13
# File 'lib/pages.rb', line 11

def namespace
  @namespace
end

- (Object) new (readonly)

Returns the value of attribute new



11
12
13
# File 'lib/pages.rb', line 11

def new
  @new
end

- (Object) title (readonly)

Returns the value of attribute title



11
12
13
# File 'lib/pages.rb', line 11

def title
  @title
end

Instance Method Details

This method fetches any article that links to the article given in 'title'. Returned in alphabetical order.

Raises:



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

def backlinks (titles, options = nil)
  raise VersionTooLowError unless meets_version_requirement(1,9)

  post_me = {'list' => 'backlinks', 'titles' => "#{title}" }

  post_me.merge!(options) if options

  backlinks_result = make_request('query', post_me)

  backlinks_result.success? ?
    backlinks_result.get_result.fetch('backlinks') :
    backlinks_result.get_message
end

- (Object) content(options = nil)

This will get only the content of the article. It is a modification of revisions to specifically pull the content. I thought it would be useful.



36
37
38
39
40
41
42
43
# File 'lib/pages.rb', line 36

def content(options=nil)
  post_me = {'prop' => 'revisions', 'titles' => @title, 'rvprop' => 'content'}

  post_me.merge!(options) if options

  revisions_result = @bot.make_request('query', post_me )
  revisions_result.fetch('pages').fetch('page').fetch('revisions').fetch('rev')
end

- (Object) delete(reason = "Deleted by RWikiBot"))

If you have to ask what this method does, don't use it. Seriously, use with caution - this method does not have a confirmation step, and deleted (while restorable) are immediate.



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

def delete(reason="Deleted by RWikiBot")
  raise RWBErrors::VersionTooLowError unless @bot.meets_version_requirement(1,12)
  raise RWBErrors::NotLoggedInError unless @bot.logged_in?

  post_me = {
    'title'     => @title ,
    'token'     => get_token('delete') ,
    'reason'    => reason
  }

  @bot.make_request('delete', post_me)
end

- (Object) embedded_in(options = nil)

This method pulls any page that includes the template requested. Please note - the template must be the full name, like "Template:Disputed" or "Template:Awesome".

Raises:



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pages.rb', line 80

def embedded_in(options=nil)
  raise VersionTooLowError unless @bot.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' => 'embeddedin', 'eititle' => @title }

  post_me.merge!(options) if options

  embeddedin_result = @bot.make_request('query', post_me)
  embeddedin_result.fetch('embeddedin').fetch('ei')
end

- (Boolean) exists?

Whether the page already exists



29
30
31
# File 'lib/pages.rb', line 29

def exists?
  !@missing
end

- (Object) info(titles)

I decided to split this up since I wanted to normalize the bot framework as much as possible, or in other words, make it as easy to use as possible. I think the sacrifice of more methods is worth having more English looking code. Its the Ruby way. Info will return information about the page, from namespace to normalized title, last touched, etc.



94
95
96
97
98
99
100
# File 'lib/pages.rb', line 94

def info(titles)
  raise RWBErrors::VersionTooLowError unless @bot.meets_version_requirement(1,9)
  
  post_me = {"prop" => "info", 'titles' => titles}
  info_result = @bot.make_request('query', post_me)
  info_result.fetch('pages').fetch('page')
end

- (Object) move(to, reason, movetalk = true, noredirect = false)

This method will let you move a page from one name to another. A move token is required for this to work. Keep that in mind. (get_token much?)



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

def move(to, reason, movetalk= true, noredirect=false)
  raise RWBErrors::VersionTooLowError unless @bot.meets_version_requirement(1,12)
  raise RWBErrors::NotLoggedInError unless @bot.logged_in?

  post_me = {
    'from'    => @title ,
    'to'      => "#{to}" ,
    'token'   => get_token('move') ,
    'reason'  => "#{reason}" ,
  }

  # These ifs are necessary because they should only be part of post_me if
  # the passed vars are true (which they are by default)
  post_me['movetalk']   = '' if movetalk
  post_me['noredirect'] = '' if noredirect

  @bot.make_request('move', post_me)
end

- (Boolean) new?

Flag whether the page is new or not.



123
124
125
# File 'lib/pages.rb', line 123

def new?
  @new
end

- (Object) protect(protections = 'edit=sysop',, expiry = 'infinite',, reason = '',, cascade = true)

This method is used to protect (and unprotect!) pages. See the API for possible values. By default, it will lock a page to require sysop level privledge and never expire.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/pages.rb', line 130

def protect(protections='edit=sysop', expiry='infinite', reason='', cascade=true)
  raise RWBErrors::VersionTooLowError unless @bot.meets_version_requirement(1,12)
  raise RWBErrors::NotLoggedInError unless @bot.logged_in?

  post_me = {
    'title'         => @title ,
    'token'         => get_token('protect') ,
    'protections'   => protections ,
    'expiry'        => expiry ,
    'reason'        => reason ,
  }
  
  post_me['cascade'] = '' if cascade
  @bot.make_request('protect', post_me)
end

- (Object) rollback(summary = "",, markbot = true)

Rollback does what it says - rolls back an article one version in the wiki. This is a function that requires not only a token, but a previous user.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/pages.rb', line 149

def rollback(summary="", markbot=true)
  raise RWBErrors::VersionTooLowError unless @bot.meets_version_requirement(1,12)
  raise RWBErrors::NotLoggedInError unless @bot.logged_in?

  temp_token = get_token("rollback") # special for rollback. Stupid rollback.
  post_me = {
    'title'     => @title,
    'token'     => temp_token['token'],
    'user'      => temp_token['user'],
    'summary'   => summary
  }

  post_me['markbot'] = '' if markbots
  @bot.make_request('rollback', post_me)
end

- (Object) save(content, summary = nil, options = nil)

This method is used to edit pages. Not much more to say about it. Be sure you're logged in and got a token (get_token). Options is an array (or hash) of extra values allowed by the API.



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

def save(content, summary=nil, options=nil)
  raise RWBErrors::VersionTooLowError unless @bot.meets_version_requirement(1,13)
  raise RWBErrors::NotLoggedInError unless @bot.logged_in?

  post_me = {
    'text'     => "#{content}" ,
    'token'    => get_token("edit") ,
    'title'    => @title ,
    'summary'  => "#{summary}" ,
    'edittime' => Time.now.strftime("%Y%m%d%H%M%S") ,
  }

  post_me.merge!(options) if options

  @bot.make_request('edit', post_me).fetch('result')
end