class Infoboxer::MediaWiki::Traits

DSL for defining “traits” for some site.

More docs (and possible refactoring) to follow.

You can look at current [English Wikipedia traits](github.com/molybdenum-99/infoboxer/blob/master/lib/infoboxer/definitions/en.wikipedia.org.rb) definitions in Infoboxer's repo.

Constants

STANDARD_NAMESPACES

See www.mediawiki.org/wiki/Help:Namespaces#Standard_namespaces

Public Class Methods

domain(d) click to toggle source

@private

# File lib/infoboxer/media_wiki/traits.rb, line 28
def domain(d)
  # NB: explicitly store all domains in base Traits class
  Traits.domains.key?(d) and
    fail(ArgumentError, "Domain binding redefinition: #{Traits.domains[d]}")

  Traits.domains[d] = self
end
domains() click to toggle source

@private

# File lib/infoboxer/media_wiki/traits.rb, line 42
def domains
  @domains ||= {}
end
for(domain, &block) click to toggle source

Define traits for some domain. Use it like:

“`ruby MediaWiki::Traits.for 'ru.wikipedia.org' do

templates do
  template '...' do
    # some template definition
  end
end

end “`

Again, you can look at current [English Wikipedia traits](github.com/molybdenum-99/infoboxer/blob/master/lib/infoboxer/definitions/en.wikipedia.org.rb) for example implementation.

# File lib/infoboxer/media_wiki/traits.rb, line 61
def for(domain, &block)
  Traits.domains[domain]&.instance_eval(&block) ||
    Class.new(self, &block).domain(domain)
end
get(domain, site_info = {}) click to toggle source

@private

# File lib/infoboxer/media_wiki/traits.rb, line 37
def get(domain, site_info = {})
  (Traits.domains[domain] || Traits).new(site_info)
end
new(site_info = {}) click to toggle source
# File lib/infoboxer/media_wiki/traits.rb, line 70
def initialize(site_info = {})
  @site_info = site_info
end
templates(&definition) click to toggle source

Define set of templates for current site's traits.

See {Templates::Set} for longer (yet insufficient) explanation.

Expected to be used inside Traits definition block.

# File lib/infoboxer/media_wiki/traits.rb, line 19
def templates(&definition)
  @templates ||= Templates::Set.new

  return @templates unless definition

  @templates.define(&definition)
end

Public Instance Methods

category_namespace() click to toggle source

@private

# File lib/infoboxer/media_wiki/traits.rb, line 88
def category_namespace
  @category_namespace ||= ns_aliases('Category')
end
file_namespace() click to toggle source

@private

# File lib/infoboxer/media_wiki/traits.rb, line 83
def file_namespace
  @file_namespace ||= ns_aliases('File')
end
interwiki?(prefix) click to toggle source
# File lib/infoboxer/media_wiki/traits.rb, line 78
def interwiki?(prefix)
  known_interwikis.key?(prefix)
end
namespace?(prefix) click to toggle source
# File lib/infoboxer/media_wiki/traits.rb, line 74
def namespace?(prefix)
  known_namespaces.include?(prefix)
end
templates() click to toggle source

@private

# File lib/infoboxer/media_wiki/traits.rb, line 93
def templates
  self.class.templates
end

Private Instance Methods

known_interwikis() click to toggle source
# File lib/infoboxer/media_wiki/traits.rb, line 108
def known_interwikis
  @known_interwikis ||=
    if @site_info.empty?
      {}
    else
      @site_info['interwikimap'].map { |iw| [iw['prefix'], iw] }.to_h
    end
end
known_namespaces() click to toggle source
# File lib/infoboxer/media_wiki/traits.rb, line 99
def known_namespaces
  @known_namespaces ||=
    if @site_info.empty?
      STANDARD_NAMESPACES
    else
      (@site_info['namespaces'].values + @site_info['namespacealiases']).map { |n| n['*'] }
    end
end
ns_aliases(base) click to toggle source
# File lib/infoboxer/media_wiki/traits.rb, line 117
def ns_aliases(base)
  return [base] if @site_info.empty?

  main = @site_info['namespaces'].values.detect { |n| n['canonical'] == base }
  [base, main['*']] +
    @site_info['namespacealiases']
    .select { |a| a['id'] == main['id'] }.flat_map { |n| n['*'] }
    .compact.uniq
end