Class: Page

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

Overview

This class represents a page. It gives methods for dealing with single pages: obtainig the content, putting it, appending content, deleting, etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = nil, site = nil) ⇒ Page

Returns a new instance of Page.



8
9
10
11
12
# File 'lib/rbmediawiki/page.rb', line 8

def initialize(title = nil, site = nil)
    @site = site ? site : Api.new()
    @title = title
    @normtitle = title.gsub(" ","_")
end

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/rbmediawiki/page.rb', line 7

def title
  @title
end

Instance Method Details

#addsection(text, title, minor = false, bot = true) ⇒ Object

adds a section to a page same as #append, but is a section what is appended. title is the title of the new section



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rbmediawiki/page.rb', line 87

def addsection(text, title, minor = false, bot = true)
    #require login
    @site.
    result = @site.query_prop_info(@normtitle, nil, 'edit')    
    token = result['query']['pages']['page']['edittoken']
    result = @site.edit(@normtitle, section, text, token, title, minor, nil, bot) 
    if result.key?('error')
        raise RbmediawikiError, "#{title}: "+result['error']['code']
    else
        return true
    end
end

#append(text, summary = nil, minor = true, bot = true) ⇒ Object

appends texto to a page same as #put, but the text is appended and the previous content preserved



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rbmediawiki/page.rb', line 55

def append(text, summary = nil, minor = true, bot = true)
    #require login
    @site.
    puts text
    result = @site.query_prop_info(@normtitle, nil, 'edit')    
    token = result['query']['pages']['page']['edittoken']
    result = @site.edit(@normtitle, nil, text, token, summary, minor, nil, bot, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, text)
    if result.key?('error')
        raise RbmediawikiError, "#{title}: "+result['error']['code']
    else
        return true
    end
end

#delete(reason = "") ⇒ Object

delete the page. reason : reason for deleting returns true if success, raises NoPage if page doesn’t exist



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rbmediawiki/page.rb', line 145

def delete(reason="")
    @site.
    result = @site.query_prop_info(@normtitle, nil, 'delete') 
    token = result['query']['pages']['page']['deletetoken']
    result = @site.delete(@normtitle, nil, token, reason)
    if result.key?('error')
        raise RbmediawikiError, "#{@title}: "+result['error']['code']
    else
        return true    
    end
end

#getObject

retrieves the content of the page



15
16
17
18
19
20
21
22
# File 'lib/rbmediawiki/page.rb', line 15

def get()
    result = @site.query_prop_revisions(@normtitle, 'content')
    if result.key?('error')
        raise RbmediawikiError, "#{title}: "+result['error']['code']
    else
        return result['query']['pages']['page']['revisions']['rev']
    end
end


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/rbmediawiki/page.rb', line 322

def get_backlinks(bllimit = 500, blnamespace = nil, blredirect = true)
    blcontinue = nil
    bls = Array.new
    loop {
        result = @site.query_list_backlinks(@normtitle, @normtitle, blcontinue, blnamespace, nil, bllimit, blredirect)
        if result['query']['pages']['page'].key?('missing')
            raise NoPage.new(), "Page [[#{@title}]] does not exist"
        end
        if result['query']['backlinks']['bl'].is_a? Array
            bls = bls + result['query']['backlinks']['bl']
        else
            bls.push(result['query']['backlinks']['bl'])
        end
        if result.key?('query-continue')
            blcontinue = result['query-continue']['backlinks']['blcontinue']
        else
            break
        end
    }
    return bls
end

#get_categories(min = nil, cllimit = 500, clshow = nil, sortkey = true) ⇒ Object

gets categories used in a page min is the minimum number of elements to return, cllimit is the number of elements to request from the API in each iteration. The method will request elements until it has at least min elements. clshow can be “hidden” or “!hidden”. Default shows both if sortkey is true will return the sortkey. Default is true



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/rbmediawiki/page.rb', line 291

def get_categories(min = nil, cllimit = 500, clshow = nil, sortkey = true)
    clcontinue = nil
    cls = Array.new
    count = 0

    if sortkey 
        clprop = "sortkey"
    end

    loop {
        result = @site.query_prop_categories(@normtitle, clprop, clshow, cllimit, clcontinue)
        if result['query']['pages']['page'].key?('missing')
            raise NoPage.new(), "Page [[#{@title}]] does not exist"
        end
        page = result['query']['pages']['page']
        if page['categories']['cl'].is_a? Array
            cls = cls + page['categories']['cl']
        else
            cls.push(page['categories']['cl'])
        end

        if result.key?('query-continue')&& min && count < min
            count += lllimit
            clcontinue = result['query-continue']['categories']['clcontinue']
        else
            break
        end
    }
    return cls
