diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 59bd1c8a7..b4af514a4 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -124,7 +124,7 @@ class PostQueryBuilder relation = add_range_relation(q[:copyright_tag_count], "posts.tag_count_copyright", relation) 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_id], "substring(posts.source, 'pixiv.net/img.*/([0-9]+)[^/]*$')::integer", relation) + relation = add_range_relation(q[:pixiv_id], "posts.pixiv_id", relation) if q[:md5] relation = relation.where(["posts.md5 IN (?)", q[:md5]]) diff --git a/app/models/post.rb b/app/models/post.rb index 7ed140238..7f4fb4254 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -15,6 +15,7 @@ class Post < ActiveRecord::Base before_save :update_tag_post_counts before_save :set_tag_counts before_validation :initialize_uploader, :on => :create + before_validation :parse_pixiv_id belongs_to :updater, :class_name => "User" belongs_to :approver, :class_name => "User" belongs_to :uploader, :class_name => "User" @@ -1011,7 +1012,21 @@ class Post < ActiveRecord::Base q end end - + + module PixivMethods + def parse_pixiv_id + if source =~ %r!http://i\d\.pixiv\.net/img-inf/img/\d+/\d+/\d+/\d+/\d+/\d+/(\d+)_s.jpg! + self.pixiv_id = $1 + elsif source =~ %r!http://i\d\.pixiv\.net/img\d+/img/[^\/]+/(\d+)! + self.pixiv_id = $1 + elsif source =~ /pixiv\.net/ && source =~ /illust_id=(\d+)/ + self.pixiv_id = $1 + else + self.pixiv_id = nil + end + end + end + include FileMethods include ImageMethods include ApprovalMethods @@ -1029,6 +1044,7 @@ class Post < ActiveRecord::Base include NoteMethods include ApiMethods extend SearchMethods + include PixivMethods def reload(options = nil) super diff --git a/app/models/tag.rb b/app/models/tag.rb index 7ef47f03c..7f0e231ff 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -1,5 +1,5 @@ class Tag < ActiveRecord::Base - METATAGS = "-user|user|-approver|approver|commenter|comm|noter|-pool|pool|-fav|fav|sub|md5|-rating|rating|-locked|locked|width|height|mpixels|score|filesize|source|id|date|order|-status|status|tagcount|gentags|arttags|chartags|copytags|parent|pixiv" + METATAGS = "-user|user|-approver|approver|commenter|comm|noter|-pool|pool|-fav|fav|sub|md5|-rating|rating|-locked|locked|width|height|mpixels|score|filesize|source|id|date|order|-status|status|tagcount|gentags|arttags|chartags|copytags|parent|pixiv_id" attr_accessible :category has_one :wiki_page, :foreign_key => "name", :primary_key => "title" @@ -370,8 +370,8 @@ class Tag < ActiveRecord::Base when "status" q[:status] = $2.downcase - when "pixiv" - q[:pixiv] = parse_helper($2) + when "pixiv_id" + q[:pixiv_id] = parse_helper($2) end diff --git a/db/migrate/20130331182719_add_pixiv_id_to_posts.rb b/db/migrate/20130331182719_add_pixiv_id_to_posts.rb new file mode 100644 index 000000000..473333aa0 --- /dev/null +++ b/db/migrate/20130331182719_add_pixiv_id_to_posts.rb @@ -0,0 +1,12 @@ +class AddPixivIdToPosts < ActiveRecord::Migration + def up + execute "set statement_timeout = 0" + add_column :posts, :pixiv_id, :integer + execute "create index index_posts_on_pixiv_id on posts (pixiv_id) where pixiv_id is not null" + end + + def down + execute "set statement_timeout = 0" + remove_column :posts, :pixiv_id + end +end diff --git a/db/structure.sql b/db/structure.sql index caeca7ce2..6d43bb823 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2330,7 +2330,8 @@ CREATE TABLE posts ( has_children boolean DEFAULT false NOT NULL, is_banned boolean DEFAULT false NOT NULL, up_score integer, - down_score integer + down_score integer, + pixiv_id integer ); @@ -5993,7 +5994,7 @@ CREATE INDEX index_posts_on_parent_id ON posts USING btree (parent_id); -- Name: index_posts_on_pixiv_id; Type: INDEX; Schema: public; Owner: -; Tablespace: -- -CREATE INDEX index_posts_on_pixiv_id ON posts USING btree ((("substring"((source)::text, 'pixiv.net/img.*/([0-9]+)[^/]*$'::text))::integer)); +CREATE INDEX index_posts_on_pixiv_id ON posts USING btree (pixiv_id) WHERE (pixiv_id IS NOT NULL); -- @@ -6410,4 +6411,6 @@ INSERT INTO schema_migrations (version) VALUES ('20130326035904'); INSERT INTO schema_migrations (version) VALUES ('20130328092739'); -INSERT INTO schema_migrations (version) VALUES ('20130331180246'); \ No newline at end of file +INSERT INTO schema_migrations (version) VALUES ('20130331180246'); + +INSERT INTO schema_migrations (version) VALUES ('20130331182719'); \ No newline at end of file diff --git a/script/fixes/011.rb b/script/fixes/011.rb index e684a7be8..4d95a286a 100644 --- a/script/fixes/011.rb +++ b/script/fixes/011.rb @@ -2,5 +2,14 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')) -kv = KeyValue.find_or_create_by_key("ApiCacheGenerator.generate_tag_cache") -kv.update_attribute(:value, "0") +1.upto(2) do |i| + Post.where("source like ?", "http://i#{i}.pixiv.net%").find_each do |post| + post.parse_pixiv_id + post.update_column(:pixiv_id, post.pixiv_id) if post.pixiv_id.present? + end +end + +Post.where("source like ?", "http://www.pixiv.net%").find_each do |post| + post.parse_pixiv_id + post.update_column(:pixiv_id, post.pixiv_id) if post.pixiv_id.present? +end diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 40fd61076..3fd8ca38b 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -965,11 +965,11 @@ class PostTest < ActiveSupport::TestCase assert_equal(1, Post.tag_match("pixiv_id:34551381").count) end - should "return posts for a pixiv novel id search" do - url = "http://www.pixiv.net/novel/show.php?id=2156088" - post = FactoryGirl.create(:post, :source => url) - assert_equal(1, Post.tag_match("pixiv_novel_id:2156088").count) - end + # should "return posts for a pixiv novel id search" do + # url = "http://www.pixiv.net/novel/show.php?id=2156088" + # post = FactoryGirl.create(:post, :source => url) + # assert_equal(1, Post.tag_match("pixiv_novel_id:2156088").count) + # end should "return posts for a tag subscription search" do post1 = FactoryGirl.create(:post, :tag_string => "aaa")