示例#1
0
 public static function batch_activate($user_id, $post_ids)
 {
     $conds = $cond_params = array();
     if ($user_id) {
         $conds[] = "user_id = ?";
         $cond_params[] = $user_id;
     }
     $conds[] = "is_held";
     $conds[] = "id = ?";
     # Tricky: we want posts to show up in the index in the same order they were posted.
     # If we just bump the posts, the index_timestamps will all be the same, and they'll
     # show up in an undefined order.    We don't want to do this in the ORDER BY when
     # searching, because that's too expensive.    Instead, tweak the timestamps slightly:
     # for each post updated, set the index_timestamps 1ms newer than the previous.
     #
     # Returns the number of posts actually activated.
     $count = 0;
     # Original function is kinda confusing...
     # If anyone knows a better way to do this, it'll be welcome.
     sort($post_ids);
     $s = 1;
     $timestamp = strtotime(self::connection()->selectValue("SELECT index_timestamp FROM posts ORDER BY id DESC LIMIT 1"));
     foreach ($post_ids as $id) {
         $timestamp += $s;
         $params = array_merge(array('UPDATE posts SET index_timestamp = ?, is_held = 0 WHERE ' . implode(' AND ', $conds)), array(date('Y-m-d H:i:s', $timestamp)), $cond_params, array($id));
         self::connection()->executeSql($params);
         $count++;
         $s++;
     }
     if ($count > 0) {
         Moebooru\CacheHelper::expire();
     }
     return $count;
 }
示例#2
0
 public function expire_tag_cache_after_deletion()
 {
     Moebooru\CacheHelper::expire_tag_version();
 }
示例#3
0
文件: Pool.php 项目: JCQS04/myimouto
 public function expire_cache()
 {
     Moebooru\CacheHelper::expire();
 }
