Feed and sitemap generation Part II

Posted by Damodar Bashyal on March 14, 2009

 

This is part II of: Feed and sitemap generation Part I

I hope you were success in creating feed url and were very excited as i was.

I am working on this from long time as a part time after work and on weekends. I hope one day it will be in a condition when i'll be proud to release and be very excited for the contribution to the community. But for now i am thinking to pause uploading new files to google code but write few posts on new updates. This will help anyone to install new feature and understand what's going on.

Ok, thats too much talking, now Time for Sitemap generation. This is another community contribution on codeigniter that i used to create my sitemap. As it is not final but for now i tested this contribution like below. Rather than menu links, and other links, i'll be using last 50 post on my sitemap.

Contribution link on codeignitor: http://codeigniter.com/wiki/Google_Sitemaps/

STEP 1:

Download google_sitemap_pi.php and upload it to plugins directory.

STEP2: [frontend/controllers/tools/sitemap.php]
class Sitemap extends Controller
{
function Sitemap()
{
parent::Controller();
$this->load->helper(array('text'));
$this->load->plugin('google_sitemap'); //Load Plugin
$this->load->model('page_model');
}
function index()
{
$sitemap = new google_sitemap; //Create a new Sitemap Object
$posts = $this->page_model->getRecentPosts('50');
$item = new google_sitemap_item(site_url(), date("Y-m-d",time()), 'daily', '0.8' ); //Create a new Item
$sitemap->add_item($item);
foreach($posts->result() as $entry) {
$link = preg_replace('|[^a-z0-9]+|i','-',strtolower($entry->pages_title));
//remove last dashes if any
while(substr($link, -1) == '-') {
$link = substr($link, 0, -1);
}
$link = "page/{$entry->menu_id}/{$entry->pages_id}/$link";// . $this->config->item('url_suffix')base_url().
$item = new google_sitemap_item(site_url($link), date("Y-m-d",strtotime($entry->pages_date)), 'weekly', '0.8' ); //Create a new Item
$sitemap->add_item($item); //Append the item to the sitemap object
}
$sitemap->build("sitemap.xml"); //Build it...
//Let's compress it to gz
$data = implode("", file("sitemap.xml"));
$gzdata = gzencode($data, 9);
$fp = fopen("sitemap.xml.gz", "w");
fwrite($fp, $gzdata);
fclose($fp);
//Let's Ping google
$this->_pingGoogleSitemaps(base_url()."/sitemap.xml.gz");
}
function _pingGoogleSitemaps( $url_xml )
{
$status = 0;
$google = 'www.google.com';
if( $fp=@fsockopen($google, 80) )
{
$req = 'GET /webmasters/sitemaps/ping?sitemap=' .
urlencode( $url_xml ) . " HTTP/1.1\r\n" .
"Host: $google\r\n" .
"User-Agent: Mozilla/5.0 (compatible; " .
PHP_OS . ") PHP/" . PHP_VERSION . "\r\n" .
"Connection: Close\r\n\r\n";
fwrite( $fp, $req );
while( !feof($fp) )
{
if( @preg_match('~^HTTP/\d\.\d (\d+)~i', fgets($fp, 128), $m) )
{
$status = intval( $m[1] );
break;
}
}
fclose( $fp );
}
return( $status );
}
}
STEP 3:

Create two files sitemap.xml.gz and sitemap.xml and give write permissions to both.

STEP 4:

Now time to generate sitemap and notify google about new sitemap. To do this just call this file as yoursite/sitemap.html. It will create sitemap for you and notify google about the new sitemap just created for it come and get it :)

All Done!!!

Good Luck.

kiddo posted on - Thursday 19th of March 2009 02:52:13 PM

Good tutorial! Thank you! ;)

Manset Soft Technology posted on - Sunday 9th of August 2009 03:31:42 AM

is your script only for a blog.........

Damu posted on - Tuesday 3rd of November 2009 05:40:18 AM

You need to download CMS and view listed files to better understand the sitemap generation. You need to check page_model.php as well. This was created for codefight cms, so you may need to change code according to your requirement. sitemap for this site is generated using this code. So its fully tested and working. You can check sitemap link at the bottom of the page. Thanks for visiting.

Damu posted on - Friday 4th of September 2009 08:28:35 PM

You can modify and use for blog, cms or whatever you like according to your need.
 
not published on website


QR Code: Feed and sitemap generation Part II