Class: User

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, site = nil) ⇒ User

Returns a new instance of User.



5
6
7
8
# File 'lib/rbmediawiki/user.rb', line 5

def initialize(name = nil, site = nil)
    @username = name.gsub(" ","_")
    @site = site
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/rbmediawiki/user.rb', line 4

def name
  @name
end

Instance Method Details

#block(expiry, reason, anononly = true, nocreate = true, autoblock = true, noemail = false, allowusertalk = true, reblock = false) ⇒ Object

blocks the user with the given reason the params are the usual block options



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rbmediawiki/user.rb', line 12

def block(expiry, reason, anononly = true, nocreate = true, autoblock = true, noemail = false, allowusertalk = true, reblock = false)
    #require login
    @site.
    result = @site.query_prop_info(@username, nil, 'block') 
    token = result['query']['pages']['page']['blocktoken']
    result = @site.block(@username, token, nil, expiry, reason, anononly, nocreate, autoblock, noemail, nil, allowusertalk, reblock)
    if result.key?('error')
        raise RbykimediaError, "#{@username}: "+result['error']['code']
    else
        return true
    end
end

#get_usercontribs(uclimit = 500, ucstart = nil, ucnamespace = nil, ucdir = "newer") ⇒ Object

get user contributions returns false if there aren’t any



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rbmediawiki/user.rb', line 72

def get_usercontribs(uclimit = 500, ucstart = nil, ucnamespace = nil, ucdir="newer")
    uccontinue = nil
    ucs = Array.new
    loop {
        result = @site.query_list_usercontribs(nil, uclimit, ucstart, nil, uccontinue, @username, nil, ucdir, ucnamespace)
        if !result['query']['usercontribs'].has_key?('item')
            raise NoUser
        end
        items = result['query']['usercontribs']['item']
        if items.is_a? Array
            ucs = ucs + items
        else
            ucs.push(items)
        end
        if result.key?('query-continue')
            ucstart = result['query-continue']['usercontribs']['ucstart']
        else
            break
        end
    }
    return ucs
end

#infoObject

info about the user



61
62
63
64
65
66
67
68
# File 'lib/rbmediawiki/user.rb', line 61

def info
    result = @site.query_list_users(nil, "blockinfo|groups|editcount|registration|emailable", @username)
    if result.key?('error')
        raise RbykimediaError, "#{@username}: "+result['error']['code']
    else
        return result
    end
end

#rollback(since = nil, namespace = nil) ⇒ Object

rollbacks (reverts) all edits by the user since a given time. “since” is a timestamp. Default will rollback everything. “namespace”, if set, restricts the rollback to the given namespace



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rbmediawiki/user.rb', line 98

def rollback(since = nil, namespace = nil)
    contribs = get_usercontribs(nil, since, nil)
    array_c = Array.new
    puts array_c
    contribs.each{|c| array_c.push(c['title'])}
    array_c.uniq!
    puts array_c
    array_c.each{|p| 
        page = Page.new(p, @site)
        begin
        page.rollback(@username, "desde aquí", false)
        rescue RbykimediaError => error
            puts error
        end
    }
end

#unblock(reason) ⇒ Object

unblocks the user



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rbmediawiki/user.rb', line 26

def unblock(reason)
    #require login
    @site.
    result = @site.query_prop_info(@username, nil, 'unblock') 
    token = result['query']['pages']['page']['unblocktoken']
    result = @site.unblock(nil, @username, token, nil, reason)
    if result.key?('error')
        raise RbykimediaError, "#{@username}: "+result['error']['code']
    else
        return true
    end
end

#write_email(subject, text, ccme = nil) ⇒ Object

write an email to the user



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rbmediawiki/user.rb', line 46

def write_email(subject, text, ccme = nil)
    #require login
    @site.
    result = @site.query_prop_info(@username, nil, 'email') 
    puts result
    token = result['query']['pages']['page']['emailtoken']
    result = @site.emailuser(@username, subject, text, token, ccme) 
    if result.key?('error')
        raise RbykimediaError, "#{@username}: "+result['error']['code']
    else
        return true
    end
end

#write_msg(msg, summary = nil) ⇒ Object

write a message in the user’s talk page



40
41
42
43
# File 'lib/rbmediawiki/user.rb', line 40

def write_msg(msg, summary = nil)
    page = Page.new("User talk:"+@username, @site)
    return page.append(msg, summary, false)
end