Feed and sitemap generation Part II
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/sitemap.php]
CODE:
<?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 gz31-$data = implode(““, file(“sitemap.xml“));32-$gzdata = gzencode($data, 9);33-$fp = fopen(“sitemap.xml.gz“, “w“);34-fwrite($fp, $gzdata);35-fclose($fp);36-//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.

Good tutorial! Thank you! ;)
18/03/09 03:52:13|kiddo
is your script only for a blog.........
08/08/09 05:31:42|Manset Soft Technology
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.
02/11/09 04:40:18|Damu
You can modify and use for blog, cms or whatever you like according to your need.
03/09/09 10:28:35|Damu