Kill trailing whitespace in ruby files

This commit is contained in:
小太
2013-03-19 23:10:10 +11:00
parent c107f96cec
commit cba839ba76
319 changed files with 2710 additions and 2710 deletions

View File

@@ -1,18 +1,18 @@
class AliasAndImplicationImporter
attr_accessor :text, :commands, :forum_id
def initialize(text, forum_id)
@forum_id = forum_id
@text = text
end
def process!
tokens = tokenize(text)
parse(tokens)
end
private
def tokenize(text)
text.gsub!(/^\s+/, "")
text.gsub!(/\s+$/, "")
@@ -33,7 +33,7 @@ private
end
end
end
def parse(tokens)
ActiveRecord::Base.transaction do
tokens.map do |token|
@@ -41,21 +41,21 @@ private
when :create_alias
tag_alias = TagAlias.create(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2])
tag_alias.delay(:queue => "default").process!
when :create_implication
tag_implication = TagImplication.create(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2])
tag_implication.delay(:queue => "default").process!
when :remove_alias
tag_alias = TagAlias.where("antecedent_name = ?", token[1]).first
raise "Alias for #{token[1]} not found" if tag_alias.nil?
tag_alias.destroy
when :remove_implication
tag_implication = TagImplication.where("antecedent_name = ? and consequent_name = ?", token[1], token[2]).first
raise "Implication for #{token[1]} not found" if tag_implication.nil?
tag_implication.destroy
else
raise "Unknown token: #{token[0]}"
end

View File

@@ -11,15 +11,15 @@ class AnonymousUser
def comment_threshold
0
end
def created_at
Time.now
end
def updated_at
Time.now
end
def name
"Anonymous"
end
@@ -31,11 +31,11 @@ class AnonymousUser
def is_anonymous?
true
end
def has_mail?
false
end
def has_forum_been_updated?
false
end
@@ -47,11 +47,11 @@ class AnonymousUser
def ban
false
end
def always_resize_images?
false
end
def show_samples?
true
end
@@ -59,15 +59,15 @@ class AnonymousUser
def tag_subscriptions
[]
end
def favorite_tags
nil
end
def upload_limit
0
end
def base_upload_limit
0
end
@@ -75,15 +75,15 @@ class AnonymousUser
def uploaded_tags
""
end
def uploaded_tags_with_types
[]
end
def recent_tags
""
end
def recent_tags_with_types
[]
end
@@ -91,15 +91,15 @@ class AnonymousUser
def can_upload?
false
end
def can_comment?
false
end
def can_remove_from_pools?
false
end
def blacklisted_tags
""
end
@@ -107,64 +107,64 @@ class AnonymousUser
def time_zone
"Eastern Time (US & Canada)"
end
def default_image_size
"large"
end
def blacklisted_tags
[]
end
def email
""
end
def last_forum_read_at
Time.now
end
def update_column(*params)
end
def increment!(field)
end
def decrement!(field)
end
def role
:anonymous
end
def tag_query_limit
2
end
def favorite_limit
0
end
def favorite_count
0
end
def enable_post_navigation
true
end
def new_post_navigation_layout
true
end
def enable_privacy_mode
false
end
def enable_sequential_post_navigation
true
end
%w(member banned privileged builder platinum contributor janitor moderator admin).each do |name|
define_method("is_#{name}?") do
false

View File

@@ -4,7 +4,7 @@ class Cache
Cache.put(key, val.to_i + 1)
ActiveRecord::Base.logger.debug('MemCache Incr %s' % [key])
end
def self.decr(key, expiry = 0)
val = Cache.get(key, expiry)
if val.to_i > 0
@@ -12,7 +12,7 @@ class Cache
end
ActiveRecord::Base.logger.debug('MemCache Decr %s' % [key])
end
def self.get_multi(keys, prefix, expiry = 0)
key_to_sanitized_key_hash = keys.inject({}) do |hash, x|
hash[x] = "#{prefix}:#{Cache.sanitize(x)}"
@@ -30,11 +30,11 @@ class Cache
Cache.put(sanitized_key, result_hash[key], expiry)
end
end
ActiveRecord::Base.logger.debug('MemCache Multi-Get (%0.6f) %s' % [elapsed, keys.join(",")])
end
end
def self.get(key, expiry = 0)
begin
start_time = Time.now
@@ -55,11 +55,11 @@ class Cache
value
end
end
def self.put(key, value, expiry = 0)
key.gsub!(/\s/, "_")
key = key[0, 200]
begin
start_time = Time.now
MEMCACHE.set key, value, expiry
@@ -71,7 +71,7 @@ class Cache
nil
end
end
def self.delete(key, delay = nil)
begin
start_time = Time.now
@@ -84,7 +84,7 @@ class Cache
nil
end
end
def self.sanitize(key)
key.gsub(/\W/) {|x| "%#{x.ord}"}.slice(0, 240)
end

View File

@@ -2,10 +2,10 @@ class CurrentUser
def self.scoped(user, ip_addr)
old_user = self.user
old_ip_addr = self.ip_addr
self.user = user
self.ip_addr = ip_addr
begin
yield
ensure
@@ -17,19 +17,19 @@ class CurrentUser
def self.user=(user)
Thread.current[:current_user] = user
end
def self.ip_addr=(ip_addr)
Thread.current[:current_ip_addr] = ip_addr
end
def self.user
Thread.current[:current_user]
end
def self.ip_addr
Thread.current[:current_ip_addr]
end
def self.id
if user.nil?
nil
@@ -37,11 +37,11 @@ class CurrentUser
user.id
end
end
def self.name
user.name
end
def self.method_missing(method, *params, &block)
if user.respond_to?(method)
user.__send__(method, *params, &block)

View File

