Class: Pages::Page

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, title = '') ⇒ Page

Creates a new Page object.



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

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

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



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

def counter
  @counter
end

#lastrevidObject (readonly)

Returns the value of attribute lastrevid.



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

def lastrevid
  @lastrevid
end

#lengthObject (readonly)

Returns the value of attribute length.



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

def length
  @length
end

#missingObject (readonly)

Returns the value of attribute missing.



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

def missing
  @missing
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#newObject (readonly)

Returns the value of attribute new.



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

def new
  @new
end

#titleObject (readonly)

Returns the value of attribute title.



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

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:



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pages.rb', line 66

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

#content(options = nil) ⇒ Object

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.



39
40
41
42
43
44
45
46
# File 'lib/pages.rb', line 39

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

#delete(reason = "Deleted by RWikiBot") ⇒ Object

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.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pages.rb', line 51

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

#embedded_in(options = nil) ⇒ Object

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:



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pages.rb', line 83

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

#exists?Boolean

I used to have an exists method (page_exists), but I got rid of it in 2.0, but I’m bringing it back.

Returns:

  • (Boolean)


28
29
30
31
32
33
34
# File 'lib/pages.rb', line 28

def exists?
  if @missing
    return false
  else
    return true
  end
end

#info(titles) ⇒ Object

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.



97
98
99
100
101
102
103
# File 'lib/pages.rb', line 97

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

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

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?)



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

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

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

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.



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

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

#rollback(summary = "", markbot = true) ⇒ Object

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.



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

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

#save(content, summary = nil, options = nil) ⇒ Object

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.



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

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