Find a class for generating sitemap by PHP with complete functions and clear Code. For details, refer
The Code is as follows:
/**
* Website map update Controller
*
* @ Author Garbin
* @ Usage none
*/
Class SitemapApp extends FrontendApp
{
Function _ construct ()
{
$ This-> SitemapApp ();
}
Function SitemapApp ()
{
Parent: :__ construct ();
$ This-> _ google_sitemmap_file = ROOT_PATH. '/data/google_sitemmap.xml ';
}
Function index ()
{
If (! Conf: get ('sitemap _ enabled '))
{
Return;
}
$ From = empty ($ _ GET ['from'])? 'Google ': trim ($ _ GET ['from']);
Switch ($ from)
{
Case 'Google ':
$ This-> _ output_google_sitemap ();
Break;
}
}
/**
* Output Google sitemap
*
* @ Author Garbin
* @ Return void
*/
Function _ output_google_sitemap ()
{
Header ("Content-type: application/xml ");
Echo $ this-> _ get_google_sitemap ();
}
/**
* Get Google sitemap
*
* @ Author Garbin
* @ Return string
*/
Function _ get_google_sitemap ()
{
$ Sitemap = "";
If ($ this-> _ google_sitemap_expired ())
{
/* Expired, regenerate */
/* Get the updated project */
$ Updated_items = $ this-> _ get_updated_items ($ this-> _ get_google_sitemap_lastupdate ());
/* Rebuilding sitemap */
$ Sitemap = $ this-> _ build_google_sitemap ($ updated_items );
/* Write File */
$ This-> _ write_google_sitemap ($ sitemap );
}
Else
{
/* Directly return the old sitemap */
$ Sitemap = file_get_contents ($ this-> _ google_sitemmap_file );
}
Return $ sitemap;
}
/**
* Determine if Google sitemap has expired
*
* @ Author Garbin
* @ Return boolean
*/
Function _ google_sitemap_expired ()
{
If (! Is_file ($ this-> _ google_sitemmap_file ))
{
Return true;
}
$ Frequency = Conf: get ('sitemap _ frequency ') * 3600;
$ Filemtime = $ this-> _ get_google_sitemap_lastupdate ();
Return (time ()> = $ filemtime + $ frequency );
}
/**
* Get the last update date
*
* @ Author Garbin
* @ Return int
*/
Function _ get_google_sitemap_lastupdate ()
{
Return is_file ($ this-> _ google_sitemmap_file )? Filemtime ($ this-> _ google_sitemmap_file): 0;
}
/**
* Get updated Projects
*
* @ Author Garbin
* @ Return array
*/
Function _ get_updated_items ($ timeline = 0)
{
$ Timeline & $ timeline-= date ('Z ');
$ Limit = 5000;
$ Result = array ();
/* Updated store */
$ Model_store = & m ('store ');
$ Updated_store = $ model_store-> find (array (
'Fields' => 'store _ id, add_time ',
'Condition' => "add_time >={ $ timeline} AND state =". STORE_OPEN,
'Limit' => "0, {$ limit }",
));
If (! Empty ($ updated_store ))
{
Foreach ($ updated_store as $ _ store_id => $ _ v)
{
$ Result [] = array (
'Url' => SITE_URL. '/index. php? App = store & id = '. $ _ store_id,
'Lastmod' => date ("Y-m-d", $ _ v ['add _ time']),
'Changefreq' => 'daily ',
'Priority '=> '1 ',
);
}
}
/* Updated article */
$ Model_article = & m ('Article ');
$ Updated_article = $ model_article-> find (array (
'Fields' => 'Article _ id, add_time ',
'Condition' => "add_time >={ $ timeline} AND if_show = 1 ",
'Limit' => "0, {$ limit }",
));
If (! Empty ($ updated_article ))
{
Foreach ($ updated_article as $ _ article_id => $ _ v)
{
$ Result [] = array (
'Url' => SITE_URL. '/index. php? App = article & act = view & article_id = '. $ _ article_id,
'Lastmod' => date ("Y-m-d", $ _ v ['add _ time']),
'Changefreq' => 'daily ',
'Priority '=> '0. 8 ',
);
}
}
/* Updated item */
$ Model_goods = & m ('goods ');
$ Updated_goods = $ model_goods-> find (array (
'Fields' => 'goods _ id, last_update ',
'Condition' => "last_update >={ $ timeline} AND if_show = 1 AND closed = 0 ",
'Limit' => "0, {$ limit }",
));
If (! Empty ($ updated_goods ))
{
Foreach ($ updated_goods as $ _ goods_id => $ _ v)
{
$ Result [] = array (
'Url' => SITE_URL. '/index. php? App = goods & id = '. $ _ goods_id,
'Lastmod' => date ("Y-m-d", $ _ v ['last _ Update']),
'Changefreq' => 'daily ',
'Priority '=> '0. 8 ',
);
}
}
Return $ result;
}
/**
* Generate Google sitemap
*
* @ Author Garbin
* @ Param array $ items
* @ Return string
*/
Function _ build_google_sitemap ($ items)
{
$ Sitemap =" \ R \ n \ R \ n ";
$ Sitemap. =" \ R \ n ". Htmlentities (SITE_URL, ENT_QUOTES )." \ R \ n ". Date ('Y-m-d', gmtime ())." \ R \ n Always \ R \ n 1 \ R \ n ";
If (! Empty ($ items ))
{
Foreach ($ items as $ item)
{
$ Sitemap. = "\ r \ n \ R \ n ". Htmlentities ($ item ['url'], ENT_QUOTES )." \ R \ n {$ Item ['lastmod']} \ R \ n {$ Item ['changefreq']} \ R \ n {$ Item ['priority ']} \ R \ n ";
}
}
$ Sitemap. = "\ r \ n ";
Return $ sitemap;
}
/**
* Write the Google sitemap File
*
* @ Author Garbin
* @ Param string $ sitemap
* @ Return void
*/
Function _ write_google_sitemap ($ sitemap)
{
File_put_contents ($ this-> _ google_sitemmap_file, $ sitemap );
}
}
?>