2013-02-11 07:43:06 +00:00
|
|
|
<?php
|
2014-04-28 06:24:19 +00:00
|
|
|
|
2013-02-11 07:43:06 +00:00
|
|
|
/*
|
|
|
|
* Name: XML Sitemap
|
|
|
|
* Author: Sein Kraft <mail@seinkraft.info>
|
2013-02-15 03:31:31 +00:00
|
|
|
* Author: Drudex Software <support@drudexsoftware.com>
|
2013-03-01 16:29:28 +00:00
|
|
|
* Link: http://drudexsoftware.com
|
2013-02-11 07:43:06 +00:00
|
|
|
* License: GPLv2
|
2013-02-15 03:31:31 +00:00
|
|
|
* Description: Sitemap with caching & advanced priorities
|
2013-02-11 07:43:06 +00:00
|
|
|
* Documentation:
|
|
|
|
*/
|
|
|
|
|
2014-04-28 06:24:19 +00:00
|
|
|
class XMLSitemap extends Extension
|
|
|
|
{
|
|
|
|
private $sitemap_queue = "";
|
|
|
|
private $sitemap_filepath = ""; // set onPageRequest
|
|
|
|
|
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
|
|
|
if ($event->page_matches("sitemap.xml")) {
|
|
|
|
global $config;
|
|
|
|
|
2015-08-09 11:14:28 +00:00
|
|
|
$this->sitemap_filepath = data_path("cache/sitemap.xml");
|
2014-04-28 06:24:19 +00:00
|
|
|
// determine if new sitemap needs to be generated
|
|
|
|
if ($this->new_sitemap_needed()) {
|
|
|
|
// determine which type of sitemap to generate
|
|
|
|
if ($config->get_bool("sitemap_generatefull", false)) {
|
|
|
|
$this->handle_full_sitemap(); // default false until cache fixed
|
|
|
|
} else {
|
|
|
|
$this->handle_smaller_sitemap();
|
|
|
|
}
|
|
|
|
} else $this->display_existing_sitemap();
|
|
|
|
}
|
2013-02-15 02:04:58 +00:00
|
|
|
}
|
2014-04-28 06:24:19 +00:00
|
|
|
|
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
|
|
|
{
|
2013-02-15 03:31:31 +00:00
|
|
|
$sb = new SetupBlock("Sitemap");
|
2014-04-28 06:24:19 +00:00
|
|
|
|
2013-02-15 02:04:58 +00:00
|
|
|
$sb->add_bool_option("sitemap_generatefull", "Generate full sitemap");
|
2014-04-28 06:24:19 +00:00
|
|
|
$sb->add_label("<br>(Enabled: every image and tag in sitemap, generation takes longer)");
|
|
|
|
$sb->add_label("<br>(Disabled: only display the last 50 uploads in the sitemap)");
|
|
|
|
|
2013-02-15 02:04:58 +00:00
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
2014-04-28 06:24:19 +00:00
|
|
|
|
|
|
|
// sitemap with only the latest 50 images
|
|
|
|
private function handle_smaller_sitemap()
|
|
|
|
{
|
|
|
|
/* --- Add latest images to sitemap with higher priority --- */
|
|
|
|
$latestimages = Image::find_images(0, 50, array());
|
2015-08-09 11:14:28 +00:00
|
|
|
if(empty($latestimages)) return;
|
2014-04-28 06:24:19 +00:00
|
|
|
$latestimages_urllist = array();
|
|
|
|
foreach ($latestimages as $arrayid => $image) {
|
|
|
|
// create url from image id's
|
|
|
|
$latestimages_urllist[$arrayid] = "post/view/$image->id";
|
|
|
|
}
|
|
|
|
|
2015-09-27 20:09:27 +00:00
|
|
|
$this->add_sitemap_queue($latestimages_urllist, "monthly", "0.8", date("Y-m-d", strtotime($image->posted)));
|
2014-04-28 06:24:19 +00:00
|
|
|
|
|
|
|
/* --- Display page --- */
|
|
|
|
// when sitemap is ok, display it from the file
|
|
|
|
$this->generate_display_sitemap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full sitemap
|
|
|
|
private function handle_full_sitemap()
|
|
|
|
{
|
|
|
|
global $database, $config;
|
|
|
|
|
|
|
|
// add index
|
|
|
|
$index = array();
|
|
|
|
$index[0] = $config->get_string("front_page");
|
|
|
|
$this->add_sitemap_queue($index, "weekly", "1");
|
|
|
|
|
|
|
|
/* --- Add 20 most used tags --- */
|
|
|
|
$popular_tags = $database->get_all("SELECT tag, count FROM tags ORDER BY `count` DESC LIMIT 0,20");
|
|
|
|
foreach ($popular_tags as $arrayid => $tag) {
|
|
|
|
$tag = $tag['tag'];
|
|
|
|
$popular_tags[$arrayid] = "post/list/$tag/";
|
|
|
|
}
|
|
|
|
$this->add_sitemap_queue($popular_tags, "monthly", "0.9" /* not sure how to deal with date here */);
|
|
|
|
|
|
|
|
/* --- Add latest images to sitemap with higher priority --- */
|
|
|
|
$latestimages = Image::find_images(0, 50, array());
|
|
|
|
$latestimages_urllist = array();
|
|
|
|
foreach ($latestimages as $arrayid => $image) {
|
|
|
|
// create url from image id's
|
|
|
|
$latestimages_urllist[$arrayid] = "post/view/$image->id";
|
|
|
|
}
|
2015-09-27 20:09:27 +00:00
|
|
|
$this->add_sitemap_queue($latestimages_urllist, "monthly", "0.8", date("Y-m-d", strtotime($image->posted)));
|
2014-04-28 06:24:19 +00:00
|
|
|
|
|
|
|
/* --- Add other tags --- */
|
|
|
|
$other_tags = $database->get_all("SELECT tag, count FROM tags ORDER BY `count` DESC LIMIT 21,10000000");
|
|
|
|
foreach ($other_tags as $arrayid => $tag) {
|
|
|
|
$tag = $tag['tag'];
|
|
|
|
// create url from tags (tagme ignored)
|
|
|
|
if ($tag != "tagme")
|
|
|
|
$other_tags[$arrayid] = "post/list/$tag/";
|
|
|
|
}
|
|
|
|
$this->add_sitemap_queue($other_tags, "monthly", "0.7" /* not sure how to deal with date here */);
|
|
|
|
|
|
|
|
/* --- Add all other images to sitemap with lower priority --- */
|
|
|
|
$otherimages = Image::find_images(51, 10000000, array());
|
|
|
|
foreach ($otherimages as $arrayid => $image) {
|
|
|
|
// create url from image id's
|
|
|
|
$otherimages[$arrayid] = "post/view/$image->id";
|
|
|
|
}
|
2015-09-27 20:09:27 +00:00
|
|
|
$this->add_sitemap_queue($otherimages, "monthly", "0.6", date("Y-m-d", strtotime($image->posted)));
|
2014-04-28 06:24:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* --- Display page --- */
|
|
|
|
// when sitemap is ok, display it from the file
|
|
|
|
$this->generate_display_sitemap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an array of urls to the sitemap with the given information.
|
|
|
|
*
|
|
|
|
* @param array $urls
|
|
|
|
* @param string $changefreq
|
|
|
|
* @param string $priority
|
|
|
|
* @param string $date
|
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
private function add_sitemap_queue(array $urls, $changefreq = "monthly",
|
|
|
|
$priority = "0.5", $date = "2013-02-01")
|
2014-04-28 06:24:19 +00:00
|
|
|
{
|
|
|
|
foreach ($urls as $url) {
|
|
|
|
$link = make_http(make_link("$url"));
|
|
|
|
$this->sitemap_queue .= "
|
2013-02-11 07:43:06 +00:00
|
|
|
<url>
|
|
|
|
<loc>$link</loc>
|
|
|
|
<lastmod>$date</lastmod>
|
|
|
|
<changefreq>$changefreq</changefreq>
|
|
|
|
<priority>$priority</priority>
|
|
|
|
</url>";
|
2014-04-28 06:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sets sitemap with entries in sitemap_queue
|
|
|
|
private function generate_display_sitemap()
|
|
|
|
{
|
|
|
|
global $page;
|
|
|
|
|
|
|
|
$xml = "<" . "?xml version=\"1.0\" encoding=\"utf-8\"?" . ">
|
|
|
|
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">
|
|
|
|
$this->sitemap_queue
|
|
|
|
</urlset>";
|
|
|
|
|
|
|
|
// Generate new sitemap
|
|
|
|
file_put_contents($this->sitemap_filepath, $xml);
|
|
|
|
$page->set_mode("data");
|
|
|
|
$page->set_type("application/xml");
|
|
|
|
$page->set_data($xml);
|
|
|
|
}
|
|
|
|
|
2014-04-28 22:22:57 +00:00
|
|
|
/**
|
|
|
|
* Returns true if a new sitemap is needed.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-04-28 06:24:19 +00:00
|
|
|
private function new_sitemap_needed()
|
|
|
|
{
|
2015-08-09 11:14:28 +00:00
|
|
|
if(!file_exists($this->sitemap_filepath)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-28 06:24:19 +00:00
|
|
|
$sitemap_generation_interval = 86400; // allow new site map every day
|
|
|
|
$last_generated_time = filemtime($this->sitemap_filepath);
|
|
|
|
|
|
|
|
// if file doesn't exist, return true
|
|
|
|
if ($last_generated_time == false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's been a day since last sitemap creation, return true
|
|
|
|
if ($last_generated_time + $sitemap_generation_interval < time()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function display_existing_sitemap()
|
|
|
|
{
|
|
|
|
global $page;
|
|
|
|
|
|
|
|
$xml = file_get_contents($this->sitemap_filepath);
|
|
|
|
|
|
|
|
$page->set_mode("data");
|
|
|
|
$page->set_type("application/xml");
|
|
|
|
$page->set_data($xml);
|
2013-02-11 07:43:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-26 02:54:51 +00:00
|
|
|
|