lib/media_wiki/gateway.rb in mediawiki-gateway-0.1.4 vs lib/media_wiki/gateway.rb in mediawiki-gateway-0.1.5

- old
+ new

@@ -45,11 +45,11 @@ form_data = {'action' => 'query', 'prop' => 'revisions', 'rvprop' => 'content', 'titles' => page_title} page = make_api_request(form_data).elements["query/pages/page"] if ! page or page.attributes["missing"] nil else - page.elements["revisions/rev"].text + page.elements["revisions/rev"].text || "" end end # Render a MediaWiki page as HTML # @@ -74,11 +74,11 @@ if parsed.attributes["revid"] != '0' rendered = parsed.elements["text"].text.gsub(/<!--(.|\s)*?-->/, '') # OPTIMIZE: unifiy the keys in +options+ like symbolize_keys! but w/o if options["linkbase"] or options[:linkbase] linkbase = options["linkbase"] || options[:linkbase] - rendered = rendered.gsub(/\shref="\/wiki\/(.*)"/, ' href="' + linkbase + '/wiki/\1"') + rendered = rendered.gsub(/\shref="\/wiki\/([\w\(\)_\-\.%\d:,]*)"/, ' href="' + linkbase + '/wiki/\1"') end if options["noeditsections"] or options[:noeditsections] rendered = rendered.gsub(/<span class="editsection">\[.+\]<\/span>/, '') end if options["noimages"] or options[:noimages] @@ -422,15 +422,19 @@ end end # Get API XML response - # If there are errors, print and bail out + # If there are errors, raise exception # Otherwise return XML root def get_response(res) - doc = REXML::Document.new(res).root + begin + doc = REXML::Document.new(res).root + rescue REXML::ParseException => e + raise "Response is not XML. Are you sure you are pointing to api.php?" + end @log.debug("RES: #{doc}") - raise "API error, response does not contain Mediawiki API XML: #{res}" unless [ "api", "mediawiki" ].include? doc.name + raise "Response does not contain Mediawiki API XML: #{res}" unless [ "api", "mediawiki" ].include? doc.name if doc.elements["error"] code = doc.elements["error"].attributes["code"] info = doc.elements["error"].attributes["info"] raise "API error: code '#{code}', info '#{info}'" end