Skip to content

Commit

Permalink
Add all files
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Jun 16, 2016
1 parent bd367a8 commit 347130f
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 1 deletion.
32 changes: 31 additions & 1 deletion README.md
@@ -1 +1,31 @@
# SiteMap
Description
===========

Adds ability to create both HTML and XML versions of a sitemap on silverstripe sites.
BOth are based off the site tree, with the ability to elect (via a checkbox in the CMS) to exclude any page from the sitemaps.
Default HTML and XML sitemaps are created by running dev/build after installing this module.
Also generates an xml file (sitemap.xml) in root of site, updated on load of the xml sitemap in the sitetree.



Dependencies
============

* SilverStripe 3.1+


Features
============

* Adds HTML and XML versions of a sitemap based on sitetree
* Exclude any page from sitemaps with CMS chackbox (on Settings tab)
* Uses partial caching, refreshed when any page in the sitetree is updated
* Creates/updates an actual sitemap.xml file in the root of the site on load of xml sitemap page


Installation
============

1. Install module
2. Run /dev/build?flush=1
3. Pages can be excluded from sitemap using the checkbox in the Settings CMS tab (under Visibility)
8 changes: 8 additions & 0 deletions _config.php
@@ -0,0 +1,8 @@
<?php
define('SITEMAP_BASE', basename(dirname(__FILE__)));

// set cache lifetime to 3 hours
SS_Cache::set_cache_lifetime('sitemap', 60*60*3);

// Extensions
DataObject::add_extension('Page', 'SitemapPageExtension');
69 changes: 69 additions & 0 deletions code/HTMLSitemap.php
@@ -0,0 +1,69 @@
<?php

class HTMLSitemap extends Page {

private static $allowed_children = 'none';
private static $description = 'Adds an html sitemap generated from the site tree';
private static $icon = 'sitemap/images/sitemap.png';

private static $db = array(
);

private static $has_one = array(
);

private static $defaults = array(
'ShowInMenus' => 0
);

public function getCMSFields(){
$fields = parent::getCMSFields();
$fields->removeByName('Content');
return $fields;
}

/**
* Add default record to database
*
*/
public function requireDefaultRecords() {
parent::requireDefaultRecords();

// if html sitemap page does not exist
if($this->class == 'HTMLSitemap' && $this->config()->create_default_pages) {
if( !SiteTree::get_by_link('html-sitemap') ){
$HTMLSitemap = new HTMLSitemap();
$HTMLSitemap->Title = 'HTML Sitemap';
$HTMLSitemap->Content = '';
$HTMLSitemap->write();
$HTMLSitemap->publish('Stage', 'Live');
$HTMLSitemap->flushCache();
DB::alteration_message('Sitemap HTML page created', 'created');
}

}

/*
// schema migration
// @todo Move to migration task once infrastructure is implemented
if($this->class == 'SiteTree') {
$conn = DB::getConn();
// only execute command if fields haven't been renamed to _obsolete_<fieldname> already by the task
if(array_key_exists('Viewers', $conn->fieldList('SiteTree'))) {
$task = new UpgradeSiteTreePermissionSchemaTask();
$task->run(new SS_HTTPRequest('GET','/'));
}
}*/
}


}

class HTMLSitemap_Controller extends Page_Controller {

public function init() {
parent::init();
Requirements::css(SITEMAP_BASE . '/css/sitemap.css');
}

}
91 changes: 91 additions & 0 deletions code/XMLSitemap.php
@@ -0,0 +1,91 @@
<?php

class XMLSitemap extends Page{

private static $allowed_children = 'none';
private static $description = 'Adds an XML sitemap generated from the site tree';
private static $icon = 'sitemap/images/sitemap.png';

private static $db = array(
);

private static $has_one = array(
);

private static $defaults = array(
'ShowInMenus' => 0
);

public function getCMSFields(){
$fields = parent::getCMSFields();
$fields->removeByName('Content');
$fields->removeByName('Metadata');
return $fields;
}

/**
* Add default record to database
*
*/
public function requireDefaultRecords() {
parent::requireDefaultRecords();

// if xml sitemap page does not exist
if($this->class == 'XMLSitemap' && $this->config()->create_default_pages) {
if( !SiteTree::get_by_link('sitemap') ){
$XMLSitemap = new XMLSitemap();
$XMLSitemap->Title = 'XML Sitemap';
$XMLSitemap->Content = '';
$XMLSitemap->URLSegment = 'sitemap';
$XMLSitemap->write();
$XMLSitemap->publish('Stage', 'Live');
$XMLSitemap->flushCache();
DB::alteration_message('Sitemap XML page created', 'created');
}

}

}


}

