Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Wordpress post rendering #3135

Closed
wants to merge 10 commits into from
Closed

Conversation

mlbrgl
Copy link
Member

@mlbrgl mlbrgl commented Jan 25, 2024

⚠️ This PR should not be merged as-is, but is a foundational step towards baking from the grapher DB.

This PR swaps the data source for the rendering of Wordpress posts from the Wordpress API to the synced grapher DB.

This change aims to be as surgical as possible, but still touches a lot of rendering code, including the
sitemap, algolia, blog index, country profiles, post previews and post rendering.

The next steps will be centered around the render functions in formatWordpressPost(), e.g. renderProminentLinks(), which relied on PHP code that is not part of the rendering pipeline anymore (e.g. prominent-link.php)

Note: I kept getFullPost and getPostRowEnriched separate but we could imagine merging them. I didn't want to propagate getFullPost edge cases to the parts of the codebase already relying on getPostRowEnriched. If this overall approach seems promising, we might decide to spend more time on refactoring for clarity, although the opportunity cost needs to be carefully weighed given the expected temporary and frozen nature of this part of the codebase.

  • update owid-staging WP DB (used by staging-site servers)
  • blog index: manually compare pages 1, 10 and 19
  • sitemap: live and staging sitemaps are identical
  • country profile: all render fine (with the caveat mentioned above about PHP rendering)

Summary by CodeRabbit

  • New Features
    • Enhanced post retrieval and rendering processes for better performance and accuracy.
  • Bug Fixes
    • Fixed issues related to post data handling and rendering, ensuring more reliable content display.
  • Refactor
    • Updated post data model and types for improved clarity and consistency across the application.
    • Streamlined post fetching and enrichment logic to enhance system efficiency.
  • Chores
    • Removed outdated code and dependencies to maintain a clean and efficient codebase.

Copy link

coderabbitai bot commented Jan 25, 2024

Walkthrough

The recent updates primarily involve refining the post retrieval and enrichment processes across various components of a web application. By eliminating outdated WordPress API dependencies, enhancing database interactions, and updating data models to better reflect enriched post information, the changes aim to streamline content management and presentation. This overhaul not only simplifies the codebase by removing redundant checks and outdated references but also improves the efficiency and accuracy of data handling within the application.

Changes

Files Change Summary
adminSiteServer/.../mockSiteRouter.tsx Removed isWordpressAPIEnabled check and not found page logic for /headerMenu.json route.
baker/SiteBaker.tsx
baker/algolia/...
baker/sitemap.ts
db/wpdb.ts
db/model/Post.ts
Updated post retrieval and enrichment processes, including changes in imports, function calls, and data handling.
baker/.../pageOverrides.test.ts
baker/pageOverrides.tsx
baker/siteRenderers.tsx
Modified post data models, updated test cases and logic for post overrides and rendering.
packages/@ourworldindata/types/... Updated data models and types to align with enriched post information.

🐇✨
In a burrow, deep and wide,
Changes bloom from inside.
Code and data, hand in hand,
Craft a future, bright and grand.
🌟🐾

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@mlbrgl
Copy link
Member Author

mlbrgl commented Jan 25, 2024

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.

@mlbrgl mlbrgl changed the base branch from tablepress-migration to listed-posts January 26, 2024 16:10
@mlbrgl mlbrgl force-pushed the migrate-wp-post-rendering branch from 9afea05 to a063282 Compare January 26, 2024 16:10
@mlbrgl mlbrgl marked this pull request as ready for review January 26, 2024 16:31
@mlbrgl
Copy link
Member Author

mlbrgl commented Jan 26, 2024

@coderabbitai review

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 16d6405 and 993a2dd.
Files selected for processing (11)
  • adminSiteServer/mockSiteRouter.tsx (2 hunks)
  • baker/SiteBaker.tsx (3 hunks)
  • baker/algolia/indexToAlgolia.tsx (5 hunks)
  • baker/pageOverrides.test.ts (5 hunks)
  • baker/pageOverrides.tsx (4 hunks)
  • baker/siteRenderers.tsx (9 hunks)
  • baker/sitemap.ts (3 hunks)
  • db/model/Post.ts (3 hunks)
  • db/wpdb.ts (9 hunks)
  • packages/@ourworldindata/types/src/domainTypes/Posts.ts (1 hunks)
  • packages/@ourworldindata/types/src/wordpressTypes/WordpressTypes.ts (2 hunks)
Additional comments: 20
packages/@ourworldindata/types/src/domainTypes/Posts.ts (1)
  • 29-34: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [11-11]

The removal of the path property from the FullPost interface and the addition of an id property of type number changes the data model. Ensure all usages of FullPost throughout the codebase are updated to reflect this change.

packages/@ourworldindata/types/src/wordpressTypes/WordpressTypes.ts (2)
  • 1-1: The addition of an import statement for PostRowRaw is necessary for the updated FilterFnPostRestApi type. Ensure that PostRowRaw is correctly defined and imported from the expected location.
  • 56-56: The modification of FilterFnPostRestApi from (post: PostRestApi) => boolean to (post: PostRowRaw) => boolean aligns the type with the new data model. Verify that all usages of FilterFnPostRestApi have been updated to pass PostRowRaw objects instead of PostRestApi.
