Caching not working properly for various reasons, removed for now

This commit is contained in:
DrudexSoftware 2013-02-11 08:43:06 +01:00
parent b7b8786f18
commit 59ec209c4a

View File

@ -1,135 +1,91 @@
<?php <?php
/* /*
* Name: XML Sitemap * Name: XML Sitemap
* Author: Sein Kraft <mail@seinkraft.info>, Drudex Software <support@drudexsoftware.com * Author: Sein Kraft <mail@seinkraft.info>
* License: GPLv2 * License: GPLv2
* Description: Adds sitemap.xml on request. * Description: Adds sitemap.xml on request.
* Documentation: * Documentation:
*/ */
class XMLSitemap extends Extension { class XMLSitemap extends Extension {
private $sitemap_queue = ""; private $sitemap_queue = "";
private $time_structure = "YmdHis";
private $sitemap_creation_interval = 6; // number of hours between sitemap regeneration public function onPageRequest(PageRequestEvent $event) {
global $database, $config;
public function onInitExt(InitExtEvent $event) { if($event->page_matches("sitemap.xml"))
global $config; {
// add index
// adds last_sitemap to config $index[0] = $base_href = $config->get_string("front_page");
$config->set_default_string("last_sitemap", // set initial date to a year ago $this->add_sitemap_queue($index, "weekly", "1");
date($this->time_structure, strtotime('-1 year',time())));
} /* --- Add 20 most used tags --- */
$popular_tags = $database->get_all("SELECT tag, count FROM tags ORDER BY `count` DESC LIMIT 0,20");
public function onPageRequest(PageRequestEvent $event) { foreach($popular_tags as $arrayid => $tag) {
global $database, $config; $tag = $tag['tag'];
if($event->page_matches("sitemap.xml")) $popular_tags[$arrayid] = "post/list/$tag/";
{ }
// creates default value $this->add_sitemap_queue($popular_tags, "monthly", "0.9" /* not sure how to deal with date here */);
$config->set_default_string("last_sitemap", // set initial date to a year ago
date($this->time_structure, strtotime('-1 year',time()))); /* --- Add latest images to sitemap with higher priority --- */
$latestimages = Image::find_images(0, 50, array());
// remakes sitemap if needed $latestimages_urllist = array();
$lastsitemaptime = date($this->time_structure, $config->get_string("last_sitemap")); foreach($latestimages as $arrayid => $image)
$sitemap_creation_allowed_time = date($this->time_structure, strtotime("+$this->sitemap_creation_interval hours", $lastsitemaptime)); // create url from image id's
if ($sitemap_creation_allowed_time < time() || // sitemap is allowed to reset $latestimages_urllist[$arrayid] = "post/view/$image->id";
!file_exists($_SERVER["DOCUMENT_ROOT"]."/sitemap.xml")) // or sitemap can be remade $this->add_sitemap_queue($latestimages_urllist, "monthly", "0.8", date("Y-m-d", $image->posted_timestamp));
{
// add index /* --- Add other tags --- */
$index[0] = $base_href = make_http(make_link("post/list")); $other_tags = $database->get_all("SELECT tag, count FROM tags ORDER BY `count` DESC LIMIT 21,10000000");
$this->add_sitemap_queue($index, "weekly", "1"); foreach($other_tags as $arrayid => $tag) {
$tag = $tag['tag'];
/* --- Add 20 most used tags --- */ // create url from tags (tagme ignored)
$popular_tags = $database->get_all("SELECT tag, count FROM tags ORDER BY `count` DESC LIMIT 0,20"); if ($tag != "tagme")
foreach($popular_tags as $arrayid => $tag) { $other_tags[$arrayid] = "post/list/$tag/";
$tag = $tag['tag']; }
// create url from tags (tagme ignored) $this->add_sitemap_queue($other_tags, "monthly", "0.7" /* not sure how to deal with date here */);
if ($tag != "tagme")
$popular_tags[$arrayid] = "post/list/$tag/"; /* --- Add all other images to sitemap with lower priority --- */
} $otherimages = Image::find_images(51, 10000000, array());
$this->add_sitemap_queue($popular_tags, "monthly", "0.9" /* not sure how to deal with date here */); foreach($otherimages as $arrayid => $image)
// create url from image id's
/* --- Add latest images to sitemap with higher priority --- */ $otherimages[$arrayid] = "post/view/$image->id";
$latestimages = Image::find_images(0, 50, array()); $this->add_sitemap_queue($otherimages, "monthly", "0.6", date("Y-m-d", $image->posted_timestamp));
$latestimages_urllist = array();
foreach($latestimages as $arrayid => $image)
// create url from image id's /* --- Display page --- */
$latestimages_urllist[$arrayid] = "post/view/$image->id"; // when sitemap is ok, display it from the file
$this->add_sitemap_queue($latestimages_urllist, "monthly", "0.8", date("Y-m-d", $image->posted_timestamp)); $this->display_sitemap();
}
/* --- 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) { // Adds an array of urls to the sitemap with the given information
$tag = $tag['tag']; private function add_sitemap_queue(/*array(urls)*/ $urls, $changefreq="monthly", $priority="0.5", $date="2013-02-01") {
// create url from tags (tagme ignored) foreach($urls as $url) {
if ($tag != "tagme") $link = make_http(make_link("$url"));
$other_tags[$arrayid] = "post/list/$tag/"; $this->sitemap_queue .= "
} <url>
$this->add_sitemap_queue($other_tags, "monthly", "0.7" /* not sure how to deal with date here */); <loc>$link</loc>
<lastmod>$date</lastmod>
/* --- Add all other images to sitemap with lower priority --- */ <changefreq>$changefreq</changefreq>
$otherimages = Image::find_images(51, 10000000, array()); <priority>$priority</priority>
foreach($otherimages as $arrayid => $image) </url>";
// create url from image id's }
$otherimages[$arrayid] = "post/view/$image->id"; }
$this->add_sitemap_queue($otherimages, "monthly", "0.6", date("Y-m-d", $image->posted_timestamp));
// sets sitemap with entries in the queue
// Creates the sitemap file private function display_sitemap()
$this->create_sitemap(); {
} global $page;
$page->set_mode("data");
/* --- Display page --- */ $page->set_type("application/xml");
// when sitemap is ok, display it from the file
$this->display_sitemap(); $xml = "<"."?xml version=\"1.0\" encoding=\"utf-8\"?".">
} <urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">
} $this->sitemap_queue
</urlset>";
// Adds an array of urls to the sitemap with the given information
private function add_sitemap_queue(/*array(urls)*/ $urls, $changefreq="monthly", $priority="0.5", $date="2013-02-01") { // sets
foreach($urls as $url) { $page->set_data($xml);
$link = make_http(make_link("$url")); }
$this->sitemap_queue .= " }
<url> ?>
<loc>$link</loc>
<lastmod>$date</lastmod>
<changefreq>$changefreq</changefreq>
<priority>$priority</priority>
</url>";
}
}
// sets sitemap with entries in the queue
private function display_sitemap()
{
global $page;
$page->set_mode("data");
$page->set_type("application/xml");
// read file
$filename = $_SERVER['DOCUMENT_ROOT']."/sitemap.xml";
$xmlreader = fopen($filename, 'r') or die("can't open file");
$xml = fread($xmlreader, filesize($filename));
fclose($xmlreader);
// sets
$page->set_data($xml);
}
// creates the xml sitemap file from the queue
private function create_sitemap()
{
global $config;
$xml = "<"."?xml version=\"1.0\" encoding=\"utf-8\"?".">
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">
$this->sitemap_queue
</urlset>";
$fh = fopen($_SERVER["DOCUMENT_ROOT"]."/sitemap.xml", 'w') or die("can't open file");
fwrite($fh, $this->sitemap_queue);
fclose($fh);
file_put_contents($_SERVER['DOCUMENT_ROOT']."/sitemap.xml", $xml);
$config->set_string("last_sitemap", date($this->time_structure, time()));
}
}
?>