: put retrodojo in place, push wordpress database

This commit is contained in:
VC
2025-02-17 14:50:14 +01:00
parent f8013e3fea
commit 5a71894134
8 changed files with 180 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
---
- name: Create retrodojo db user
community.mysql.mysql_user:
login_unix_socket: "/var/run/mysqld/mysqld.sock"
login_user: root
login_password: "{{ mariadb_root_pass }}"
name: "{{ retrodojo_maria_user }}"
password: "{{ retrodojo_maria_password }}"
priv: "{{ retrodojo_maria_database }}.wp_dojohistoricalid:SELECT/{{ retrodojo_maria_database }}.wp_posts:SELECT/{{ retrodojo_maria_database }}.wp_terms:SELECT/{{ retrodojo_maria_database }}.wp_term_relationships:SELECT"

View File

@@ -0,0 +1,12 @@
---
- name: Init db
ansible.builtin.include_tasks: db.yml
- name: Put program file in place
ansible.builtin.template:
src: "index.php.j2"
dest: "{{ retrodojo_home }}/index.php"
owner: root
group: www-data
mode: "0o640"

View File

@@ -0,0 +1,110 @@
<?php
// conversion from the letter ID to the datetime+rank ID
define("CODE", "aQwZsXeDcRfVtGbYhNuJiKoLpMAqWzSxEdCrFvTgByHnUjIkOlPm");
define("MYSQL_USER", "{{ retrodojo_maria_user }}");
define("MYSQL_PASSWORD", "{{ retrodojo_maria_password }}");
define("MYSQL_DATABASE", "{{ retrodojo_maria_database }}");
function decode_article($code) {
$id = 0;
foreach(str_split($code) as $key => $value) {
$transposed = strrpos(CODE, $value);
$id += 10 ** (3 - $key) * ($transposed % 10);
}
return $id;
}
function get_current_guid_from_historical_id_for($conn, $article_type, $hist_id) {
$guid = "";
$query = <<<QUERY
SELECT guid
FROM wp_dojohistoricalid
INNER JOIN wp_posts ON wp_dojohistoricalid.current_id = wp_posts.ID
INNER JOIN wp_terms ON wp_dojohistoricalid.term_id = wp_terms.term_id
WHERE slug = "$article_type"
AND historical_id = $hist_id;
QUERY;
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) == 1) {
$row = mysqli_fetch_row($result);
$guid = $row[0];
}
return $guid;
}
// get the article ID no matter what
if (isset($_GET["id"])) {
$article_id = substr($_GET["id"], 0, 4);
}
if (preg_match("/\/index\.php\/(.+)\/(.+)\/([A-Za-z]{4})-/", $_SERVER["DOCUMENT_URI"], $matches)) {
$article_id = $matches[3];
}
// connect to MySQL via WP login
$conn = mysqli_connect("localhost", MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
if (isset($article_id)) {
if (preg_match("/\/nouvelles\//", $_SERVER["DOCUMENT_URI"])) {
/*
This part of the script decodes the Nouvelles ID and search the current DB for the right article
*/
$decoded_day = strrpos(CODE, $article_id[0]);
$decoded_month = strrpos(CODE, $article_id[1]);
$decoded_year = strrpos(CODE, $article_id[2]) + 2000;
$decoded_rank = strrpos(CODE, $article_id[3]);
$query = <<<QUERY
SELECT guid
FROM wp_posts INNER JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
WHERE term_taxonomy_id = 3
AND YEAR(post_date) = $decoded_year
AND MONTH(post_date) = $decoded_month
AND DAY(post_date) = $decoded_day
ORDER BY post_date;
QUERY;
$result = mysqli_query($conn, $query);
mysqli_data_seek($result, $decoded_rank - 1);
$row = mysqli_fetch_row($result);
header("Location: " . str_replace("http:", "https:", $row[0]), true, 301);
} elseif(preg_match("/\/nezvroses\//", $_SERVER["DOCUMENT_URI"])) {
$current_guid = get_current_guid_from_historical_id_for($conn, "nezvroses", decode_article($article_id));
if (!empty($current_guid)) {
header("Location: " . str_replace("http:", "https:", $current_guid), true, 301);
} else {
http_response_code(404);
}
} elseif(preg_match("/\/analyses\//", $_SERVER["DOCUMENT_URI"])) {
$current_guid = get_current_guid_from_historical_id_for($conn, "analyses", decode_article($article_id));
if (!empty($current_guid)) {
header("Location: " . str_replace("http:", "https:", $current_guid), true, 301);
} else {
http_response_code(404);
}
} elseif(preg_match("/\/editoriaux\//", $_SERVER["DOCUMENT_URI"])) {
$current_guid = get_current_guid_from_historical_id_for($conn, "editos", decode_article($article_id));
if (!empty($current_guid)) {
header("Location: " . str_replace("http:", "https:", $current_guid), true, 301);
} else {
http_response_code(404);
}
} elseif(preg_match("/\/apercus\//", $_SERVER["DOCUMENT_URI"])) {
$current_guid = get_current_guid_from_historical_id_for($conn, "apercus", decode_article($article_id));
if (!empty($current_guid)) {
header("Location: " . str_replace("http:", "https:", $current_guid), true, 301);
} else {
http_response_code(404);
}
} else {
// any other matches (apercus, etc…) will be sent a 404 for now
http_response_code(404);
}
} else {
header("Location: https://www.nintendojo.fr", true, 307);
}
mysqli_close($conn);

View File

@@ -0,0 +1,4 @@
---
retrodojo_access_url: "{{ web_hostname | selectattr('type', 'defined') | selectattr('type', '==', 'retrodojo') | map(attribute='host') | first }}"
retrodojo_home: "/srv/http/{{ retrodojo_access_url }}"