Skip to content

Commit

Permalink
Update FindDocumentRefs.php
Browse files Browse the repository at this point in the history
  • Loading branch information
EarthlingDavey committed Dec 20, 2024
1 parent e88e156 commit 94e4807
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions public/app/themes/clarity/inc/commands/FindDocumentRefs.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,25 @@ 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
$part = explode("\n", $part)[0];
$part = explode("<", $part)[0];
$part = explode("?", $part)[0];
$part = explode("#", $part)[0];
$part = explode(">", $part)[0];

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

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

// Try and get the document by removing the date from the URL
if (!$document_id) {
$document_slug = end(explode('/', $part));

$document_id = url_to_postid('/documents/' . $document_slug);
}

if (!$document_id) {
// Log the $part, so we can try and manually find it.
WP_CLI::line("Could not find document ID for: {$part}");
Expand All @@ -104,12 +115,14 @@ public function __invoke($args, $assoc_args)
$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)
];
if (!isset($broken_links[$part])) {
$broken_links[$part] = [
'locations' => [$post_url],
'link' => 'https://intranet.justice.gov.uk/documents/' . $part,
];
} else {
$broken_links[$part]['locations'][] = $post_url;
}
}

continue;
Expand Down Expand Up @@ -141,6 +154,7 @@ public function __invoke($args, $assoc_args)
// If the document doesn't exist in the documents array, add it.
if (!isset($documents[$document_id])) {
$documents[$document_id] = [
'title' => get_the_title($document_id),
'document_id' => $document_id,
'links' => [$post_url]
];
Expand Down Expand Up @@ -169,6 +183,7 @@ public function __invoke($args, $assoc_args)
$documents_flat = array_map(static function ($document) {
return [
$document['document_id'],
$document['title'],
...$document['links']
];
}, $documents);
Expand All @@ -184,12 +199,19 @@ public function __invoke($args, $assoc_args)

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

$broken_links_flat = array_map(static function ($link) {
return [
$link['link'],
...$link['locations']
];
}, $broken_links);

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

0 comments on commit 94e4807

Please sign in to comment.