Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tied/7af36b00b3bf5480aa8e77cc685bc69c to your computer and use it in GitHub Desktop.
Save tied/7af36b00b3bf5480aa8e77cc685bc69c to your computer and use it in GitHub Desktop.
List all Confluence spaces with indicators in raw SQL
SELECT DISTINCT
s.spaceid as space_id,
s.spacename as space_name,
s.spacekey as space_key,
s.spacetype as space_type,
s.spacestatus as space_status,
sd.body as space_description,
s.creationdate as space_creation_date,
up.username as space_creator_username,
u.display_name as space_creator_displayname,
u.email_address as space_creator_email,
MAX(t.nb_of_page) as nb_of_page,
MAX(t.nb_of_blogpost) as nb_of_blogpost,
MAX(t.nb_of_attachment) as nb_of_attachment,
MAX(t.nb_of_custom) as nb_of_custom,
MAX(t2.nb_of_comment) as nb_of_comment,
MAX(t3.nb_of_like) as nb_of_like,
MAX(t4.total_attachments_size) as total_attachments_size,
last_creation.username as last_creator,
last_creation.creationdate as last_creation_date,
last_modification.username as last_modifier,
last_modification.lastmoddate as last_modification_date
FROM spaces s
-- Most recent date of content creation with the creator
LEFT JOIN (
SELECT c.spaceid, up.username, c.creationdate
FROM content c
LEFT JOIN user_mapping up ON c.creator = up.user_key,
(
SELECT c2.spaceid, MAX(c2.creationdate) as last_creationdate
FROM content c2
WHERE c2.content_status = 'current'
AND c2.creationdate IS NOT NULL
AND c2.contenttype != 'SPACEDESCRIPTION'
GROUP BY c2.spaceid
) last_creation
WHERE c.content_status = 'current'
AND c.spaceid = last_creation.spaceid
AND c.creationdate = last_creation.last_creationdate
) last_creation ON last_creation.spaceid = s.spaceid
-- Most recent date of content modification with the modifier
LEFT JOIN (
SELECT c3.spaceid, up2.username, c3.lastmoddate
FROM content c3
LEFT JOIN user_mapping up2 ON c3.lastmodifier = up2.user_key,
(
SELECT c4.spaceid, MAX(c4.lastmoddate) as last_modificationdate
FROM content c4
WHERE c4.content_status = 'current'
AND c4.lastmoddate IS NOT NULL
GROUP BY c4.spaceid
) last_modification
WHERE c3.content_status = 'current'
AND c3.contenttype != 'SPACEDESCRIPTION'
AND c3.spaceid = last_modification.spaceid
AND c3.lastmoddate = last_modification.last_modificationdate
) last_modification ON last_modification.spaceid = s.spaceid
-- Number of page, blogpost, attachment and custom
LEFT JOIN(
SELECT
c5.spaceid,
c5.contenttype,
CASE WHEN c5.contenttype = 'PAGE'
THEN COUNT(c5.contenttype) END nb_of_page,
CASE WHEN c5.contenttype = 'BLOGPOST'
THEN COUNT(c5.contenttype) END nb_of_blogpost,
CASE WHEN c5.contenttype = 'ATTACHMENT'
THEN COUNT(c5.contenttype) END nb_of_attachment,
CASE WHEN c5.contenttype = 'CUSTOM'
THEN COUNT(c5.contenttype) END nb_of_custom
FROM content c5
WHERE c5.content_status = 'current'
AND c5.contenttype != 'SPACEDESCRIPTION'
GROUP BY c5.contenttype, c5.spaceid
) t ON t.spaceid = s.spaceid
-- Number of comment
LEFT JOIN(
SELECT c7.spaceid, COUNT(c6.contenttype) as nb_of_comment
FROM content c6
JOIN content c7 ON c6.pageid = c7.contentid
WHERE c6.prevver IS NULL
AND c6.content_status = 'current'
AND c6.contenttype = 'COMMENT'
GROUP BY c6.contenttype, c7.spaceid
) t2 ON t2.spaceid = s.spaceid
-- Number of like
LEFT JOIN(
SELECT c8.spaceid, COUNT(l.id) as nb_of_like
FROM likes l
JOIN content c8 ON l.contentid = c8.contentid
WHERE c8.prevver IS NULL
AND c8.content_status = 'current'
GROUP BY c8.spaceid
) t3 ON t3.spaceid = s.spaceid
-- Total attachments size
LEFT JOIN(
SELECT c9.spaceid, SUM(cp.LONGVAL) as total_attachments_size
FROM content c9
JOIN content c10 ON c9.contentid = c10.pageid
JOIN contentproperties cp ON c10.contentid = cp.contentid
WHERE c10.contenttype = 'ATTACHMENT'
GROUP BY c9.spaceid
) t4 ON t4.spaceid = s.spaceid
LEFT JOIN user_mapping up ON up.user_key = s.creator
LEFT JOIN cwd_user u ON u.user_name = up.username
LEFT JOIN content ON content.spaceid = s.spaceid
LEFT JOIN bodycontent sd ON sd.contentid = content.contentid
WHERE content.contenttype = 'SPACEDESCRIPTION'
GROUP BY
s.spaceid,
s.spacename,
s.spacekey,
s.spacetype,
s.spacestatus,
sd.body,
up.username,
u.user_name,
u.display_name,
u.email_address,
last_creator,
last_creation_date,
last_modifier,
last_modification_date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment