Hello,
I would like to share my sitemap script with you.
My script creates for everey of you categorie a own sitemap xml + a unsorted for everey image without a category.
This script just need 2 files (1 route + 1 template).
I`m using only external storage, you may have to do few changes to this script (from line 82).
Route (upload to app/routes/route.createSitemap.php)
Template (upload to app/themes/Peafowl/views/createSitemap.php)
Thats its. You just need a cron which runs your route script for example every hour.
Command: crontab -e
Or use a website like https://www.setcronjob.com/
Todo:
I would like to share my sitemap script with you.
My script creates for everey of you categorie a own sitemap xml + a unsorted for everey image without a category.
This script just need 2 files (1 route + 1 template).
I`m using only external storage, you may have to do few changes to this script (from line 82).
Route (upload to app/routes/route.createSitemap.php)
PHP:
<?php
$route = function($handler) {
try {
//var_dump($handler);
$sitemapsFolder = "sitemaps/";
$absSitemapsFolder = getcwd()."/".$sitemapsFolder;
$last_pictures = 50000; // 50k is maximum https://support.google.com/webmasters/answer/183668?hl=en
$handler::$vars['sitemapsFolder'] = $sitemapsFolder;
/////////////////////////////////////////////////////////////////////
// Basic things
if( !file_exists($absSitemapsFolder)){
if(!mkdir($absSitemapsFolder))
throw new Exception("Can not create ".$sitemapsFolder);
if(!is_readable($absSitemapsFolder) or !is_writeable($absSitemapsFolder))
throw new Exception("Can not read/write to ".$sitemapsFolder. ". Check permissions.");
}
// lastmod
$objDateTime = new DateTime('NOW');
$dateTime = $objDateTime->format(DATE_W3C);
/////////////////////////////////////////////////////////////////////
// Get categories
$categories = CHV\DB::queryFetchAll("SELECT category_id, category_name, category_url_key FROM chv_categories");
// Add unsorted
$categories[] = array("category_id" => null, "category_name" => "unsorted", "category_url_key" => "unsorted");
$handler::$vars['categories'] = $categories;
/////////////////////////////////////////////////////////////////////
// Create index sitemap on root dir
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><sitemapindex />');
$xml->addAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$xml->addAttribute('xmlns:image', 'http://www.google.com/schemas/sitemap-image/1.1', 'http://www.w3.org/2001/XMLSchema-instance');
foreach($categories as $child) {
$c = $xml->addChild("sitemap");
$xml_url = $handler->base_url.$sitemapsFolder.$child["category_url_key"].".xml";
$xml_local = $sitemapsFolder.$child["category_url_key"].".xml";
$c->addChild("loc", $xml_url);
$c->addChild("lastmod", $dateTime );
/////////////////////////////////////////////////////////////////////
// Lets create a sitemap for every category
$xml2 = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset />');
$xml2->addAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$xml2->addAttribute('xmlns:image', 'http://www.google.com/schemas/sitemap-image/1.1', 'http://www.w3.org/2001/XMLSchema-instance');
$DB = CHV\DB::getInstance();
if($child["category_name"] == "unsorted"){
$q = $DB->query("SELECT image_id, image_title, image_nsfw, DATE_FORMAT(image_date_gmt, '%Y') as year, DATE_FORMAT(image_date_gmt, '%m') as month, DATE_FORMAT(image_date_gmt, '%d') as day, image_storage_mode, image_date_gmt, image_name, image_extension, storage_url FROM chv_images
LEFT JOIN chv_storages on storage_id = image_storage_id
WHERE image_category_id IS NULL
ORDER BY image_date_gmt DESC LIMIT :limit");
} else {
$q = $DB->query("SELECT image_id, image_title, image_nsfw, image_album_id, DATE_FORMAT(image_date_gmt, '%Y') as year, DATE_FORMAT(image_date_gmt, '%m') as month, DATE_FORMAT(image_date_gmt, '%d') as day, image_storage_mode, image_date_gmt, image_name, image_extension, storage_url FROM chv_images
LEFT JOIN chv_storages on storage_id = image_storage_id
WHERE image_category_id = :category
ORDER BY image_date_gmt DESC LIMIT :limit");
$DB->bind(":category", $child["category_id"]);
}
$DB->bind(":limit", $last_pictures);
$images = $DB->fetchAll();
foreach($images as $image){
//var_dump($image);
$folder = "";
switch ($image["image_storage_mode"]){
case "datefolder":
$folder = $image["year"]."/".$image["month"]."/".$image["day"]."/";
break;
default:
$folder = "";
break;
}
// Every image get his own URL
$cc = $xml2->addChild("url");
$cc->addChild("loc", $handler->base_url."image/".CHV\encodeID($image["image_id"], "encode") );
$cc_image = $cc->addChild("xmlns:image:image");
$cc_image->addChild("xmlns:image:title", $image["image_title"]);
if($image["image_nsfw"] == 0){
$cc_image->addChild("xmlns:image:family_friendly", "yes");
} else {
$cc_image->addChild("xmlns:image:family_friendly", "no");
}
$cc_image->addChild("xmlns:image:loc", $image["storage_url"].$folder.$image["image_name"].".".$image["image_extension"]);
}
$dom = new DomDocument();
$dom->loadXML($xml2->asXML());
$dom->formatOutput = true;
file_put_contents( $xml_local, $dom->saveXML(), LOCK_EX);
//
}
$dom = new DomDocument();
$dom->loadXML($xml->asXML());
$dom->formatOutput = true;
if(file_put_contents($sitemapsFolder."sitemap.xml", $dom->saveXML(), LOCK_EX) === false){
die("Could not write sitemap.xml");
}
} catch(Exception $e) {
G\exception_to_error($e);
}
};
?>
Template (upload to app/themes/Peafowl/views/createSitemap.php)
HTML:
<?php if(!defined('access') or !access) die('This file cannot be directly accessed.'); ?>
<?php G\Render\include_theme_header(); ?>
<div class="content-width">
<div class="page-not-found">
<h1>Sitemap</h1>
<h4><a href="<?php echo G\get_base_url()."/".get_sitemapsFolder()."sitemap.xml";?>">Sitemap index</a></h4>
<p>Sitemaps created for:</p>
<div>
<ul>
<?php
$cat = get_categories();
foreach($cat as $id => $key){
echo "<li>".$key["category_name"]." = <a href=\"".G\get_base_url()."/".get_sitemapsFolder().$key["category_name"].".xml"."\">".$key["category_name"].".xml</a></li>";
}
?>
</ul>
</div>
</div>
</div>
<?php G\Render\include_theme_footer(); ?>
<?php if(isset($_REQUEST["deleted"])) { ?>
<script>PF.fn.growl.call("<?php echo (G\get_route_name() == 'user' ? _s('The user has been deleted') : _s('The content has been deleted.')); ?>"); </script>
<?php } ?>
Thats its. You just need a cron which runs your route script for example every hour.
Command: crontab -e
Code:
1 * * * * wget -O /dev/null -o /dev/null https://www.domain.tld/createSitemap
Or use a website like https://www.setcronjob.com/
Todo:
- Private images should not be in a sitemap
Last edited: