Skip to content

Commit

Permalink
💎 🎂 get_category_members only gets pages, but you can use the new typ…
Browse files Browse the repository at this point in the history
…e parameter to get multiple data sets in one request. New methods to get subcats and files in a cat. Bump version.
  • Loading branch information
elifoster committed Dec 19, 2015
1 parent 1d8865f commit 019be67
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog
## Version 0
### Version 0.9.0
* get_category_members has a new parameter, type, which can be used to get more data in a single result.
* get_category_members no longer gets files and subcategories by default. Use the above to get more data at once.
* New get_subcategories and get_files_in_category for specifically getting files or subcategories.

### Version 0.8.2
* Fix outdated usage of @namespaces variable, causing a NoMethodError on get_random_pages calls.
* Greatly improved documentation.
Expand Down
33 changes: 30 additions & 3 deletions lib/mediawiki/query/lists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,30 @@ def what_links_here(title, limit = 500)
ret
end

# Returns an array of all page titles that belong to a given category.
# 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.
# @param category [String] The category title. It can include "Category:",
# or not, it doesn't really matter because we will add it if it is
# missing.
# @param limit [Int] The maximum number of members to get. Defaults to
# 500, and cannot be greater than that unless the user is a bot.
# If the user is a bot, the limit cannot be greater than 5000.
# @param type [String] 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'.
# @see https://www.mediawiki.org/wiki/API:Categorymembers MediaWiki
# Category Members API Docs
# @since 0.1.0
# @return [Array] All category members until the limit
def get_category_members(category, limit = 500)
def get_category_members(category, limit = 500, type = 'page')
params = {
action: 'query',
list: 'categorymembers',
cmprop: 'title',
cmlimit: get_limited(limit)
cmlimit: get_limited(limit),
cmtype: type
}

if category =~ /[Cc]ategory\:/
Expand All @@ -59,6 +66,26 @@ def get_category_members(category, limit = 500)
ret
end

# Gets the subcategories of a given category.
# @param category [String] See {#get_category_members}
# @param limit [Int] See {#get_category_members}
# @see {#get_category_members}
# @since 0.9.0
# @return [Array<String>] All subcategories.
def get_subcategories(category, limit = 500)
get_category_members(category, limit, 'subcat')
end

# Gets all of the files in a given category.
# @param category [String] See {#get_category_members}
# @param limit [Int] See {#get_category_members}
# @see {#get_category_members}
# @since 0.9.0
# @return [Array<String>] All files in the category.
def get_files_in_category(category, limit = 500)
get_category_members(category, limit, 'file')
end

# Returns an array of random pages titles.
# @param number_of_pages [Int] The number of articles to get.
# Defaults to 1. Cannot be greater than 10 for normal users,
Expand Down
2 changes: 1 addition & 1 deletion mediawiki-butt.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Gem::Specification.new do |s|
s.authors = ['Eli Foster', 'Eric Schneider (xbony2)']
s.name = 'mediawiki-butt'
s.summary = 'Interacting with the MediaWiki API'
s.version = '0.8.2'
s.version = '0.9.0'
s.license = 'CC-BY-NC-ND-4.0'
# Expand on this description eventually.
s.description = <<-EOF
Expand Down

0 comments on commit 019be67

Please sign in to comment.