From 24bb5fb20f92c27f9a8f448d32f06cf09e592c36 Mon Sep 17 00:00:00 2001 From: Ike Saunders Date: Thu, 1 Jun 2023 17:19:27 -0400 Subject: [PATCH 1/5] :tada: featured image in posts column --- .../1685653967284-PostsFeaturedImage.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 db/migration/1685653967284-PostsFeaturedImage.ts 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; + `) + } +} From da7717e2e92e41ff1b0fbfbb4f30a77174601eab Mon Sep 17 00:00:00 2001 From: Ike Saunders Date: Thu, 1 Jun 2023 17:19:41 -0400 Subject: [PATCH 2/5] :tada: sync featured image from WP --- baker/postUpdatedHook.ts | 27 ++++++++++++++++--- .../@ourworldindata/utils/src/owidTypes.ts | 1 + 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/baker/postUpdatedHook.ts b/baker/postUpdatedHook.ts index 6dbc0d8d2e4..2a3bad329e7 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, + (SELECT guid FROM wp_posts WHERE ID = fi.featured_image_id) 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/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 { From 05e4bedd7bdad650a8317cc3e758278b6c391beb Mon Sep 17 00:00:00 2001 From: Ike Saunders Date: Thu, 1 Jun 2023 17:27:43 -0400 Subject: [PATCH 3/5] :tada: include featured image in archieML migration --- db/migrateWpPostsToArchieMl.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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, From 17ba872fb3ea5af0bf6c59a3a6b5edb1055847e4 Mon Sep 17 00:00:00 2001 From: Ike Saunders Date: Thu, 26 Oct 2023 21:08:26 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=E2=9C=A8=20transfer=20featured=5Fimage=20i?= =?UTF-8?q?n=20syncPostsToGrapher?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/syncPostsToGrapher.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/db/syncPostsToGrapher.ts b/db/syncPostsToGrapher.ts index d248535331f..7d7dc2bb566 100644 --- a/db/syncPostsToGrapher.ts +++ b/db/syncPostsToGrapher.ts @@ -156,16 +156,32 @@ 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 guid FROM wp_posts WHERE ID = fi.featured_image_id) 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 +218,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[] From e24d0bd522a3611140352bf5cd1623ca032be60b Mon Sep 17 00:00:00 2001 From: Daniel Bachler Date: Tue, 31 Oct 2023 15:56:33 +0100 Subject: [PATCH 5/5] :hammer: normalize thumbnail urls so that they never point to owid.cloud --- baker/postUpdatedHook.ts | 2 +- db/syncPostsToGrapher.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/baker/postUpdatedHook.ts b/baker/postUpdatedHook.ts index 2a3bad329e7..93b5793beb9 100644 --- a/baker/postUpdatedHook.ts +++ b/baker/postUpdatedHook.ts @@ -76,7 +76,7 @@ const syncPostToGrapher = async ( p.*, pwa.authors, fr.created_at as created_at, - (SELECT guid FROM wp_posts WHERE ID = fi.featured_image_id) AS featured_image + 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 = p.id diff --git a/db/syncPostsToGrapher.ts b/db/syncPostsToGrapher.ts index 7d7dc2bb566..a8d6be458bb 100644 --- a/db/syncPostsToGrapher.ts +++ b/db/syncPostsToGrapher.ts @@ -177,7 +177,8 @@ const syncPostsToGrapher = async (): Promise => { p.*, pwa.authors as authors, fr.created_at as created_at, - (SELECT guid FROM wp_posts WHERE ID = fi.featured_image_id) AS featured_image + -- 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