end

#get_images(min = nil, imlimit = 500) ⇒ Object

gets image links of a page min is the minimum number of elements to return, imlimit is the number of elements to request from the API in each iteration. The method will request elements until it has at least min elements. returns false if there aren’t any, and raises NoPage if page doesn’t exist



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/rbmediawiki/page.rb', line 258

def get_images(min = nil, imlimit = 500)
    imcontinue = nil
    ims = Array.new
    count = 0
    loop {
        result = @site.query_prop_images(@normtitle, imlimit, imcontinue)
        proc_res = result['query']['pages']['page']
        if proc_res.key?('missing')
            raise NoPage.new(), "Page [[#{@title}]] does not exist"
        end
        if !proc_res.key?('images')
            return nil
        end
        if proc_res['images']['im'].is_a? Array
            ims = ims + proc_res['images']['im']
        else
            ims.push(proc_res['images']['im'])
        end
        if result.key?('query-continue') && min && count < min
            count += lllimit
            imcontinue = result['query-continue']['images']['imcontinue']
        else
            break
        end
    }
    return ims
end

#get_interwikis(min = nil, lllimit = 500) ⇒ Object

get interwiki links min is the minimum number of elements to return, lllimit is the number of elements to request from the API in each iteration. The method will request elements until it has at least min elements. returns false if there aren’t any, and raises NoPage if page doesn’t exist



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/rbmediawiki/page.rb', line 225

def get_interwikis(min = nil, lllimit = 500)
    llcontinue = nil
    iws = Hash.new
    count = 0
    loop {
        result = @site.query_prop_langlinks(@normtitle, lllimit, llcontinue)
        if result['query']['pages']['page'].key?('missing')
            raise NoPage.new(), "Page [[#{title}]] does not exist"
        end
        if !result['query'].key?('interwikis')
            return nil
        end
        if result['query']['interwikis']['iw'].is_a? Array
            iws = iws + result['query']['interwikis']['iw']
        else
            iws.push(result['query']['interwikis']['iw'])
        end
        if result.key?('query-continue') && min && count < min
            count += lllimit
            llcontinue = result['query-continue']['langlinks']['llcontinue']
        else
            break
        end
    }
    return iws
end

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

moves a page

  • reason: reason or summary

  • movetalk: move talk pages too (default->true)

  • noredirect: don’t leave a redirect (default->nil)



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rbmediawiki/page.rb', line 104

def move(to, reason = nil, movetalk = true, noredirect = nil) 
    #require login
    @site.
    result = @site.query_prop_info(@normtitle, nil, 'move') 
    token = result['query']['pages']['page']['movetoken']
    result = @site.move(@normtitle, nil, to, token, reason, movetalk, nil, noredirect)
    if result.key?('error')
        raise RbmediawikiError, "#{title}: "+result['error']['code']
    else
        return true
    end
end

#prepend(text, summary = nil, minor = true, bot = true) ⇒ Object

prepends text to a page same as #put, but the text is prepended and the previous content preserved



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rbmediawiki/page.rb', line 71

def prepend(text, summary = nil, minor = true, bot = true)
    #require login
    @site.
    result = @site.query_prop_info(@normtitle, nil, 'edit')    
    token = result['query']['pages']['page']['edittoken']
    result = @site.edit(@normtitle, nil, text, token, summary, minor, nil, bot, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, text)
    if result.key?('error')
        raise RbmediawikiError, "#{title}: "+result['error']['code']
    else
        return true
    end
end

#protect(reason = nil, expiry = 'infinite', protections = 'edit=sysop|move=sysop') ⇒ Object

protects a page. reason is the reason for the protection expiry is a timescamp (default is infinite). protections is the action and group that can perform that action, separated by pipes. Exapmple “edit=sysop|move=autoconfirmed”.Default is edit=sysop|move=sysop



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rbmediawiki/page.rb', line 122

