lib/media_wiki/gateway.rb in mediawiki-gateway-0.5.1 vs lib/media_wiki/gateway.rb in mediawiki-gateway-0.5.2
- old
+ new
@@ -31,10 +31,11 @@
:logdevice => STDERR,
:loglevel => Logger::WARN,
:maxlag => 5,
:retry_count => 3,
:retry_delay => 10,
+ :max_results => 500
}
@options = default_options.merge(options)
@wiki_url = url
@log = Logger.new(@options[:logdevice])
@log.level = @options[:loglevel]
@@ -323,15 +324,16 @@
# Get a list of pages with matching content in given namespaces
#
# [key] Search key
# [namespaces] Array of namespace names to search (defaults to main only)
# [limit] Maximum number of hits to ask for (defaults to 500; note that Wikimedia Foundation wikis allow only 50 for normal users)
+ # [max_results] Maximum total number of results to return
#
# Returns array of page titles (empty if no matches)
- def search(key, namespaces=nil, limit=@options[:limit])
+ def search(key, namespaces=nil, limit=@options[:limit], max_results=@options[:max_results])
titles = []
- offset = nil
+ offset = 0
in_progress = true
form_data = { 'action' => 'query',
'list' => 'search',
'srwhat' => 'text',
@@ -342,13 +344,14 @@
namespaces = [ namespaces ] unless namespaces.kind_of? Array
form_data['srnamespace'] = namespaces.map! do |ns| namespaces_by_prefix[ns] end.join('|')
end
begin
form_data['sroffset'] = offset if offset
+ form_data['srlimit'] = [limit, max_results - offset.to_i].min
res, offset = make_api_request(form_data, '//query-continue/search/@sroffset')
titles += REXML::XPath.match(res, "//p").map { |x| x.attributes["title"] }
- end while offset
+ end while offset && offset.to_i < max_results.to_i
titles
end
# Get a list of users
#
@@ -377,11 +380,11 @@
# [options] Optional hash of options, eg. { 'ucnamespace' => 4 }. See http://www.mediawiki.org/wiki/API:Usercontribs
#
# Returns array of hashes containing the "item" attributes defined here: http://www.mediawiki.org/wiki/API:Usercontribs
def contributions(user, count = nil, options = {})
result = []
- ucstart = nil
+ ucstart = options[:ucstart] or nil
begin
limit = [count, @options[:limit]].compact.min
form_data = options.merge(
{'action' => 'query',
'list' => 'usercontribs',
@@ -472,9 +475,39 @@
form_data = {'action' => 'query', 'prop' => 'info', 'titles' => page_title}
page = make_api_request(form_data).first.elements["query/pages/page"]
!!(valid_page?(page) and page.attributes["redirect"])
end
+ # Get image list for given article[s]. Follows redirects.
+ #
+ # _article_or_pageid_ is the title or pageid of a single article
+ # _imlimit_ is the maximum number of images to return (defaults to 200)
+ #
+ # Example:
+ # images = mw.images('Gaborone')
+ # _images_ would contain ['File:Gaborone at night.jpg', 'File:Gaborone2.png', ...]
+
+ def images(article_or_pageid, imlimit = 200)
+ form_data = {'action' => 'query', 'prop' => 'images', 'imlimit' => imlimit, 'redirects' => true}
+ case article_or_pageid
+ when Fixnum
+ form_data['pageids'] = article_or_pageid
+ else
+ form_data['titles'] = article_or_pageid
+ end
+ xml, dummy = make_api_request(form_data)
+ page = xml.elements["query/pages/page"]
+ if valid_page? page
+ if xml.elements["query/redirects/r"]
+ # We're dealing with redirect here.
+ images(page.attributes["pageid"].to_i, imlimit)
+ else
+ imgs = REXML::XPath.match(page, "images/im").map { |x| x.attributes["title"] }
+ end
+ else
+ nil
+ end
+ end
# Requests image info from MediaWiki. Follows redirects.
#
# _file_name_or_page_id_ should be either:
# * a file name (String) you want info about without File: prefix.
# * or a Fixnum page id you of the file.