2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-07-05 17:17:03 -04:00
|
|
|
require_relative 'database_base'
|
|
|
|
require_relative '../support/constants'
|
|
|
|
|
|
|
|
module ImportScripts::PhpBB3
|
|
|
|
class Database_3_0 < DatabaseBase
|
|
|
|
def count_users
|
|
|
|
count(<<-SQL)
|
|
|
|
SELECT COUNT(*) AS count
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}users u
|
2015-07-05 17:17:03 -04:00
|
|
|
WHERE u.user_type != #{Constants::USER_TYPE_IGNORE}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2022-02-16 07:04:31 -05:00
|
|
|
def fetch_users(last_user_id, _profile_fields)
|
2016-01-14 16:43:19 -05:00
|
|
|
query(<<-SQL, :user_id)
|
2015-08-20 16:15:57 -04:00
|
|
|
SELECT u.user_id, u.user_email, u.username, u.user_password, u.user_regdate, u.user_lastvisit, u.user_ip,
|
2015-07-05 17:17:03 -04:00
|
|
|
u.user_type, u.user_inactive_reason, g.group_name, b.ban_start, b.ban_end, b.ban_reason,
|
2018-03-16 19:36:12 -04:00
|
|
|
u.user_posts, u.user_website, u.user_from, u.user_birthday, u.user_avatar_type, u.user_avatar
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}users u
|
2016-08-19 15:35:32 -04:00
|
|
|
LEFT OUTER JOIN #{@table_prefix}groups g ON (g.group_id = u.group_id)
|
2016-07-11 17:59:15 -04:00
|
|
|
LEFT OUTER JOIN #{@table_prefix}banlist b ON (
|
2015-07-05 17:17:03 -04:00
|
|
|
u.user_id = b.ban_userid AND b.ban_exclude = 0 AND
|
|
|
|
(b.ban_end = 0 OR b.ban_end >= UNIX_TIMESTAMP())
|
|
|
|
)
|
2016-01-14 16:43:19 -05:00
|
|
|
WHERE u.user_id > #{last_user_id} AND u.user_type != #{Constants::USER_TYPE_IGNORE}
|
|
|
|
ORDER BY u.user_id
|
2015-07-05 17:17:03 -04:00
|
|
|
LIMIT #{@batch_size}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
def count_anonymous_users
|
|
|
|
count(<<-SQL)
|
2018-01-18 04:39:06 -05:00
|
|
|
SELECT COUNT(DISTINCT p.post_username) AS count
|
|
|
|
FROM #{@table_prefix}posts p
|
|
|
|
JOIN #{@table_prefix}users u ON (p.poster_id = u.user_id)
|
|
|
|
WHERE p.post_username <> '' AND u.user_type = #{Constants::USER_TYPE_IGNORE}
|
2015-07-05 17:17:03 -04:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2016-01-14 16:43:19 -05:00
|
|
|
def fetch_anonymous_users(last_username)
|
|
|
|
last_username = escape(last_username)
|
|
|
|
|
|
|
|
query(<<-SQL, :post_username)
|
2018-01-18 04:39:06 -05:00
|
|
|
SELECT p.post_username, MIN(p.post_time) AS first_post_time
|
|
|
|
FROM #{@table_prefix}posts p
|
|
|
|
JOIN #{@table_prefix}users u ON (p.poster_id = u.user_id)
|
|
|
|
WHERE p.post_username > '#{last_username}' AND u.user_type = #{Constants::USER_TYPE_IGNORE}
|
|
|
|
GROUP BY p.post_username
|
|
|
|
ORDER BY p.post_username
|
2015-07-05 17:17:03 -04:00
|
|
|
LIMIT #{@batch_size}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2021-01-14 14:44:43 -05:00
|
|
|
def fetch_groups
|
|
|
|
query(<<-SQL)
|
|
|
|
SELECT g.group_id, g.group_type, g.group_name, g.group_desc
|
|
|
|
FROM #{@table_prefix}groups g
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_group_users
|
|
|
|
query(<<-SQL)
|
|
|
|
SELECT ug.group_id, ug.user_id, ug.group_leader
|
|
|
|
FROM #{@table_prefix}user_group ug
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2015-07-05 17:17:03 -04:00
|
|
|
def fetch_categories
|
|
|
|
query(<<-SQL)
|
2015-07-27 17:02:45 -04:00
|
|
|
SELECT f.forum_id, f.parent_id, f.forum_name, f.forum_desc, x.first_post_time
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}forums f
|
2015-07-05 17:17:03 -04:00
|
|
|
LEFT OUTER JOIN (
|
|
|
|
SELECT MIN(topic_time) AS first_post_time, forum_id
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}topics
|
2015-07-05 17:17:03 -04:00
|
|
|
GROUP BY forum_id
|
|
|
|
) x ON (f.forum_id = x.forum_id)
|
|
|
|
WHERE f.forum_type != #{Constants::FORUM_TYPE_LINK}
|
2016-01-14 16:43:19 -05:00
|
|
|
ORDER BY f.parent_id, f.left_id
|
2015-07-05 17:17:03 -04:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
def count_posts
|
|
|
|
count(<<-SQL)
|
|
|
|
SELECT COUNT(*) AS count
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}posts
|
2015-07-05 17:17:03 -04:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2016-01-14 16:43:19 -05:00
|
|
|
def fetch_posts(last_post_id)
|
|
|
|
query(<<-SQL, :post_id)
|
2015-07-05 17:17:03 -04:00
|
|
|
SELECT p.post_id, p.topic_id, t.forum_id, t.topic_title, t.topic_first_post_id, p.poster_id,
|
2018-03-07 13:27:03 -05:00
|
|
|
p.post_text, p.post_time, t.topic_status, t.topic_type, t.poll_title, t.topic_views,
|
2015-07-05 17:17:03 -04:00
|
|
|
CASE WHEN t.poll_length > 0 THEN t.poll_start + t.poll_length ELSE NULL END AS poll_end,
|
2018-03-16 13:32:27 -04:00
|
|
|
t.poll_max_options, p.post_attachment, p.poster_ip,
|
2018-01-18 04:39:06 -05:00
|
|
|
CASE WHEN u.user_type = #{Constants::USER_TYPE_IGNORE} THEN p.post_username ELSE NULL END post_username
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}posts p
|
|
|
|
JOIN #{@table_prefix}topics t ON (p.topic_id = t.topic_id)
|
2020-03-06 16:54:40 -05:00
|
|
|
LEFT OUTER JOIN #{@table_prefix}users u ON (p.poster_id = u.user_id)
|
2016-01-14 16:43:19 -05:00
|
|
|
WHERE p.post_id > #{last_post_id}
|
|
|
|
ORDER BY p.post_id
|
2015-07-05 17:17:03 -04:00
|
|
|
LIMIT #{@batch_size}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_first_post_id(topic_id)
|
2015-09-30 15:55:34 -04:00
|
|
|
query(<<-SQL).try(:first).try(:[], :topic_first_post_id)
|
2015-07-05 17:17:03 -04:00
|
|
|
SELECT topic_first_post_id
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}topics
|
2015-07-05 17:17:03 -04:00
|
|
|
WHERE topic_id = #{topic_id}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_poll_options(topic_id)
|
|
|
|
query(<<-SQL)
|
2016-04-23 16:42:25 -04:00
|
|
|
SELECT o.poll_option_id, o.poll_option_text, o.poll_option_total AS total_votes,
|
|
|
|
o.poll_option_total - (
|
|
|
|
SELECT COUNT(DISTINCT v.vote_user_id)
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}poll_votes v
|
|
|
|
JOIN #{@table_prefix}users u ON (v.vote_user_id = u.user_id)
|
|
|
|
JOIN #{@table_prefix}topics t ON (v.topic_id = t.topic_id)
|
2016-04-23 16:42:25 -04:00
|
|
|
WHERE v.poll_option_id = o.poll_option_id AND v.topic_id = o.topic_id
|
|
|
|
) AS anonymous_votes
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}poll_options o
|
2016-04-23 16:42:25 -04:00
|
|
|
WHERE o.topic_id = #{topic_id}
|
|
|
|
ORDER BY o.poll_option_id
|
2015-07-05 17:17:03 -04:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_poll_votes(topic_id)
|
2016-04-23 16:42:25 -04:00
|
|
|
# this query ignores invalid votes that belong to non-existent users or topics
|
2015-07-05 17:17:03 -04:00
|
|
|
query(<<-SQL)
|
|
|
|
SELECT u.user_id, v.poll_option_id
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}poll_votes v
|
|
|
|
JOIN #{@table_prefix}poll_options o ON (v.poll_option_id = o.poll_option_id AND v.topic_id = o.topic_id)
|
|
|
|
JOIN #{@table_prefix}users u ON (v.vote_user_id = u.user_id)
|
|
|
|
JOIN #{@table_prefix}topics t ON (v.topic_id = t.topic_id)
|
2015-07-05 17:17:03 -04:00
|
|
|
WHERE v.topic_id = #{topic_id}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2016-04-23 16:42:25 -04:00
|
|
|
def get_voters(topic_id)
|
2015-07-05 17:17:03 -04:00
|
|
|
# anonymous voters can't be counted, but lets try to make the count look "correct" anyway
|
2016-04-23 16:42:25 -04:00
|
|
|
query(<<-SQL).first
|
|
|
|
SELECT MAX(x.total_voters) AS total_voters,
|
|
|
|
MAX(x.total_voters) - (
|
|
|
|
SELECT COUNT(DISTINCT v.vote_user_id)
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}poll_votes v
|
|
|
|
JOIN #{@table_prefix}poll_options o ON (v.poll_option_id = o.poll_option_id AND v.topic_id = o.topic_id)
|
|
|
|
JOIN #{@table_prefix}users u ON (v.vote_user_id = u.user_id)
|
|
|
|
JOIN #{@table_prefix}topics t ON (v.topic_id = t.topic_id)
|
2016-04-23 16:42:25 -04:00
|
|
|
WHERE v.topic_id = #{topic_id}
|
|
|
|
) AS anonymous_voters
|
2015-07-05 17:17:03 -04:00
|
|
|
FROM (
|
2016-04-23 16:42:25 -04:00
|
|
|
SELECT COUNT(DISTINCT vote_user_id) AS total_voters
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}poll_votes
|
2015-07-05 17:17:03 -04:00
|
|
|
WHERE topic_id = #{topic_id}
|
|
|
|
UNION
|
2016-04-23 16:42:25 -04:00
|
|
|
SELECT MAX(poll_option_total) AS total_voters
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}poll_options
|
2015-07-05 17:17:03 -04:00
|
|
|
WHERE topic_id = #{topic_id}
|
|
|
|
) x
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_max_attachment_size
|
|
|
|
query(<<-SQL).first[:filesize]
|
|
|
|
SELECT IFNULL(MAX(filesize), 0) AS filesize
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}attachments
|
2015-07-05 17:17:03 -04:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_attachments(topic_id, post_id)
|
|
|
|
query(<<-SQL)
|
|
|
|
SELECT physical_filename, real_filename
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}attachments
|
2015-07-05 17:17:03 -04:00
|
|
|
WHERE topic_id = #{topic_id} AND post_msg_id = #{post_id}
|
2016-01-14 16:43:19 -05:00
|
|
|
ORDER BY filetime DESC, post_msg_id
|
2015-07-05 17:17:03 -04:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2016-04-24 10:31:41 -04:00
|
|
|
def count_messages
|
|
|
|
count(<<-SQL)
|
|
|
|
SELECT COUNT(*) AS count
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}privmsgs m
|
2016-04-24 10:31:41 -04:00
|
|
|
WHERE NOT EXISTS ( -- ignore duplicate messages
|
|
|
|
SELECT 1
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}privmsgs x
|
2016-04-24 10:31:41 -04:00
|
|
|
WHERE x.msg_id < m.msg_id AND x.root_level = m.root_level AND x.author_id = m.author_id
|
|
|
|
AND x.to_address = m.to_address AND x.message_time = m.message_time
|
|
|
|
)
|
|
|
|
SQL
|
2015-07-05 17:17:03 -04:00
|
|
|
end
|
|
|
|
|
2016-04-24 10:31:41 -04:00
|
|
|
def fetch_messages(last_msg_id)
|
|
|
|
query(<<-SQL, :msg_id)
|
|
|
|
SELECT m.msg_id, m.root_level AS root_msg_id, m.author_id, m.message_time, m.message_subject,
|
|
|
|
m.message_text, m.to_address, r.author_id AS root_author_id, r.to_address AS root_to_address, (
|
|
|
|
SELECT COUNT(*)
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}attachments a
|
2016-04-24 10:31:41 -04:00
|
|
|
WHERE a.topic_id = 0 AND m.msg_id = a.post_msg_id
|
|
|
|
) AS attachment_count
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}privmsgs m
|
|
|
|
LEFT OUTER JOIN #{@table_prefix}privmsgs r ON (m.root_level = r.msg_id)
|
2016-04-24 10:31:41 -04:00
|
|
|
WHERE m.msg_id > #{last_msg_id}
|
|
|
|
AND NOT EXISTS ( -- ignore duplicate messages
|
|
|
|
SELECT 1
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}privmsgs x
|
2016-04-24 10:31:41 -04:00
|
|
|
WHERE x.msg_id < m.msg_id AND x.root_level = m.root_level AND x.author_id = m.author_id
|
|
|
|
AND x.to_address = m.to_address AND x.message_time = m.message_time
|
|
|
|
)
|
|
|
|
ORDER BY m.msg_id
|
|
|
|
LIMIT #{@batch_size}
|
|
|
|
SQL
|
2015-07-05 17:17:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def count_bookmarks
|
|
|
|
count(<<-SQL)
|
|
|
|
SELECT COUNT(*) AS count
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}bookmarks
|
2015-07-05 17:17:03 -04:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2016-01-14 16:43:19 -05:00
|
|
|
def fetch_bookmarks(last_user_id, last_topic_id)
|
|
|
|
query(<<-SQL, :user_id, :topic_first_post_id)
|
2015-07-05 17:17:03 -04:00
|
|
|
SELECT b.user_id, t.topic_first_post_id
|
2016-07-11 17:59:15 -04:00
|
|
|
FROM #{@table_prefix}bookmarks b
|
|
|
|
JOIN #{@table_prefix}topics t ON (b.topic_id = t.topic_id)
|
2022-02-16 07:04:31 -05:00
|
|
|
WHERE (b.user_id, b.topic_id) > (#{last_user_id}, #{last_topic_id})
|
2016-01-14 16:43:19 -05:00
|
|
|
ORDER BY b.user_id, b.topic_id
|
2015-07-05 17:17:03 -04:00
|
|
|
LIMIT #{@batch_size}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2022-02-16 07:04:31 -05:00
|
|
|
def count_likes
|
|
|
|
count(<<-SQL)
|
|
|
|
SELECT COUNT(*) AS count
|
|
|
|
FROM #{@table_prefix}thanks
|
|
|
|
WHERE user_id <> poster_id
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_likes(last_post_id, last_user_id)
|
|
|
|
query(<<-SQL, :post_id, :user_id)
|
|
|
|
SELECT post_id, user_id, thanks_time
|
|
|
|
FROM #{@table_prefix}thanks
|
|
|
|
WHERE user_id <> poster_id
|
|
|
|
AND (post_id, user_id) > (#{last_post_id}, #{last_user_id})
|
|
|
|
ORDER BY post_id, user_id
|
|
|
|
LIMIT #{@batch_size}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2021-01-14 14:44:43 -05:00
|
|
|
def get_smiley(smiley_code)
|
|
|
|
query(<<-SQL).first
|
|
|
|
SELECT emotion, smiley_url
|
|
|
|
FROM #{@table_prefix}smilies
|
|
|
|
WHERE code = '#{smiley_code}'
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2015-07-05 17:17:03 -04:00
|
|
|
def get_config_values
|
|
|
|
query(<<-SQL).first
|
|
|
|
SELECT
|
2016-07-11 17:59:15 -04:00
|
|
|
(SELECT config_value FROM #{@table_prefix}config WHERE config_name = 'version') AS phpbb_version,
|
|
|
|
(SELECT config_value FROM #{@table_prefix}config WHERE config_name = 'avatar_gallery_path') AS avatar_gallery_path,
|
|
|
|
(SELECT config_value FROM #{@table_prefix}config WHERE config_name = 'avatar_path') AS avatar_path,
|
|
|
|
(SELECT config_value FROM #{@table_prefix}config WHERE config_name = 'avatar_salt') AS avatar_salt,
|
|
|
|
(SELECT config_value FROM #{@table_prefix}config WHERE config_name = 'smilies_path') AS smilies_path,
|
|
|
|
(SELECT config_value FROM #{@table_prefix}config WHERE config_name = 'upload_path') AS attachment_path
|
2015-07-05 17:17:03 -04:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|