lib/media_wiki/gateway.rb in mediawiki-gateway-0.3.3 vs lib/media_wiki/gateway.rb in mediawiki-gateway-0.3.4
- old
+ new
@@ -13,10 +13,11 @@
#
# [url] Path to API of target MediaWiki (eg. "http://en.wikipedia.org/w/api.php")
# [options] Hash of options
#
# Options:
+ # [:ignorewarnings] Log API warnings and invalid page titles, instead of aborting with an error.
# [:limit] Maximum number of results returned per search (see http://www.mediawiki.org/wiki/API:Query_-_Lists#Limits), defaults to the MediaWiki default of 500.
# [:loglevel] Log level to use, defaults to Logger::WARN. Set to Logger::DEBUG to dump every request and response to the log.
# [:maxlag] Maximum allowed server lag (see http://www.mediawiki.org/wiki/Manual:Maxlag_parameter), defaults to 5 seconds.
# [:retry_count] Number of times to try before giving up if MediaWiki returns 503 Service Unavailable, defaults to 3 (original request plus two retries).
# [:retry_delay] Seconds to wait before retry if MediaWiki returns 503 Service Unavailable, defaults to 10 seconds.
@@ -58,13 +59,11 @@
#
# Returns content of page as string, nil if the page does not exist.
def get(page_title)
form_data = {'action' => 'query', 'prop' => 'revisions', 'rvprop' => 'content', 'titles' => page_title}
page = make_api_request(form_data).first.elements["query/pages/page"]
- if ! page or page.attributes["missing"]
- nil
- else
+ if valid_page? page
page.elements["revisions/rev"].text || ""
end
end
# Render a MediaWiki page as HTML
@@ -117,11 +116,18 @@
def create(title, content, options={})
form_data = {'action' => 'edit', 'title' => title, 'text' => content, 'summary' => (options[:summary] || ""), 'token' => get_token('edit', title)}
form_data['createonly'] = "" unless options[:overwrite]
make_api_request(form_data)
end
-
+
+ # Edit page
+ #
+ # Same options as create, but always overwrites existing pages (and creates them if they don't exist already).
+ def edit(title, content, options)
+ create(title, content, {:overwrite => true}.merge(options))
+ end
+
# Move a page to a new title
#
# [from] Old page name
# [to] New page name
# [options] Hash of additional options
@@ -315,11 +321,11 @@
#
# Returns true if the page is a redirect, false if it is not or the page does not exist.
def redirect?(page_title)
form_data = {'action' => 'query', 'prop' => 'info', 'titles' => page_title}
page = make_api_request(form_data).first.elements["query/pages/page"]
- !!(page and page.attributes["redirect"])
+ !!(valid_page?(page) and page.attributes["redirect"])
end
# Requests image info from MediaWiki. Follows redirects.
#
# _file_name_or_page_id_ should be either:
@@ -362,17 +368,19 @@
form_data['titles'] = "File:#{file_name_or_page_id}"
end
xml, dummy = make_api_request(form_data)
page = xml.elements["query/pages/page"]
- if ! page or page.attributes["missing"]
- nil
- elsif xml.elements["query/redirects/r"]
- # We're dealing with redirect here.
- image_info(page.attributes["pageid"].to_i, options)
+ if valid_page? page
+ if xml.elements["query/redirects/r"]
+ # We're dealing with redirect here.
+ image_info(page.attributes["pageid"].to_i, options)
+ else
+ page.elements["imageinfo/ii"].attributes
+ end
else
- page.elements["imageinfo/ii"].attributes
+ nil
end
end
# Download _file_name_. Returns file contents. All options are passed to
# #image_info however options['iiprop'] is forced to url. You can still
@@ -525,12 +533,31 @@
if doc.elements["error"]
code = doc.elements["error"].attributes["code"]
info = doc.elements["error"].attributes["info"]
raise "API error: code '#{code}', info '#{info}'"
end
- if doc.elements["warnings"] and !@options[:ignorewarnings]
- raise "API warning: #{doc.elements["warnings"].children.map {|e| e.text}.join(", ")}"
+ if doc.elements["warnings"]
+ warning("API warning: #{doc.elements["warnings"].children.map {|e| e.text}.join(", ")}")
end
doc
+ end
+
+ def valid_page?(page)
+ return false unless page
+ return false if page.attributes["missing"]
+ if page.attributes["invalid"]
+ warning("Invalid title '#{page.attributes["title"]}'")
+ else
+ true
+ end
+ end
+
+ def warning(msg)
+ if @options[:ignorewarnings]
+ log.warn(msg)
+ return false
+ else
+ raise msg
+ end
end
end
end