Module: MediaWiki::Query::Lists::Categories

Included in:
MediaWiki::Query::Lists
Defined in:
lib/mediawiki/query/lists/categories.rb

Instance Method Summary collapse

Instance Method Details

#get_category_members(category, type = 'page', limit = @query_limit_default) ⇒ Array<String>

Returns an array of all page titles that belong to a given category. By default, it will only get the pages. Files and subcategories can be gotten through #get_subcategories and #get_files_in_category or setting the type parameter.

Parameters:

  • type (String) (defaults to: 'page')

    The type of stuff to get. There are 3 valid values: page, file, and subcat. Separate these with a pipe character, e.g., ‘page|file|subcat’.

  • category (String)

    The category title. Can include “Category:” prefix or not, it works either way.

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

    The maximum number of members to get. Cannot be greater than 500 for users, 5000 for bots.

Returns:

  • (Array<String>)

    All category members until the limit

See Also:

Since:

  • 0.1.0



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mediawiki/query/lists/categories.rb', line 14

def get_category_members(category, type = 'page', limit = @query_limit_default)
  params = {
    list: 'categorymembers',
    cmprop: 'title',
    cmlimit: get_limited(limit),
    cmtype: type
  }

  params[:cmtitle] = category =~ /[Cc]ategory:/ ? category : "Category:#{category}"

  query_ary(params, 'categorymembers', 'title')
end

#get_files_in_category(category, limit = @query_limit_default) ⇒ Array<String>

Gets all of the files in a given category.

Parameters:

  • category (String)

    The category title. Can include “Category:” prefix or not, it works either way.

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

    The maximum number of members to get. Cannot be greater than 500 for users, 5000 for bots.

Returns:

  • (Array<String>)

    All files in the category.

See Also:

Since:

  • 0.9.0



42
43
44
# File 'lib/mediawiki/query/lists/categories.rb', line 42

def get_files_in_category(category, limit = @query_limit_default)
  get_category_members(category, 'file', limit)
end

#get_subcategories(category, limit = @query_limit_default) ⇒ Array<String>

Gets the subcategories of a given category.

Parameters:

  • category (String)

    The category title. Can include “Category:” prefix or not, it works either way.

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

    The maximum number of members to get. Cannot be greater than 500 for users, 5000 for bots.

Returns:

  • (Array<String>)

    All subcategories.

See Also:

Since:

  • 0.9.0



33
34
35
# File 'lib/mediawiki/query/lists/categories.rb', line 33

def get_subcategories(category, limit = @query_limit_default)
  get_category_members(category, 'subcat', limit)
end