Permalink
Browse files

Patch from Michael Witrant:

* Add url to exception message
* NoEditFormFound case
* Article::what_links_here
* Article::fast_what_links_here using Regexp, not REXML
* Support for de & fr language


git-svn-id: svn://78.47.249.61/ruby-mediawiki/trunk@70 ba9c31aa-a806-0410-9a81-9f13d15ee83b
  • Loading branch information...
1 parent e85fa63 commit f119dae51ace5b43eda5e2c3347b1d264ba33bad astro committed Oct 23, 2007
Showing with 48 additions and 9 deletions.
  1. +9 −2 lib/mediawiki.rb
  2. +38 −6 lib/mediawiki/article.rb
  3. +1 −1 lib/mediawiki/minibrowser.rb
View
@@ -40,15 +40,21 @@ class Wiki
# The URL-Path to index.php (without index.php) as given
# to Wiki#initialize
attr_reader :url
+
+
+ ##
+ # The language of the wiki (used for special pages)
+ # Supported: "fr", "de"
+ # Default: "de"
+ attr_accessor :language
##
# Initialize a new Wiki instance.
# url:: [String] URL-Path to index.php (without index.php), may containt <tt>user:password</tt> combination.
# user:: [String] If not nil, log in with that MediaWiki username (see Wiki#login)
# password:: [String] If not nil, log in with that MediaWiki password (see Wiki#login)
# loglevel:: [Integer] Loglevel, default is to log all messages >= Logger::WARN = 2
- def initialize(url, user = nil, password = nil, loglevel = Logger::WARN)
-
+ def initialize(url, user = nil, password = nil, loglevel = Logger::WARN, language = "de")
if ENV['MEDIAWIKI_DEBUG']
MediaWiki::logger.level = Logger::DEBUG
else
@@ -57,6 +63,7 @@ def initialize(url, user = nil, password = nil, loglevel = Logger::WARN)
@url = URI.parse( url.match(/\/$/) ? url : url + '/' )
@browser = MiniBrowser.new(@url)
+ @language = language
login( user, password ) if user and password
end
View
@@ -106,6 +106,9 @@ def reload
parse @wiki.browser.get_content("#{@wiki.article_url(full_name, @section)}&action=edit")
end
+ class NoEditFormFound < RuntimeError
+ end
+
def parse(html)
doc = to_rexml( html )
# does not work for MediaWiki 1.4.x and is always the same name you ask for under 1.5.x
@@ -123,9 +126,13 @@ def parse(html)
# wpEditToken might be missing, that's ok
end
else
- # the article is probably locked and you do not have sufficient privileges
- @text = doc.elements['//textarea'].text
- @read_only = true
+ if doc.elements['//textarea']
+ # the article is probably locked and you do not have sufficient privileges
+ @text = doc.elements['//textarea'].text
+ @read_only = true
+ else
+ raise NoEditFormFound, "Error while parsing result, no edit form found"
+ end
end
end
@@ -144,12 +151,13 @@ def submit(summary, minor_edit=false, watch_this=false, retries=10)
data['wpWatchthis'] = 'on' if watch_this
begin
parse @wiki.browser.post_content("#{@wiki.article_url(full_name, @section)}&action=submit", data)
- rescue NoMethodError
+ rescue NoEditFormFound
# This means, we havn't got the preview page, but the posted article
# So everything is Ok, but we must reload the edit page here, to get
# a new wpEditToken and wpEdittime
reload
return
+ rescue Net::HTTPInternalServerError
end
unless @wp_edittoken.to_s == '' and @wp_edittime.to_s == ''
@@ -189,16 +197,40 @@ def unprotect(reason)
end
##
+ # "what links here" url for this article
+ def what_links_here_url(count = nil)
+ case @wiki.language
+ when "de"
+ page = "Spezial:Whatlinkshere"
+ else
+ page = "Special:Whatlinkshere"
+ end
+ url = @wiki.article_url("#{page}/#{full_name}")
+ url << "&limit=#{count}" if count
+ end
+
+
+ ##
# What articles link to this article?
# result:: [Array] of [String] Article names
- def what_links_here
+ def what_links_here(count = nil)
res = []
- links = to_rexml(@wiki.browser.get_content(@wiki.article_url("Spezial:Whatlinkshere/#{full_name}")))
+ url = what_links_here_url(count)
+ links = to_rexml(@wiki.browser.get_content(url))
links.each_element('//div[@id="bodyContent"]//ul/li/a') { |a|
res << a.attributes['title']
}
res
end
+
+ def fast_what_links_here(count = nil)
+ res = []
+ url = what_links_here_url(count)
+ content = @wiki.browser.get_content(url)
+ content.scan(%r{<li><a href=".+?" title="(.+?)">.+?</a>.+?</li>}).flatten.map { |title|
+ REXML::Text.unnormalize(title)
+ }
+ end
protected
def to_rexml( html )
@@ -115,7 +115,7 @@ def post_content(url, data)
end
return get_content(response['Location'])
else
- raise "Unknown Response: #{response.inspect}"
+ raise "Unknown Response on #{url}: #{response.inspect}"
end
end
end

0 comments on commit f119dae

Please sign in to comment.