Skip to content

Commit

Permalink
updated to version 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Roumen Damianoff committed Sep 16, 2012
1 parent 4c67fd4 commit be59bc2
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 36 deletions.
48 changes: 36 additions & 12 deletions libraries/Sitemap.php
@@ -1,42 +1,66 @@
<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');
/**
* Sitemap class for ci-sitemap library.
*
*
* @author Roumen Damianoff <roumen@dawebs.com>
* @version 1.1
* @link https://github.com/RoumenMe/ci-sitemap GitHub
* @version 1.2
* @link http://roumen.me/projects/ci-sitemap
* @license http://opensource.org/licenses/mit-license.php MIT License
*/

class Sitemap
{

public $records = array();
public $items = array();
public $title;
public $link;


/**
* Add new sitemap item to $records array
* Add new sitemap item to $items array
*
* @param string $loc
* @param string $lastmod
* @param string $priority
* @param string $freq
* @param string $title
*
* @return void
*/
public function add($loc, $lastmod = null, $priority = '0.50', $freq = 'monthly')
public function add($loc, $lastmod = null, $priority = '0.50', $freq = 'monthly', $title = null)
{
$this->records[] = array(
'loc' => $loc,
'lastmod' => $lastmod,
'priority' => $priority,
'freq'=> $freq
'freq' => $freq,
'title' => $title
);
}


/**
* Returns xml document with all sitemap items from $records array
* Returns document with all sitemap items from $items array
*
* @param string $format (options: xml, html, txt, ror-rss, ror-rdf)
*
* @return View
*/
public function render()
public function render($format = 'xml')
{
$CI =& get_instance();
$data['records'] = $this->records;

$CI->load->view('sitemap/view', $data);

if (empty($this->link)) $this->link = $CI->config->item('base_url');
if (empty($this->title)) $this->title = 'Sitemap for ' . $this->link;

$data['channel'] = array(
'title' => $this->title,
'link' => $this->link
);

$data['items'] = $this->items;

$CI->load->view('sitemap/'.$format, $data);
}

}
47 changes: 39 additions & 8 deletions readme.md
Expand Up @@ -6,7 +6,7 @@ A simple sitemap generator for CodeIgniter.

To install just copy/paste the content of ci-sitemap in your application folder.

## Example
## Example (xml)

```php
public function sitemap()
Expand All @@ -17,18 +17,49 @@ public function sitemap()
// create new instance
$sitemap = new Sitemap();

// add new pages (url, date, priority, freq)
$sitemap->add('http://mysite.tld/','2012-08-25T20:10:00+02:00','1.0','daily');
$sitemap->add('http://mysite.tld/page1','2012-08-26T22:30:00+02:00','0.6','monthly');
$sitemap->add('http://mysite.tld/page2','2012-08-26T23:45:00+02:00','0.9','weekly');
// add items to your sitemap (url, date, priority, freq)
$sitemap->add('http://mysite.tld/', '2012-08-25T20:10:00+02:00', '1.0', 'daily');
$sitemap->add('http://mysite.tld/page1', '2012-08-26T22:30:00+02:00', '0.6', 'monthly');
$sitemap->add('http://mysite.tld/page2', '2012-08-26T23:45:00+02:00', '0.9', 'weekly');

// add multiple items with loop
// add multiple items with a loop
foreach ($posts as $post)
{
$sitemap->add($post->slug, $post->date, $post->priority, $post->freq);
}

// show the sitemap
$sitemap->render();
// show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
$sitemap->render('xml');
}
```

## Example (ror-rdf)