示例#4
0
 /**
  * It's strange how actions like changing pool's sequence, that updates the post via
  * update_batch and sends only one metatag: 'pool:$id:$seq' and doesn't send old_tags, don't
  * mess up the rest of the tags (because that metatag is discarted, and the post is left without
  * old_tags or any tag).
  * So we're gonna do a workaround for that: if the new tags only contain metatags, we won't
  * take any further action but commit such metatags.
  * We'll use $had_metatags for that.
  */
 protected function commit_tags()
 {
     if ($this->isNewRecord() || !$this->new_tags) {
         return;
     }
     if ($this->old_tags) {
         # If someone else committed changes to this post before we did,
         # then try to merge the tag changes together.
         $current_tags = explode(' ', $this->cached_tags);
         $this->old_tags = Tag::scan_tags($this->old_tags);
         $this->new_tags = array_filter(array_merge(array_diff(array_merge($current_tags, $this->new_tags), $this->old_tags), array_intersect($current_tags, $this->new_tags)));
     }
     $this->commit_metatags();
     $meta_tags = ['-pool:', 'pool:', 'rating:', 'parent:', 'child:', 'source:'];
     $ratings = ['q', 's', 'e'];
     $had_metatags = false;
     foreach ($this->new_tags as $k => $tag) {
         # To avoid preg_match.
         $is_mt = false;
         foreach ($meta_tags as $mt) {
             if (strpos($tag, $mt) === 0 || in_array($tag, $ratings)) {
                 $is_mt = true;
                 break;
             }
         }
         if (!$is_mt) {
             continue;
         }
         $had_metatags = true;
         unset($this->new_tags[$k]);
         if (in_array($tag, $ratings)) {
             $tag = 'rating:' . $tag;
         }
         $subparam = explode(':', $tag, 3);
         $metatag = array_shift($subparam);
         $param = array_shift($subparam);
         $subparam = empty($subparam) ? null : array_shift($subparam);
         switch ($metatag) {
             case 'rating':
                 # Change rating. This will override rating selected on radio buttons.
                 if (in_array($param, $ratings)) {
                     $this->rating = $param;
                 }
                 break;
             case 'pool':
                 try {
                     $name = $param;
                     $seq = $subparam;
                     # Workaround: I don't understand how can the pool be found when finding_by_name
                     # using the id.
                     if (ctype_digit($name)) {
                         $pool = Pool::where(['id' => $name])->first();
                     } else {
                         $pool = Pool::where(['name' => $name])->first();
                     }
                     # Set :ignore_already_exists, so pool:1:2 can be used to change the sequence number
                     # of a post that already exists in the pool.
                     $options = array('user' => User::where('id = ?', $this->updater_user_id)->first(), 'ignore_already_exists' => true);
                     if ($seq) {
                         $options['sequence'] = $seq;
                     }
                     if (!$pool and !ctype_digit($name)) {
                         $pool = Pool::create(array('name' => $name, 'is_public' => false, 'user_id' => $this->updater_user_id));
                     }
                     if (!$pool || !$pool->can_change(current_user(), null)) {
                         continue;
                     }
                     $pool->add_post($this->id, $options);
                 } catch (Pool_PostAlreadyExistsError $e) {
                 } catch (Pool_AccessDeniedError $e) {
                 }
                 break;
             case '-pool':
                 $name = $param;
                 $cmd = $subparam;
                 $pool = Pool::where(['name' => $name])->first();
                 if (!$pool->can_change(current_user(), null)) {
                     break;
                 }
                 if ($cmd == "parent") {
                     # If we have a parent, remove ourself from the pool and add our parent in
                     # our place.    If we have no parent, do nothing and leave us in the pool.
                     if (!empty($this->parent_id)) {
                         $pool->transfer_post_to_parent($this->id, $this->parent_id);
                         break;
                     }
                 }
                 $pool && $pool->remove_post($id);
                 break;
             case 'source':
                 $this->source = $param;
                 break;
             case 'parent':
                 if (is_numeric($param)) {
                     $this->parent_id = (int) $param;
                 }
                 break;
             case 'child':
                 unset($this->new_tags[$k]);
                 break;
         }
     }
     if (!$this->new_tags) {
         if ($had_metatags) {
             return;
         }
         $this->new_tags[] = "tagme";
     }
     // $this->tags = implode(' ', array_unique(TagImplication::with_implied(TagAlias::to_aliased($this->new_tags))));
     $this->new_tags = TagAlias::to_aliased($this->new_tags);
     $this->new_tags = array_unique(TagImplication::with_implied($this->new_tags));
     sort($this->new_tags);
     // $this->tags = implode(' ', $this->tags());
     # TODO: be more selective in deleting from the join table
     self::connection()->executeSql("DELETE FROM posts_tags WHERE post_id = ?", $this->id);
     $new_tags_ids = [];
     $new_tags_names = [];
     $this->new_tags = array_map(function ($x) use(&$new_tags_ids, &$new_tags_names) {
         $tag = Tag::find_or_create_by_name($x);
         if (!in_array($tag->id, $new_tags_ids)) {
             $new_tags_ids[] = $tag->id;
             $new_tags_names[] = $tag->name;
             return $tag;
         }
     }, $this->new_tags);
     unset($new_tags_ids);
     $this->new_tags = array_filter($this->new_tags);
     # If any tags are newly active, expire the tag cache.
     if ($this->new_tags) {
         $any_new_tags = false;
         $previous_tags = explode(' ', $this->cached_tags);
         foreach ($this->new_tags as $tag) {
             # If this tag is in old_tags, then it's already active and we just removed it
             # in the above DELETE, so it's not really a newly activated tag.    (This isn't
             # self.old_tags; that's the tags the user saw before he edited, not the data
             # we're replacing.)
             if ($tag->post_count == 0 and !in_array($tag->name, $previous_tags)) {
                 $any_new_tags = true;
                 break;
             }
         }
         if ($any_new_tags) {
             Moebooru\CacheHelper::expire_tag_version();
         }
     }
     # Sort
     sort($new_tags_names);
     sort($this->new_tags);
     $tag_set = implode(", ", array_map(function ($x) {
         return "(" . $this->id . ", " . $x->id . ")";
     }, $this->new_tags));
     $this->cached_tags = implode(' ', $new_tags_names);
     $sql = "INSERT INTO posts_tags (post_id, tag_id) VALUES " . $tag_set;
     self::connection()->executeSql($sql);
     # Store the old cached_tags, so we can expire them.
     $this->old_cached_tags = $this->cached_tags;
     // Commenting the following line; it will cause cached_tags to not to be updated.
     // They're set above.
     // $this->cached_tags = self::connection()->selectValue("SELECT cached_tags FROM posts WHERE id = " . $this->id);
     $this->new_tags = null;
 }