Module: MediaWiki

Defined in:
lib/media_wiki.rb,
lib/media_wiki/utils.rb,
lib/media_wiki/config.rb,
lib/media_wiki/gateway.rb,
lib/media_wiki/exception.rb

Defined Under Namespace

Classes: APIError, Config, Exception, Gateway, Unauthorized

Constant Summary collapse

VERSION =
"0.6.1"

Class Method Summary collapse

Class Method Details

.get_base_name(title) ⇒ Object

Extract base name. If there are no subpages, return page name.

Examples: get_base_name(“Namespace:Foo/Bar/Baz”) -> “Namespace:Foo” get_base_name(“Namespace:Foo”) -> “Namespace:Foo”

title

Page name string in Wiki format



11
12
13
# File 'lib/media_wiki/utils.rb', line 11

def get_base_name(title)
  title.split('/').first if title
end

.get_path_to_subpage(title) ⇒ Object

Extract path leading up to subpage. If title does not contain a subpage, returns nil.

Examples: get_path_to_subpage(“Namespace:Foo/Bar/Baz”) -> “Namespace:Foo/Bar” get_path_to_subpage(“Namespace:Foo”) -> nil

title

Page name string in Wiki format



22
23
24
25
# File 'lib/media_wiki/utils.rb', line 22

def get_path_to_subpage(title)
  return nil unless title and title.include? '/'
  title.split(/\/([^\/]*)$/).first
end

.get_subpage(title) ⇒ Object

Extract subpage name. If there is no hierarchy above, return page name.

Examples: get_subpage(“Namespace:Foo/Bar/Baz”) -> “Baz” get_subpage(“Namespace:Foo”) -> “Namespace:Foo”

title

Page name string in Wiki format



34
35
36
# File 'lib/media_wiki/utils.rb', line 34

def get_subpage(title)
  title.split('/').last if title
end

.uri_to_wiki(uri) ⇒ Object

Convert URL-ized page name (“getting_there_%26_away”) into Wiki display format page name (“Getting there & away”). Also capitalizes first letter, replaces underscores with spaces and strips out any illegal characters (#<>[]|{}, cf. meta.wikimedia.org/wiki/Help:Page_name#Restrictions).

wiki

Page name string in URL



42
43
44
# File 'lib/media_wiki/utils.rb', line 42

def uri_to_wiki(uri)
  upcase_first_char(CGI.unescape(uri).tr('_', ' ').tr('#<>[]|{}', '')) if uri
end

.versionObject

Return current version of MediaWiki::Gateway



54
55
56
# File 'lib/media_wiki/utils.rb', line 54

def version
  MediaWiki::VERSION
end

.wiki_to_uri(wiki) ⇒ Object

Convert a Wiki page name (“Getting there & away”) to URI-safe format (“Getting_there_%26_away”), taking care not to mangle slashes or colons

wiki

Page name string in Wiki format



49
50
51
# File 'lib/media_wiki/utils.rb', line 49

def wiki_to_uri(wiki)
  wiki.to_s.split('/').map {|chunk| CGI.escape(CGI.unescape(chunk).tr(' ', '_')) }.join('/').gsub('%3A', ':') if wiki
end