```php
public function ror-sitemap()
{
// first load the library
$this->load->library('sitemap');

// create new instance
$sitemap = new Sitemap();

// set sitemap's title and url (only for html, ror-rss and ror-rdf)
$sitemap->title = 'ROR sitemap';
$sitemap->link = 'http://domain.tld';

// add items to your sitemap (url, date, sortOrder, updatePeriod, title)
$sitemap->add('http://mysite.tld/', '2012-08-25T20:10:00+02:00', '0', 'daily', 'Some Title');
$sitemap->add('http://mysite.tld/page1', '2012-08-26T22:30:00+02:00', '1', 'monthly', 'Some Title');
$sitemap->add('http://mysite.tld/page2', '2012-08-26T23:45:00+02:00', '2', 'weekly', 'Some Title');

// add multiple items with a loop
foreach ($posts as $post)
{
$sitemap->add($post->slug, $post->date, $post->priority, $post->freq);
}

// show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
$sitemap->render('ror-rdf');
}
```
18 changes: 18 additions & 0 deletions views/sitemap/html.php
@@ -0,0 +1,18 @@
<?php header("Content-type: text/html; charset=utf-8"); ?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $channel['title']; ?></title>
</head>
<body>
<h1><a href="<?php echo $channel['link']; ?>"><?php echo $channel['title']; ?></a></h1>
<ul>
<?php foreach($items as $item): ?>
<li>
<a href="<?php echo $item['loc']; ?>"><?php echo (empty($item['title'])) ? $item['loc'] : $item['title']; ?></a>
<small>(last updated: <?php echo date('Y-m-d\TH:i:sP', strtotime($item['lastmod'])) ?>)</small>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
19 changes: 19 additions & 0 deletions views/sitemap/ror-rdf.php
@@ -0,0 +1,19 @@
<?php header("Content-type: text/rdf+xml; charset=utf-8"); ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'."\n"; ?>
<rdf:RDF xmlns="http://rorweb.com/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<Resource rdf:about="sitemap">
<title><?php echo $channel['title']; ?></title>
<url><?php echo $channel['link']; ?></url>
<type>sitemap</type>
</Resource>
<?php foreach($items as $item): ?>
<Resource>
<url><?php echo $item['loc'] ?></url>
<title><?php echo $item['title'] ?></title>
<updated><?php echo date('Y-m-d\TH:i:sP', strtotime($item['lastmod'])) ?></updated>
<updatePeriod><?php echo $item['freq'] ?></updatePeriod>
<sortOrder><?php echo $item['priority'] ?></sortOrder>
<resourceOf rdf:resource="sitemap"/>
</Resource>
<?php endforeach; ?>
</rdf:RDF>
18 changes: 18 additions & 0 deletions views/sitemap/ror-rss.php
@@ -0,0 +1,18 @@
<?php header("Content-type: text/rss+xml; charset=utf-8"); ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'."\n"; ?>
<rss version="2.0" xmlns:ror="http://rorweb.com/0.1/" >
<channel>
<title><?php echo $channel['title']; ?></title>
<link><?php echo $channel['link']; ?></link>
<?php foreach($items as $item): ?>
<item>
<link><?php echo $item['loc'] ?></link>
<title><?php echo $item['title'] ?></title>
<ror:updated><?php echo date('Y-m-d\TH:i:sP', strtotime($item['lastmod'])) ?></ror:updated>
<ror:updatePeriod><?php echo $item['freq'] ?></ror:updatePeriod>
<ror:sortOrder><?php echo $item['priority'] ?></ror:sortOrder>
<ror:resourceOf>sitemap</ror:resourceOf>
</item>
<?php endforeach; ?>
</channel>
</rss>
3 changes: 3 additions & 0 deletions views/sitemap/txt.php
@@ -0,0 +1,3 @@
<?php
header("Content-type: text/plain");
foreach($items as $item) echo $item['loc']."\n";
16 changes: 0 additions & 16 deletions views/sitemap/view.php

This file was deleted.

16 changes: 16 additions & 0 deletions views/sitemap/xml.php
@@ -0,0 +1,16 @@
<?php header("Content-type: text/xml; charset=utf-8"); ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'."\n"; ?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<?php foreach($items as $item): ?>
<url>
<loc><?php echo $item['loc'] ?></loc>
<priority><?php echo $item['priority'] ?></priority>
<lastmod><?php echo date('Y-m-d\TH:i:sP', strtotime($item['lastmod'])) ?></lastmod>
<changefreq><?php echo $item['freq'] ?></changefreq>
</url>
<?php endforeach; ?>
</urlset>

0 comments on commit be59bc2

Please sign in to comment.