Module: MediaWiki::Query::Properties::Contributors

Included in:
MediaWiki::Query::Properties
Defined in:
lib/mediawiki/query/properties/contributors.rb

Instance Method Summary collapse

Instance Method Details

#get_anonymous_contributors_count(title, limit = @query_limit_default) ⇒ Fixnum, Nil (private)

Gets the total number of anonymous contributors for the given page.

Parameters:

  • title (String)

    The page title.

  • limit (Fixnum) (defaults to: @query_limit_default)

    The maximum number of users to get. Defaults to 500 and cannot be greater than that

Returns:

  • (Fixnum)

    The number of anonymous contributors for the page.

  • (Nil)

    If title is not a valid page.

See Also:

Since:

  • 0.8.0



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mediawiki/query/properties/contributors.rb', line 61

def get_anonymous_contributors_count(title, limit = @query_limit_default)
  ret = 0

  get_contributors_response(title, limit) do |_, query|
    pageid = query['pages'].keys.find(MediaWiki::Constants::MISSING_PAGEID_PROC) { |id| id != '-1' }
    return if query['pages'][pageid].key?('missing')
    ret += query['pages'][pageid]['anoncontributors'].to_i
  end

  ret
end

#get_contributors_response(title, limit = @query_limit_default) ⇒ Hash (private)

Gets the parsed response for the contributors property.

Parameters:

  • title (String)

    The page title.

  • limit (Fixnum) (defaults to: @query_limit_default)

    The maximum number of users to get. Defaults to 500 and cannot be greater than that

Returns:

See Also:

Since:

  • 0.8.0



45
46
47
48
49
50
51
52
53
# File 'lib/mediawiki/query/properties/contributors.rb', line 45

def get_contributors_response(title, limit = @query_limit_default)
  params = {
    prop: 'contributors',
    titles: title,
    pclimit: get_limited(limit)
  }

  query(params) { |return_val, query| yield(return_val, query) }
end

#get_logged_in_contributors(title, limit = @query_limit_default) ⇒ Array<String>

Gets the non-anonymous contributors for the given page.

Parameters:

  • title (String)

    The page title.

  • limit (Fixnum) (defaults to: @query_limit_default)

    The maximum number of users to get. Defaults to 500 and cannot be greater than that

Returns:

  • (Array<String>)

    All usernames for the contributors.

See Also:

Since:

  • 0.8.0



30
31
32
33
34
35
36
# File 'lib/mediawiki/query/properties/contributors.rb', line 30

def get_logged_in_contributors(title, limit = @query_limit_default)
  get_contributors_response(title, limit) do |return_val, query|
    pageid = query['pages'].keys.find(MediaWiki::Constants::MISSING_PAGEID_PROC) { |id| id != '-1' }
    return if query['pages'][pageid].key?('missing')
    query['pages'][pageid]['contributors'].each { |c| return_val << c['name'] }
  end
end

#get_total_contributors(title, limit = @query_limit_default) ⇒ Fixnum, Nil

Gets the total amount of contributors for the given page. unless the user is a bot. If the user is a bot, the limit cannot be greater than 5000.

Parameters:

  • title (String)

    The page title.

  • limit (Fixnum) (defaults to: @query_limit_default)

    The maximum number of users to get. Defaults to 500 and cannot be greater than that

Returns:

  • (Fixnum)

    The number of contributors to that page.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



17
18
19
20
21
22
23
# File 'lib/mediawiki/query/properties/contributors.rb', line 17

def get_total_contributors(title, limit = @query_limit_default)
  anon_users = get_anonymous_contributors_count(title, limit)
  users = get_logged_in_contributors(title, limit)

  return if users.nil?
  users.size + anon_users
end