Permalink
Browse files

Merge branch 'master' into newui

  • Loading branch information...
2 parents 915ddfd + e79af99 commit 6f54d49be3f5dabc472436bb84f7345ebe620e5b @njh committed Jun 13, 2012
View
@@ -6,9 +6,11 @@
class DbpediaLite < Sinatra::Base
- set :public_folder, File.join(File.dirname(__FILE__), 'public')
+ set :public_folder, File.join(root, 'public')
DEFAULT_HOST = 'dbpedialite.org'
+ APP_LAST_UPDATED = File.mtime(root).gmtime
+ GIT_LAST_COMMIT = ENV['COMMIT_HASH'] || `git rev-parse HEAD`
def negotiate_content(object, format, html_view)
if format.empty?
@@ -190,7 +192,7 @@ def format_iso8061(datetime)
headers 'Cache-Control' => 'public,max-age=3600'
redirect "/", 301 if params[:url].nil? or params[:url].empty?
- if params[:url] =~ %r{^http://(\w+)\.wikipedia.org/wiki/(.+)$}
+ if params[:url] =~ %r{^http://(\w+)\.wikipedia.org/wiki/(.+)(\#\w*)?$}
redirect_from_title($2)
elsif params[:url] =~ %r{^http://dbpedia.org/(page|resource|data)/(.+)$}
redirect_from_title($2)
@@ -201,7 +203,7 @@ def format_iso8061(datetime)
rescue FreebaseApi::NotFound
not_found("No Wikipedia page id found for Freebase topic")
end
- elsif params[:url] =~ %r{^http://([\w\.\-\:]+)/(things|categories)/(\d+)$}
+ elsif params[:url] =~ %r{^http://([\w\.\-\:]+)/(things|categories)/(\d+)(\#\w*)?$}
begin
data = WikipediaApi.page_info(:pageids => $3)
escaped = WikipediaApi.escape_title(data['title'])
View
@@ -25,9 +25,10 @@ def initialize(pageid, title)
ABSTRACT_TRUNCATE_LENGTH = 700
HTTP_TIMEOUT = 5
NBSP = Nokogiri::HTML("&nbsp;").text
+ UNSAFE_REGEXP = Regexp.new('[^-_\.!~*\'()a-zA-Z0-9;/:@&=$,]', false, 'N').freeze
def self.escape_query(str)
- URI::escape(str, ' ?#%"+=|')
+ URI::escape(str, UNSAFE_REGEXP)
end
def self.escape_title(title)
@@ -36,7 +37,7 @@ def self.escape_title(title)
def self.clean_displaytitle(hash)
if hash['displaytitle']
- hash['displaytitle'].gsub!(%r|<.+?>|, '')
+ hash['displaytitle'] = Nokogiri::HTML(hash['displaytitle']).text
end
end
@@ -123,7 +124,8 @@ def self.category_members(pageid, args={})
:inprop => 'displaytitle'
}.merge(args))
- data['query']['pages'].values
+ values = data['query']['pages'].values
+ values.each {|v| clean_displaytitle(v) }
end
def self.page_categories(pageid, args={})
@@ -135,7 +137,8 @@ def self.page_categories(pageid, args={})
:gcllimit => 500,
}.merge(args))
- data['query']['pages'].values
+ values = data['query']['pages'].values
+ values.each {|v| clean_displaytitle(v) }
end
View
@@ -170,23 +170,22 @@ a img { border: none; padding: 0; margin: 0; }
#lastmodified
{
- margin: 3em 0 0 0;
- font-family: helvetica, arial, sans-serif;
- font-size: 8pt;
- font-style: oblique;
- font-variant: normal;
- font-weight: lighter;
+ margin: 3em 0 0 0;
+ font-family: helvetica, arial, sans-serif;
+ font-size: 8pt;
+ font-style: oblique;
+ font-variant: normal;
+ font-weight: lighter;
}
/*--------------------------------------------------------------
Footer
--------------------------------------------------------------*/
-#footer
-{
+#footer {
height: 40px;
margin: 10px 0 0;
- padding: 10px 0 0;
+ padding: 10px 0 10px 0;
clear: both;
border-top: 1px solid #ccc;
font-size: 90%;
View
@@ -720,6 +720,26 @@ def app
end
end
+ context "flipping from a dbpedia lite thing page with a fragment identifier" do
+ before :each do
+ FakeWeb.register_uri(
+ :get, %r[http://en.wikipedia.org/w/api.php],
+ :body => fixture_data('pageinfo-rat.json'),
+ :content_type => 'application/json'
+ )
+ get '/flipr?url=http%3A%2F%2Fdbpedialite.org%2Fthings%2F26471%23id'
+ end
+
+ it "should redirect to the coresponding wikipedia page" do
+ last_response.status.should == 301
+ last_response.location.should == 'http://en.wikipedia.org/wiki/Rat'
+ end
+
+ it "should be cachable" do
+ last_response.headers['Cache-Control'].should =~ /max-age=([1-9]+)/
+ end
+ end
+
context "flipping from a dbpedia lite category page" do
before :each do
FakeWeb.register_uri(
@@ -786,6 +806,7 @@ def app
end
it "should display an error message" do
+ last_response.status.should == 200
last_response.body.should =~ %r{Sorry but I don't know how to flip from: http://www.bbc.co.uk/}
end
end

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -44,6 +44,10 @@
it "should convert 'C#' to 'C%23'" do
WikipediaApi.escape_query('C#').should == 'C%23'
end
+
+ it "should convert 'Café' to 'Café'" do
+ WikipediaApi.escape_query('Café').should == 'Caf%C3%A9'
+ end
end
context "parsing a page" do
@@ -134,6 +138,33 @@
end
end
+ context "parsing a page with HTML in the display title" do
+ before :each do
+ FakeWeb.register_uri(
+ :get, 'http://en.wikipedia.org/w/api.php?action=parse&format=json&pageid=6268880&prop=text%7Cdisplaytitle',
+ :body => fixture_data('parse-6268880.json'),
+ :content_type => 'application/json'
+ )
+ @data = WikipediaApi.parse(6268880)
+ end
+
+ it "should return a hash" do
+ @data.should be_a(Hash)
+ end
+
+ it "should return the article's page url title" do
+ @data['title'].should == "On Her Majesty's Secret Service (film)"
+ end
+
+ it "should return the article display title" do
+ @data['displaytitle'].should == "On Her Majesty's Secret Service (film)"
+ end
+
+ it "should return the article abstract" do
+ @data['abstract'].should =~ /^On Her Majesty's Secret Service \(1969\) is the sixth spy film in the James Bond series/
+ end
+ end
+
context "parsing a page with <p> in the infobox" do
before :each do
FakeWeb.register_uri(
View
@@ -45,6 +45,9 @@
<div class="links">
Source code: <%= link_to('http://github.com/njh/dbpedialite') %>
</div>
+ <div class="links">
+ Code last updated: <%= link_to(APP_LAST_UPDATED, 'http://github.com/njh/dbpedialite/tree/' + GIT_LAST_COMMIT) %>
+ </div>
</div>
</body>

0 comments on commit 6f54d49

Please sign in to comment.