|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Ideas extension for the phpBB Forum Software package. |
| 5 | + * |
| 6 | + * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | + * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +namespace phpbb\ideas\migrations; |
| 12 | + |
| 13 | +class m9_remove_idea_bot extends \phpbb\db\migration\migration |
| 14 | +{ |
| 15 | + public function effectively_installed() |
| 16 | + { |
| 17 | + return !$this->config->offsetExists('ideas_poster_id'); |
| 18 | + } |
| 19 | + |
| 20 | + static public function depends_on() |
| 21 | + { |
| 22 | + return [ |
| 23 | + '\phpbb\ideas\migrations\m1_initial_schema', |
| 24 | + '\phpbb\ideas\migrations\m3_acp_data', |
| 25 | + '\phpbb\ideas\migrations\m4_update_statuses', |
| 26 | + '\phpbb\ideas\migrations\m6_migrate_old_tables', |
| 27 | + '\phpbb\ideas\migrations\m7_drop_old_tables', |
| 28 | + ]; |
| 29 | + } |
| 30 | + |
| 31 | + public function update_data() |
| 32 | + { |
| 33 | + return [ |
| 34 | + ['custom', [[$this, 'update_topic_authors']]], |
| 35 | + ['config.remove', ['ideas_poster_id']], |
| 36 | + ]; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Replace the Ideas Bot stored in the posts and topics tables with the |
| 41 | + * original author's information. Bot gone, order restored to universe. |
| 42 | + */ |
| 43 | + public function update_topic_authors() |
| 44 | + { |
| 45 | + // Return if the Ideas Bot does not exist at this point for some reason. |
| 46 | + if (!$this->config->offsetExists('ideas_poster_id')) |
| 47 | + { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // Get real author info for ideas that were posted by the Ideas Bot |
| 52 | + $topics = []; |
| 53 | + $sql_array = [ |
| 54 | + 'SELECT' => 'i.topic_id, i.idea_author, u.username, u.user_colour, t.topic_first_post_id', |
| 55 | + 'FROM' => [ |
| 56 | + $this->table_prefix . 'ideas_ideas' => 'i', |
| 57 | + ], |
| 58 | + 'LEFT_JOIN' => [ |
| 59 | + [ |
| 60 | + 'FROM' => [$this->table_prefix . 'topics' => 't'], |
| 61 | + 'ON' => 't.topic_id = i.topic_id', |
| 62 | + ], |
| 63 | + [ |
| 64 | + 'FROM' => [$this->table_prefix . 'users' => 'u'], |
| 65 | + 'ON' => 'u.user_id = i.idea_author', |
| 66 | + ], |
| 67 | + ], |
| 68 | + 'WHERE' => 't.topic_poster = ' . (int) $this->config->offsetGet('ideas_poster_id'), |
| 69 | + ]; |
| 70 | + |
| 71 | + $sql = $this->db->sql_build_query('SELECT', $sql_array); |
| 72 | + $result = $this->db->sql_query($sql); |
| 73 | + while ($row = $this->db->sql_fetchrow($result)) |
| 74 | + { |
| 75 | + $topics[$row['topic_id']] = [ |
| 76 | + 'topic_poster_id' => $row['idea_author'] ?: ANONYMOUS, |
| 77 | + 'topic_poster_name' => $row['username'] ?: '', |
| 78 | + 'topic_poster_colour' => $row['user_colour'] ?: '', |
| 79 | + 'topic_first_post_id' => $row['topic_first_post_id'] ?: 0, |
| 80 | + ]; |
| 81 | + } |
| 82 | + $this->db->sql_freeresult($result); |
| 83 | + |
| 84 | + // Begin updating topics and posts |
| 85 | + $this->db->sql_transaction('begin'); |
| 86 | + foreach ($topics as $topic_id => $data) |
| 87 | + { |
| 88 | + // Update topic author (first poster) |
| 89 | + $sql = 'UPDATE ' . $this->table_prefix . 'topics |
| 90 | + SET ' . $this->db->sql_build_array('UPDATE', [ |
| 91 | + 'topic_poster' => $data['topic_poster_id'], |
| 92 | + 'topic_first_poster_name' => $data['topic_poster_name'], |
| 93 | + 'topic_first_poster_colour' => $data['topic_poster_colour'], |
| 94 | + ]) . ' |
| 95 | + WHERE topic_id = ' . (int) $topic_id; |
| 96 | + $this->db->sql_query($sql); |
| 97 | + |
| 98 | + // Update last poster if it's also the Ideas Bot (i.e: no replies) |
| 99 | + $sql = 'UPDATE ' . $this->table_prefix . 'topics |
| 100 | + SET ' . $this->db->sql_build_array('UPDATE', [ |
| 101 | + 'topic_last_poster_id' => $data['topic_poster_id'], |
| 102 | + 'topic_last_poster_name' => $data['topic_poster_name'], |
| 103 | + 'topic_last_poster_colour' => $data['topic_poster_colour'], |
| 104 | + ]) . ' |
| 105 | + WHERE topic_id = ' . (int) $topic_id . ' |
| 106 | + AND topic_last_poster_id = ' . (int) $this->config->offsetGet('ideas_poster_id'); |
| 107 | + $this->db->sql_query($sql); |
| 108 | + |
| 109 | + // Update first post's poster id if it's the Ideas Bot |
| 110 | + $sql = 'UPDATE ' . $this->table_prefix . 'posts' . ' |
| 111 | + SET poster_id = ' . (int) $data['topic_poster_id'] . ' |
| 112 | + WHERE post_id = ' . (int) $data['topic_first_post_id'] . ' |
| 113 | + AND poster_id = ' . (int) $this->config->offsetGet('ideas_poster_id'); |
| 114 | + $this->db->sql_query($sql); |
| 115 | + } |
| 116 | + $this->db->sql_transaction('commit'); |
| 117 | + } |
| 118 | +} |
0 commit comments