Skip to content

Instantly share code, notes, and snippets.

@samhavens
Last active August 29, 2015 14:01
Show Gist options
  • Save samhavens/0483ccfc04c4f9906e92 to your computer and use it in GitHub Desktop.
Save samhavens/0483ccfc04c4f9906e92 to your computer and use it in GitHub Desktop.
Sitemap Generator from db for sites OTF www.site.com/type/sub_type
<!--
This generates a sitemap from the database schema yourdb.
It assumes you have an updated copy of the database available locally.
It has options for setting the relative priority of type- and sub_type-specific pages.
It can generate a new robots.txt.
TO-DO: add "auto-submit to google" option (first need to submit manually once).
-->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>XML Sitemap Generator</title>
</head>
<body>
<h2>Sitemap Generator</h2>
<form action="sitemapgenerator.php" id="urlform" method="post">
Base URL: <input type="text" name="baseurl" value="www.your_site.com"> <br>
type Priority: <input type="text" name="typepriority" value="0.8"> <br>
sub_type Priority: <input type="text" name="sub_typepriority" value="0.5"> <br>
Generate new robots.txt <input type="checkbox" name="robots" value="Yes"><br>
<input type="submit">
</form>
<br>
<br>
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8', 'username', 'password');
} catch(PDOException $ex) {
echo "An Error occured!";
}
//if requested, type a new robots.txt
if (isset($_POST['robots']) && $_POST['robots']=='Yes')
{
$roboto = fopen('robots.txt', 'w+');
fwrite($roboto, "Sitemap: http://{$_POST['baseurl']}/sitemap.xml\n");
fwrite($roboto, "User-agent:*\nDisallow:");
echo '<p>New robots.txt was created to ' . realpath('robots.txt') . '</p>';
fclose($roboto);
}
if (!empty($_POST['baseurl']))
{
// type the entry for the base url (www.gtvintage.com or the like)
$smap = fopen('sitemap.xml', 'w+');
fwrite($smap, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
fwrite($smap, "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n");
fwrite($smap, "<url>\n");
fwrite($smap, "<loc>http://{$_POST['baseurl']}</loc>\n");
fwrite($smap, "<priority>1</priority>\n");
fwrite($smap, "</url>\n\n");
//Get all needed info from database
$sql = $db->query('SELECT id, type, sub_type FROM type_table, sub_type_table WHERE (conditions if any) ORDER BY id, sub_type;');
$results_array = $sql->fetchAll(PDO::FETCH_ASSOC);
$ids = array();
foreach($results_array as $row)
{
//give convenient names and make URL-encoded
$id = $row['id'];
$type = $row['type'];
$type = str_replace(' ', '-', $type);
$type = str_replace('&', '%26', $type);
$type = str_replace('!', '%21', $type);
$sub_type = $row['sub_type'];
$sub_type = str_replace(' ', '-', $sub_type);
$sub_type = str_replace('&', '%26', $sub_type);
$sub_type = str_replace('!', '%21', $sub_type);
//if type already has a page, just make sub_type-specific pages
if (in_array($id, $ids))
{
//creates the entries for the sub_type-specific pages
fwrite($smap, "<url>\n");
fwrite($smap, "<loc>http://{$_POST['baseurl']}/{$type}/{$sub_type}</loc>\n");
fwrite($smap, "<priority>{$_POST['sub_typepriority']}</priority>\n");
fwrite($smap, "</url>\n\n");
}
else //type does not have page so need to make one
{
//creates the entry for the type-specific page
fwrite($smap, "<url>\n");
fwrite($smap, "<loc>http://{$_POST['baseurl']}/{$type}</loc>\n");
fwrite($smap, "<priority>{$_POST['typepriority']}</priority>\n");
fwrite($smap, "</url>\n");
//creates the entries for the sub_type-specific pages
fwrite($smap, "<url>\n");
fwrite($smap, "<loc>http://{$_POST['baseurl']}/{$type}/{$sub_type}</loc>\n");
fwrite($smap, "<priority>{$_POST['sub_typepriority']}</priority>\n");
fwrite($smap, "</url>\n\n");
// now this type has its own page so don't have to create it again.
$ids[] = $id;
}
}
fwrite($smap, "</urlset>");
echo '<p>New sitemap was created to ' . realpath('sitemap.xml') . '</p>';
fclose($smap);
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment