Compare commits

...

18 Commits
3.0.1 ... main

Author SHA1 Message Date
Will Rossiter 794778ae20
Merge pull request #198 from somardigital/main
BUG - Pass empty string instead of null when no image exists
2024-04-08 16:04:08 +12:00
Sukhwinder Singh 05e6963f04 BUG - Pass empty string instead of null when no image exists 2024-04-08 15:08:56 +12:00
Will Rossiter 3f75c06a77
Merge pull request #197 from silverstripeltd/feature/removal-of-ping
Removal of ping for google site maps
2024-03-26 16:04:23 +13:00
jareddreyerss a13d3dd9de Removal of ping from google site maps
See https://developers.google.com/search/blog/2023/06/sitemaps-lastmod-ping for context.

Accordingly removed all references to notification config and/or ping.
2024-03-26 14:20:14 +13:00
Will Rossiter 59acd419ef
Merge pull request #194 from sunnysideup/patch-1 2024-02-22 07:24:41 +13:00
Sunny Side Up aeeb960566
DOC: fixing yml annotation 2024-02-21 20:50:47 +13:00
Will Rossiter c7f260bb33
Merge pull request #193 from BrookeNZ/main 2024-01-03 07:13:52 +13:00
Brooke Lord c2f5162e97 Additional styling tweaks 2023-12-29 20:29:05 +13:00
Brooke Lord 59e56c3b69 Update xml-sitemap.ss 2023-12-29 20:20:32 +13:00
Brooke Lord d9da059a83 Update Silverstripe CMS wording 2023-12-29 17:13:43 +13:00
Brooke Lord 54ddc6ccf7 UI refresh 2023-12-29 17:13:43 +13:00
Will Rossiter cbccf88a62
chore: lint 2023-10-29 13:53:26 +13:00
Will Rossiter 8a7c8aa9cc
feat: implement Sitemapable (#181) 2023-10-29 13:30:08 +13:00
Will Rossiter def6be347c
fix: cast variables (#192) 2023-10-28 07:59:55 +13:00
Will Rossiter 523d196d12
Merge pull request #191 from satrun77/pulls/fix-trailing-slash
Ensure trailing slash removed from AbsoluteLink
2023-08-29 16:36:46 +12:00
Mohamed Alsharaf f064330acd Ensure trailing slash removed from AbsoluteLink
This is to resolve duplicated trailing slash when add_trailing_slash
property is set to true in YML config.

Also, relates to #188
2023-08-29 15:57:24 +12:00
Will Rossiter f1bddb626d
Merge pull request #190 from visualmetricsio/fix/absolute-link-scope
Fix absolute link scope
2023-08-29 06:16:25 +12:00
Luke Fromhold 77a366871e Fix absolute link scope 2023-08-28 20:33:51 +10:00
21 changed files with 444 additions and 441 deletions

View File

@ -6,13 +6,13 @@
## Maintainer Contact
* Will Rossiter (Nickname: wrossiter, willr) <will@fullscreen.io>
- Will Rossiter (Nickname: wrossiter, willr) <will@fullscreen.io>
## Installation
> composer require "wilr/silverstripe-googlesitemaps"
If you're using Silverstripe 5 then version 3 or `dev-main` will work.
If you're using Silverstripe 5 then version `3` or `dev-main` will work.
For Silverstripe 4 use the `2.x` branch line.
@ -34,4 +34,4 @@ See docs/en for more information about configuring the module.
## Troubleshooting
* Flush this route to ensure the changes take effect (e.g http://yoursite.com/sitemap.xml?flush=1)
- Flush this route to ensure the changes take effect (e.g http://yoursite.com/sitemap.xml?flush=1)

10
_config.php Normal file
View File

@ -0,0 +1,10 @@
<?php
use SilverStripe\Core\ClassInfo;
use Wilr\GoogleSitemaps\GoogleSitemap;
if (0 === strpos(ltrim($_SERVER['REQUEST_URI'], '/'), 'sitemap')) {
foreach (ClassInfo::implementorsOf(Sitemapable::class) as $className) {
GoogleSitemap::register_dataobject($className);
}
}

View File

@ -4,7 +4,6 @@ Name: googlesitemaps
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
google_notification_enabled: false
use_show_in_search: true
---
Only:

View File

@ -1,144 +1,145 @@
*{
margin: 0;
padding: 0;
:root {
--color-black: #292B2E;
--color-blue-bright: #005AE1;
--color-blue-dark: #003E94;
--color-blue-light: #E6EDF4;
--color-grey-dark: #595D64;
--color-grey-light: #f7f8f8;
--color-grey-medium: #DCDEE0;
--color-white: #fff;
}
* {
margin: 0;
padding: 0;
}
html {
font-size: 10px;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
}
body, table {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 1.4rem;
font-weight: 400;
line-height: 1.4;
}
body {
font-family: Helvetica, Arial, sans-serif;
font-size: 14px;
color: #545353;
color: #005A92;
background: #B0BEC7;
padding: 20px;
}
#content {
margin: 0 auto;
max-width: 1200px;
background: #fff;
padding: 20px 30px;
-webkit-box-shadow: 2px 2px 5px 1px rgba(0, 0, 0, 0.2);
box-shadow: 2px 2px 5px 1px rgba(0, 0, 0, 0.2);
-webkit-border-radius: 5px;
border-radius: 5px;
}
h1{
font-size: 20px;
line-height: 24px;
font-weight: bold;
color: #1556B2;
color: #005A92;
/* text-shadow: 1px 1px 1px rgba(0,0,0,0.7);
filter: dropshadow(color=#000, offx=2, offy=2);*/
padding-left: 31px;
background: url("../images/logo_small.png") transparent left top no-repeat scroll;
margin: 0 0 20px 0;
display: inline-block;
}
h1 a{
font: inherit;
color: inherit;
text-decoration: none;
}
h1 .ss_link{
visibility: hidden;
font-size: 9px;
display: block;
text-align: right;
margin-top: -5px;
}
h1:hover .ss_link{
visibility: visible;
text-decoration: underline;
}
.expl {
margin: 10px 3px;
line-height: 1.3em;
}
.expl a {
color: #1556B2;
font-weight: bold;
text-decoration: none;
}
.expl a:hover{
text-decoration: underline;
background: var(--color-white);
color: var(--color-grey-dark);
}
table.tablesorter {
background-color: #CDCDCD;
margin:20px 0pt 15px;
font-size: 8pt;
width: 100%;
text-align: left;
border: none;
border-collapse: collapse;
border-bottom: 1px solid #005A92;
}
table.tablesorter thead tr th, table.tablesorter tfoot tr th {
/*background-color: #e6EEEE;*/
background-color: #F5FAFA;
font-size: 8pt;
padding: 4px 20px 4px 10px;
}
table.tablesorter thead tr .headerSortDown, table.tablesorter thead tr .headerSortUp {
background-color: #B0BEC7;
background-color: #e6EEEE;
table {
border-collapse: collapse;
color: var(--color-black);
text-align: left;
width: 100%;
}
table.tablesorter thead tr .header {
background-image: url("../images/bg.gif");
background-repeat: no-repeat;
background-position: center right;
cursor: pointer;
border-bottom: 1px solid #005A92;
}
table.tablesorter thead tr .headerSortUp {
background-image: url("../images/asc.gif");
}
table.tablesorter thead tr .headerSortDown {
background-image: url("../images/desc.gif");
a {
color: inherit;
text-decoration: none;
}
table.tablesorter tbody td {
color: #005A92;
padding: 4px;
vertical-align: top;
}
table.tablesorter tbody tr{
background-color: #FFF;
}
table.tablesorter tbody tr.odd {
background-color:#EFF2F3;
}
table.tablesorter tbody tr:hover {
background-color: #D8E1E8;
}
table.tablesorter tbody a{
color: #444;color: #005A92;
text-decoration: none;
}
table.tablesorter tbody a:hover{
text-decoration: underline;
}
table.tablesorter tbody tr:hover td,
table.tablesorter tbody tr:hover a{
color: #000;
}
table.imagestable,
table.imagestable a {
font-size: 8pt;
}
table.imagestable {
margin-top: 15px;
}
table.imagestable tr.odd {
background: transparent;
}
table.imagestable tr.even {
background: transparent;
.content {
margin: 0 auto;
max-width: 122rem;
padding: 2rem;
}
#Footer{
margin: 50px 0 10px;
text-align: right;
font-size: 0.8em;
.content__title {
align-items: center;
color: var(--color-black);
display: inline-flex;
font-size: 2rem;
font-weight: 600;
margin: 0 0 3rem;
}
.content__title::before {
content: "";
background: url("../images/cms_logo.svg") left center no-repeat;
border-right: 0.1rem solid var(--color-grey-medium);
display: inline-block;
height: 3.2rem;
margin-right: 1.5rem;
padding-right: 1.5rem;
width: 3.2rem;
}
.content__text {
margin: 1rem 0;
}
.content__text a {
color: var(--color-blue-bright);
text-decoration: underline;
}
.content__text a:hover {
color: var(--color-blue-dark);
text-decoration: none;
}
.table-wrapper {
border-top: 0.1rem solid var(--color-grey-medium);
margin: 3rem 0 5rem;
overflow-x: auto;
}
.table__cell {
padding: 1rem;
}
.table__cell--w-10 {
width: 10%;
}
.table__cell--w-15 {
width: 15%;
}
.table__cell--w-65 {
width: 65%;
}
.table__cell--w-85 {
width: 85%;
}
.table tbody tr:nth-child(odd) td {
background-color: var(--color-grey-light);
}
.table tbody tr:hover td {
background-color: var(--color-blue-light);
}
.table a:hover {
text-decoration: underline;
}
.image-table__cell {
padding: 1rem 1rem 0 0;
}
.image-table__cell--image {
width: 6rem;
}
.image-table__cell--text {
width: calc(100% - 6rem);
}
.image-table__image {
height: 4rem;
object-fit: cover;
width: 6rem;
}
.image-table__title {
display: block;
}

View File

@ -1,23 +1,23 @@
# Google Sitemaps Module
SilverStripe provides support for the Google Sitemaps XML system, enabling
Silverstripe CMS provides support for the Google Sitemaps XML system, enabling
Google and other search engines to see all pages on your site. This helps
your SilverStripe website rank well in search engines, and to encourage the
your Silverstripe CMS website rank well in search engines, and to encourage the
information on your site to be discovered by Google quickly.
Therefore, all Silverstripe websites contain a special controller which can be
visited: http://yoursite.com/sitemap.xml. This is not a file directly, but
Therefore, all Silverstripe CMS websites contain a special controller which can
be visited: http://yoursite.com/sitemap.xml. This is not a file directly, but
rather a custom route which points to the GoogleSitemap controller.
See http://en.wikipedia.org/wiki/Sitemaps for info on the Google Sitemap
format.
Whenever you publish a new or republish an existing page, SilverStripe can
Whenever you publish a new or republish an existing page, Silverstripe CMS can
automatically inform Google of the change, encouraging a Google to take notice.
If you install the SilverStripe Google Analytics module, you can see if Google
If you install the Silverstripe CMS Google Analytics module, you can see if Google
has updated your page as a result.
By default, SilverStripe informs Google that the importance of a page depends
By default, Silverstripe CMS informs Google that the importance of a page depends
on its position of in the sitemap. "Top level" pages are most important, and
the deeper a page is nested, the less important it is. (For each level,
Importance drops from 1.0, to 0.9, to 0.8, and so on, until 0.1 is reached).
@ -27,45 +27,32 @@ manually, including requesting to have the page excluded from the sitemap.
## Configuration
Most module configuration is done via the SilverStripe Config API. Create a new
config file `mysite/_config/googlesitemaps.yml` with the following outline:
Most module configuration is done via the Silverstripe CMS Config API. Create a
new config file `mysite/_config/googlesitemaps.yml` with the following outline:
---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
google_notification_enabled: false
use_show_in_search: true
```yml
---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
use_show_in_search: true
```
You can now alter any of those properties to set your needs. A popular option
is to turn on automatic pinging so that Google is notified of any updates to
your page. You can set this in the file we created in the last paragraph by
editing the `google_notification_enabled` option to true
You can now alter any of those properties to set your needs.
---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
google_notification_enabled: true
use_show_in_search: true
### Bing Ping Support
To ping Bing whenever your sitemap is updated, set `bing_notification_enabled`
---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
bing_notification_enabled: true
```yml
---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
use_show_in_search: true
```
### Including DataObjects
@ -76,41 +63,40 @@ database as DataObject subclasses.
To include a DataObject instance in the Sitemap it requires that your subclass
defines two functions:
* AbsoluteLink() function which returns the URL for this DataObject
* canView() function which returns a boolean value.
- AbsoluteLink() function which returns the URL for this DataObject
- canView() function which returns a boolean value.
The following is a barebones example of a DataObject called 'MyDataObject'. It
assumes that you have a controller called 'MyController' which has a show method
to show the DataObject by its ID.
<?php
<?php
use SilverStripe\ORM\DataObject;
use SilverStripe\Control\Director;
class MyDataObject extends DataObject {
class MyDataObject extends DataObject {
function canView($member = null) {
return true;
}
function canView($member = null) {
return true;
}
function AbsoluteLink() {
return Director::absoluteURL($this->Link());
}
function Link() {
return 'MyController/show/'. $this->ID;
}
}
function AbsoluteLink() {
return Director::absoluteURL($this->Link());
}
function Link() {
return 'MyController/show/'. $this->ID;
}
}
After those methods have been defined on your DataObject you now need to tell
the Google Sitemaps module that it should be listed in the sitemap.xml file. To
do that, include the following in your _config.php file.
do that, include the following in your \_config.php file.
use Wilr\GoogleSitemaps\GoogleSitemap;
GoogleSitemap::register_dataobject('MyDataObject');
GoogleSitemap::register_dataobject('MyDataObject');
If you need to change the frequency of the indexing, you can pass the change
frequency (daily, weekly, monthly) as a second parameter to register_dataobject(), So
@ -118,7 +104,7 @@ instead of the previous code you would write:
use Wilr\GoogleSitemaps\GoogleSitemap;
GoogleSitemap::register_dataobject('MyDataObject', 'daily');
GoogleSitemap::register_dataobject('MyDataObject', 'daily');
See the following blog post for more information:
@ -126,15 +112,33 @@ http://www.silvercart.org/blog/dataobjects-and-googlesitemaps/
### Including custom routes
Occasionally you may have a need to include custom url's in your sitemap for
Occasionally you may have a need to include custom URLs in your sitemap for
your Controllers and other pages which don't exist in the database. To update
the sitemap to include those links call register_routes() with your array of
urls to include.
URLs to include.
use Wilr\GoogleSitemaps\GoogleSitemap;
GoogleSitemap::register_routes(array(
'/my-custom-controller/',
'/Security/',
'/Security/login/'
));
GoogleSitemap::register_routes(array(
'/my-custom-controller/',
'/Security/',
'/Security/login/'
));
### Sitemapable
For automatic registration of a DataObject subclass, implement the `Sitemapable`
extension.
```
<?php
class MyDataObject extends DataObject implements Sitemapable
{
public function AbsoluteLink()
{
// ..
}
}
```

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 B

4
images/cms_logo.svg Normal file
View File

@ -0,0 +1,4 @@
<svg width="32" height="19" viewBox="0 0 32 19" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M15.832 1.85439L9.09495 6.56978C8.16307 7.22084 7.93658 8.50206 8.58764 9.43478C9.2387 10.3667 10.5199 10.5931 11.4526 9.94209L18.1897 5.2267C20.9787 3.27352 24.8307 3.95216 26.7772 6.74111C28.7304 9.53005 28.0518 13.3821 25.2628 15.3286L23.145 16.8087C24.8993 19.7899 28.8675 18.5354 30.6702 15.3353C32.5549 11.9905 32.4863 7.72146 30.1428 4.37673C26.8926 -0.263432 20.4714 -1.39422 15.832 1.85439Z" fill="#005AE1"/>
<path d="M16.1671 16.6776L22.9042 11.9622C23.836 11.3112 24.0625 10.03 23.4115 9.09724C22.7604 8.16536 21.4792 7.93887 20.5465 8.58993L13.8094 13.3053C11.0204 15.2585 7.1684 14.5799 5.22191 11.7909C3.26873 9.00196 3.94737 5.14992 6.73631 3.20343L8.85414 1.72329C7.1007 -1.25705 3.1325 -0.00256691 1.32975 3.19758C-0.554895 6.54231 -0.486362 10.8114 1.85712 14.1561C5.10574 18.7955 11.5269 19.9338 16.1671 16.6785V16.6776Z" fill="#005AE1"/>
</svg>

After

Width:  |  Height:  |  Size: 970 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>
<file>src</file>
<file>tests</file>
<!-- base rules are PSR-2 -->
<rule ref="PSR2" >
<rule ref="PSR2">
<!-- Current exclusions -->
<exclude name="PSR1.Methods.CamelCapsMethodName" />
</rule>

View File

@ -75,8 +75,7 @@ class GoogleSitemapController extends Controller
}
}
if (
GoogleSitemap::enabled()
if (GoogleSitemap::enabled()
&& $class
&& ($page > 0)
&& ($class == SiteTree::class || $class == 'GoogleSitemapRoute' || GoogleSitemap::is_registered($class))
@ -133,6 +132,6 @@ class GoogleSitemapController extends Controller
public function AbsoluteLink($action = null)
{
return Controller::join_links(Director::absoluteBaseURL(), 'sitemap.xml', $action);
return rtrim(Controller::join_links(Director::absoluteBaseURL(), 'sitemap.xml', $action), '/');
}
}

View File

@ -33,7 +33,7 @@ class GoogleSitemapExtension extends DataExtension
}
}
$objHttp = parse_url($this->owner->AbsoluteLink(), PHP_URL_HOST);
$objHttp = parse_url($this->owner->AbsoluteLink() ?? '', PHP_URL_HOST);
if ($objHttp != $hostHttp) {
$can = false;
@ -73,24 +73,6 @@ class GoogleSitemapExtension extends DataExtension
return $can;
}
/**
* @return void
*/
public function onAfterPublish()
{
GoogleSitemap::ping();
}
/**
* @return void
*/
public function onAfterUnpublish()
{
GoogleSitemap::ping();
}
/**
* The default value of the priority field depends on the depth of the page in
* the site tree, so it must be calculated dynamically.

View File

@ -29,17 +29,8 @@ use ReflectionException;
* The GoogleSitemap handle requests to 'sitemap.xml' the other two classes are
* used to render the sitemap.
*
* You can notify ("ping") Google about a changed sitemap automatically whenever
* a new page is published or unpublished.
*
* By default, Google is not notified, and will pick up your new sitemap
* whenever the GoogleBot visits your website.
*
* To Enable notification of Google after every publish set google_notification_enabled
* to true in the googlesitemaps.yml config file.
*
* This file is usually located in the _config folder of your project folder.
* e.g mysite/_config/googlesitemaps.yml
* The config file is usually located in the _config folder of your project folder.
* e.g. app/_config/googlesitemaps.yml
*
* <example>
* ---
@ -49,7 +40,6 @@ use ReflectionException;
* Wilr\GoogleSitemaps\GoogleSitemap:
* enabled: true
* objects_per_sitemap: 1000
* google_notification_enabled: true
* use_show_in_search: true
* </example>
*
@ -84,16 +74,6 @@ class GoogleSitemap
*/
private static $exclude_redirector_pages = true;
/**
* @config
*
* @var array
*/
private static $search_indexes = [
'google' => 'http://www.google.com/webmasters/sitemaps/ping?sitemap=',
'bing' => 'http://www.bing.com/ping?sitemap=',
];
/**
* Decorates the given DataObject with {@link GoogleSitemapDecorator}
* and pushes the class name to the registered DataObjects.
@ -236,7 +216,8 @@ class GoogleSitemap
*/
public function getItems($class, $page = 1)
{
//normalise the class name
$page = (int) $page;
try {
$reflectionClass = new ReflectionClass($class);
$class = $reflectionClass->getName();
@ -246,9 +227,9 @@ class GoogleSitemap
}
$output = new ArrayList();
$count = Config::inst()->get(__CLASS__, 'objects_per_sitemap');
$filter = Config::inst()->get(__CLASS__, 'use_show_in_search');
$redirector = Config::inst()->get(__CLASS__, 'exclude_redirector_pages');
$count = (int) Config::inst()->get(__CLASS__, 'objects_per_sitemap');
$filter = Config::inst()->get(__CLASS__, 'use_show_in_search');
$redirector = Config::inst()->get(__CLASS__, 'exclude_redirector_pages');
// todo migrate to extension hook or DI point for other modules to
// modify state filters
@ -416,7 +397,7 @@ class GoogleSitemap
->limit($countPerFile, ($i - 1) * $countPerFile)
->last();
$lastModified = ($sliced) ? date('Y-m-d', strtotime($sliced->LastEdited)): date('Y-m-d');
$lastModified = ($sliced) ? date('Y-m-d', strtotime($sliced->LastEdited)) : date('Y-m-d');
$sitemaps->push(new ArrayData(array(
'ClassName' => $this->sanitiseClassName($class),
@ -452,41 +433,6 @@ class GoogleSitemap
return static::inst()->getSitemaps();
}
/**
* Notifies search indexes about changes to your sitemap. This behavior is disabled
* by default, to enable, read the documentation provided in the docs folder.
*
* After notifications have been enabled, every publish / unpublish of a page.
* will notify enabled search indexes of the update.
*
* If the site is in development mode no ping will be sent.
*
* @return boolean
*/
public static function ping()
{
if (!self::enabled() || Director::isDev()) {
return false;
}
$location = urlencode(Controller::join_links(
Director::absoluteBaseURL(),
'sitemap.xml'
));
$response = true;
foreach (self::config()->search_indexes as $name => $url) {
$configName = $name . '_notification_enabled';
if (self::config()->$configName) {
$response = $response && file_get_contents($url . $location);
}
}
return $response;
}
/**
* Is GoogleSitemap enabled?
*

14
src/Sitemapable.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace Wilr\GoogleSitemaps;
interface Sitemapable
{
/**
* Return the absolute URL for this object
*
* @return string
*/
public function AbsoluteLink();
}

View File

@ -2,7 +2,7 @@
<?xml-stylesheet type='text/xsl' href='{$AbsoluteLink('styleSheetIndex')}'?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><% loop Sitemaps %>
<sitemap>
<loc>{$AbsoluteLink('sitemap')}/{$ClassName}/{$Page.xml}</loc>
<loc>{$Up.AbsoluteLink('sitemap')}/{$ClassName}/{$Page.xml}</loc>
<% if $LastModified %><lastmod>{$LastModified}</lastmod><% end_if %>
</sitemap><% end_loop %>
</sitemapindex>

View File

@ -9,82 +9,80 @@
<link rel="stylesheet" href="$resourceURL(wilr/silverstripe-googlesitemaps:css/style.css)" />
</head>
<body>
<div id="content">
<h1>
<a href="http://silverstripe.org" target="_blank">XML Sitemap
<span class="ss_link">&#8594; silverstripe.org</span>
</a>
<div class="content">
<h1 class="content__title">
<a href="https://www.silverstripe.org" target="_blank" rel="noopener noreferrer">XML Sitemap</a>
</h1>
<p class="expl">
<p class="content__text">
This sitemap contains <xsl:value-of select="count(sitemap:urlset/sitemap:url)"/> URLs.
</p>
<table id="sitemap" cellpadding="3" class="tablesorter">
<thead>
<tr>
<th width="76%">URL</th>
<th width="7%">Priority</th>
<th width="7%">Change Freq.</th>
<th width="10%">Last Change</th>
</tr>
</thead>
<tbody>
<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:for-each select="sitemap:urlset/sitemap:url">
<div class="table-wrapper">
<table id="sitemap" class="table">
<thead>
<tr>
<td>
<xsl:variable name="itemURL">
<xsl:value-of select="sitemap:loc"/>
</xsl:variable>
<xsl:variable name="imagesCount" select="count(image:image)"/>
<a href="{\$itemURL}">
<xsl:value-of select="sitemap:loc"/>
</a>
<xsl:if test="\$imagesCount &gt; 0">
<table class="imagestable" cellpadding="0" cellspacing="0">
<tr>
<th>Images</th>
</tr>
<xsl:for-each select="image:image">
<xsl:variable name="imageURL">
<xsl:value-of select="image:loc"/>
</xsl:variable>
<tr>
<td>
<img src="{\$imageURL}" width="40px"/>
</td>
<td>
<xsl:value-of select="image:title"/><br/>
<a href="{\$imageURL}">
<xsl:value-of select="image:loc"/>
</a>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:if>
</td>
<td>
<xsl:value-of select="concat(sitemap:priority*100,'%')"/>
</td>
<td>
<xsl:value-of select="concat(translate(substring(sitemap:changefreq, 1, 1),concat(\$lower, \$upper),concat(\$upper, \$lower)),substring(sitemap:changefreq, 2))"/>
</td>
<td>
<xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/>
</td>
<th class="table__cell table__cell--w-65">URL</th>
<th class="table__cell table__cell--w-10">Priority</th>
<th class="table__cell table__cell--w-10">Change Freq.</th>
<th class="table__cell table__cell--w-15">Last Change</th>
</tr>
</xsl:for-each>
</tbody>
</table>
<p id="Footer" class="expl">Generated by the SilverStripe
<a href="https://github.com/wilr/silverstripe-googlesitemaps" target="_blank" title="SilverStripe Google Sitemaps module on Github">Google Sitemaps Module</a>
<br />More information about XML sitemaps on <a href="http://sitemaps.org" target="_blank">sitemaps.org</a>.
</thead>
<tbody>
<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:for-each select="sitemap:urlset/sitemap:url">
<tr>
<td class="table__cell">
<xsl:variable name="itemURL">
<xsl:value-of select="sitemap:loc"/>
</xsl:variable>
<xsl:variable name="imagesCount" select="count(image:image)"/>
<a href="{\$itemURL}">
<xsl:value-of select="sitemap:loc"/>
</a>
<xsl:if test="\$imagesCount &gt; 0">
<table class="image-table">
<tr>
<th class="image-table__cell">Images</th>
</tr>
<xsl:for-each select="image:image">
<xsl:variable name="imageURL">
<xsl:value-of select="image:loc"/>
</xsl:variable>
<tr>
<td class="image-table__cell image-table__cell--image">
<img class="image-table__image" src="{\$imageURL}" width="60" height="40" />
</td>
<td class="image-table__cell image-table__cell--text">
<span class="image-table__title"><xsl:value-of select="image:title"/></span>
<a href="{\$imageURL}">
<xsl:value-of select="image:loc"/>
</a>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:if>
</td>
<td class="table__cell">
<xsl:value-of select="concat(sitemap:priority*100,'%')"/>
</td>
<td class="table__cell">
<xsl:value-of select="concat(translate(substring(sitemap:changefreq, 1, 1),concat(\$lower, \$upper),concat(\$upper, \$lower)),substring(sitemap:changefreq, 2))"/>
</td>
<td class="table__cell">
<xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</div>
<p class="content__text">
Generated by the Silverstripe CMS
<a href="https://github.com/wilr/silverstripe-googlesitemaps" target="_blank" rel="noopener noreferrer" title="Silverstripe CMS Google Sitemaps module on Github">Google Sitemaps Module</a>
</p>
<p class="content__text">
More information about XML sitemaps on <a href="https://sitemaps.org" target="_blank" rel="noopener noreferrer">sitemaps.org</a>
</p>
</div>
</body>

View File

@ -9,46 +9,48 @@
<link rel="stylesheet" href="$resourceURL(wilr/silverstripe-googlesitemaps:css/style.css)" />
</head>
<body>
<div id="content">
<h1>
<a href="http://silverstripe.org" target="_blank">XML Sitemap
<span class="ss_link">&#8594; silverstripe.org</span>
</a>
<div class="content">
<h1 class="content__title">
<a href="https://www.silverstripe.org" target="_blank" rel="noopener noreferrer">XML Sitemap</a>
</h1>
<p class="expl">
<p class="content__text">
This sitemap consists of <xsl:value-of select="count(sitemap:sitemapindex/sitemap:sitemap)"/> part(s).
</p>
<table id="sitemapindex" cellpadding="3" class="tablesorter">
<thead>
<tr>
<th width="90%">URL</th>
<th width="10%">Last Change</th>
</tr>
</thead>
<tbody>
<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:for-each select="sitemap:sitemapindex/sitemap:sitemap">
<div class="table-wrapper">
<table id="sitemapindex" class="table">
<thead>
<tr>
<td>
<xsl:variable name="itemURL">
<xsl:value-of select="sitemap:loc"/>
</xsl:variable>
<a href="{\$itemURL}">
<xsl:value-of select="sitemap:loc"/>
</a>
</td>
<td>
<xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/>
</td>
<th class="table__cell table__cell--w-85">URL</th>
<th class="table__cell table__cell--w-15">Last Change</th>
</tr>
</xsl:for-each>
</tbody>
</table>
<p id="Footer" class="expl">Generated by the SilverStripe
<a href="https://github.com/wilr/silverstripe-googlesitemaps" target="_blank" title="SilverStripe Google Sitemaps module on Github">Google Sitemaps Module</a>
<br />More information about XML sitemaps on <a href="http://sitemaps.org" target="_blank">sitemaps.org</a>.
</thead>
<tbody>
<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:for-each select="sitemap:sitemapindex/sitemap:sitemap">
<tr>
<td class="table__cell">
<xsl:variable name="itemURL">
<xsl:value-of select="sitemap:loc"/>
</xsl:variable>
<a href="{\$itemURL}">
<xsl:value-of select="sitemap:loc"/>
</a>
</td>
<td class="table__cell">
<xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</div>
<p class="content__text">
Generated by the Silverstripe CMS
<a href="https://github.com/wilr/silverstripe-googlesitemaps" target="_blank" rel="noopener noreferrer" title="SilverStripe Google Sitemaps module on Github">Google Sitemaps Module</a>
</p>
<p class="content__text">
More information about XML sitemaps on <a href="https://sitemaps.org" target="_blank" rel="noopener noreferrer">sitemaps.org</a>
</p>
</div>
</body>

View File

@ -20,12 +20,22 @@ use Wilr\GoogleSitemaps\Tests\Model\UnviewableDataObject;
class GoogleSitemapTest extends FunctionalTest
{
protected static $fixture_file = [
'GoogleSitemapTest.yml',
'GoogleSitemapPageTest.yml',
'GoogleSitemapTest.yml'
];
protected $usesDatabase = true;
public static function get_fixture_file()
{
$files = [__DIR__ . '/GoogleSitemapTest.yml'];
if (class_exists('Page')) {
$files[] = __DIR__ . '/GoogleSitemapPageTest.yml';
}
return $files;
}
protected static $extra_dataobjects = [
TestDataObject::class,
OtherDataObject::class,
@ -55,13 +65,13 @@ class GoogleSitemapTest extends FunctionalTest
public function testCanIncludeInGoogleSitemap(): void
{
GoogleSitemap::register_dataobject(TestDataObject::class, '');
$unused = $this->objFromFixture(TestDataObject::class, 'UnindexedDataObject');
$this->assertFalse($unused->canIncludeInGoogleSitemap());
$used = $this->objFromFixture(TestDataObject::class, 'DataObjectTest2');
$this->assertTrue($used->canIncludeInGoogleSitemap());
$used->setPrivate();
$this->assertFalse($used->canIncludeInGoogleSitemap());
}
public function testIndexFileWithCustomRoute(): void
@ -71,37 +81,39 @@ class GoogleSitemapTest extends FunctionalTest
$response = $this->get('sitemap.xml');
$body = $response->getBody();
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/xml/' . __FUNCTION__ . '.xml', $body, 'A link to the custom routes exists');
$this->assertXmlStringEqualsXmlFile(
__DIR__ . '/xml/' . __FUNCTION__ . '.xml',
$body,
'A link to the custom routes exists'
);
}
public function testGetItems(): void
{
GoogleSitemap::register_dataobject(TestDataObject::class, '');
$google = new GoogleSitemap();
$items = GoogleSitemap::get_items(TestDataObject::class, 1);
$this->assertEquals(2, $items->count());
$items = $google->getItems(TestDataObject::class, 1);
$this->assertListEquals(array(
array("Priority" => "0.2"),
array("Priority" => "0.4")
), $items);
$this->assertEquals(3, $items->count());
GoogleSitemap::register_dataobject(OtherDataObject::class);
$this->assertEquals(1, GoogleSitemap::get_items(OtherDataObject::class, 1)->count());
$this->assertEquals(1, $google->getItems(OtherDataObject::class, 1)->count());
GoogleSitemap::register_dataobject(UnviewableDataObject::class);
$this->assertEquals(0, GoogleSitemap::get_items(UnviewableDataObject::class, 1)->count());
$this->assertEquals(0, $google->getItems(UnviewableDataObject::class, 1)->count());
}
public function testGetItemsWithCustomRoutes(): void
{
GoogleSitemap::register_routes(array(
GoogleSitemap::register_routes([
'/test-route/',
'/someother-route/',
'/fake-sitemap-route/'
));
]);
$items = GoogleSitemap::get_items('GoogleSitemapRoute', 1);
$google = new GoogleSitemap();
$items = $google->getItems('GoogleSitemapRoute', 1);
$this->assertEquals(3, $items->count());
}
@ -110,6 +122,16 @@ class GoogleSitemapTest extends FunctionalTest
GoogleSitemap::register_dataobject(TestDataObject::class);
GoogleSitemap::register_dataobject(OtherDataObject::class);
$obj = $this->objFromFixture(TestDataObject::class, 'DataObjectTest1');
$table = $obj->baseTable();
DB::query("UPDATE \"" . $table . "\" SET \"LastEdited\"='2023-02-13 00:00:00'");
$obj2 = $this->objFromFixture(OtherDataObject::class, 'OtherDataObjectTest2');
$table = $obj2->baseTable();
DB::query("UPDATE \"" . $table . "\" SET \"LastEdited\"='2023-02-13 00:00:00'");
$response = $this->get('sitemap.xml');
$body = $response->getBody();
@ -128,12 +150,12 @@ class GoogleSitemapTest extends FunctionalTest
$page->publishSingle();
$page->flushCache();
$page2 = $this->objFromFixture('Page', 'Page2');
$page2->publishSingle();
$page2->flushCache();
$p2 = $this->objFromFixture('Page', 'Page2');
$p2->publishSingle();
$p2->flushCache();
DB::query("UPDATE \"SiteTree_Live\" SET \"LastEdited\"='2014-03-14 00:00:00' WHERE \"ID\"='" . $page->ID . "'");
DB::query("UPDATE \"SiteTree_Live\" SET \"LastEdited\"='2014-01-01 00:00:00' WHERE \"ID\"='" . $page2->ID . "'");
DB::query("UPDATE \"SiteTree_Live\" SET \"LastEdited\"='2014-01-01 00:00:00' WHERE \"ID\"='" . $p2->ID . "'");
$response = $this->get('sitemap.xml');
$body = $response->getBody();
@ -153,9 +175,19 @@ class GoogleSitemapTest extends FunctionalTest
Config::inst()->set(GoogleSitemap::class, 'objects_per_sitemap', 1);
GoogleSitemap::register_dataobject(TestDataObject::class);
$obj = $this->objFromFixture(TestDataObject::class, 'DataObjectTest1');
$obj1 = $this->objFromFixture(TestDataObject::class, 'DataObjectTest2');
$obj2 = $this->objFromFixture(TestDataObject::class, 'UnindexedDataObject');
$t = $obj->baseTable();
DB::query("UPDATE \"" . $t . "\" SET \"LastEdited\"='2023-02-13 00:00:00' WHERE \"ID\"='" . $obj->ID . "'");
DB::query("UPDATE \"" . $t . "\" SET \"LastEdited\"='2023-02-13 00:00:00' WHERE \"ID\"='" . $obj1->ID . "'");
DB::query("UPDATE \"" . $t . "\" SET \"LastEdited\"='2023-02-13 00:00:00' WHERE \"ID\"='" . $obj2->ID . "'");
$response = $this->get('sitemap.xml');
$body = $response->getBody();
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/xml/' . __FUNCTION__ . '.xml', $body);
Config::inst()->set(GoogleSitemap::class, 'objects_per_sitemap', $original);
@ -164,11 +196,11 @@ class GoogleSitemapTest extends FunctionalTest
public function testRegisterRoutesIncludesAllRoutes(): void
{
GoogleSitemap::register_route('/test/');
GoogleSitemap::register_routes(array(
GoogleSitemap::register_routes([
'/test/', // duplication should be replaced
'/unittests/',
'/anotherlink/'
), 'weekly');
], 'weekly');
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapRoute/1');
$body = $response->getBody();
@ -193,7 +225,7 @@ class GoogleSitemapTest extends FunctionalTest
public function testGetItemsWithPages(): void
{
if (!class_exists('Page')) {
if (!class_exists(SiteTree::class)) {
$this->markTestIncomplete('No cms module installed, page related test skipped');
}
@ -205,10 +237,10 @@ class GoogleSitemapTest extends FunctionalTest
$page2->publishSingle();
$page2->flushCache();
$this->assertListContains(array(
array('Title' => 'Testpage1'),
array('Title' => 'Testpage2')
), GoogleSitemap::inst()->getItems(SiteTree::class), "There should be 2 pages in the sitemap after publishing");
$this->assertListContains([
['Title' => 'Testpage1'],
['Title' => 'Testpage2']
], GoogleSitemap::inst()->getItems(SiteTree::class), "There should be 2 pages in the sitemap after publishing");
// check if we make a page readonly that it is hidden
$page2->CanViewType = 'LoggedInUsers';
@ -217,9 +249,9 @@ class GoogleSitemapTest extends FunctionalTest
$this->logOut();
$this->assertListEquals(array(
array('Title' => 'Testpage1')
), GoogleSitemap::inst()->getItems(SiteTree::class), "There should be only 1 page, other is logged in only");
$this->assertListEquals([
['Title' => 'Testpage1']
], GoogleSitemap::inst()->getItems(SiteTree::class), "There should be only 1 page, other is logged in only");
}
public function testAccess(): void

View File

@ -1,15 +1,15 @@
Wilr\GoogleSitemaps\Tests\Model\TestDataObject:
DataObjectTest1:
Priority: 0.4
DataObjectTest2:
Priority: 0.2
UnindexedDataObject:
Priority: -1
DataObjectTest1:
Priority: 0.4
DataObjectTest2:
Priority: 0.2
UnindexedDataObject:
Priority: -1
Wilr\GoogleSitemaps\Tests\Model\OtherDataObject:
OtherDataObjectTest2:
Priority: 0.3
OtherDataObjectTest2:
Priority: 0.3
Wilr\GoogleSitemaps\Tests\Model\UnviewableDataObject:
Unviewable1:
Priority: 0.4
Unviewable1:
Priority: 0.4

View File

@ -8,6 +8,7 @@ use SilverStripe\Control\Director;
class TestDataObject extends DataObject implements TestOnly
{
protected $private = false;
private static $db = array(
'Priority' => 'Varchar(10)'
@ -15,9 +16,19 @@ class TestDataObject extends DataObject implements TestOnly
public function canView($member = null)
{
if ($this->private) {
return false;
}
return true;
}
public function setPrivate()
{
$this->private = true;
}
public function AbsoluteLink()
{
return Director::absoluteBaseURL();