Prechádzať zdrojové kódy

Fix Delete for User, Forum, Topic and Post models

Visman 4 rokov pred
rodič
commit
ae339fb655

+ 17 - 11
app/Models/Forum/Delete.php

@@ -22,7 +22,7 @@ class Delete extends Action
             throw new InvalidArgumentException('No arguments, expected User(s) or Forum(s)');
         }
 
-        $users   = [];
+        $uids    = [];
         $forums  = [];
         $all     = [];
         $isUser  = 0;
@@ -33,18 +33,21 @@ class Delete extends Action
                 if ($arg->isGuest) {
                     throw new RuntimeException('Guest can not be deleted');
                 }
-                $users[] = $arg->id;
-                $isUser  = 1;
+
+                $uids[$arg->id] = $arg->id;
+                $isUser            = 1;
             } elseif ($arg instanceof Forum) {
                 if (! $this->c->forums->get($arg->id) instanceof Forum) {
                     throw new RuntimeException('Forum unavailable');
                 }
+
                 $forums[$arg->id] = $arg;
                 $all[$arg->id]    = true;
+                $isForum          = 1;
+
                 foreach (\array_keys($arg->descendants) as $id) { //???? а если не админ?
                     $all[$id] = true;
                 }
-                $isForum = 1;
             } else {
                 throw new InvalidArgumentException('Expected User(s) or Forum(s)');
             }
@@ -60,11 +63,9 @@ class Delete extends Action
 
         $this->c->topics->delete(...$args);
 
