All files / owid-grapher/baker redirects.ts

11.96% Statements 11/92
100% Branches 0/0
0% Functions 0/1
11.96% Lines 11/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 871x 1x 1x 1x 1x                                                                                                                                                                   1x
import * as db from "../db/db"
import * as wpdb from "../db/wpdb"
import { getCountryDetectionRedirects } from "../clientUtils/countries"
 
export const getRedirects = async () => {
    const redirects = [
        // RSS feed
        "/feed /atom.xml 302!",

        // Backwards compatibility-- admin urls
        "/wp-admin/* https://owid.cloud/wp/wp-admin/:splat 301",
        "/grapher/admin/* https://owid.cloud/grapher/admin/:splat 301",

        // TODO: this should only get triggered by external hits (indexed .pdf files for instance)
        // and should be removed when no evidence of these inbound links can be found.
        "/wp-content/uploads/* /uploads/:splat 301",
        // TODO: temporary fix for the /blog page thumbnails, which paths are not being
        // transformed through the formatting step. Potentially applies to other
        // pages as well.
        "/app/uploads/* /uploads/:splat 301",

        // Backwards compatibility-- old Max stuff that isn't static-friendly
        "/roser/* https://www.maxroser.com/roser/:splat 301",
        "/uploads/nvd3/* https://www.maxroser.com/owidUploads/nvd3/:splat 301",
        "/uploads/datamaps/* https://www.maxroser.com/owidUploads/datamaps/:splat 301",
        "/slides/Max_PPT_presentations/* https://www.maxroser.com/slides/Max_PPT_presentations/:splat 301",
        "/slides/Max_Interactive_Presentations/* https://www.maxroser.com/slides/Max_Interactive_Presentations/:splat 301",

        // Backwards compatibility-- public urls
        "/entries/* /:splat 301",
        "/entries /#entries 302",
        "/data/food-agriculture/* /:splat 301",
        "/data/political-regimes/* /:splat 301",
        "/data/population-growth-vital-statistics/* /:splat 301",
        "/data/growth-and-distribution-of-prosperity/* /:splat 301",

        // Backwards compatibility-- grapher url style
        "/chart-builder/* /grapher/:splat 301",
        "/grapher/public/* /grapher/:splat 301",
        "/grapher/view/* /grapher/:splat 301",

        "/slides/* https://slides.ourworldindata.org/:splat 301",
        "/subscribe /#subscribe 301",
    ]

    getCountryDetectionRedirects().forEach((redirect) =>
        redirects.push(redirect)
    )

    // Redirects from Wordpress admin UI
    const rows = await wpdb.singleton.query(
        `SELECT url, action_data, action_code FROM wp_redirection_items WHERE status = 'enabled'`
    )
    redirects.push(
        ...rows.map(
            (row) =>
                `${row.url.replace(/__/g, "/")} ${row.action_data.replace(
                    /__/g,
                    "/"
                )} ${row.action_code}!`
        )
    )

    // Redirect /grapher/latest
    const latestRows = await db.queryMysql(
        `SELECT JSON_EXTRACT(config, "$.slug") as slug FROM charts where starred=1`
    )
    for (const row of latestRows) {
        redirects.push(`/grapher/latest /grapher/${JSON.parse(row.slug)} 302`)
    }

    // Redirect old slugs to new slugs
    const chartRedirectRows = await db.queryMysql(`
    SELECT chart_slug_redirects.slug, chart_id, JSON_EXTRACT(charts.config, "$.slug") as trueSlug
    FROM chart_slug_redirects INNER JOIN charts ON charts.id=chart_id
`)

    for (const row of chartRedirectRows) {
        const trueSlug = JSON.parse(row.trueSlug)
        if (row.slug !== trueSlug) {
            redirects.push(`/grapher/${row.slug} /grapher/${trueSlug} 302!`)
        }
    }

    return redirects
}