Created
November 22, 2021 23:23
-
-
Save geraldvillorente/7e1a6317a41b8c71a5f69f6ca3e89ca7 to your computer and use it in GitHub Desktop.
Cache warmer - Pantheon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ($_ENV['PANTHEON_ENVIRONMENT'] != 'live') { | |
die(); | |
} | |
$config['email'] = '[EMAIL]'; | |
$config['sitemap'] = 'https://www.mydomain.com/post-sitemap.xml'; | |
check_sitemap_urls($config['sitemap'], $config['email'], 30); | |
/** | |
* Everything below this line is repeated across all scripts in this directory because files in private directory cannot be included/required | |
* Check the urls from provided sitemap and send resuls by email(email currently disabled) | |
*/ | |
function check_sitemap_urls($sitemap_url, $email, $limit = -1, $offset = 0) { | |
$time_start = microtime(true); | |
$check_urls = get_url_list($sitemap_url); | |
if (!isset($check_urls) || empty($check_urls)) { | |
die('Must define urls to check.'); | |
} | |
$failed = 0; | |
$results = array(); | |
// if a limit is set make sure we get the latest entries to the limit | |
if($limit !== -1 && is_int($limit) && $limit > 0 && is_int($offset)) { | |
$check_urls = array_slice(array_reverse($check_urls), $offset, $limit); | |
} | |
foreach ( $check_urls as $url ) { | |
$status = url_checker_test_url($url); | |
$results[] = array( | |
'url' => $url, | |
'status' => $status | |
); | |
if ($status != 200) { | |
$failed++; | |
} | |
} | |
$tested = sizeof($results); | |
$output = url_checker_build_output($results, $failed); | |
$time_end = microtime(true); | |
print $output; | |
print 'Tested: ' . $tested . "\n"; | |
print 'Time: ' . ($time_end - $time_start) . " seconds\n\n"; | |
if (isset($config['email'])) { | |
$subject = 'Cache warm report: www.asapp.com'; | |
$message = ''; | |
$message .= 'Failed: ' . $failed . "\n"; | |
$message .= 'Tested: ' . $tested . "\n"; | |
$message .= 'Time: ' . ($time_end - $time_start) . " seconds\n\n"; | |
$message .= "Below is a list of each tested url and its status:\n\n"; | |
$message .= $output; | |
$acceptedForDelivery = mail($config['email'], $subject, $message); | |
if ($acceptedForDelivery) { | |
print "Sent email to {$config['email']}.\n"; | |
} | |
else { | |
print "Notification email to {$config['email']} could not be queued for delivery.\n"; | |
} | |
} | |
} | |
/** | |
* Gets a list of urls from a provided sitemap address | |
*/ | |
function get_url_list($sitemap_url) { | |
$check_urls = array(); | |
$xml = simplexml_load_file($sitemap_url); | |
foreach ($xml->url as $entry) { | |
$check_urls[] = $entry->loc; | |
} | |
return $check_urls; | |
} | |
/** | |
* Constructs workflow output | |
*/ | |
function url_checker_build_output($results, $failed) { | |
$output = "\nURL Checks\n--------\n"; | |
foreach ($results as $item) { | |
$output .= ' ' . $item['status'] . ' - ' . $item['url'] . "\n"; | |
} | |
$output .= "--------\n" . $failed . " failed\n\n"; | |
return $output; | |
} | |
/** | |
* Try to access the specified URL, and return the http status code. | |
*/ | |
function url_checker_test_url($url) { | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_exec($ch); | |
$info = curl_getinfo($ch); | |
curl_close($ch); | |
return $info['http_code']; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment