Skip to content

Commit

Permalink
Add mechanism to export broken links.
Browse files Browse the repository at this point in the history
  • Loading branch information
EarthlingDavey committed Dec 19, 2024
1 parent c118ab1 commit e88e156
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions public/app/themes/clarity/inc/commands/FindDocumentRefs.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function __invoke($args, $assoc_args)
// Init an array to store the documents and their references.
$documents = [];

// Broken document links
$broken_links = [];

foreach ($results as $result) {
if ($result->type === 'post') {
WP_CLI::line("Processing Post: {$result->ID}");
Expand All @@ -74,11 +77,41 @@ public function __invoke($args, $assoc_args)
$part = explode('"', $part)[0];
$part = explode('\'', $part)[0];
$part = explode(')', $part)[0];
$part = explode("\n", $part)[0]; // 2211 before this, 2194 after

// Trim all white space
$part = trim($part); // 2194 before this, 2187 after

// Get the document ID
$document_id = url_to_postid('/documents/' . rtrim($part, '/'));

if (!$document_id) {
// Log the $part, so we can try and manually find it.
WP_CLI::line("Could not find document ID for: {$part}");

// Get the agencies for this post
$terms = get_the_terms($result->ID, 'agency');

// If there is only one agency and the slug is the same as the agency we're looking for, log the part.
if (is_array($terms) && count($terms) === 1 && $terms[0]->slug === $agency) {
WP_CLI::line("Document link not found: {$part}");
WP_CLI::line("On {$agency} page: " . get_permalink($result->ID));

$post_url = get_permalink($result->ID);

// If we're running locally, replace the URL with the production URL.
if (get_home_url() === 'http://intranet.docker') {
$post_url = str_replace('http://intranet.docker', 'https://intranet.justice.gov.uk', get_permalink($result->ID));
}

$broken_links[] = [
'location_id' => $result->ID,
'location' => $post_url,
'link' => $part,
'post_type' => get_post_type($result->ID)
];
}

continue;
}

Expand Down Expand Up @@ -123,6 +156,10 @@ public function __invoke($args, $assoc_args)
}
}

/**
* Document references
*/

// Sort the documents by document_id
uasort($documents, static function ($a, $b) {
return $b['document_id'] <=> $a['document_id'];
Expand All @@ -140,6 +177,20 @@ public function __invoke($args, $assoc_args)
$fd = fopen("/var/www/html/tmp/{$agency}_document_references.csv", 'w');
WP_CLI\Utils\write_csv($fd, $documents_flat);
fclose($fd);

/**
* Broken links
*/

// Sort the broken links by location
uasort($broken_links, static function ($a, $b) {
return $b['location_id'] <=> $a['location_id'];
});

// Write the CSV
$fd = fopen("/var/www/html/tmp/{$agency}_broken_links.csv", 'w');
WP_CLI\Utils\write_csv($fd, $broken_links);
fclose($fd);
}
}

Expand Down

0 comments on commit e88e156

Please sign in to comment.