public function run() { Rails::systemExit()->register(function () { if (!$this->finished) { $this->active = false; $this->data->success = false; $this->data->error = "Couldn't finish successfuly"; $this->save(); } }); # Ugly: set the current user ID to the one set in the batch, so history entries # will be created as that user. // $old_thread_user = Thread::current["danbooru-user"]; // $old_thread_user_id = Thread::current["danbooru-user_id"]; // $old_ip_addr = Thread::current["danbooru-ip_addr"]; // Thread::current["danbooru-user"] = User::find_by_id(self.user_id) // Thread::current["danbooru-user_id"] = $this->user_id // Thread::current["danbooru-ip_addr"] = $this->ip $this->active = true; $this->save(); $post = Post::create(['source' => $this->url, 'tags' => $this->tags, 'updater_user_id' => $this->user_id, 'updater_ip_addr' => $this->ip, 'user_id' => $this->user_id, 'ip_addr' => $this->ip, 'status' => "active"]); if ($post->errors()->blank()) { if (CONFIG()->dupe_check_on_upload && $post->image() && !$post->parent_id) { $options = ['services' => SimilarImages::get_services("local"), 'type' => 'post', 'source' => $post]; $res = SimilarImages::similar_images($options); if (!empty($res['posts'])) { $post->tags = $post->tags() . " possible_duplicate"; $post->save(); } } $this->data->success = true; $this->data->post_id = $post->id; } elseif ($post->errors()->on('md5')) { // $p = $post->errors(); $p = Post::where(['md5' => $post->md5])->first(); $this->data->success = false; $this->data->error = "Post already exists"; $this->data->post_id = $p->id; } else { // p $post.errors $this->data->success = false; $this->data->error = $post->errors()->fullMessages(", "); } if ($this->data->success) { $this->status = 'finished'; } else { $this->status = 'error'; } $this->active = false; $this->save(); $this->finished = true; // Thread::current["danbooru-user"] = old_thread_user // Thread::current["danbooru-user_id"] = old_thread_user_id // Thread::current["danbooru-ip_addr"] = old_ip_addr }
public function execute_external_data_search() { # current_user will be needed to save post history. # Set the first admin as current user. User::set_current_user(User::where('level = ?', CONFIG()->user_levels['Admin'])->first()); if (empty($this->data->last_post_id)) { $this->data->last_post_id = 0; } $post_id = $this->data->last_post_id + 1; $config = array_merge(['servers' => [], 'interval' => 3, 'source' => true, 'merge_tags' => true, 'limit' => 100, 'set_rating' => false, 'exclude_tags' => [], 'similarity' => 90], CONFIG()->external_data_search_config); $limit = $config['limit']; $interval = $config['interval']; $search_options = ['type' => 'post', 'data_search' => true, 'services' => $config['servers'], 'threshold' => $config['similarity']]; $post_count = !$limit ? -1 : 0; while ($post_count < $limit) { if (!($post = Post::where('id >= ? AND status != "deleted"', $post_id)->order('id ASC')->first())) { break; } $search_options['source'] = $post; $new_tags = []; $source = null; $external_posts = SimilarImages::similar_images($search_options)['posts_external']; $rating_set = false; foreach ($external_posts as $ep) { if (!$rating_set && $config['set_rating'] && $ep->rating) { $post->rating = $ep->rating; $rating_set = true; } if ($config['source'] && !$source && $ep->source) { $source = $ep->source; } $new_tags = array_merge($new_tags, explode(' ', $ep->tags)); } # Exclude tags. $new_tags = array_diff($new_tags, $config['exclude_tags']); if ($config['merge_tags']) { $new_tags = array_merge($new_tags, $post->tags); } $new_tags = array_filter(array_unique($new_tags)); $post->new_tags = $new_tags; if ($source) { } $post->source = $source; $post->save(); if ($limit) { $post_count++; } $this->update_data(['last_post_id' => $post->id]); $post_id = $post->id + 1; if ($config['interval']) { sleep($config['interval']); } } }
public function searchExternalData() { if (!CONFIG()->enable_find_external_data) { throw new Rails\ActiveRecord\Exception\RecordNotFoundException(); } if ($this->params()->ids) { $ids = $this->params()->ids; !is_array($ids) && ($ids = [$ids]); $this->posts = Post::where('id IN (?)', $ids)->take(); } else { $this->posts = new Rails\ActiveRecord\Collection(); } $this->services = SimilarImages::get_services('all'); }