Skip to content
This repository has been archived by the owner on Mar 9, 2024. It is now read-only.

Commit

Permalink
Patch from Michael Witrant:
Browse files Browse the repository at this point in the history
* 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
astro committed Oct 23, 2007
1 parent e85fa63 commit f119dae
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
11 changes: 9 additions & 2 deletions lib/mediawiki.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,15 +40,21 @@ class Wiki
# The URL-Path to index.php (without index.php) as given # The URL-Path to index.php (without index.php) as given
# to Wiki#initialize # to Wiki#initialize
attr_reader :url 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. # Initialize a new Wiki instance.
# url:: [String] URL-Path to index.php (without index.php), may containt <tt>user:password</tt> combination. # 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) # 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) # 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 # 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'] if ENV['MEDIAWIKI_DEBUG']
MediaWiki::logger.level = Logger::DEBUG MediaWiki::logger.level = Logger::DEBUG
else else
Expand All @@ -57,6 +63,7 @@ def initialize(url, user = nil, password = nil, loglevel = Logger::WARN)


@url = URI.parse( url.match(/\/$/) ? url : url + '/' ) @url = URI.parse( url.match(/\/$/) ? url : url + '/' )
@browser = MiniBrowser.new(@url) @browser = MiniBrowser.new(@url)
@language = language


login( user, password ) if user and password login( user, password ) if user and password
end end
Expand Down
44 changes: 38 additions & 6 deletions lib/mediawiki/article.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def reload
parse @wiki.browser.get_content("#{@wiki.article_url(full_name, @section)}&action=edit") parse @wiki.browser.get_content("#{@wiki.article_url(full_name, @section)}&action=edit")
end end


class NoEditFormFound < RuntimeError
end

def parse(html) def parse(html)
doc = to_rexml( 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 # does not work for MediaWiki 1.4.x and is always the same name you ask for under 1.5.x
Expand All @@ -123,9 +126,13 @@ def parse(html)
# wpEditToken might be missing, that's ok # wpEditToken might be missing, that's ok
end end
else else
# the article is probably locked and you do not have sufficient privileges if doc.elements['//textarea']
@text = doc.elements['//textarea'].text # the article is probably locked and you do not have sufficient privileges
@read_only = true @text = doc.elements['//textarea'].text
@read_only = true
else
raise NoEditFormFound, "Error while parsing result, no edit form found"
end
end end
end end


Expand All @@ -144,12 +151,13 @@ def submit(summary, minor_edit=false, watch_this=false, retries=10)
data['wpWatchthis'] = 'on' if watch_this data['wpWatchthis'] = 'on' if watch_this
begin begin
parse @wiki.browser.post_content("#{@wiki.article_url(full_name, @section)}&action=submit", data) 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 # 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 # So everything is Ok, but we must reload the edit page here, to get
# a new wpEditToken and wpEdittime # a new wpEditToken and wpEdittime
reload reload
return return
rescue Net::HTTPInternalServerError
end end


unless @wp_edittoken.to_s == '' and @wp_edittime.to_s == '' unless @wp_edittoken.to_s == '' and @wp_edittime.to_s == ''
Expand Down Expand Up @@ -188,17 +196,41 @@ def unprotect(reason)
result = @wiki.browser.post_content("#{@wiki.article_url(full_name)}&action=unprotect", data) result = @wiki.browser.post_content("#{@wiki.article_url(full_name)}&action=unprotect", data)
end 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? # What articles link to this article?
# result:: [Array] of [String] Article names # result:: [Array] of [String] Article names
def what_links_here def what_links_here(count = nil)
res = [] 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| links.each_element('//div[@id="bodyContent"]//ul/li/a') { |a|
res << a.attributes['title'] res << a.attributes['title']
} }
res res
end 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 protected
def to_rexml( html ) def to_rexml( html )
Expand Down
2 changes: 1 addition & 1 deletion lib/mediawiki/minibrowser.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def post_content(url, data)
end end
return get_content(response['Location']) return get_content(response['Location'])
else else
raise "Unknown Response: #{response.inspect}" raise "Unknown Response on #{url}: #{response.inspect}"
end end
end end
end end
Expand Down

0 comments on commit f119dae

Please sign in to comment.