class XMLSitemap_Controller extends Page_Controller {

public function init() {
parent::init();

// update xml file when url is visited
$this->UpdateXMLFile();
}

/**
* Update xml file when this page is viewed
**/
function UpdateXMLFile(){

// get the value of the XMLSitemap page
$xml = $this->GetXMLValue();

// get link to xml file to be created/updated in site root
$file = BASE_PATH . '/sitemap.xml';
$fp = fopen($file, "w");
// prepend xml tag to result (gets excluded from curl)
$output = '<?xml version="1.0" encoding="UTF-8"?>';
$output .= $xml;

// write xml file
fwrite($fp, $output);


}

/**
* Get the value of this page
* Render with template
**/
function GetXMLValue(){
return $this->renderWith('XMLSitemap')->Value;
}

}
13 changes: 13 additions & 0 deletions code/extensions/SitemapPageExtension.php
@@ -0,0 +1,13 @@
<?php

class SitemapPageExtension extends DataExtension {

static $db = array(
'ExcludeFromSitemap' => 'Boolean'
);

public function updateSettingsFields(FieldList $fields) {
$fields->addFieldToTab("Root.Settings", new CheckBoxField('ExcludeFromSitemap', 'Exclude from sitemap?'), 'ShowInSearch');
}

}
11 changes: 11 additions & 0 deletions css/sitemap.css
@@ -0,0 +1,11 @@
.sitemap ul {
list-style: disc;
}

.sitemap ul li {
font-size: 16px;
}

.sitemap ul li a:hover {
text-decoration: underline;
}
Binary file added images/sitemap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions templates/Includes/SitemapNextLevel.ss
@@ -0,0 +1,12 @@
<% if Children %>
<ul>
<% loop Children %>
<% if not ExcludeFromSitemap %>
<li>
<a href="$Link" title="{$Title}">$MenuTitle.XML</a>
<% include SitemapNextLevel %>
</li>
<% end_if %>
<% end_loop %>
</ul>
<% end_if %>
11 changes: 11 additions & 0 deletions templates/Includes/XMLSitemapNextLevel.ss
@@ -0,0 +1,11 @@
<% loop Children %>
<% if not ExcludeFromSitemap %>
<url>
<loc>$AbsoluteLink</loc>
<lastmod>$LastEdited</lastmod>
</url>
<% if Children %>
<% include XMLSitemapNextLevel %>
<% end_if %>
<% end_if %>
<% end_loop %>
34 changes: 34 additions & 0 deletions templates/Layout/HTMLSitemap.ss
@@ -0,0 +1,34 @@
<div class="main-content sitemap">

<% cached 'sitemap', $List('SiteTree').max('LastEdited'), $List('SiteTree').count() %>

<% if Menu(1) %>
<%-- level 1 --%>
<ul>
<% loop Menu(1) %>
<% if not ExcludeFromSitemap %>
<li>
<a href="$Link" title="{$Title}">$MenuTitle.XML</a>
<% if Children %>
<% include SitemapNextLevel %>
<% end_if %>
</li>
<% end_if %>
<% end_loop %>
</ul>
<% else %>

<p>There are no pages in this site yet.</p>
$Content
$Form

<% end_if %>

<% end_cached %>

<div class="clear"></div>

</div>



22 changes: 22 additions & 0 deletions templates/XMLSitemap.ss
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<% cached 'sitemap', $List('SiteTree').max('LastEdited'), $List('SiteTree').count() %>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<% if Menu(1) %>
<% loop Menu(1) %>
<% if not ExcludeFromSitemap %>
<url>
<loc>$AbsoluteLink</loc>
<lastmod>$LastEdited</lastmod>
</url>
<% if Children %>
<% include XMLSitemapNextLevel %>
<% end_if %>
<% end_if %>
<% end_loop %>
<% end_if %>

</urlset>

<% end_cached %>

0 comments on commit 347130f

Please sign in to comment.