Skip to content

Commit

Permalink
refactor: bake redirects from db (#3186)
Browse files Browse the repository at this point in the history
- refactor: bake redirects from db
- refactor: resolve internal redirects from db

### Internal redirect resolver

The internal resolver is currently only used for prominent links. The following query reveals some examples of where it might be used:

```
select
    *
from
    posts_links pl
    join posts p on pl.sourceId = p.id
where
    pl.linkType = 'grapher'
    and componentType = 'prominent-link'
    and p.status = 'publish'
    and p.gdocSuccessorId is NULL
    and pl.target in (
        select
            slug
        from
            chart_slug_redirects csr
    )
```

For instance, in https://ourworldindata.org/ukraine-war 

![Screenshot 2024-02-10 at 09.10.18.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/0SFFiIjKuUK6UPYHVe6u/f120f2fc-5e01-4eb2-9990-050ddd0827f8.png)


### formatWpUrl refactor

This PR also simplifies the formatWpUrl function since none of the paths in redirects contain the "__" string:

```
SELECT url, action_data, action_code FROM wp_redirection_items WHERE (url like "%\_\_%" OR action_data like "%\_\_%")  and status = "enabled"
```
--> 0 results
  • Loading branch information
mlbrgl authored Feb 19, 2024
2 parents 2d3378f + a7f9c83 commit 1012333
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
36 changes: 8 additions & 28 deletions baker/redirects.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as db from "../db/db.js"
import * as wpdb from "../db/wpdb.js"
import { memoize, JsonError, Url } from "@ourworldindata/utils"
import { isCanonicalInternalUrl } from "./formatting.js"
import { resolveExplorerRedirect } from "./replaceExplorerRedirects.js"
import { logErrorAndMaybeSendToBugsnag } from "../serverUtils/errorLog.js"
import { getRedirectsFromDb } from "../db/model/Redirect.js"

export const getRedirects = async () => {
const staticRedirects = [
Expand Down Expand Up @@ -57,22 +57,17 @@ export const getRedirects = async () => {
"/grapher/exports/* https://assets.ourworldindata.org/grapher/exports/:splat 301",
]

// Redirects from Wordpress admin UI
const wpRedirectRows = await wpdb.singleton.query(
`SELECT url, action_data, action_code FROM wp_redirection_items WHERE status = 'enabled'`
)
const wpRedirects = wpRedirectRows.map(
(row) =>
`${formatWpUrl(row.url)} ${formatWpUrl(row.action_data)} ${
row.action_code
}`
// Get redirects from the database (exported from the Wordpress DB)
// Redirects are assumed to be trailing-slash-free (see syncRedirectsToGrapher.ts)
const redirectsFromDb = (await getRedirectsFromDb()).map(
(row) => `${row.source} ${row.target} ${row.code}`
)

// Add newlines in between so we get some more overview
return [
...staticRedirects,
"",
...wpRedirects,
...redirectsFromDb,
"",
...dynamicRedirects, // Cloudflare requires all dynamic redirects to be at the very end of the _redirects file
]
Expand All @@ -96,25 +91,10 @@ export const getGrapherRedirectsMap = async (
)
}

export const formatWpUrl = (url: string) => {
if (url === "/") return url

return url
.replace(/__/g, "/") // replace __: abc__xyz -> abc/xyz
.replace(/\/$/, "") // remove trailing slash: /abc/ -> /abc
}

export const getWordpressRedirectsMap = async () => {
const wordpressRedirectRows = (await wpdb.singleton.query(
`SELECT url, action_data FROM wp_redirection_items WHERE status = 'enabled'`
)) as Array<{ url: string; action_data: string }>
const redirectsFromDb = await getRedirectsFromDb()

return new Map(
wordpressRedirectRows.map((row) => [
formatWpUrl(row.url),
formatWpUrl(row.action_data),
])
)
return new Map(redirectsFromDb.map((row) => [row.source, row.target]))
}

export const getGrapherAndWordpressRedirectsMap = memoize(
Expand Down
12 changes: 9 additions & 3 deletions baker/syncRedirectsToGrapher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as db from "../db/db"
import * as wpdb from "../db/wpdb"
import { getRedirectsFromDb } from "../db/model/Redirect.js"
import { formatWpUrl, resolveRedirectFromMap } from "./redirects.js"
import { resolveRedirectFromMap } from "./redirects.js"
import { Redirect, Url } from "@ourworldindata/utils"

// A close cousing of the getWordpressRedirectsMap() function from
Expand All @@ -12,13 +12,19 @@ const getWordpressRedirectsMapFromRedirects = (
return new Map(redirects.map((r) => [r.source, r.target]))
}

const stripTrailingSlash = (url: string) => {
if (url === "/") return url

return url.replace(/\/$/, "") // remove trailing slash: /abc/ -> /abc
}

export const syncRedirectsToGrapher = async (): Promise<void> => {
const allWordpressRedirectsRaw = await wpdb.FOR_SYNC_ONLY_getRedirects()

const allWordpressRedirects = allWordpressRedirectsRaw.map((r) => ({
...r,
source: formatWpUrl(r.source),
target: formatWpUrl(r.target),
source: stripTrailingSlash(r.source),
target: stripTrailingSlash(r.target),
}))

const existingRedirectsFromDb = await getRedirectsFromDb()
Expand Down

0 comments on commit 1012333

Please sign in to comment.