Verification successful

The search results confirm that FilterFnPostRestApi has been updated in its definition to use PostRowRaw and is being used accordingly within the codebase, particularly in db/model/Post.ts where it's applied as a filter function for posts. This aligns with the review comment's request to verify that all usages of FilterFnPostRestApi have been updated to pass PostRowRaw objects instead of PostRestApi. Therefore, the modification and its application across the codebase have been successfully verified.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for usages of `FilterFnPostRestApi` to ensure it's now used with `PostRowRaw` objects.
ast-grep --lang typescript --pattern $'type FilterFnPostRestApi = ($_: PostRowRaw) => boolean'
# Expecting results confirming the updated usage.

Length of output: 232



Script:

#!/bin/bash
# Search for actual usages of `FilterFnPostRestApi` across the codebase to ensure it's being used with `PostRowRaw` objects.
rg "FilterFnPostRestApi" --type ts
# This command aims to find references to `FilterFnPostRestApi` in TypeScript files, which will help in verifying if the usages have been updated to work with `PostRowRaw` objects.

Length of output: 507

baker/pageOverrides.test.ts (2)
  • 11-30: The replacement of FullPost with PostRowEnriched and the addition of several new properties in the mockCreatePost function align the test suite with the updated data model. Ensure that these new properties (status, isListed, published_at, etc.) are correctly utilized in tests and reflect the actual data model changes.
  • 76-86: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [36-84]

The replacement of getPostBySlugLogToSlackNoThrow with getPostEnrichedBySlug in test cases and the addition of various test scenarios to reflect the updated logic and data model are appropriate. Verify that the new getPostEnrichedBySlug function is implemented correctly and that all test cases accurately test the intended functionality.

Verification successful

The attempt to verify the implementation of getPostEnrichedBySlug using ast-grep did not yield any results. This could indicate that the function might not be present in the expected form or there was an issue with the search parameters. To ensure a thorough verification, a different approach to search for the function's implementation is necessary.


The search for the getPostEnrichedBySlug function using ast-grep did not return any results, but the subsequent rg search successfully identified multiple references to getPostEnrichedBySlug across various files, including its import in pageOverrides.tsx and GrapherBaker.tsx, as well as its definition in db/model/Post.ts. This confirms the presence and usage of the getPostEnrichedBySlug function within the codebase, aligning with the context provided in the review comment regarding its implementation and use in test cases.

Given this information, it can be concluded that the replacement of getPostBySlugLogToSlackNoThrow with getPostEnrichedBySlug in test cases and the addition of various test scenarios to reflect the updated logic and data model are indeed appropriate and have been verified to be correctly implemented in the codebase.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify the implementation of `getPostEnrichedBySlug`.
ast-grep --lang typescript --pattern $'export async function getPostEnrichedBySlug($_: string) {
  $$$
}'
# Expecting to find the function definition and its logic.

Length of output: 106



Script:

#!/bin/bash
# Attempting a broader search for the function `getPostEnrichedBySlug` without specifying it must be exported or async, to catch any form of its declaration.
ast-grep --lang typescript --pattern $'function getPostEnrichedBySlug($_: string) {
  $$$
}'

Length of output: 93


Script:

#!/bin/bash
# Additionally, using ripgrep to search for any mention of `getPostEnrichedBySlug` in TypeScript files, in case the function is declared in a way that ast-grep's pattern does not capture.
rg "getPostEnrichedBySlug" -tts

Length of output: 1292

baker/pageOverrides.tsx (1)
  • 1-16: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [3-57]

The modifications in pageOverrides.tsx, including changes to imports and adjustments to the getLandingOnlyIfParent and getPageOverrides functions, are necessary to align with the new data model. Ensure that the removal of getPostBySlugLogToSlackNoThrow and the adjustments made to parameter types and logic do not introduce regressions and that all references to these functions are updated accordingly.

baker/sitemap.ts (1)
  • 60-66: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [15-93]

The removal of the wpdb dependency and the update of the lastmod field in the makeSitemap function to use updated_at instead of modified_gmt are aligned with the transition to using the grapher DB for content rendering. Ensure that the getPosts function correctly retrieves and filters posts from the grapher DB and that the lastmod field accurately reflects the most recent update time of the posts.

db/wpdb.ts (4)
  • 38-38: The import for PostRowEnriched and Topic from @ourworldindata/types replaces removed imports, aligning with the new data source strategy.
  • 47-47: The addition of getPosts and filterListedPosts function imports from ./model/Post.js is crucial for fetching and filtering posts from the grapher DB, supporting the PR's objective to transition data sources.
  • 375-382: Modification of the getPostIdAndTypeBySlug function to query from the posts table instead of using the Wordpress API or GraphQL endpoint is a significant change that aligns with the PR's objective of transitioning to the grapher DB.
  • 647-673: The getFullPost function has been significantly modified to adapt to the new PostRowEnriched type. This change is crucial for integrating the new data source into the post rendering process. Ensure that all fields used from PostRowEnriched match the expected structure and types.
