SELECT 
	*,
	'ok' AS query_result,
	( SELECT 
		GROUP_CONCAT(page_language_iso) 
		FROM cms_pages
		WHERE page_node_id = IF(node_original_node_id=0,node_id,node_original_node_id)
		AND ( page_is_published OR :preview )
		AND ( page_start_publishing <= NOW() OR :preview OR ISNULL(page_start_publishing))
		AND ( page_end_publishing >= NOW() OR :preview OR ISNULL(page_end_publishing))
			# AND page_language_iso != '*'
	) AS available_languages_concat,
	( SELECT -- subquery prob costley
		GROUP_CONCAT(keyword_string) 
		FROM cms_keywords
		LEFT JOIN cms_keywords_in_pages ON kip_keyword_id = keyword_id
		WHERE kip_page_id = page_id
	) AS keywords
FROM cms_nodes
LEFT JOIN 
	cms_pages ON 
		( ISNULL(:ids) AND page_node_id = IF(node_original_node_id=0,node_id,node_original_node_id)) OR
		( !ISNULL(:ids) AND page_id = 1 AND node_id = 1 ) -- default record
LEFT JOIN cms_rewritepaths ON rewritepath_page_id = page_id AND rewritepath_is_primary
LEFT JOIN ( SELECT -- IF(TABLE_EXISTS(cms_tags_in_nodes))
	tin_node_id,GROUP_CONCAT(tin_tag_id) AS tag_ids_concat 
	FROM cms_tags_in_nodes  GROUP BY tin_node_id
) tags ON tags.tin_node_id = node_id
LEFT JOIN ( SELECT -- not (yet) used
	node_original_node_id AS alias_node_original_node_id,
	GROUP_CONCAT(node_id) AS alias_ids_concat
	FROM cms_nodes
	GROUP BY node_original_node_id
) aliases ON alias_node_original_node_id = node_id AND node_id != 1 -- default record
-- LEFT JOIN usm_users ON node_user_id = user_id -- owner 
WHERE
	(ISNULL(:ids) AND (
		( node_id = :node_id OR node_parent_node_id = :node_parent_node_id )
		AND ( page_language_iso = :lang  OR page_language_iso = '*')
		AND ( page_is_published OR :preview )
		AND ( page_start_publishing <= NOW() OR :preview OR ISNULL(page_start_publishing))
		AND ( page_end_publishing >= NOW() OR :preview OR ISNULL(page_end_publishing))
		AND ( !node_is_trashed OR :showtrashed)
			)) OR (!ISNULL(:ids) AND page_id = 1)
ORDER BY node_ordering
LIMIT 1

-- :node_id
-- :node_parent_node_id
-- :preview
-- :lang
-- :user_id
-- :user_group_ids
