Skip to content

Instantly share code, notes, and snippets.

@chadsten
Created March 6, 2014 23:00
Show Gist options
  • Save chadsten/9401608 to your computer and use it in GitHub Desktop.
Save chadsten/9401608 to your computer and use it in GitHub Desktop.
<?php
# Unit test for paths imported by feeds
# Not amazing, but will gather 404 info
main();
function main() {
$paths = parse_csv('all-paths.csv',',');
$compare_path = compare_paths($paths);
render_results($compare_path['pathing'], 'Missing Path & Working Content');
render_results($compare_path['node'], 'Missing Content & Working Path');
render_results($compare_path['unpublished'], 'Working & Likely Unpublished');
render_results($compare_path['misc'], 'Misc');
render_results($compare_path['missing'], 'Missing Content');
render_results($compare_path['working'], 'Fully Working');
}
function render_results($results, $title) {
echo "<h2>" . $title . "</h2>";
echo "<table>";
foreach ($results as $result) {
echo "<tr>";
echo "<td>" . $result . "</td>";
echo "</tr>";
}
echo "</table>";
}
function parse_csv($filename='', $delimiter=',') {
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE) {
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
if(!$header) {
$header = $row;
} else {
$data[] = array_combine($header, $row);
}
}
fclose($handle);
}
#print_r($data);
return $data;
}
function compare_paths($paths) {
foreach ($paths as $path) {
$t['p'] = check_url($path['path']);
$t['a'] = check_url($path['alias']);
if (($t['p'] == $t['a']) AND ($t['p'] == '404')) {
$pages['missing'][] = $path['alias'] . "(" . $t['a'] . ")" . " -> " . $path['path'] . "(" . $t['p'] . ")";
} elseif (($t['p'] == '200') AND ($t['a'] == '301')) {
$pages['working'][] = $path['alias'] . "(" . $t['a'] . ")" . " -> " . $path['path'] . "(" . $t['p'] . ")";
} elseif (($t['p'] != $t['a']) AND ($t['a'] == '404')) {
$pages['pathing'][] = $path['alias'] . "(" . $t['a'] . ")" . " -> " . $path['path'] . "(" . $t['p'] . ")";
} elseif (($t['p'] != $t['a']) AND ($t['p'] == '404')) {
$pages['node'][] = $path['alias'] . "(" . $t['a'] . ")" . " -> " . $path['path'] . "(" . $t['p'] . ")";
} elseif (($t['a'] == '301') AND ($t['p'] == '403')) {
$pages['unpublished'][] = $path['alias'] . "(" . $t['a'] . ")" . " -> " . $path['path'] . "(" . $t['p'] . ")";
} else {
$pages['misc'][] = $path['alias'] . "(" . $t['a'] . ")" . " -> " . $path['path'] . "(" . $t['p'] . ")";
}
}
#print_r($pages);
return $pages;
}
function check_url($url) {
$url = 'http://centerforfaithandwork.com/' . $url;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($handle);
$http_code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
return $http_code;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment