Module: MediaWiki::Gateway::Site

Included in:
MediaWiki::Gateway
Defined in:
lib/media_wiki/gateway/site.rb

Instance Method Summary collapse

Instance Method Details

#export(page_titles, options = {}) ⇒ Object

Exports a page or set of pages

page_titles

String or array of page titles to fetch

options

Hash of additional options

Returns MediaWiki XML dump



30
31
32
33
34
35
36
37
# File 'lib/media_wiki/gateway/site.rb', line 30

def export(page_titles, options = {})
  send_request(options.merge(
    'action'       => 'query',
    'titles'       => Array(page_titles).join('|'),
    'export'       => nil,
    'exportnowrap' => nil
  ))
end

#extensions(options = {}) ⇒ Object

Get a list of all installed (and registered) extensions

options

Hash of additional options

Returns array of extensions (name => version)



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/media_wiki/gateway/site.rb', line 82

def extensions(options = {})
  res = send_request(options.merge(
    'action' => 'query',
    'meta'   => 'siteinfo',
    'siprop' => 'extensions'
  ))

  REXML::XPath.match(res, '//ext').each_with_object({}) { |extension, extensions|
    name = extension.attributes['name'] || ''
    extensions[name] = extension.attributes['version']
  }
end

#import(xmlfile, options = {}) ⇒ Object

Imports a MediaWiki XML dump

xml

String or array of page names to fetch

options

Hash of additional options

Returns XML array <api><import><page/><page/>… <page revisions=“1”> (or more) means successfully imported <page revisions=“0”> means duplicate, not imported



15
16
17
18
19
20
21
22
# File 'lib/media_wiki/gateway/site.rb', line 15

def import(xmlfile, options = {})
  send_request(options.merge(
    'action'  => 'import',
    'xml'     => File.new(xmlfile),
    'token'   => get_token('import', 'Main Page'), # NB: dummy page name
    'format'  => 'xml'
  ))
end

#namespaces_by_prefix(options = {}) ⇒ Object

Get a list of all known namespaces

options

Hash of additional options

Returns array of namespaces (name => id)



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/media_wiki/gateway/site.rb', line 64

def namespaces_by_prefix(options = {})
  res = send_request(options.merge(
    'action' => 'query',
    'meta'   => 'siteinfo',
    'siprop' => 'namespaces'
  ))

  REXML::XPath.match(res, '//ns').each_with_object({}) { |namespace, namespaces|
    prefix = namespace.attributes['canonical'] || ''
    namespaces[prefix] = namespace.attributes['id'].to_i
  }
end

#siteinfo(options = {}) ⇒ Object

Get the wiki’s siteinfo as a hash. See www.mediawiki.org/wiki/API:Siteinfo.

options

Hash of additional options



42
43
44
45
46
47
48
49
50
# File 'lib/media_wiki/gateway/site.rb', line 42

def siteinfo(options = {})
  res = send_request(options.merge(
    'action' => 'query',
    'meta'   => 'siteinfo'
  ))

  REXML::XPath.first(res, '//query/general')
    .attributes.each_with_object({}) { |(k, v), h| h[k] = v }
end

#version(options = {}) ⇒ Object

Get the wiki’s MediaWiki version.

options

Hash of additional options passed to #siteinfo



55
56
57
# File 'lib/media_wiki/gateway/site.rb', line 55

def version(options = {})
  siteinfo(options).fetch('generator', '').split.last
end