• Welcome to the Chevereto user community!

    Here users from all over the world gather around to learn the latest about Chevereto and contribute with ideas to improve the software.

    Please keep in mind:

    • 😌 This community is user driven. Be polite with other users.
    • 👉 Is required to purchase a Chevereto license to participate in this community (doesn't apply to Pre-sales).
    • 💸 Purchase a Pro Subscription to get access to active software support and faster ticket response times.
  • Chevereto Support CLST

    Support response

    Support checklist

XML Sitemap

This is basically improved version of this one

Changes:

1) Includes all public images
2) Added "caption" element
3) Excluded images of private profiles
4) Added albums sitemap (only public ones which are not password protected or from private profiles)
5) Added users sitemap (only public profiles)

Info:

This generates a sitemap-index.xml in root and images1.xml, images2.xml, etc... , albums1.xml, etc... , users1.xml, etc... in "xml-sitemaps" folder

Optional edits:

Line 4 to change sitemaps folder to something other then "xml-sitemaps"
Line 5 to change maximum number of urls to be included in each sitemap

Files

Route (upload/add to app/routes/route.xml-sitemap.php)

PHP:
<?php
$route = function($handler) {
try {
  $sitemapsFolder = "xml-sitemaps/"; //Child sitemaps folder(Optional change)
  $max_url_limit = 10000; // 50k is maximum  https://support.google.com/webmasters/answer/183668?hl=en

  //DO NOT CHANGE
  //////////////////////////////////////////////////////////
  $absSitemapsFolder = getcwd()."/".$sitemapsFolder;
  $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 paths
  $site_upload_image_route = CHV\getSetting('upload_image_path');
  $site_album_route = CHV\getSetting('route_album');
  $site_image_route = CHV\getSetting('route_image');
  $site_user_route = CHV\getSetting('user_routing');

  // To get no of child sitemaps in each one
  /////////////////////////////////////////////////////////////////////////////////////////////////

  // Count no of images and then round off to higher number.
  $count_images = CHV\DB::queryFetchSingle("SELECT COUNT(*) as total FROM " . CHV\DB::getTable('images'). "")['total'];
  if ($count_images > 0) {
  $image_sitemap_count = ceil($count_images/$max_url_limit);
  for ($i=1; $i<= $image_sitemap_count; $i++) {
  $lists[] = array("list_id" => images, "list_name" => "Images ".$i, "list_slug" => "images".$i);
  }
  }

  // Count no of albums and then round off to higher number.
  $count_albums = CHV\DB::queryFetchSingle("SELECT COUNT(*) as total FROM " . CHV\DB::getTable('albums'). " WHERE album_privacy = 'public' AND album_password IS NULL AND NOT album_image_count = 0")['total'];
  if ($count_albums > 0) {
  $album_sitemap_count = ceil($count_albums/$max_url_limit);
  for ($j=1; $j<= $album_sitemap_count; $j++) {
  $lists[] = array("list_id" => albums, "list_name" => "Albums ".$j, "list_slug" => "albums".$j);
  }
  }

  // Count no of users and then round off to higher number.
  $count_users = CHV\DB::queryFetchSingle("SELECT COUNT(*) as total FROM " . CHV\DB::getTable('users'). " WHERE user_is_private = '0'")['total'];
  if ($count_users > 0) {
  $user_sitemap_count = ceil($count_users/$max_url_limit);
  for ($k=1; $k<= $user_sitemap_count; $k++) {
  $lists[] = array("list_id" => users, "list_name" => "Users ".$k, "list_slug" => "users".$k);
  }
  }

  $handler::$vars['lists'] = $lists;
 
  //Strip away the unwanted chars
  function utf8_for_xml($string)
  {
  return preg_replace ('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string);
  }
 

  // Create index sitemap on root dir
  //////////////////////////////////////////////////////////////////////////////////////
  $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" />');


  foreach($lists as $list) {
  $c = $xml->addChild("sitemap");

  $xml_url  = $handler->base_url.$sitemapsFolder.$list["list_slug"].".xml";
  $xml_local  = $sitemapsFolder.$list["list_slug"].".xml";

  $c->addChild("loc", $xml_url);
  $c->addChild("lastmod", $dateTime );


  // Lets create a sitemaps
  /////////////////////////////////////////////////////////////////////////////////
  $xml2 = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" />');

  $DB = CHV\DB::getInstance();

  if($list["list_id"] == "images" and $count_images > 0) {

  for ($l=1; $l<= $image_sitemap_count; $l++) {

  $iurl_offset = $max_url_limit*($l-1);
  if($list["list_slug"] == "images".$l) {
  $q = $DB->query("SELECT image_id, image_title, image_description, DATE_FORMAT(image_date, '%Y') as year, DATE_FORMAT(image_date, '%m') as month, DATE_FORMAT(image_date, '%d') as day, image_storage_mode, image_storage_id, image_date, image_name, image_extension, storage_url FROM " . CHV\DB::getTable('images'). "
  LEFT JOIN " . CHV\DB::getTable('storages'). " on storage_id = image_storage_id
  LEFT JOIN " . CHV\DB::getTable('users'). " on user_id = image_user_id
  WHERE user_is_private = '0' AND image_user_id IS NOT NULL
  UNION
  SELECT image_id, image_title, image_description, DATE_FORMAT(image_date, '%Y') as year, DATE_FORMAT(image_date, '%m') as month, DATE_FORMAT(image_date, '%d') as day, image_storage_mode, image_storage_id, image_date, image_name, image_extension, storage_url FROM " . CHV\DB::getTable('images'). "
  LEFT JOIN " . CHV\DB::getTable('storages'). " on storage_id = image_storage_id
  WHERE image_user_id IS NULL
  ORDER BY image_date DESC LIMIT ".$max_url_limit." OFFSET ".$iurl_offset."");
  }
  }

  }
 
  if($list["list_id"] == "albums" and $count_albums > 0) {

  for ($m=1; $m<= $album_sitemap_count; $m++) {

  $aurl_offset = $max_url_limit*($m-1);
  if($list["list_slug"] == "albums".$m) { 
  $q = $DB->query("SELECT album_id FROM " . CHV\DB::getTable('albums'). " LEFT JOIN " . CHV\DB::getTable('users'). " on user_id = album_user_id WHERE album_privacy = 'public' AND album_password IS NULL AND NOT album_image_count = 0 AND user_is_private = '0' ORDER BY album_id DESC LIMIT ".$max_url_limit." OFFSET ".$aurl_offset."");
  }
  } 

  }
  if($list["list_id"] == "users" and $count_users > 0) {

  for ($n=1; $n<= $user_sitemap_count; $n++) {
  $uurl_offset = $max_url_limit*($n-1);
  if($list["list_slug"] == "users".$n) { 
  $q = $DB->query("SELECT user_username FROM " . CHV\DB::getTable('users'). " WHERE user_is_private = '0' ORDER BY user_id DESC LIMIT ".$max_url_limit." OFFSET ".$uurl_offset."");
  }
  }
  }

  $images = $DB->fetchAll();
  //Inner xml elements
  /////////////////////////////////////////////////////////////
  foreach($images as $image) {

  if($list["list_id"] == "albums" and $count_albums > 0) {

  // Every album gets its own URL
  $cc = $xml2->addChild("url");
  $cc->addChild("loc", $handler->base_url.$site_album_route."/".CHV\encodeID($image["album_id"], "encode") ); 

  }
 
  if($list["list_id"] == "users" and $count_users > 0) {

  if ($site_user_route == "1") {
  $user_base_url = $handler->base_url;
  } else {
  $user_base_url = $handler->base_url.'user/';
  }
 
  // Every user gets their own URL
  $cc = $xml2->addChild("url");
  $cc->addChild("loc", $user_base_url.$image["user_username"]); 

  }
 
  if($list["list_id"] == "images" and $count_images > 0) { 

  $folder = "";

  switch ($image["image_storage_mode"]){
  case "datefolder":
  $folder = $image["year"]."/".$image["month"]."/".$image["day"]."/";
  break;

  default:
  $folder = "";
  break;
  }
 
  // Every image gets its own URL
  $cc = $xml2->addChild("url");
  $cc->addChild("loc", $handler->base_url.$site_image_route."/".CHV\encodeID($image["image_id"], "encode") );
 
  if ($image["image_storage_id"] == NULL) {
  $image_base_url = $handler->base_url.$site_upload_image_route.'/';
  } else {
  $image_base_url = $image["storage_url"];
  } 

  $cc_image = $cc->addChild("xmlns:image:image");
  $cc_image->addChild("xmlns:image:loc", $image_base_url.$folder.$image["image_name"].".".$image["image_extension"]);
  $cc_image->addChild("xmlns:image:title", utf8_for_xml($image["image_title"]));
  if (strlen($image["image_description"]) > 3) {
  $cc_image->addChild("xmlns:image:caption", utf8_for_xml($image["image_description"]));
  }
  }

  }

  $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("sitemap-index.xml", $dom->saveXML(), LOCK_EX) === false){
  die("Could not write sitemap-index.xml");
  }

  $handler::setVar('pre_doctitle', _s('XML Sitemaps'));

} catch(Exception $e) {
  G\exception_to_error($e);
}
};

?>

Template
(upload/add to app/themes/YOUR_THERE_NAME/views/xml-sitemap.php)
Using default one? then (upload/add to app/themes/Peafowl/views/xml-sitemap.php)

PHP:
<?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>XML Sitemaps</h1>
  <h4><a href="<?php echo G\get_base_url()."/sitemap-index.xml";?>">Index</a></h4>
  <br />

  <h5>Child Sitemaps</h5>
  <br />

  <div>
  <ul>
  <?php
  $cat = get_lists();
  foreach($cat as $key){
  echo "<li><a href=\"".G\get_base_url()."/".get_sitemapsFolder().$key["list_slug"].".xml"."\">".$key["list_name"]."</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 } ?>

Add cron job
(The below one runs two times a day at 6th and 18th hr)
Code:
0 6,18 * * *   wget -O /dev/null -o /dev/null http://www.domain.tld/xml-sitemap

(Do not forget to submit (or change) sitemap in webmaster section)

Thats it
I'm using the version:
3.20.18 and I tried but failed whether this article is old compared to the current chevereto version?
 
Back
Top