diff --git a/baker/postUpdatedHook.ts b/baker/postUpdatedHook.ts index 6dbc0d8d2e4..93b5793beb9 100644 --- a/baker/postUpdatedHook.ts +++ b/baker/postUpdatedHook.ts @@ -53,18 +53,36 @@ const syncPostToGrapher = async ( left join wp_term_taxonomy t on t.term_taxonomy_id = r.term_taxonomy_id WHERE t.taxonomy = 'author' and p.ID = ? group by p.ID - ) + ), + post_featured_image AS ( + SELECT + p.ID, + ( + SELECT + meta_value + FROM + wp_postmeta + WHERE + post_id = p.ID + AND meta_key = '_thumbnail_id') AS featured_image_id + FROM + wp_posts p + WHERE + p.ID = ? + ) -- finally here we select all the fields from posts_with_authors and -- then we join in the first_revision to get the created_at field select p.*, pwa.authors, - fr.created_at as created_at + fr.created_at as created_at, + regexp_replace((SELECT guid FROM wp_posts WHERE ID = fi.featured_image_id), '^https://owid.cloud/(app|wp-content)/', 'https://ourworldindata.org/wp-content/') AS featured_image from wp_posts p left join post_ids_with_authors pwa on pwa.id = p.id - left join first_revision fr on fr.post_id = pwa.id + left join first_revision fr on fr.post_id = p.id + left join post_featured_image fi on fi.ID = p.id where p.id = ?`, - [postId, postId, postId] + [postId, postId, postId, postId] ) const dereferenceReusableBlocksFn = await buildReusableBlocksResolver() @@ -80,6 +98,7 @@ const syncPostToGrapher = async ( type: wpPost.post_type, status: wpPost.post_status, content: wpPost.post_content, + featured_image: wpPost.featured_image || "", published_at: wpPost.post_date_gmt === zeroDateString ? null diff --git a/db/migrateWpPostsToArchieMl.ts b/db/migrateWpPostsToArchieMl.ts index 970f2f04513..3527df446a3 100644 --- a/db/migrateWpPostsToArchieMl.ts +++ b/db/migrateWpPostsToArchieMl.ts @@ -90,7 +90,8 @@ const migrate = async (): Promise => { "authors", "excerpt", "created_at_in_wordpress", - "updated_at" + "updated_at", + "featured_image" ).from(db.knexTable(Post.postsTable)) //.where("id", "=", "29766")) for (const post of posts) { @@ -159,6 +160,7 @@ const migrate = async (): Promise => { subtitle: post.excerpt, excerpt: post.excerpt, authors: parsePostAuthors(post.authors), + "featured-image": post.featured_image, dateline: dateline, // TODO: this discards block level elements - those might be needed? refs: undefined, diff --git a/db/migration/1685653967284-PostsFeaturedImage.ts b/db/migration/1685653967284-PostsFeaturedImage.ts new file mode 100644 index 00000000000..fe9e43a71fd --- /dev/null +++ b/db/migration/1685653967284-PostsFeaturedImage.ts @@ -0,0 +1,17 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class PostsFeaturedImage1685653967284 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + queryRunner.query(` + ALTER TABLE posts + ADD COLUMN featured_image VARCHAR(1024) NOT NULL DEFAULT ""; + `) + } + + public async down(queryRunner: QueryRunner): Promise { + queryRunner.query(` + ALTER TABLE posts + DROP COLUMN featured_image; + `) + } +} diff --git a/db/syncPostsToGrapher.ts b/db/syncPostsToGrapher.ts index d248535331f..a8d6be458bb 100644 --- a/db/syncPostsToGrapher.ts +++ b/db/syncPostsToGrapher.ts @@ -156,16 +156,33 @@ const syncPostsToGrapher = async (): Promise => { from wp_posts p left join posts_authors pa on p.ID = pa.id group by p.ID - ) + ), + post_featured_image AS ( + SELECT + p.ID, + ( + SELECT + meta_value + FROM + wp_postmeta + WHERE + post_id = p.ID + AND meta_key = '_thumbnail_id') AS featured_image_id + FROM + wp_posts p + ) -- Finally collect all the fields we want to keep - this is everything from wp_posts, the authors from the -- posts_with_authors CTE and the created_at from the first_revision CTE select p.*, pwa.authors as authors, - fr.created_at as created_at + fr.created_at as created_at, + -- select the featured image url and normalize the to point to our full domain at the wp-content folder + regexp_replace((SELECT guid FROM wp_posts WHERE ID = fi.featured_image_id), '^https://owid.cloud/(app|wp-content)/', 'https://ourworldindata.org/wp-content/') AS featured_image from wp_posts p left join post_ids_with_authors pwa on p.ID = pwa.ID left join first_revision fr on fr.post_id = pwa.ID + left join post_featured_image fi on fi.ID = p.id where p.post_type in ('post', 'page') AND post_status != 'trash' ` ) @@ -202,6 +219,7 @@ const syncPostsToGrapher = async (): Promise => { excerpt: post.post_excerpt, created_at_in_wordpress: post.created_at === zeroDateString ? null : post.created_at, + featured_image: post.featured_image || "", } }) as PostRow[] diff --git a/packages/@ourworldindata/utils/src/owidTypes.ts b/packages/@ourworldindata/utils/src/owidTypes.ts index 6e4889d13fb..070a213a6bc 100644 --- a/packages/@ourworldindata/utils/src/owidTypes.ts +++ b/packages/@ourworldindata/utils/src/owidTypes.ts @@ -181,6 +181,7 @@ export interface PostRow { authors: string excerpt: string created_at_in_wordpress: Date | null + featured_image: string } export interface PostRowWithGdocPublishStatus extends PostRow {