def protect(reason = nil, expiry = 'infinite', protections = 'edit=sysop|move=sysop') 
    #require login
    @site.
    result = @site.query_prop_info(@normtitle, nil, 'protect') 
    token = result['query']['pages']['page']['protecttoken']
    result = @site.protect(@normtitle, token, protections, expiry, reason)
    if result.key?('error')
        raise RbmediawikiError, "#{title}: "+result['error']['code']
    else
        return true
    end
end

#protected?Boolean

gets info about the protections of a page. Returns an array as for instance => sysop,type => edit,expiry => infinity => sysop,type => move,expiry => infinity

Returns:

  • (Boolean)


206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rbmediawiki/page.rb', line 206

def protected?()
    result = @site.query_prop_info(@normtitle, 'protection')
    if result.key?('error')
        raise RbmediawikiError, "#{@title}: "+result['error']['code']
    end
    if result['query']['pages']['page'].key?('missing')
        raise NoPage, "Page [[#{@title}]] does not exist"
 
    else
        return result['query']['pages']['page']['protection']['pr']
    end
end

#put(text, summary = nil, minor = true, bot = true, password = nil) ⇒ Object

puts the text of a page.

  • text: the new content of the page

  • summary: editting summary

  • minor: is a minor edit? default->true

  • bot: is a bot flagged edit?



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rbmediawiki/page.rb', line 39

def put(text, summary = nil, minor = true, bot = true, password = nil)
    #require login
    @site.(password)
    result = @site.query_prop_info(@normtitle, nil, 'edit') 
    token = result['query']['pages']['page']['edittoken']
    result = @site.edit(@normtitle, nil, text, token, summary, minor, nil, bot)
    if result.key?('error')
        raise RbmediawikiError, "#{title}: "+result['error']['code']
    else
        return true
    end
    puts "content put"
end

#redirect?Boolean

returns false if it is not a redirect, the redirected title if it is

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/rbmediawiki/page.rb', line 25

def redirect?()
    txt = this.get
    if (txt =~ /#REDIRECT\s+\[\[(.*)\]\]/)
        return $1
    else
        return false
    end
end

#rollback(user = nil, summary = nil, markbot = nil) ⇒ Object

rollback (revert) editr by user. Summary can be given



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rbmediawiki/page.rb', line 176

def rollback(user = nil, summary = nil, markbot = nil)
    @site.
    result = @site.query_prop_revisions(@normtitle, nil, nil, nil, nil, nil, nil, nil, user, nil, nil, nil, nil, 'rollback') 
    #Page exists?
    if result['query']['pages']['page'].key?('missing')
        raise NoPage, "Page [[#{@title}]] does not exist"
    end
    #Has user edited this?
    if !result['query']['pages']['page'].key?('revisions')
        raise RbmediawikiError, "#{@title}: No edits by user #{user}"
    end

    #If the user made more than one contribs, this is an array
    #but the token is the same. We only want the token
    if result['query']['pages']['page']['revisions']['rev'].is_a? Array
        token = result['query']['pages']['page']['revisions']['rev'][0]['rollbacktoken']
    else
        token =  result['query']['pages']['page']['revisions']['rev']['rollbacktoken']
    end
    result = @site.rollback(@normtitle, user, token, summary, markbot)
    if result.key?('error')
        raise RbmediawikiError, "#{@title}: "+result['error']['code']
    else
        return true    
    end
end

#semiprotect(reason = nil, expiry = 'infinite') ⇒ Object

semipotects a page. is the same as protect, but default for protections is “edit=autoconfirmed|move=autoconfirmed”



137
138
139
140
# File 'lib/rbmediawiki/page.rb', line 137

def semiprotect(reason = nil, expiry = 'infinite') 
    protect(reason, expiry, 'edit=autoconfirmed|move=autoconfirmed')
    #possible errors: user doesn't have privileges
end

#undelete(reason = "") ⇒ Object

undeletes a page. reason: reason for deleting returns true if success, false if there aren’t deleted revisions



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rbmediawiki/page.rb', line 161

def undelete(reason="")
    @site.
    result = @site.query_list_deletedrevs(@normtitle, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 'token') 
    if result.key?('error')
        raise RbmediawikiError, "#{@title}: "+result['error']['code']
    end
    if !result.has_key?('deletedRevs')
        return false
    end
    token = result['query']['deletedrevs']['page']['token']
    result = @site.undelete(@normtitle, token, reason)
    return true    
end