-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathislandora_log_404_cleaner.module
36 lines (33 loc) · 1.2 KB
/
islandora_log_404_cleaner.module
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
<?php
/**
* Implements hook_cron().
*/
function islandora_log_404_cleaner_cron() {
islandora_log_404_cleaner_remove_entries();
}
function islandora_log_404_cleaner_remove_entries() {
$database = \Drupal::database();
// Query covers both _format=json and _format=jsonld.
$entries_result = $database->query("SELECT wid, location FROM {watchdog} WHERE type = 'page not found' AND location LIKE '%_format=json%'");
$stale_not_founds = [];
foreach ($entries_result as $row) {
$client = \Drupal::httpClient();
try {
$result = $client->head($row->location);
if ($result->getStatusCode() == 200 && str_contains($row->location, '/media/')) {
$stale_not_founds[] = $row->wid;
}
}
catch (ClientException $e) {
\Drupal::logger('islandora_log_404_cleaner')->error('Caught exception when pinging media: ' . $e->getMessage() . "\n");
}
}
// Now that we have a list of media that do in fact exist, delete the entries from the watchdog table.
if (count($stale_not_founds) > 0) {
foreach ($stale_not_founds as $wid) {
$delete_query = \Drupal::database()->delete('watchdog');
$delete_query->condition('wid', $wid);
$delete_query->execute();
}
}
}