@@ -5,11 +5,11 @@ class DText
def self.u(string)
CGI.escape(string)
end
def self.h(string)
CGI.escapeHTML(string)
end
def self.parse_inline(str, options = {})
str.gsub!(/&/, "&")
str.gsub!(/</, "&lt;")
@@ -25,7 +25,7 @@ class DText
str = parse_id_links(str)
str
end
def self.parse_links(str)
str.gsub(/("[^"]+":(https?:\/\/|\/)[^\s\r\n<>]+|https?:\/\/[^\s\r\n<>]+)+/) do |url|
if url =~ /^"([^"]+)":(.+)$/
@@ -34,7 +34,7 @@ class DText
else
text = url
end
if url =~ /([;,.!?\)\]<>])$/
url.chop!
ch = $1
@@ -45,7 +45,7 @@ class DText
'<a href="' + url + '">' + text + '</a>' + ch
end
end
def self.parse_aliased_wiki_links(str)
str.gsub(/\[\[([^\|\]]+)\|([^\]]+)\]\]/m) do
text = CGI.unescapeHTML($2)
@@ -53,7 +53,7 @@ class DText
%{<a href="/wiki_pages/show_or_new?title=#{u(title)}">#{h(text)}</a>}
end
end
def self.parse_wiki_links(str)
str.gsub(/\[\[([^\]]+)\]\]/) do
text = CGI.unescapeHTML($1)
@@ -61,14 +61,14 @@ class DText
%{<a href="/wiki_pages/show_or_new?title=#{u(title)}">#{h(text)}</a>}
end
end
def self.parse_post_links(str)
str.gsub(/\{\{([^\}]+)\}\}/) do
tags = CGI.unescapeHTML($1)
%{<a href="/posts?tags=#{u(tags)}">#{h(tags)}</a>}
end
end
def self.parse_id_links(str)
str = str.gsub(/\bpost #(\d+)/i, %{<a href="/posts/\\1">post #\\1</a>})
str = str.gsub(/\bforum #(\d+)/i, %{<a href="/forum_posts/\\1">forum #\\1</a>})
@@ -76,7 +76,7 @@ class DText
str = str.gsub(/\bpool #(\d+)/i, %{<a href="/pools/\\1">pool #\\1</a>})
str = str.gsub(/\buser #(\d+)/i, %{<a href="/users/\\1">user #\\1</a>})
end
def self.parse_list(str, options = {})
html = ""
layout = []
@@ -115,27 +115,27 @@ class DText
def self.parse(str, options = {})
return "" if str.blank?
# Make sure quote tags are surrounded by newlines
unless options[:inline]
str.gsub!(/\s*\[quote\]\s*/m, "\n\n[quote]\n\n")
str.gsub!(/\s*\[\/quote\]\s*/m, "\n\n[/quote]\n\n")
str.gsub!(/\s*\[spoilers?\](?!\])\s*/m, "\n\n[spoiler]\n\n")
str.gsub!(/\s*\[\/spoilers?\]\s*/m, "\n\n[/spoiler]\n\n")
end
str.gsub!(/(?:\r?\n){3,}/, "\n\n")
str.strip!
blocks = str.split(/(?:\r?\n){2}/)
stack = []
html = blocks.map do |block|
case block
when /^(h[1-6])\.\s*(.+)$/
tag = $1
content = $2
content = $2
if options[:inline]
"<h6>" + parse_inline(content, options) + "</h6>"
else
@@ -144,7 +144,7 @@ class DText
when /^\s*\*+ /
parse_list(block, options)
when "[quote]"
if options[:inline]
""
@@ -152,7 +152,7 @@ class DText
stack << "blockquote"
"<blockquote>"
end
when "[/quote]"
if options[:inline]
""
@@ -166,7 +166,7 @@ class DText
when /\[spoilers?\](?!\])/
stack << "div"
'<div class="spoiler">'
when /\[\/spoilers?\]/
if stack.last == "div"
stack.pop
@@ -177,7 +177,7 @@ class DText
'<p>' + parse_inline(block) + "</p>"
end
end
stack.reverse.each do |tag|
if tag == "blockquote"
html << "</blockquote>"
@@ -188,10 +188,10 @@ class DText
sanitize(html.join("")).html_safe
end
def self.sanitize(text)
text.gsub!(/<( |-|\Z)/, "&lt;\\1")
Sanitize.clean(
text,
:elements => %w(code center tn h1 h2 h3 h4 h5 h6 a span div blockquote br p ul li ol em strong small big b i font u s),

View File

@@ -1,14 +1,14 @@
module Downloads
class File
class Error < Exception ; end
attr_accessor :source, :content_type, :file_path
def initialize(source, file_path)
@source = source
@file_path = file_path
end
def download!
http_get_streaming do |response|
self.content_type = response["Content-Type"]
@@ -18,23 +18,23 @@ module Downloads
end
after_download
end
def before_download(url, headers)
Strategies::Base.strategies.each do |strategy|
url, headers = strategy.new.rewrite(url, headers)
end
return [url, headers]
end
def after_download
fix_image_board_sources
end
def url
URI.parse(source)
end
def http_get_streaming(options = {})
max_size = options[:max_size] || Danbooru.config.max_file_size
max_size = nil if max_size == 0 # unlimited
@@ -49,7 +49,7 @@ module Downloads
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
}
@source, headers = before_download(source, headers)
Net::HTTP.start(url.host, url.port, :use_ssl => url.is_a?(URI::HTTPS)) do |http|
http.read_timeout = 10
http.request_get(url.request_uri, headers) do |res|
@@ -76,7 +76,7 @@ module Downloads
end # http.start
end # while
end # def
def fix_image_board_sources
if source =~ /\/src\/\d{12,}|urnc\.yi\.org|yui\.cynthia\.bne\.jp/
@source = "Image board"

View File

@@ -4,7 +4,7 @@ module Downloads
def self.strategies
[Pixiv]
end
def rewrite(url, headers)
return [url, headers]
end

View File

@@ -8,16 +8,16 @@ module Downloads
url, headers = rewrite_small_images(url, headers)
url, headers = rewrite_small_manga_pages(url, headers)
end
return [url, headers]
end
protected
def rewrite_headers(url, headers)
headers["Referer"] = "http://www.pixiv.net"
return [url, headers]
end
def rewrite_html_pages(url, headers)
# example: http://www.pixiv.net/member_illust.php?mode=big&illust_id=23828655
@@ -29,16 +29,16 @@ module Downloads
return [url, headers]
end
end
def rewrite_small_images(url, headers)
if url =~ %r!(/img/.+?/.+?)_m.+$!
match = $1
url.sub!(match + "_m", match)
end
return [url, headers]
end
def rewrite_small_manga_pages(url, headers)
if url =~ %r!(\d+_p\d+)\.!
match = $1
@@ -48,7 +48,7 @@ module Downloads
url = big_url
end
end
return [url, headers]
end

View File

@@ -1,10 +1,10 @@
class JanitorTrialTester
attr_reader :user
def initialize(user_name)
@user = User.find_by_name(user_name)
end
def test
if user.nil?
"User not found"

View File

@@ -7,16 +7,16 @@ module Moderator
def self.all(min_date, max_level)
sql = <<-EOS
SELECT artist_versions.updater_id AS updater_id, count(*)
FROM artist_versions
JOIN users ON users.id = artist_versions.updater_id
WHERE
artist_versions.created_at > ?
AND users.level <= ?
GROUP BY artist_versions.updater_id
ORDER BY count(*) DESC
FROM artist_versions
JOIN users ON users.id = artist_versions.updater_id
WHERE
artist_versions.created_at > ?
AND users.level <= ?
GROUP BY artist_versions.updater_id
ORDER BY count(*) DESC
LIMIT 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
end

View File

@@ -6,20 +6,20 @@ module Moderator
def self.all(min_date, max_level)
sql = <<-EOS
SELECT comment_votes.comment_id, count(*)
FROM comment_votes
JOIN comments ON comments.id = comment_id
JOIN users ON users.id = comments.creator_id
WHERE
comment_votes.created_at > ?
AND comments.score < 0
AND users.level <= ?
GROUP BY comment_votes.comment_id
SELECT comment_votes.comment_id, count(*)
FROM comment_votes
JOIN comments ON comments.id = comment_id
JOIN users ON users.id = comments.creator_id
WHERE
comment_votes.created_at > ?
AND comments.score < 0
AND users.level <= ?
GROUP BY comment_votes.comment_id
HAVING count(*) >= 3
ORDER BY count(*) DESC
ORDER BY count(*) DESC
LIMIT 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
end

View File

@@ -3,23 +3,23 @@ module Moderator
module Queries
class Note
attr_reader :user, :count
def self.all(min_date, max_level)
sql = <<-EOS
SELECT note_versions.updater_id, count(*)
FROM note_versions
JOIN users ON users.id = note_versions.updater_id
WHERE
note_versions.created_at > ?
AND users.level <= ?
GROUP BY note_versions.updater_id
ORDER BY count(*) DESC
SELECT note_versions.updater_id, count(*)
FROM note_versions
JOIN users ON users.id = note_versions.updater_id
WHERE
note_versions.created_at > ?
AND users.level <= ?
GROUP BY note_versions.updater_id
ORDER BY count(*) DESC
LIMIT 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
end
def initialize(hash)
@user = ::User.find(hash["updater_id"])
@count = hash["count"]

View File

@@ -10,14 +10,14 @@ module Moderator
FROM post_appeals
JOIN posts ON posts.id = post_appeals.post_id
WHERE
post_appeals.created_at > ?
post_appeals.created_at > ?
and posts.is_deleted = true
and posts.is_pending = false
GROUP BY post_appeals.post_id
ORDER BY count(*) DESC
LIMIT 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date).map {|x| new(x)}
end

View File

@@ -6,22 +6,22 @@ module Moderator
def self.all(min_date)
sql = <<-EOS
SELECT post_flags.post_id, count(*)
FROM post_flags
JOIN posts ON posts.id = post_flags.post_id
WHERE
post_flags.created_at > ?
AND post_flags.reason <> ?
SELECT post_flags.post_id, count(*)
FROM post_flags
JOIN posts ON posts.id = post_flags.post_id
WHERE
post_flags.created_at > ?
AND post_flags.reason <> ?
AND posts.is_deleted = false
and posts.is_pending = false
GROUP BY post_flags.post_id
ORDER BY count(*) DESC
GROUP BY post_flags.post_id
ORDER BY count(*) DESC
LIMIT 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date, "Unapproved in three days").map {|x| new(x)}
end
def initialize(hash)
@post = Post.find(hash["post_id"])
@count = hash["count"]

View File

@@ -3,25 +3,25 @@ module Moderator
module Queries
class Tag
attr_reader :user, :count
def self.all(min_date, max_level)
sql = <<-EOS
SELECT post_versions.updater_id, count(*)
FROM post_versions
JOIN users ON users.id = post_versions.updater_id
WHERE
post_versions.updated_at > ?
AND users.level <= ?
GROUP BY post_versions.updater_id
ORDER BY count(*) DESC
SELECT post_versions.updater_id, count(*)
FROM post_versions
JOIN users ON users.id = post_versions.updater_id
WHERE
post_versions.updated_at > ?
AND users.level <= ?
GROUP BY post_versions.updater_id
ORDER BY count(*) DESC
LIMIT 10
EOS
ActiveRecord::Base.without_timeout do
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
end
end
def initialize(hash)
@user = ::User.find(hash["updater_id"])
@count = hash["count"]

View File

@@ -3,23 +3,23 @@ module Moderator
module Queries
class Upload
attr_reader :user, :count
def self.all(min_date, max_level)
sql = <<-EOS
select uploader_id, count(*)
from posts
join users on uploader_id = users.id
where
posts.created_at > ?
and level <= ?
group by posts.uploader_id
order by count(*) desc
select uploader_id, count(*)
from posts
join users on uploader_id = users.id
where
posts.created_at > ?
and level <= ?
group by posts.uploader_id
order by count(*) desc
limit 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
end
def initialize(hash)
@user = ::User.find(hash["uploader_id"])
@count = hash["count"]

View File

@@ -3,23 +3,23 @@ module Moderator
module Queries
class WikiPage
attr_reader :user, :count
def self.all(min_date, max_level)
sql = <<-EOS
SELECT wiki_page_versions.updater_id, count(*)
FROM wiki_page_versions
JOIN users ON users.id = wiki_page_versions.updater_id
WHERE
wiki_page_versions.created_at > ?
AND users.level <= ?
GROUP BY wiki_page_versions.updater_id
ORDER BY count(*) DESC
SELECT wiki_page_versions.updater_id, count(*)
FROM wiki_page_versions
JOIN users ON users.id = wiki_page_versions.updater_id
WHERE
wiki_page_versions.created_at > ?
AND users.level <= ?
GROUP BY wiki_page_versions.updater_id
ORDER BY count(*) DESC
LIMIT 10
EOS
ActiveRecord::Base.select_all_sql(sql, min_date, max_level).map {|x| new(x)}
end
def initialize(hash)
@user = ::User.find(hash["updater_id"])
@count = hash["count"]

View File

@@ -2,18 +2,18 @@ module Moderator
module Dashboard
class Report
attr_reader :min_date, :max_level
def initialize(min_date, max_level)
@min_date = min_date.present? ? min_date.to_date : 1.week.ago
@max_level = max_level.present? ? User::Levels::MEMBER : max_level.to_i
end
def artists
ActiveRecord::Base.without_timeout do
Queries::Artist.all(min_date, max_level)
end
end
def comments
ActiveRecord::Base.without_timeout do
Queries::Comment.all(min_date, max_level)
@@ -55,7 +55,7 @@ module Moderator
Queries::Upload.all(min_date, max_level)
end
end
def user_feedbacks
ActiveRecord::Base.without_timeout do
Queries::UserFeedback.all

View File

@@ -1,12 +1,12 @@
module Moderator
class IpAddrSearch
attr_reader :params, :errors
def initialize(params)
@params = params
@errors = []
end
def execute
if params[:user_id].present?
search_by_user_id(params[:user_id].split(/,/))
@@ -18,41 +18,41 @@ module Moderator
[]
end
end
private
def select_all_sql(sql, *params)
ActiveRecord::Base.select_all_sql(sql, *params)
end
def search_by_ip_addr(ip_addrs)
sums = Hash.new {|h, k| h[k] = 0}
add_row(sums, "select creator_id as k, count(*) from comments where ip_addr in (?) group by k", ip_addrs)
add_row(sums, "select updater_id as k, count(*) from post_versions where updater_ip_addr in (?) group by k", ip_addrs)
add_row(sums, "select updater_id as k, count(*) from note_versions where updater_ip_addr in (?) group by k", ip_addrs)
add_row(sums, "select updater_id as k, count(*) from pool_versions where updater_ip_addr in (?) group by k", ip_addrs)
add_row(sums, "select updater_id as k, count(*) from wiki_page_versions where updater_ip_addr in (?) group by k", ip_addrs)
sums
end
def search_by_user_name(user_names)
users = User.where("name in (?)", user_names)
search_by_user_id(users.map(&:id))
end
def search_by_user_id(user_ids)
sums = Hash.new {|h, k| h[k] = 0}
add_row(sums, "select ip_addr as k, count(*) from comments where creator_id in (?) group by k", user_ids)
add_row(sums, "select updater_ip_addr as k, count(*) from post_versions where updater_id in (?) group by k", user_ids)
add_row(sums, "select updater_ip_addr as k, count(*) from note_versions where updater_id in (?) group by k", user_ids)
add_row(sums, "select updater_ip_addr as k, count(*) from pool_versions where updater_id in (?) group by k", user_ids)
add_row(sums, "select updater_ip_addr as k, count(*) from wiki_page_versions where updater_id in (?) group by k", user_ids)
sums
end
def add_row(sums, sql, ip_addrs)
select_all_sql(sql, ip_addrs).each do |row|
sums[row["k"]] += row["count"].to_i

View File

@@ -1,15 +1,15 @@
module Moderator
class TagBatchChange < Struct.new(:antecedent, :consequent, :updater_id, :updater_ip_addr)
class Error < Exception ; end
def perform
raise Error.new("antecedent is missing") if antecedent.blank?
normalized_antecedent = TagAlias.to_aliased(::Tag.scan_tags(antecedent))
normalized_consequent = TagAlias.to_aliased(::Tag.scan_tags(consequent))
updater = User.find(updater_id)
CurrentUser.scoped(updater, updater_ip_addr) do
::Post.tag_match(antecedent).each do |post|
tags = (post.tag_array - normalized_antecedent + normalized_consequent).join(" ")

View File

@@ -1,16 +1,16 @@
class PostPruner
attr_reader :admin
def initialize
@admin = User.where(:level => User::Levels::ADMIN).first
end
def prune!
prune_pending!
prune_flagged!
prune_mod_actions!
end
protected
def prune_pending!
@@ -25,7 +25,7 @@ protected
end
end
end
def prune_flagged!
CurrentUser.scoped(admin, "127.0.0.1") do
Post.where("is_deleted = ? and is_flagged = ?", false, true).each do |post|
@@ -40,7 +40,7 @@ protected
end
end
end
def prune_mod_actions!
ModAction.destroy_all(["creator_id = ? and description like ?", admin.id, "deleted post %"])
end

View File

@@ -1,24 +1,24 @@
class PostQueryBuilder
attr_accessor :query_string, :has_constraints
def initialize(query_string)
@query_string = query_string
@has_constraint = false
end
def has_constraints?
@has_constraints
end
def has_constraints!
@has_constraints = true
end
def add_range_relation(arr, field, relation)
return relation if arr.nil?
has_constraints!
case arr[0]
when :eq
if arr[1].is_a?(Time)
@@ -38,7 +38,7 @@ class PostQueryBuilder
when :lte
relation.where(["#{field} <= ?", arr[1]])
when :in
relation.where(["#{field} in (?)", arr[1]])
@@ -76,10 +76,10 @@ class PostQueryBuilder
if tag_query_sql.any?
relation = relation.where("posts.tag_index @@ to_tsquery('danbooru', E?)", tag_query_sql.join(" & "))
end
relation
end
def add_tag_subscription_relation(subscriptions, relation)
subscriptions.each do |subscription|
if subscription =~ /^(.+?):(.+)$/
@@ -93,25 +93,25 @@ class PostQueryBuilder
return relation if user.nil?
post_ids = TagSubscription.find_post_ids(user.id)
end
post_ids = [0] if post_ids.empty?
relation = relation.where(["posts.id IN (?)", post_ids])
end
relation
end
def build
unless query_string.is_a?(Hash)
q = Tag.parse_query(query_string)
end
relation = Post.scoped
if q[:tag_count].to_i > Danbooru.config.tag_query_limit
raise ::Post::SearchError.new("You cannot search for more than #{Danbooru.config.tag_query_limit} tags at a time")
end
relation = add_range_relation(q[:post_id], "posts.id", relation)
relation = add_range_relation(q[:mpixels], "posts.image_width * posts.image_height / 1000000.0", relation)
relation = add_range_relation(q[:width], "posts.image_width", relation)
@@ -125,12 +125,12 @@ class PostQueryBuilder
relation = add_range_relation(q[:character_tag_count], "posts.tag_count_character", relation)
relation = add_range_relation(q[:post_tag_count], "posts.tag_count", relation)
relation = add_range_relation(q[:pixiv], "substring(posts.source, 'pixiv.net/img.*/([0-9]+)[^/]*$')::integer", relation)
if q[:md5]
relation = relation.where(["posts.md5 IN (?)", q[:md5]])
has_constraints!
end
if q[:status] == "pending"
relation = relation.where("posts.is_pending = TRUE")
elsif q[:status] == "flagged"
@@ -167,26 +167,26 @@ class PostQueryBuilder
if q[:uploader_id_neg]
relation = relation.where("posts.uploader_id not in (?)", q[:uploader_id_neg])
end
if q[:uploader_id]
relation = relation.where("posts.uploader_id = ?", q[:uploader_id])
has_constraints!
end
if q[:approver_id_neg]
relation = relation.where("posts.approver_id not in (?)", q[:approver_id_neg])
end
if q[:approver_id]
relation = relation.where("posts.approver_id = ?", q[:approver_id])
has_constraints!
end
if q[:parent_id]
relation = relation.where("(posts.id = ? or posts.parent_id = ?)", q[:parent_id], q[:parent_id])
has_constraints!
end
if q[:rating] =~ /^q/
relation = relation.where("posts.rating = 'q'")
elsif q[:rating] =~ /^s/
@@ -202,13 +202,13 @@ class PostQueryBuilder
elsif q[:rating_negated] =~ /^e/
relation = relation.where("posts.rating <> 'e'")
end
relation = add_tag_string_search_relation(q[:tags], relation)
if q[:order] == "rank"
relation = relation.where("posts.score > 0 and posts.created_at >= ?", 2.days.ago)
end
case q[:order]
when "id", "id_asc"
relation = relation.order("posts.id ASC")
@@ -221,10 +221,10 @@ class PostQueryBuilder
when "score_asc"
relation = relation.order("posts.score ASC, posts.id DESC")
when "favcount"
relation = relation.order("posts.fav_count DESC, posts.id DESC")
when "favcount_asc"
relation = relation.order("posts.fav_count ASC, posts.id DESC")

View File

@@ -1,14 +1,14 @@
class PostSearchContext
attr_reader :params, :post_id
def initialize(params)
@params = params
raise unless params[:seq].present?
raise unless params[:id].present?
@post_id = find_post_id
end
def find_post_id
if params[:seq] == "prev"
post = Post.tag_match(params[:tags]).where("posts.id > ?", params[:id].to_i).reorder("posts.id asc").first

View File

@@ -1,12 +1,12 @@
module PostSets
class Artist < PostSets::Post
attr_reader :artist
def initialize(artist)
super(artist.name)
@artist = artist
end
def posts
::Post.tag_match(@artist.name).limit(10)
rescue ::Post::SearchError

View File

@@ -3,38 +3,38 @@ module PostSets
def has_wiki?
false
end
def wiki_page
nil
end
def has_artist?
false
end
def artist
nil
end
def is_single_tag?
false
end
def presenter
raise NotImplementedError
end
def arbitrary_sql_order_clause(ids, table_name)
if ids.empty?
return "#{table_name}.id desc"
end
conditions = []
ids.each_with_index do |x, n|
conditions << "when #{x} then #{n}"
end
"case #{table_name}.id " + conditions.join(" ") + " end"
end
end

View File

@@ -1,28 +1,28 @@
module PostSets
class Favorite < Base
attr_reader :user, :page, :favorites
def initialize(user_id, page = 1)
@user = ::User.find(user_id)
@favorites = ::Favorite.for_user(user.id).paginate(page).order("favorites.id desc")
end
def tag_array
@tag_array ||= ["fav:#{user.name}"]
end
def tag_string
tag_array.uniq.join(" ")
end
def humanized_tag_string
"fav:#{user.pretty_name}"
end
def posts
favorites.includes(:post).map(&:post)
end
def presenter
@presenter ||= ::PostSetPresenters::Favorite.new(self)
end

View File

@@ -3,26 +3,26 @@ module PostSets
module ActiveRecordExtension
attr_accessor :total_pages, :current_page
end
attr_reader :pool, :page
def initialize(pool, page = 1)
@pool = pool
@page = page
end
def offset
(current_page - 1) * limit
end
def limit
Danbooru.config.posts_per_page
end
def tag_array
["pool:#{pool.id}"]
end
def posts
@posts ||= begin
x = pool.posts(:offset => offset, :limit => limit)
@@ -32,27 +32,27 @@ module PostSets
x
end
end
def tag_string
tag_array.join("")
end
def humanized_tag_string
"pool:#{pool.pretty_name}"
end
def presenter
@presenter ||= PostSetPresenters::Pool.new(self)
end
def total_pages
(pool.post_count.to_f / limit).ceil
end
def size
posts.size
end
def current_page
[page.to_i, 1].max
end

View File

@@ -1,41 +1,41 @@
module PostSets
class Popular < Base
attr_reader :date, :scale
def initialize(date, scale)
@date = date.blank? ? Time.zone.now : Time.zone.parse(date)
@scale = scale
end
def posts
::Post.where("created_at between ? and ?", min_date.beginning_of_day, max_date.end_of_day).order("score desc").limit(limit)
end
def limit
25
end
def min_date
case scale
when "week"
date.beginning_of_week
when "month"
date.beginning_of_month
else
date
end
end
def max_date
case scale
when "week"
date.end_of_week
when "month"
date.end_of_month
else
date
end

View File

@@ -1,22 +1,22 @@
module PostSets
class Post < Base
attr_reader :tag_array, :page, :per_page
def initialize(tags, page = 1, per_page = nil)
@tag_array = Tag.scan_query(tags)
@page = page
@per_page = (per_page || Danbooru.config.posts_per_page).to_i
@per_page = 200 if @per_page > 200
end
def tag_string
@tag_string ||= tag_array.uniq.join(" ")
end
def humanized_tag_string
tag_array.slice(0, 25).join(" ").tr("_", " ")
end
def has_wiki?
tag_array.size == 1 && ::WikiPage.titled(tag_string).exists?
end
@@ -28,15 +28,15 @@ module PostSets
nil
end
end
def has_deleted?
CurrentUser.is_privileged? && tag_string !~ /status/ && ::Post.tag_match("#{tag_string} status:deleted").exists?
end
def has_explicit?
posts.any? {|x| x.rating == "e"}
end
def posts
if tag_array.size > 2 && !CurrentUser.is_privileged?
raise SearchError.new("Upgrade your account to search more than two tags at once")
@@ -45,50 +45,50 @@ module PostSets
if tag_array.any? {|x| x =~ /^source:.*\*.*pixiv/} && !CurrentUser.user.is_builder?
raise SearchError.new("Your search took too long to execute and was canceled")
end
@posts ||= begin
temp = ::Post.tag_match(tag_string).paginate(page, :count => ::Post.fast_count(tag_string), :limit => per_page)
temp.all
temp
end
end
def has_artist?
tag_array.any? && ::Artist.name_equals(tag_string).exists?
end
def artist
::Artist.name_matches(tag_string).first
end
def is_single_tag?
tag_array.size == 1
end
def is_empty_tag?
tag_array.size == 0
end
def is_pattern_search?
tag_string =~ /\*/
end
def current_page
[page.to_i, 1].max
end
def is_tag_subscription?
tag_subscription.present?
end
def tag_subscription
@tag_subscription ||= tag_array.select {|x| x =~ /^sub:/}.map {|x| x.sub(/^sub:/, "")}.first
end
def tag_subscription_tags
@tag_subscription_tags ||= TagSubscription.find_tags(tag_subscription)
end
def presenter
@presenter ||= ::PostSetPresenters::Post.new(self)
end

View File

@@ -1,7 +1,7 @@
module PostSets
class SearchError < Exception
end
class WikiPage < Post
def presenter
@presenter ||= ::PostSetPresenters::WikiPage.new(self)

View File

@@ -2,25 +2,25 @@ class RelatedTagCalculator
def self.find_tags(tag, limit)
Post.tag_match(tag).limit(limit).select("posts.tag_string").reorder("posts.md5").map(&:tag_string)
end
def self.calculate_from_sample_to_array(tags, category_constraint = nil)
convert_hash_to_array(calculate_from_sample(tags, Danbooru.config.post_sample_size, category_constraint))
end
def self.calculate_from_sample(tags, limit, category_constraint = nil)
counts = Hash.new {|h, k| h[k] = 0}
case category_constraint
when Tag.categories.artist
limit *= 4
when Tag.categories.copyright
limit *= 3
when Tag.categories.character
limit *= 2
end
find_tags(tags, limit).each do |tags|
tag_array = Tag.scan_tags(tags)
if category_constraint
@@ -36,14 +36,14 @@ class RelatedTagCalculator
end
end
end
counts
end
def self.convert_hash_to_array(hash)
hash.to_a.sort_by {|x| -x[1]}.slice(0, 25)
end
def self.convert_hash_to_string(hash)
convert_hash_to_array(hash).flatten.join(" ")
end

View File

@@ -1,6 +1,6 @@
class RelatedTagQuery
attr_reader :query, :category
def initialize(query, category)
@query = query.strip
@category = category
@@ -17,7 +17,7 @@ class RelatedTagQuery
[]
end
end
def wiki_page_tags
results = wiki_page.try(:tags) || []
results.reject! do |name|
@@ -25,31 +25,31 @@ class RelatedTagQuery
end
results
end
def to_json
{:query => query, :category => category, :tags => map_with_category_data(tags), :wiki_page_tags => map_with_category_data(wiki_page_tags)}.to_json
end
protected
def map_with_category_data(list_of_tag_names)
Tag.categories_for(list_of_tag_names).to_a
end
def pattern_matching_tags
Tag.name_matches(query).where("post_count > 0").order("post_count desc").limit(50).sort_by {|x| x.name}.map(&:name)
end
def related_tags
tag = Tag.named(query.strip).first
if tag
tag.related_tag_array.map(&:first)
else
[]
end
end
def related_tags_by_category
RelatedTagCalculator.calculate_from_sample_to_array(query, Tag.categories.value_for(category)).map(&:first)
end

View File

@@ -1,10 +1,10 @@
class RemoteFileManager
attr_reader :path
def initialize(path)
@path = path
end
def distribute
Danbooru.config.other_server_hosts.each do |hostname|
Net::SFTP.start(hostname, Danbooru.config.remote_server_login) do |ftp|
@@ -12,7 +12,7 @@ class RemoteFileManager
end
end
end
def delete
Danbooru.config.other_server_hosts.each do |hostname|
Net::SFTP.start(hostname, Danbooru.config.remote_server_login) do |ftp|

View File

@@ -1,6 +1,6 @@
class SessionCreator
attr_reader :session, :cookies, :name, :password, :remember
def initialize(session, cookies, name, password, remember)
@session = session
@cookies = cookies
@@ -8,17 +8,17 @@ class SessionCreator
@password = password
@remember = remember
end
def authenticate
if User.authenticate(name, password)
user = User.find_by_name(name)
user.update_column(:last_logged_in_at, Time.now)
if remember.present?
cookies.permanent.signed[:user_name] = user.name
cookies.permanent[:password_hash] = user.bcrypt_cookie_password_hash
end
session[:user_id] = user.id
return true
else

View File

@@ -1,55 +1,55 @@
class SessionLoader
attr_reader :session, :cookies, :request
def initialize(session, cookies, request)
@session = session
@cookies = cookies
@request = request
end
def load
if session[:user_id]
load_session_user
elsif cookie_password_hash_valid?
load_cookie_user
end
if CurrentUser.user
CurrentUser.user.unban! if ban_expired?
else
CurrentUser.user = AnonymousUser.new
end
update_last_logged_in_at
set_time_zone
end
private
def load_session_user
CurrentUser.user = User.find_by_id(session[:user_id])
CurrentUser.ip_addr = request.remote_ip
end
def load_cookie_user
CurrentUser.user = User.find_by_name(cookies.signed[:user_name])
CurrentUser.ip_addr = request.remote_ip
end
def ban_expired?
CurrentUser.user.is_banned? && CurrentUser.user.ban && CurrentUser.user.ban.expired?
end
def cookie_password_hash_valid?
cookies[:password_hash] && cookies.signed[:user_name] && User.authenticate_cookie_hash(cookies.signed[:user_name], cookies[:password_hash])
end
def update_last_logged_in_at
return if CurrentUser.is_anonymous?
return if CurrentUser.last_logged_in_at && CurrentUser.last_logged_in_at > 1.week.ago
CurrentUser.user.update_attribute(:last_logged_in_at, Time.now)
end
def set_time_zone
Time.zone = CurrentUser.user.time_zone
end

View File

@@ -2,14 +2,14 @@ module Sources
class Site
attr_reader :url, :strategy
delegate :get, :referer_url, :site_name, :artist_name, :profile_url, :image_url, :tags, :artist_record, :unique_id, :to => :strategy
def self.strategies
[Strategies::NicoSeiga, Strategies::Pixiv]
end
def initialize(url)
@url = url
Site.strategies.each do |strategy|
if strategy.url_match?(url)
@strategy = strategy.new(url)
@@ -18,7 +18,7 @@ module Sources
end
end
end
def to_json
return {
:artist_name => artist_name,
@@ -30,7 +30,7 @@ module Sources
:unique_id => unique_id
}.to_json
end
def available?
strategy.present?
end

View File

@@ -3,28 +3,28 @@ module Sources
class Base
attr_reader :url
attr_reader :artist_name, :profile_url, :image_url, :tags
def self.url_match?(url)
false
end
def initialize(url)
@url = url
end
# No remote calls are made until this method is called.
def get
raise NotImplementedError
end
def site_name
raise NotImplementedError
end
def unique_id
artist_name
end
def artist_record
if artist_name.present?
Artist.other_names_match(artist_name)
@@ -32,11 +32,11 @@ module Sources
nil
end
end
def referer_url(template)
template.params[:ref] || template.params[:url]
end
protected
def agent
raise NotImplementedError

View File

@@ -8,7 +8,7 @@ module Sources
def site_name
"Nico Seiga"
end
def unique_id
profile_url =~ /\/illust\/(\d+)/
"nicoseiga" + $1
@@ -21,9 +21,9 @@ module Sources
@tags = get_tags_from_page(page)
end
end
protected
def get_profile_from_page(page)
links = page.search("div.illust_user_name a")
@@ -34,10 +34,10 @@ module Sources
profile_url = nil
artist_name = nil
end
return [artist_name, profile_url].compact
end
def get_image_url_from_page(page)
links = page.search("a#illust_link")
@@ -47,7 +47,7 @@ module Sources
nil
end
end
def get_tags_from_page(page)
links = page.search("div#tag_block nobr a.tag")
@@ -55,7 +55,7 @@ module Sources
[node.text, "http://seiga.nicovideo.jp" + node.attr("href")]
end
end
def agent
@agent ||= begin
mech = Mechanize.new

View File

@@ -4,7 +4,7 @@ module Sources
def self.url_match?(url)
url =~ /^https?:\/\/(?:\w+\.)?pixiv\.net/
end
def referer_url(template)
if template.params[:ref] =~ /pixiv\.net\/member_illust/ && template.params[:ref] !~ /mode=manga/
template.params[:ref]
@@ -12,16 +12,16 @@ module Sources
template.params[:url]
end
end
def site_name
"Pixiv"
end
def unique_id
image_url =~ /\/img\/([^\/]+)/
$1
end
def get
agent.get(URI.parse(normalized_url).request_uri) do |page|
@artist_name, @profile_url = get_profile_from_page(page)
@@ -31,13 +31,13 @@ module Sources
end
protected
def get_profile_from_page(page)
profile_url = page.search("a.user-link").first
if profile_url
profile_url = "http://www.pixiv.net" + profile_url["href"]
end
artist_name = page.search("h1.user").first
if artist_name
artist_name = artist_name.inner_text
@@ -45,7 +45,7 @@ module Sources
return [artist_name, profile_url]
end
def get_image_url_from_page(page)
element = page.search("div.works_display a img").first
if element
@@ -54,10 +54,10 @@ module Sources
nil
end
end
def get_tags_from_page(page)
# puts page.root.to_xhtml
links = page.search("ul.tags a.text").find_all do |node|
node["href"] =~ /search\.php/
end
@@ -70,7 +70,7 @@ module Sources
[]
end
end
def normalized_url
@normalized_url ||= begin
if url =~ /\/(\d+)(?:_big)?(?:_m|_p\d+)?\.(?:jpg|jpeg|png|gif)/i
@@ -84,7 +84,7 @@ module Sources
end
end
end
def agent
@agent ||= begin
mech = Mechanize.new

View File

@@ -1,17 +1,17 @@
class TagAliasCorrection
attr_reader :tag_alias_id, :tag_alias, :hostname
delegate :antecedent_name, :consequent_name, :to => :tag_alias
def initialize(tag_alias_id, hostname = Socket.gethostname)
@tag_alias_id = tag_alias_id
@tag_alias = TagAlias.find(tag_alias_id)
@hostname = hostname
end
def to_json(options = {})
statistics_hash.to_json
end
def statistics_hash
@statistics_hash ||= {
"antecedent_cache" => Cache.get("ta:" + Cache.sanitize(tag_alias.antecedent_name)),
@@ -20,7 +20,7 @@ class TagAliasCorrection
"consequent_count" => Tag.find_by_name(tag_alias.consequent_name).try(:post_count)
}
end
def fill_hash!
Net::HTTP.start(hostname, 80) do |http|
http.request_get("/tag_aliases/#{tag_alias_id}/correction.json") do |res|
@@ -32,23 +32,23 @@ class TagAliasCorrection
end
end
end
def each_server
Danbooru.config.all_server_hosts.each do |host|
other = TagAliasCorrection.new(tag_alias_id, host)
if host != Socket.gethostname
other.fill_hash!
end
yield other
end
end
def clear_cache
tag_alias.clear_all_cache
end
def fix!
clear_cache
tag_alias.delay(:queue => "default").update_posts

View File

@@ -1,28 +1,28 @@
class TagAliasRequest
class ValidationError < Exception ; end
attr_reader :antecedent_name, :consequent_name, :reason, :tag_alias, :forum_topic
def initialize(antecedent_name, consequent_name, reason)
@antecedent_name = antecedent_name.tr(" ", "_")
@consequent_name = consequent_name.tr(" ", "_")
@reason = reason
end
def create
TagAlias.transaction do
create_alias
create_forum_topic
end
end
def create_alias
@tag_alias = TagAlias.create(:antecedent_name => antecedent_name, :consequent_name => consequent_name, :status => "pending")
if @tag_alias.errors.any?
raise ValidationError.new(@tag_alias.errors.full_messages.join("; "))
end
end
def create_forum_topic
@forum_topic = ForumTopic.create(
:title => "Tag alias: #{antecedent_name} -> #{consequent_name}",
@@ -33,7 +33,7 @@ class TagAliasRequest
if @forum_topic.errors.any?
raise ValidationError.new(@forum_topic.errors.full_messages.join("; "))
end
tag_alias.update_attribute(:forum_topic_id, @forum_topic.id)
end
end

View File

@@ -1,23 +1,23 @@
class TagCorrection
attr_reader :tag_id, :tag, :hostname
def initialize(tag_id, hostname = Socket.gethostname)
@tag_id = tag_id
@tag = Tag.find(tag_id)
@hostname = hostname
end
def to_json(options = {})
statistics_hash.to_json
end
def statistics_hash
@statistics_hash ||= {
"category_cache" => Cache.get("tc:" + Cache.sanitize(tag.name)),
"post_fast_count_cache" => Cache.get("pfc:" + Cache.sanitize(tag.name))
}
end
def fill_hash!
Net::HTTP.start(hostname, 80) do |http|
http.request_get("/tags/#{tag_id}/correction.json") do |res|
@@ -29,19 +29,19 @@ class TagCorrection
end
end
end
def each_server
Danbooru.config.all_server_hosts.each do |host|
other = TagCorrection.new(tag_id, host)
if host != Socket.gethostname
other.fill_hash!
end
yield other
end
end
def fix!
tag.delay(:queue => "default").fix_post_count
Post.expire_cache_for_all([tag.name])

View File

@@ -1,28 +1,28 @@
class TagImplicationRequest
class ValidationError < Exception ; end
attr_reader :antecedent_name, :consequent_name, :reason, :tag_implication, :forum_topic
def initialize(antecedent_name, consequent_name, reason)
@antecedent_name = antecedent_name.tr(" ", "_")
@consequent_name = consequent_name.tr(" ", "_")
@reason = reason
end
def create
TagImplication.transaction do
create_implication
create_forum_topic
end
end
def create_implication
@tag_implication = TagImplication.create(:antecedent_name => antecedent_name, :consequent_name => consequent_name, :status => "pending")
if @tag_implication.errors.any?
raise ValidationError.new(@tag_implication.errors.full_messages.join("; "))
end
end
def create_forum_topic
@forum_topic = ForumTopic.create(
:title => "Tag implication: #{antecedent_name} -> #{consequent_name}",
@@ -33,7 +33,7 @@ class TagImplicationRequest
if @forum_topic.errors.any?
raise ValidationError.new(@forum_topic.errors.full_messages.join("; "))
end
tag_implication.update_attribute(:forum_topic_id, @forum_topic.id)
end
end

View File

@@ -1,10 +1,10 @@
class UploadedTagsImporter
attr_reader :user
def initialize(user)
@user = user
end
def import!
row = connection.exec("SELECT uploaded_tags FROM users WHERE id = #{user.id}").first
if row
@@ -13,7 +13,7 @@ class UploadedTagsImporter
connection.close
rescue Exception
end
def connection
@connection ||= PGconn.connect(:dbname => "danbooru", :host => "dbserver", :user => "albert")
end