module MediaWiki::Gateway::Query
Public Instance Methods
custom_query(options)
click to toggle source
Make a custom query
- options
-
query options
Returns the REXML::Element object as result
Example:
def creation_time(pagename) res = bot.custom_query(:prop => :revisions, :titles => pagename, :rvprop => :timestamp, :rvdir => :newer, :rvlimit => 1) timestr = res.get_elements('*/*/*/rev')[0].attribute('timestamp').to_s time.parse(timestr) end
# File lib/media_wiki/gateway/query.rb 86 def custom_query(options) 87 form_data = {} 88 options.each { |k, v| form_data[k.to_s] = v.to_s } 89 send_request(form_data.merge('action' => 'query')).elements['query'] 90 end
search(key, namespaces = nil, limit = @options[:limit], max_results = @options[:max_results], options = {})
click to toggle source
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
- options
-
Hash of additional options
Returns array of page titles (empty if no matches)
# File lib/media_wiki/gateway/query.rb 16 def search(key, namespaces = nil, limit = @options[:limit], max_results = @options[:max_results], options = {}) 17 titles, offset, form_data = [], 0, options.merge( 18 'action' => 'query', 19 'list' => 'search', 20 'srwhat' => 'text', 21 'srsearch' => key, 22 'srlimit' => limit 23 ) 24 25 if namespaces 26 form_data['srnamespace'] = Array(namespaces).map! { |ns| 27 namespaces_by_prefix[ns] 28 }.compact.join('|') 29 end 30 31 begin 32 form_data['sroffset'] = offset if offset 33 form_data['srlimit'] = [limit, max_results - offset.to_i].min 34 35 res, offset = make_api_request(form_data, '//query-continue/search/@sroffset') 36 37 titles += REXML::XPath.match(res, '//p').map { |x| x.attributes['title'] } 38 end while offset && offset.to_i < max_results.to_i 39 40 titles 41 end
semantic_query(query, params = [], options = {})
click to toggle source
Execute Semantic Mediawiki query
- query
-
Semantic Mediawiki query
- params
-
Array of additional parameters or options, eg. mainlabel=Foo or ?Place (optional)
- options
-
Hash of additional options
Returns result as an HTML string
# File lib/media_wiki/gateway/query.rb 50 def semantic_query(query, params = [], options = {}) 51 unless smw_version = extensions['Semantic MediaWiki'] 52 raise MediaWiki::Exception, 'Semantic MediaWiki extension not installed.' 53 end 54 55 if smw_version.to_f >= 1.7 56 send_request(options.merge( 57 'action' => 'ask', 58 'query' => [query, *params].join('|') 59 )) 60 else 61 send_request(options.merge( 62 'action' => 'parse', 63 'prop' => 'text', 64 'text' => "{{#ask:#{[query, 'format=list', *params].join('|')}}}" 65 )).elements['parse/text'].text 66 end 67 end