-        //???? опросы, предупреждения
-
-        if ($users) {
-            $vars  = [
-                ':users' => $users,
+        if ($uids) {
+            $vars = [
+                ':users' => $uids,
             ];
             $query = 'DELETE
                 FROM ::mark_of_forum
@@ -72,8 +73,13 @@ class Delete extends Action
 
             $this->c->DB->exec($query, $vars);
 
-            //???? удаление модераторов из разделов
+            $query = 'UPDATE ::forums
+                SET last_poster_id=1
+                WHERE last_poster_id IN (?ai:users)';
+
+            $this->c->DB->exec($query, $vars);
         }
+
         if ($forums) {
             $this->c->subscriptions->unsubscribe(...$forums);
 
@@ -81,7 +87,7 @@ class Delete extends Action
                 $this->c->groups->Perm->reset($forum);
             }
 
-            $vars  = [
+            $vars = [
                 ':forums' => \array_keys($forums),
             ];
             $query = 'DELETE

+ 64 - 54
app/Models/Post/Delete.php

@@ -25,39 +25,43 @@ class Delete extends Action
             throw new InvalidArgumentException('No arguments, expected User(s), Forum(s), Topic(s) or Post(s)');
         }
 
-        $users        = [];
-        $usersToGuest = [];
-        $usersDel     = [];
-        $forums       = [];
-        $topics       = [];
-        $posts        = [];
-        $parents      = [];
-        $isUser       = 0;
-        $isForum      = 0;
-        $isTopic      = 0;
-        $isPost       = 0;
+        $uidsToGuest = [];
+        $uidsDelete  = [];
+        $uidsUpdate  = [];
+        $forums      = [];
+        $topics      = [];
+        $pids        = [];
+        $parents     = [];
+        $isUser      = 0;
+        $isForum     = 0;
+        $isTopic     = 0;
+        $isPost      = 0;
 
         foreach ($args as $arg) {
             if ($arg instanceof User) {
                 if ($arg->isGuest) {
                     throw new RuntimeException('Guest can not be deleted');
                 }
+
                 if (true === $arg->deleteAllPost) {
-                    $usersDel[] = $arg->id;
+                    $uidsDelete[$arg->id] = $arg->id;
                 } else {
-                    $usersToGuest[] = $arg->id;
+                    $uidsToGuest[$arg->id] = $arg->id;
                 }
+
                 $isUser = 1;
             } elseif ($arg instanceof Forum) {
                 if (! $this->c->forums->get($arg->id) instanceof Forum) {
                     throw new RuntimeException('Forum unavailable');
                 }
+
                 $forums[$arg->id] = $arg;
                 $isForum          = 1;
             } elseif ($arg instanceof Topic) {
                 if (! $arg->parent instanceof Forum) {
                     throw new RuntimeException('Parent unavailable');
                 }
+
                 $topics[$arg->id] = $arg;
                 $isTopic          = 1;
             } elseif ($arg instanceof Post) {
@@ -67,10 +71,11 @@ class Delete extends Action
                 ) {
                     throw new RuntimeException('Parents unavailable');
                 }
-                $posts[$arg->id]         = $arg->id;
-                $parents[$arg->topic_id] = $arg->parent;
-                $users[$arg->poster_id]  = $arg->poster_id;
-                $isPost                  = 1;
+
+                $pids[$arg->id]              = $arg->id;
+                $parents[$arg->topic_id]     = $arg->parent;
+                $uidsUpdate[$arg->poster_id] = $arg->poster_id;
+                $isPost                      = 1;
             } else {
                 throw new InvalidArgumentException('Expected User(s), Forum(s), Topic(s) or Post(s)');
             }
@@ -82,11 +87,11 @@ class Delete extends Action
 
         $this->c->search->delete(...$args);
 
-        //???? подписки, опросы, предупреждения
+        //???? предупреждения
 
-        if ($usersToGuest) {
-            $vars  = [
-                ':users' => $usersToGuest,
+        if ($uidsToGuest) {
+            $vars = [
+                ':users' => $uidsToGuest,
             ];
             $query = 'UPDATE ::posts
                 SET poster_id=1
@@ -100,45 +105,43 @@ class Delete extends Action
 
             $this->c->DB->exec($query, $vars);
         }
-        if ($usersDel) {
-            $vars  = [
-                ':users' => $usersDel,
-            ];
-            $query = 'UPDATE ::posts
-                SET editor_id=1
-                WHERE editor_id IN (?ai:users)';
-
-            $this->c->DB->exec($query, $vars);
 
+        if ($uidsDelete) {
+            $vars = [
+                ':users' => $uidsDelete,
+            ];
             $query = 'SELECT p.topic_id
                 FROM ::posts as p
                 WHERE p.poster_id IN (?ai:users)
                 GROUP BY p.topic_id';
 
-            $parents = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
+            $tids = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
+
+#            $query = 'SELECT t.id
+#                FROM ::topics AS t
+#                WHERE t.poster_id IN (?ai:users)';
+#
+#            $notUse = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN); // эти темы удаляются
+#
+#            $tids = \array_diff($tids, $notUse);
 
-            $query = 'SELECT t.id
-                FROM ::topics AS t
-                INNER JOIN ::posts AS p ON t.first_post_id=p.id
-                WHERE p.poster_id IN (?ai:users)';
+            $parents = $this->c->topics->loadByIds($tids, false);
 
-            $notUse = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
+            $query = 'UPDATE ::posts
+                SET editor_id=1
+                WHERE editor_id IN (?ai:users)';
 
-            $parents = \array_diff($parents, $notUse); //????
+            $this->c->DB->exec($query, $vars);
 
             $query = 'DELETE
                 FROM ::posts
                 WHERE poster_id IN (?ai:users)';
 
             $this->c->DB->exec($query, $vars);
-
-            foreach ($parents as &$parent) {
-                $parent = $this->c->topics->load($parent); //???? ааааАААААААААААААА О_о
-            }
-            unset($parent);
         }
+
         if ($forums) {
-            $vars  = [
+            $vars = [
                 ':forums' => \array_keys($forums),
             ];
             $query = 'SELECT p.poster_id
@@ -147,7 +150,7 @@ class Delete extends Action
                 WHERE t.forum_id IN (?ai:forums)
                 GROUP BY p.poster_id';
 
-            $users = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
+            $uidsUpdate = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
 
             $query = 'DELETE
                 FROM ::posts
@@ -156,10 +159,12 @@ class Delete extends Action
                     FROM ::topics
                     WHERE forum_id IN (?ai:forums)
                 )';
+
             $this->c->DB->exec($query, $vars);
         }
+
         if ($topics) {
-            $vars  = [
+            $vars = [
                 ':topics' => \array_keys($topics),
             ];
             $query = 'SELECT p.poster_id
@@ -167,7 +172,7 @@ class Delete extends Action
                 WHERE p.topic_id IN (?ai:topics)
                 GROUP BY p.poster_id';
 
-            $users = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
+            $uidsUpdate = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
 
             $query = 'DELETE
                 FROM ::posts
@@ -175,9 +180,10 @@ class Delete extends Action
 
             $this->c->DB->exec($query, $vars);
         }
-        if ($posts) {
-            $vars  = [
-                ':posts' => $posts,
+
+        if ($pids) {
+            $vars = [
+                ':posts' => $pids,
             ];
             $query = 'DELETE
                 FROM ::posts
@@ -185,21 +191,25 @@ class Delete extends Action
 
             $this->c->DB->exec($query, $vars);
         }
+
         if ($parents) {
             $topics  = $parents;
             $parents = [];
 
             foreach ($topics as $topic) {
-                $parents[$topic->forum_id] = $topic->parent;
+                $parents[$topic->parent->id] = $topic->parent;
                 $this->c->topics->update($topic->calcStat());
             }
 
-            foreach($parents as $forum) {
-                $this->c->forums->update($forum->calcStat());
+            if (! $forums) {
+                foreach ($parents as $forum) {
+                    $this->c->forums->update($forum->calcStat());
+                }
             }
         }
-        if ($users) {
-            $this->c->users->updateCountPosts(...$users);
+
+        if ($uidsUpdate) {
+            $this->c->users->updateCountPosts(...$uidsUpdate);
         }
     }
 }

+ 61 - 77
app/Models/Topic/Delete.php

@@ -24,42 +24,45 @@ class Delete extends Action
             throw new InvalidArgumentException('No arguments, expected User(s), Forum(s) or Topic(s)');
         }
 
-        $users        = [];
-        $usersToGuest = [];
-        $usersDel     = [];
-        $usersUpd     = [];
-        $forums       = [];
-        $topics       = [];
-        $parents      = [];
-        $isUser       = 0;
-        $isForum      = 0;
-        $isTopic      = 0;
+        $uids        = [];
+        $uidsToGuest = [];
+        $uidsDelete  = [];
+        $uidsUpdate  = [];
+        $forums      = [];
+        $topics      = [];
+        $parents     = [];
+        $isUser      = 0;
+        $isForum     = 0;
+        $isTopic     = 0;
 
         foreach ($args as $arg) {
             if ($arg instanceof User) {
                 if ($arg->isGuest) {
                     throw new RuntimeException('Guest can not be deleted');
                 }
+
                 if (true === $arg->deleteAllPost) {
-                    $usersDel[] = $arg->id;
+                    $uidsDelete[$arg->id] = $arg->id;
                 } else {
-                    $usersToGuest[] = $arg->id;
+                    $uidsToGuest[$arg->id] = $arg->id;
                 }
-                $users[] = $arg->id;
-                $isUser  = 1;
+
+                $uids[$arg->id] = $arg->id;
+                $isUser         = 1;
             } elseif ($arg instanceof Forum) {
                 if (! $this->c->forums->get($arg->id) instanceof Forum) {
                     throw new RuntimeException('Forum unavailable');
                 }
+
                 $forums[$arg->id] = $arg;
                 $isForum          = 1;
             } elseif ($arg instanceof Topic) {
                 if (! $arg->parent instanceof Forum) {
                     throw new RuntimeException('Parent unavailable');
                 }
-                $topics[$arg->id]        = $arg;
-                $parents[$arg->forum_id] = $arg->parent;
-                $isTopic                 = 1;
+                $topics[$arg->id]          = $arg;
+                $parents[$arg->parent->id] = $arg->parent;
+                $isTopic                   = 1;
             } else {
                 throw new InvalidArgumentException('Expected User(s), Forum(s) or Topic(s)');
             }
@@ -70,47 +73,36 @@ class Delete extends Action
         }
 
         if ($forums) {
-            $vars  = [
+            $vars = [
                 ':forums' => \array_keys($forums),
             ];
-            $query = 'SELECT p.poster_id
+            $query = 'SELECT t.id
                 FROM ::topics AS t
-                INNER JOIN ::posts AS p ON t.first_post_id=p.id
-                WHERE t.forum_id IN (?ai:forums) AND t.moved_to=0
-                GROUP BY p.poster_id';
+                WHERE t.forum_id IN (?ai:forums)';
 
-            $usersUpd = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
+            $tids   = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
+            $topics = $this->manager->loadByIds($tids, false);
         }
-        if ($topics) {
-            $vars  = [
-                ':topics' => \array_keys($topics),
-            ];
-            $query = 'SELECT p.poster_id
-                FROM ::topics AS t
-                INNER JOIN ::posts AS p ON t.first_post_id=p.id
-                WHERE t.id IN (?ai:topics) AND t.moved_to=0
-                GROUP BY p.poster_id';
 
-            $usersUpd = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
+        if ($topics) {
+            foreach ($topics as $topic) {
+                $uidsUpdate[$topic->poster_id] = $topic->poster_id;
+            }
         }
-        if ($usersDel) {
-            $vars  = [
-                ':users' => $usersDel,
+
+        if ($uidsDelete) {
+            $vars = [
+                ':users' => $uidsDelete,
             ];
-            $query = 'SELECT t.id, t.forum_id
+            $query = 'SELECT t.id
                 FROM ::topics AS t
-                INNER JOIN ::posts AS p ON t.first_post_id=p.id
-                WHERE p.poster_id IN (?ai:users)';
+                WHERE t.poster_id IN (?ai:users)';
 
-            $topics = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_KEY_PAIR); //????
+            $tids   = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
+            $topics = $this->manager->loadByIds($tids, false);
 
-            if ($topics) {
-                foreach ($topics as $value) { // ????
-                    if (isset($parents[$value])) {
-                        continue;
-                    }
-                    $parents[$value] = $this->c->forums->get($value);
-                }
+            foreach ($topics as $topic) {
+                $parents[$topic->parent->id] = $topic->parent;
             }
         }
 
@@ -118,11 +110,9 @@ class Delete extends Action
 
         //???? опросы, предупреждения
 
-        // удаление тем-ссылок на удаляемые темы
-
-        if ($users) {
-            $vars  = [
-                ':users' => $users,
+        if ($uids) {
+            $vars = [
+                ':users' => $uids,
             ];
             $query = 'DELETE
                 FROM ::mark_of_topic
@@ -130,9 +120,10 @@ class Delete extends Action
 
             $this->c->DB->exec($query, $vars);
         }
-        if ($usersToGuest) {
-            $vars  = [
-                ':users' => $usersToGuest,
+
+        if ($uidsToGuest) {
+            $vars = [
+                ':users' => $uidsToGuest,
             ];
             $query = 'UPDATE ::topics
                 SET poster_id=1
@@ -146,30 +137,15 @@ class Delete extends Action
 
             $this->c->DB->exec($query, $vars);
         }
-        if ($forums) {
-            $vars  = [
-                ':forums' => \array_keys($forums),
-            ];
-            $query = 'DELETE
-                FROM ::mark_of_topic
-                WHERE tid IN (
-                    SELECT id
-                    FROM ::topics
-                    WHERE forum_id IN (?ai:forums)
-                )';
-
-            $this->c->DB->exec($query, $vars);
-
-            $query = 'DELETE
-                FROM ::topics
-                WHERE forum_id IN (?ai:forums)';
 
-            $this->c->DB->exec($query, $vars);
-        }
         if ($topics) {
+            if (isset($topics[0])) { // O_o
+                throw new RuntimeException('Bad topic');
+            }
+
             $this->c->subscriptions->unsubscribe(...$topics);
 
-            $vars  = [
+            $vars = [
                 ':topics' => \array_keys($topics),
             ];
             $query = 'DELETE
@@ -184,13 +160,21 @@ class Delete extends Action
 
             $this->c->DB->exec($query, $vars);
 
+            $query = 'DELETE
+                FROM ::topics
+                WHERE moved_to IN (?ai:topics)';
+
+            $this->c->DB->exec($query, $vars);
+        }
+
+        if ($parents) {
             foreach ($parents as $forum) {
                 $this->c->forums->update($forum->calcStat());
             }
         }
 
-        if ($usersUpd) {
-            $this->c->users->UpdateCountTopics(...$usersUpd);
+        if ($uidsUpdate) {
+            $this->c->users->UpdateCountTopics(...$uidsUpdate);
         }
     }
 }

+ 14 - 16
app/Models/User/Delete.php

@@ -7,7 +7,6 @@ namespace ForkBB\Models\User;
 use ForkBB\Models\Action;
 use ForkBB\Models\User\Model as User;
 use ForkBB\Models\Forum\Model as Forum;
-use ForkBB\Models\Forum\Manager as ForumManager;
 use InvalidArgumentException;
 use RuntimeException;
 
@@ -22,28 +21,27 @@ class Delete extends Action
             throw new InvalidArgumentException('No arguments, expected User(s)');
         }
 
-        $ids          = [];
-        $moderators   = [];
-        $adminPresent = false;
+        $ids        = [];
+        $moderators = [];
+        $resetAdmin = false;
+
         foreach ($users as $user) {
             if ($user->isGuest) {
                 throw new RuntimeException('Guest can not be deleted');
             }
-
-            $ids[] = $user->id;
-
             if ($user->isAdmMod) {
                 $moderators[$user->id] = $user;
             }
             if ($user->isAdmin) {
-                $adminPresent = true;
+                $resetAdmin = true;
             }
+
+            $ids[] = $user->id;
         }
 
-        if (! empty($moderators)) {
-            $forums = new ForumManager($this->c);
-            $forums->init($this->c->groups->get($this->c->GROUP_ADMIN));
-            $root = $forums->get(0);
+        if ($moderators) {
+            $forums = $this->c->ForumManager->init($this->c->groups->get($this->c->GROUP_ADMIN));
+            $root   = $forums->get(0);
 
             if ($root instanceof Forum) {
                 foreach ($root->descendants as $forum) {
@@ -53,10 +51,10 @@ class Delete extends Action
             }
         }
 
-        $this->c->forums->delete(...$users);
         $this->c->subscriptions->unsubscribe(...$users);
+        $this->c->forums->delete(...$users);
 
-        //???? опросы, предупреждения
+        //???? предупреждения
 
         foreach ($users as $user) {
             $this->c->Online->delete($user);
@@ -64,7 +62,7 @@ class Delete extends Action
             $user->deleteAvatar();
         }
 
-        $vars  = [
+        $vars = [
             ':users' => $ids,
         ];
         $query = 'DELETE
@@ -73,7 +71,7 @@ class Delete extends Action
 
         $this->c->DB->exec($query, $vars);
 
-        if ($adminPresent) {
+        if ($resetAdmin) {
             $this->c->admins->reset();
         }
         $this->c->stats->reset();