baker/siteRenderers.tsx (5)
  • 67-67: The addition of getFullPost to the import list from ../db/wpdb.js is necessary for fetching enriched post data from the new data source, supporting the PR's objective.
  • 89-93: The replacement of getPostBySlug with getPostEnrichedBySlug and getFullPost for enriching and retrieving post data is a key change that aligns with the PR's objective of utilizing the grapher DB for content rendering.
  • 200-203: The renderPageBySlug function now uses getPostEnrichedBySlug and getFullPost to fetch and enrich post data, which is a significant change that aligns with the PR's objective of transitioning to the grapher DB for post rendering.
  • 207-210: Similar to renderPageBySlug, the renderPreview function's adaptation to use getPostEnrichedById and getFullPost for fetching and enriching post data supports the PR's objective of utilizing the grapher DB.
  • 241-241: The renderPost function's modification to use getPageOverrides with the post slug instead of the post object is a subtle but important change that could impact how page overrides are applied. Ensure that this change is tested thoroughly to avoid unintended behavior in post rendering.
baker/SiteBaker.tsx (4)
  • 74-74: The import statement for getPosts from "../db/model/Post.js" has been added. This change aligns with the PR's objective to transition data sources from the Wordpress API to the grapher DB.
  • 389-389: The addition of db.getConnection() before fetching posts with getPosts() in the removeDeletedPosts method is crucial for establishing a database connection. This ensures that the data retrieval is performed on an active connection, which is a good practice for database interactions.
  • 391-391: Replacing wpdb.getPosts() with getPosts() in the removeDeletedPosts method is consistent with the PR's goal of transitioning away from the Wordpress API towards using the grapher DB. This change is necessary for fetching posts directly from the new data source.
  • 385-394: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [74-420]

The modifications in SiteBaker.tsx, including the addition of a new import and changes within the removeDeletedPosts method, are well-aligned with the PR's objectives to shift the data source for rendering Wordpress posts. These changes are correctly implemented and should not introduce any issues based on the provided context. It's important to ensure that all other parts of the system that interact with these modified areas are also updated to accommodate the new data source.

@mlbrgl mlbrgl requested a review from danyx23 January 29, 2024 13:32
@mlbrgl mlbrgl mentioned this pull request Jan 29, 2024
11 tasks
@mlbrgl
Copy link
Member Author

mlbrgl commented Jan 29, 2024

@danyx23 rebasing onto the lastest DB access updates. Will reopen once I'm done.

@mlbrgl mlbrgl marked this pull request as draft January 29, 2024 17:22
@danyx23
Copy link
Contributor

danyx23 commented Jan 29, 2024

@mlbrgl thank you!

@mlbrgl mlbrgl force-pushed the migrate-wp-post-rendering branch from da807f5 to 1eb8e4f Compare January 29, 2024 19:26
@mlbrgl
Copy link
Member Author

mlbrgl commented Jan 29, 2024

@danyx23 ok done, good to go!

Re-baked with (in addition to the partial bake done on push): yarn buildLocalBake http://staging-site-migrate-wp-post-rendering /home/owid/live-data/bakedSite --steps blogIndex gdocPosts wordpressPosts

@mlbrgl mlbrgl marked this pull request as ready for review January 29, 2024 19:39
@mlbrgl mlbrgl force-pushed the migrate-wp-post-rendering branch from 1eb8e4f to 4d891f9 Compare January 30, 2024 15:32
@danyx23
Copy link
Contributor

danyx23 commented Jan 31, 2024

Hey @mlbrgl, this looks good already. I checked it manually for a few pages and found that for example https://ourworldindata.org/books looks different. Is this expected at this point? Should I investigate these differences more?

I think it would be useful to rebase on top of current master so that we can use the screenshot tool with the new improments that Ike build that depend on the new events. I think gt sync should do the trick in the easiest way?

@mlbrgl
Copy link
Member Author

mlbrgl commented Feb 2, 2024

@danyx23 thanks for the review! Yes this is to be expected at this point, so no need to spend time on investigating further.

I also revised my thinking and I am now pursing a different approach as you know from our last conversation: baking from rendered API response (vs baking from unprocessed source HTML here)

So I'll retract this PR for now. Thanks again for spending time on this regardless! 🙏

@mlbrgl mlbrgl marked this pull request as draft February 2, 2024 22:07
@mlbrgl mlbrgl removed the request for review from danyx23 February 2, 2024 22:08
@mlbrgl mlbrgl force-pushed the migrate-wp-post-rendering branch from 4d891f9 to b70c6f4 Compare February 13, 2024 08:41
@mlbrgl mlbrgl closed this Feb 28, 2024
@mlbrgl mlbrgl deleted the migrate-wp-post-rendering branch February 28, 2024 06:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants