Compare commits

..

No commits in common. "main" and "2.1.9" have entirely different histories.
main ... 2.1.9

34 changed files with 704 additions and 648 deletions

View File

@ -1,11 +0,0 @@
name: CI
on:
push:
pull_request:
workflow_dispatch:
jobs:
ci:
name: CI
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1

3
.gitignore vendored
View File

@ -1,4 +1 @@
.DS_Store
/vendor
/public
/composer.lock

69
.scrutinizer.yml Normal file
View File

@ -0,0 +1,69 @@
inherit: true
checks:
php:
verify_property_names: true
verify_argument_usable_as_reference: true
verify_access_scope_valid: true
useless_calls: true
use_statement_alias_conflict: true
variable_existence: true
unused_variables: true
unused_properties: true
unused_parameters: true
unused_methods: true
unreachable_code: true
too_many_arguments: true
sql_injection_vulnerabilities: true
simplify_boolean_return: true
side_effects_or_types: true
security_vulnerabilities: true
return_doc_comments: true
return_doc_comment_if_not_inferrable: true
require_scope_for_properties: true
require_scope_for_methods: true
require_php_tag_first: true
psr2_switch_declaration: true
psr2_class_declaration: true
property_assignments: true
prefer_while_loop_over_for_loop: true
precedence_mistakes: true
precedence_in_conditions: true
phpunit_assertions: true
php5_style_constructor: true
parse_doc_comments: true
parameter_non_unique: true
parameter_doc_comments: true
param_doc_comment_if_not_inferrable: true
optional_parameters_at_the_end: true
one_class_per_file: true
no_unnecessary_if: true
no_trailing_whitespace: true
no_property_on_interface: true
no_non_implemented_abstract_methods: true
no_error_suppression: true
no_duplicate_arguments: true
no_commented_out_code: true
newline_at_end_of_file: true
missing_arguments: true
method_calls_on_non_object: true
instanceof_class_exists: true
foreach_traversable: true
fix_line_ending: true
fix_doc_comments: true
duplication: true
deprecated_code_usage: true
deadlock_detection_in_loops: true
code_rating: true
closure_use_not_conflicting: true
catch_class_exists: true
blank_line_after_namespace_declaration: false
avoid_multiple_statements_on_same_line: true
avoid_duplicate_types: true
avoid_conflicting_incrementers: true
avoid_closing_tag: true
assignment_of_null_return: true
argument_type_checks: true
filter:
paths: [code/*, tests/*]

32
.travis.yml Normal file
View File

@ -0,0 +1,32 @@
language: php
dist: trusty
env:
global:
- COMPOSER_ROOT_VERSION=2.x-dev
matrix:
include:
- php: 7.2
env: DB=MYSQL PHPUNIT_TEST=1 PHPCS_TEST=1
- php: 7.3
env: DB=MYSQL PHPUNIT_COVERAGE_TEST=1
before_script:
# Init PHP
- phpenv rehash
- phpenv config-rm xdebug.ini
- composer validate
- composer require silverstripe/recipe-cms 4.5.x-dev --no-update
- if [[ $DB == PGSQL ]]; then composer require silverstripe/postgresql:2.0.x-dev --no-update; fi
- if [[ $PHPCS_TEST ]]; then composer global require squizlabs/php_codesniffer:^3 --prefer-dist --no-interaction --no-progress --no-suggest -o; fi
- composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile
script:
- if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit tests/; fi
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml; fi
- if [[ $PHPCS_TEST ]]; then vendor/bin/phpcs src/ tests/ ; fi
after_success:
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then bash <(curl -s https://codecov.io/bash) -f coverage.xml; fi

29
CHANGELOG.md Normal file
View File

@ -0,0 +1,29 @@
# Changelog
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
Older releases can be found on Github.
## 2.1.7
- Adds legacy file for migration from Silverstripe 3
Full changelog: https://github.com/wilr/silverstripe-googlesitemaps/compare/2.1.6...2.1.7
## 2.1.6
- Fixes incorrect priority calculations
Full changelog: https://github.com/wilr/silverstripe-googlesitemaps/compare/2.1.5...2.1.6
## 2.1.5
- FIX: enable stream context to pass through CWP egres proxy, fixes #156
Full changelog: https://github.com/wilr/silverstripe-googlesitemaps/compare/2.1.4...2.1.5
## 2.1.4
- Fixes extending alterSiteMap to support falsey values
Full changelog: https://github.com/wilr/silverstripe-googlesitemaps/compare/2.1.3...2.1.4

View File

@ -6,16 +6,12 @@
## 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.
For Silverstripe 4 use the `2.x` branch line.
## Documentation
Provides support for the [Sitemaps XML Protocol](http://www.sitemaps.org/protocol.html),
@ -34,4 +30,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)

View File

@ -1,10 +0,0 @@
<?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,6 +4,7 @@ Name: googlesitemaps
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
google_notification_enabled: false
use_show_in_search: true
---
Only:

View File

@ -1,34 +1,27 @@
{
"name": "wilr/silverstripe-googlesitemaps",
"description": "SilverStripe support for the Google Sitemaps XML, enabling Google and other search engines to see all urls on your site. This helps your SilverStripe website rank well in search engines, and to encourage the information on your site to be discovered quickly.",
"type": "silverstripe-vendormodule",
"keywords": [
"silverstripe",
"googlesitemaps",
"seo"
],
"homepage": "https://github.com/wilr/silverstripe-googlesitemaps",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Will Rossiter",
"email": "will@fullscreen.io"
}
],
"require": {
"php": "^8.1",
"silverstripe/framework": "^5"
},
"name": "wilr/silverstripe-googlesitemaps",
"description": "SilverStripe support for the Google Sitemaps XML, enabling Google and other search engines to see all urls on your site. This helps your SilverStripe website rank well in search engines, and to encourage the information on your site to be discovered quickly.",
"type": "silverstripe-vendormodule",
"keywords": ["silverstripe", "googlesitemaps", "seo"],
"homepage": "https://github.com/wilr/silverstripe-googlesitemaps",
"license": "BSD-3-Clause",
"authors": [{
"name": "Will Rossiter",
"email": "will@fullscreen.io"
}],
"require": {
"silverstripe/framework": "^4"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^5.7",
"squizlabs/php_codesniffer": "^3"
},
"replace": {
"silverstripe/googlesitemaps": "*"
"silverstripe/googlesitemaps":"*"
},
"extra": {
"extra": {
"branch-alias": {
"dev-main": "3.x-dev"
"dev-master": "2.x-dev"
},
"expose": [
"images",
@ -42,12 +35,5 @@
}
},
"prefer-stable": true,
"minimum-stability": "dev",
"config": {
"allow-plugins": {
"composer/installers": true,
"silverstripe/vendor-plugin": true,
"silverstripe/recipe-plugin": true
}
}
"minimum-stability": "dev"
}

View File

@ -1,145 +1,144 @@
: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;
}
* {
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 {
background: var(--color-white);
color: var(--color-grey-dark);
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;
}
table {
border-collapse: collapse;
color: var(--color-black);
text-align: left;
width: 100%;
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;
}
a {
color: inherit;
text-decoration: none;
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");
}
.content {
margin: 0 auto;
max-width: 122rem;
padding: 2rem;
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__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;
#Footer{
margin: 50px 0 10px;
text-align: right;
font-size: 0.8em;
}

View File

@ -1,23 +1,23 @@
# Google Sitemaps Module
Silverstripe CMS provides support for the Google Sitemaps XML system, enabling
SilverStripe 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 CMS website rank well in search engines, and to encourage the
your SilverStripe website rank well in search engines, and to encourage the
information on your site to be discovered by Google quickly.
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
Therefore, all Silverstripe 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 CMS can
Whenever you publish a new or republish an existing page, SilverStripe can
automatically inform Google of the change, encouraging a Google to take notice.
If you install the Silverstripe CMS Google Analytics module, you can see if Google
If you install the SilverStripe Google Analytics module, you can see if Google
has updated your page as a result.
By default, Silverstripe CMS informs Google that the importance of a page depends
By default, SilverStripe 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,32 +27,45 @@ manually, including requesting to have the page excluded from the sitemap.
## Configuration
Most module configuration is done via the Silverstripe CMS Config API. Create a
new config file `mysite/_config/googlesitemaps.yml` with the following outline:
Most module configuration is done via the SilverStripe Config API. Create a new
config file `mysite/_config/googlesitemaps.yml` with the following outline:
```yml
---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
use_show_in_search: true
```
---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
google_notification_enabled: false
use_show_in_search: true
You can now alter any of those properties to set your needs.
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
```yml
---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
use_show_in_search: true
```
---
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
### Including DataObjects
@ -63,40 +76,41 @@ 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 AbsoluteLink() {
return Director::absoluteURL($this->Link());
}
function Link() {
return 'MyController/show/'. $this->ID;
}
}
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
@ -104,7 +118,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:
@ -112,33 +126,15 @@ http://www.silvercart.org/blog/dataobjects-and-googlesitemaps/
### Including custom routes
Occasionally you may have a need to include custom URLs in your sitemap for
Occasionally you may have a need to include custom url's 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/'
));
### Sitemapable
For automatic registration of a DataObject subclass, implement the `Sitemapable`
extension.
```
<?php
class MyDataObject extends DataObject implements Sitemapable
{
public function AbsoluteLink()
{
// ..
}
}
```
GoogleSitemap::register_routes(array(
'/my-custom-controller/',
'/Security/',
'/Security/login/'
));

BIN
images/asc.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 B

BIN
images/bg.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 B

View File

@ -1,4 +0,0 @@
<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>

Before

Width:  |  Height:  |  Size: 970 B

BIN
images/desc.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 B

BIN
images/logo_small.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -1,9 +0,0 @@
de:
GoogleSitemaps:
METANOTEPRIORITY: 'Manuelle Google Sitemaps-Priorität für diese Seite (%s)'
METAPAGEPRIO: 'Page Priorität'
PRIORITYAUTOSET: 'Automatisch basierend auf Verschachtelung im Seitenbaum'
PRIORITYLEASTIMPORTANT: 'Am wenigsten wichtig'
PRIORITYMOSTIMPORTANT: 'Am wichtigsten'
PRIORITYNOTINDEXED: 'Nicht indexiert'
TABGOOGLESITEMAP: 'Google Sitemap'

View File

@ -1,10 +1,9 @@
<?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

@ -1,14 +1,14 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</coverage>
<testsuite name="Default">
<directory>tests</directory>
</testsuite>
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests</directory>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

View File

@ -4,8 +4,9 @@ namespace Wilr\GoogleSitemaps\Control;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use Wilr\GoogleSitemaps\GoogleSitemap;
use SilverStripe\View\ArrayData;
@ -51,7 +52,7 @@ class GoogleSitemapController extends Controller
$this->extend('updateGoogleSitemaps', $sitemaps);
return $this->customise(new ArrayData([
'Sitemaps' => $sitemaps,
'Sitemaps' => $sitemaps
]))->renderWith(__CLASS__);
} else {
return new HTTPResponse('Page not found', 404);
@ -67,17 +68,11 @@ class GoogleSitemapController extends Controller
public function sitemap()
{
$class = $this->unsanitiseClassName($this->request->param('ID'));
$page = intval($this->request->param('OtherID'));
if ($page) {
if (!is_numeric($page)) {
return new HTTPResponse('Page not found', 404);
}
}
$page = $this->request->param('OtherID');
if (GoogleSitemap::enabled()
&& $class
&& ($page > 0)
&& $page
&& ($class == SiteTree::class || $class == 'GoogleSitemapRoute' || GoogleSitemap::is_registered($class))
) {
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
@ -100,7 +95,7 @@ class GoogleSitemapController extends Controller
*/
protected function unsanitiseClassName($class)
{
return str_replace('-', '\\', (string) $class);
return str_replace('-', '\\', $class);
}
/**
@ -128,10 +123,4 @@ class GoogleSitemapController extends Controller
return $html;
}
public function AbsoluteLink($action = null)
{
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;
@ -66,13 +66,27 @@ class GoogleSitemapExtension extends DataExtension
}
}
if (is_array($can) && isset($can[0])) {
return $can[0];
}
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

@ -8,7 +8,6 @@ use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\Tab;
use SilverStripe\ORM\ArrayList;
use Throwable;
class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension
{
@ -104,10 +103,6 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension
$result = parent::canIncludeInGoogleSitemap();
$result = ($this->owner instanceof ErrorPage) ? false : $result;
if (is_array($result) && isset($result[0])) {
return $result[0];
}
return $result;
}
@ -140,21 +135,13 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension
$cachedImages = [];
foreach ($this->owner->hasOne() as $field => $type) {
if (strpos($type, '.') !== false) {
$type = explode('.', $type)[0];
}
if (singleton($type) instanceof Image) {
$image = $this->owner->getComponent($field);
try {
if ($image && $image->exists() && !isset($cachedImages[$image->ID])) {
$cachedImages[$image->ID] = true;
if ($image && $image->exists() && !isset($cachedImages[$image->ID])) {
$cachedImages[$image->ID] = true;
$list->push($image);
}
} catch (Throwable $e) {
//
$list->push($image);
}
}
}
@ -164,22 +151,18 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension
$images = $this->owner->getComponents($field);
foreach ($images as $image) {
try {
if ($image && $image->exists() && !isset($cachedImages[$image->ID])) {
$cachedImages[$image->ID] = true;
if ($image && $image->exists() && !isset($cachedImages[$image->ID])) {
$cachedImages[$image->ID] = true;
$list->push($image);
}
} catch (Throwable $e) {
//
$list->push($image);
}
}
}
}
foreach ($this->owner->manyMany() as $field => $type) {
$image = false;
if (is_array($type) && isset($type['through'])) {
if (singleton($type['through']) instanceof Image) {
$image = true;
@ -188,7 +171,7 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension
if (strpos($type, '.') !== false) {
$type = explode('.', $type)[0];
}
if (singleton($type) instanceof Image) {
$image = true;
}
@ -198,14 +181,10 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension
$images = $this->owner->$field();
foreach ($images as $image) {
try {
if ($image && $image->exists() && !isset($cachedImages[$image->ID])) {
$cachedImages[$image->ID] = true;
if ($image && $image->exists() && !isset($cachedImages[$image->ID])) {
$cachedImages[$image->ID] = true;
$list->push($image);
}
} catch (Throwable $e) {
//
$list->push($image);
}
}
}

View File

@ -29,8 +29,17 @@ use ReflectionException;
* The GoogleSitemap handle requests to 'sitemap.xml' the other two classes are
* used to render the sitemap.
*
* The config file is usually located in the _config folder of your project folder.
* e.g. app/_config/googlesitemaps.yml
* 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
*
* <example>
* ---
@ -40,6 +49,7 @@ use ReflectionException;
* Wilr\GoogleSitemaps\GoogleSitemap:
* enabled: true
* objects_per_sitemap: 1000
* google_notification_enabled: true
* use_show_in_search: true
* </example>
*
@ -74,6 +84,16 @@ 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.
@ -216,8 +236,7 @@ class GoogleSitemap
*/
public function getItems($class, $page = 1)
{
$page = (int) $page;
//normalise the class name
try {
$reflectionClass = new ReflectionClass($class);
$class = $reflectionClass->getName();
@ -227,9 +246,9 @@ class GoogleSitemap
}
$output = new ArrayList();
$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');
$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');
// todo migrate to extension hook or DI point for other modules to
// modify state filters
@ -371,7 +390,7 @@ class GoogleSitemap
for ($i = 1; $i <= $neededForPage; $i++) {
$lastEdited = $instances
->limit($countPerFile, ($i - 1) * $countPerFile)
->sort(null)
->sort(array())
->max('LastEdited');
$lastModified = ($lastEdited) ? date('Y-m-d', strtotime($lastEdited)) : date('Y-m-d');
@ -397,7 +416,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),
@ -433,6 +452,41 @@ 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?
*
@ -461,6 +515,6 @@ class GoogleSitemap
*/
protected function sanitiseClassName($class)
{
return str_replace('\\', '-', (string) $class);
return str_replace('\\', '-', $class);
}
}

View File

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

View File

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

View File

@ -1,16 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='{$AbsoluteLink('styleSheet')}'?>
<?xml-stylesheet type='text/xsl' href='{$BaseHref}sitemap.xml/styleSheet'?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<% loop $Items %>
<url>
<loc>{$AbsoluteLink}</loc>
<% if $LastEdited %><lastmod>{$LastEdited.Rfc3339()}</lastmod><% end_if %>
<loc>$AbsoluteLink</loc>
<% if $LastEdited %><lastmod>$LastEdited.Rfc3339()</lastmod><% end_if %>
<% if $ChangeFrequency %><changefreq>$ChangeFrequency</changefreq><% end_if %>
<% if $GooglePriority %><priority>$GooglePriority</priority><% end_if %>
<% if $ImagesForSitemap %><% loop $ImagesForSitemap %>
<image:image>
<image:loc>{$AbsoluteLink}</image:loc>
<image:title>{$Title}</image:title>
</image:image>
<% end_loop %><% end_if %>
</url>

View File

@ -9,80 +9,81 @@
<link rel="stylesheet" href="$resourceURL(wilr/silverstripe-googlesitemaps:css/style.css)" />
</head>
<body>
<div class="content">
<h1 class="content__title">
<a href="https://www.silverstripe.org" target="_blank" rel="noopener noreferrer">XML Sitemap</a>
<div id="content">
<h1>
<a href="http://silverstripe.org" target="_blank">XML Sitemap
<span class="ss_link">&#8594; silverstripe.org</span>
</a>
</h1>
<p class="content__text">
<p class="expl">
This sitemap contains <xsl:value-of select="count(sitemap:urlset/sitemap:url)"/> URLs.
</p>
<div class="table-wrapper">
<table id="sitemap" class="table">
<thead>
<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">
<tr>
<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>
</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">
<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>
<th class="image-table__cell">Images</th>
<td>
<img src="{\$imageURL}" width="40px"/>
</td>
<td>
<a href="{\$imageURL}">
<xsl:value-of select="image:loc"/>
</a>
</td>
</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>
</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>
</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>.
</p>
</div>
</body>

View File

@ -9,48 +9,46 @@
<link rel="stylesheet" href="$resourceURL(wilr/silverstripe-googlesitemaps:css/style.css)" />
</head>
<body>
<div class="content">
<h1 class="content__title">
<a href="https://www.silverstripe.org" target="_blank" rel="noopener noreferrer">XML Sitemap</a>
<div id="content">
<h1>
<a href="http://silverstripe.org" target="_blank">XML Sitemap
<span class="ss_link">&#8594; silverstripe.org</span>
</a>
</h1>
<p class="content__text">
<p class="expl">
This sitemap consists of <xsl:value-of select="count(sitemap:sitemapindex/sitemap:sitemap)"/> part(s).
</p>
<div class="table-wrapper">
<table id="sitemapindex" class="table">
<thead>
<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">
<tr>
<th class="table__cell table__cell--w-85">URL</th>
<th class="table__cell table__cell--w-15">Last Change</th>
<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>
</tr>
</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>
</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>.
</p>
</div>
</body>

View File

@ -4,6 +4,7 @@ namespace Wilr\GoogleSitemaps\Tests;
use Exception;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Forms\DropdownField;
@ -19,22 +20,7 @@ use Wilr\GoogleSitemaps\Tests\Model\UnviewableDataObject;
class GoogleSitemapTest extends FunctionalTest
{
protected static $fixture_file = [
'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 $fixture_file = 'GoogleSitemapTest.yml';
protected static $extra_dataobjects = [
TestDataObject::class,
@ -46,15 +32,19 @@ class GoogleSitemapTest extends FunctionalTest
GoogleSitemapExtension::class
];
protected function setUp(): void
public function setUp()
{
parent::setUp();
if (class_exists('Page')) {
$this->loadFixture($this->resolveFixturePath('GoogleSitemapPageTest.yml'));
}
GoogleSitemap::clear_registered_dataobjects();
GoogleSitemap::clear_registered_routes();
}
protected function tearDown(): void
public function tearDown()
{
parent::tearDown();
@ -62,100 +52,117 @@ class GoogleSitemapTest extends FunctionalTest
GoogleSitemap::clear_registered_routes();
}
public function testCanIncludeInGoogleSitemap(): void
public function testCanIncludeInGoogleSitemap()
{
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
public function testIndexFileWithCustomRoute()
{
GoogleSitemap::register_route('/test/');
$response = $this->get('sitemap.xml');
$body = $response->getBody();
$this->assertXmlStringEqualsXmlFile(
__DIR__ . '/xml/' . __FUNCTION__ . '.xml',
$body,
'A link to the custom routes exists'
);
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapRoute/1") ."</loc>";
$this->assertEquals(1, substr_count($body, $expected), 'A link to the custom routes exists');
}
public function testGetItems(): void
public function testGetItems()
{
GoogleSitemap::register_dataobject(TestDataObject::class, '');
$google = new GoogleSitemap();
$items = $google->getItems(TestDataObject::class, 1);
$items = GoogleSitemap::get_items(TestDataObject::class, 1);
$this->assertEquals(2, $items->count());
$this->assertEquals(3, $items->count());
$this->assertDOSEquals(array(
array("Priority" => "0.2"),
array("Priority" => "0.4")
), $items);
GoogleSitemap::register_dataobject(OtherDataObject::class);
$this->assertEquals(1, $google->getItems(OtherDataObject::class, 1)->count());
$this->assertEquals(1, GoogleSitemap::get_items(OtherDataObject::class, 1)->count());
GoogleSitemap::register_dataobject(UnviewableDataObject::class);
$this->assertEquals(0, $google->getItems(UnviewableDataObject::class, 1)->count());
$this->assertEquals(0, GoogleSitemap::get_items(UnviewableDataObject::class, 1)->count());
}
public function testGetItemsWithCustomRoutes(): void
public function testGetItemsWithCustomRoutes()
{
GoogleSitemap::register_routes([
GoogleSitemap::register_routes(array(
'/test-route/',
'/someother-route/',
'/fake-sitemap-route/'
]);
));
$google = new GoogleSitemap();
$items = $google->getItems('GoogleSitemapRoute', 1);
$items = GoogleSitemap::get_items('GoogleSitemapRoute', 1);
$this->assertEquals(3, $items->count());
}
public function testAccessingSitemapRootXMLFile(): void
public function testAccessingSitemapRootXMLFile()
{
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();
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/xml/' . __FUNCTION__ . '.xml', $body);
// the sitemap should contain <loc> to both those files and not the other
// dataobject as it hasn't been registered
$expected = "<loc>". Director::absoluteURL(
"sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1"
) ."</loc>";
$this->assertEquals(
1,
substr_count($body, $expected),
'A link to GoogleSitemapTest_DataObject exists'
);
$expected = "<loc>". Director::absoluteURL(
"sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-OtherDataObject/1"
) ."</loc>";
$this->assertEquals(
1,
substr_count($body, $expected),
'A link to GoogleSitemapTest_OtherDataObject exists'
);
$expected = "<loc>". Director::absoluteURL(
"sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-UnviewableDataObject/2"
) ."</loc>";
$this->assertEquals(
0,
substr_count($body, $expected),
'A link to a GoogleSitemapTest_UnviewableDataObject does not exist'
);
}
public function testLastModifiedDateOnRootXML(): void
public function testLastModifiedDateOnRootXML()
{
Config::inst()->set(GoogleSitemap::class, 'enabled', true);
Config::inst()->update(GoogleSitemap::class, 'enabled', true);
if (!class_exists('Page')) {
$this->markTestIncomplete('No cms module installed, page related test skipped');
}
$page = $this->objFromFixture('Page', 'Page1');
$page->publishSingle();
$page->publish('Stage', 'Live');
$page->flushCache();
$p2 = $this->objFromFixture('Page', 'Page2');
$p2->publishSingle();
$p2->flushCache();
$page2 = $this->objFromFixture('Page', 'Page2');
$page2->publish('Stage', 'Live');
$page2->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\"='" . $p2->ID . "'");
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."'");
$response = $this->get('sitemap.xml');
$body = $response->getBody();
@ -169,38 +176,43 @@ class GoogleSitemapTest extends FunctionalTest
);
}
public function testIndexFilePaginatedSitemapFiles(): void
public function testIndexFilePaginatedSitemapFiles()
{
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
Config::inst()->set(GoogleSitemap::class, 'objects_per_sitemap', 1);
Config::inst()->update(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);
$expected = "<loc>". Director::absoluteURL(
"sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1"
) ."</loc>";
$this->assertEquals(
1,
substr_count($body, $expected),
'A link to the first page of GoogleSitemapTest_DataObject exists'
);
Config::inst()->set(GoogleSitemap::class, 'objects_per_sitemap', $original);
$expected = "<loc>". Director::absoluteURL(
"sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/2"
) ."</loc>";
$this->assertEquals(
1,
substr_count($body, $expected),
'A link to the second page GoogleSitemapTest_DataObject exists'
);
Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', $original);
}
public function testRegisterRoutesIncludesAllRoutes(): void
public function testRegisterRoutesIncludesAllRoutes()
{
GoogleSitemap::register_route('/test/');
GoogleSitemap::register_routes([
GoogleSitemap::register_routes(array(
'/test/', // duplication should be replaced
'/unittests/',
'/anotherlink/'
], 'weekly');
), 'weekly');
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapRoute/1');
$body = $response->getBody();
@ -209,10 +221,10 @@ class GoogleSitemapTest extends FunctionalTest
$this->assertEquals(3, substr_count($body, "<loc>"));
}
public function testAccessingNestedSiteMap(): void
public function testAccessingNestedSiteMap()
{
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
Config::inst()->set(GoogleSitemap::class, 'objects_per_sitemap', 1);
Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', 1);
GoogleSitemap::register_dataobject(TestDataObject::class);
$response = $this->get('sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1');
@ -220,43 +232,43 @@ class GoogleSitemapTest extends FunctionalTest
$this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap');
Config::inst()->set(GoogleSitemap::class, 'objects_per_sitemap', $original);
Config::inst()->update(GoogleSitemap::class, 'objects_per_sitemap', $original);
}
public function testGetItemsWithPages(): void
public function testGetItemsWithPages()
{
if (!class_exists(SiteTree::class)) {
if (!class_exists('Page')) {
$this->markTestIncomplete('No cms module installed, page related test skipped');
}
$page = $this->objFromFixture('Page', 'Page1');
$page->publishSingle();
$page->publish('Stage', 'Live');
$page->flushCache();
$page2 = $this->objFromFixture('Page', 'Page2');
$page2->publishSingle();
$page2->publish('Stage', 'Live');
$page2->flushCache();
$this->assertListContains([
['Title' => 'Testpage1'],
['Title' => 'Testpage2']
], GoogleSitemap::inst()->getItems(SiteTree::class), "There should be 2 pages in the sitemap after publishing");
$this->assertDOSContains(array(
array('Title' => 'Testpage1'),
array('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';
$page2->write();
$page2->publishSingle();
$page2->publish('Stage', 'Live');
$this->logOut();
$this->assertListEquals([
['Title' => 'Testpage1']
], GoogleSitemap::inst()->getItems(SiteTree::class), "There should be only 1 page, other is logged in only");
$this->assertDOSEquals(array(
array('Title' => 'Testpage1')
), GoogleSitemap::inst()->getItems(SiteTree::class), "There should be only 1 page, other is logged in only");
}
public function testAccess(): void
public function testAccess()
{
Config::inst()->set(GoogleSitemap::class, 'enabled', true);
Config::inst()->update(GoogleSitemap::class, 'enabled', true);
$response = $this->get('sitemap.xml');
@ -268,7 +280,7 @@ class GoogleSitemapTest extends FunctionalTest
$this->assertEquals(200, $response->getStatusCode(), 'Sitemap returns a 200 success when enabled');
$this->assertEquals('application/xml; charset="utf-8"', $response->getHeader('Content-Type'));
Config::inst()->set(GoogleSitemap::class, 'enabled', false);
Config::inst()->update(GoogleSitemap::class, 'enabled', false);
$response = $this->get('sitemap.xml');
$this->assertEquals(404, $response->getStatusCode(), 'Sitemap index returns a 404 when disabled');
@ -277,7 +289,7 @@ class GoogleSitemapTest extends FunctionalTest
$this->assertEquals(404, $response->getStatusCode(), 'Sitemap file returns a 404 when disabled');
}
public function testDecoratorAddsFields(): void
public function testDecoratorAddsFields()
{
if (!class_exists("Page")) {
$this->markTestIncomplete('No cms module installed, page related test skipped');
@ -293,7 +305,7 @@ class GoogleSitemapTest extends FunctionalTest
$this->assertInstanceOf(LiteralField::class, $tab->fieldByName('GoogleSitemapIntro'));
}
public function testGetPriority(): void
public function testGetPriority()
{
if (!class_exists("Page")) {
$this->markTestIncomplete('No cms module installed, page related test skipped');
@ -314,7 +326,7 @@ class GoogleSitemapTest extends FunctionalTest
$this->assertFalse($page->getGooglePriority());
}
public function testUnpublishedPage(): void
public function testUnpublishedPage()
{
if (!class_exists('SilverStripe\CMS\Model\SiteTree')) {
$this->markTestSkipped('Test skipped; CMS module required for testUnpublishedPage');
@ -323,12 +335,12 @@ class GoogleSitemapTest extends FunctionalTest
$orphanedPage = new \SilverStripe\CMS\Model\SiteTree();
$orphanedPage->ParentID = 999999; // missing parent id
$orphanedPage->write();
$orphanedPage->publishSingle();
$orphanedPage->publish("Stage", "Live");
$rootPage = new \SilverStripe\CMS\Model\SiteTree();
$rootPage->ParentID = 0;
$rootPage->write();
$rootPage->publishSingle();
$rootPage->publish("Stage", "Live");
$oldMode = Versioned::get_reading_mode();
Versioned::set_reading_mode('Live');
@ -342,7 +354,7 @@ class GoogleSitemapTest extends FunctionalTest
Versioned::set_reading_mode($oldMode);
throw $ex;
} // finally {
Versioned::set_reading_mode($oldMode);
Versioned::set_reading_mode($oldMode);
// }
}
}

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,7 +8,6 @@ use SilverStripe\Control\Director;
class TestDataObject extends DataObject implements TestOnly
{
protected $private = false;
private static $db = array(
'Priority' => 'Varchar(10)'
@ -16,19 +15,9 @@ 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();

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='http://localhost/sitemap.xml/styleSheetIndex'?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://localhost/sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1</loc>
<lastmod>2023-02-13</lastmod>
</sitemap>
<sitemap>
<loc>http://localhost/sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-OtherDataObject/1</loc>
<lastmod>2023-02-13</lastmod>
</sitemap>
</sitemapindex>

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='http://localhost/sitemap.xml/styleSheetIndex'?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://localhost/sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/1</loc>
<lastmod>2023-02-13</lastmod>
</sitemap>
<sitemap>
<loc>http://localhost/sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/2</loc>
<lastmod>2023-02-13</lastmod>
</sitemap>
<sitemap>
<loc>http://localhost/sitemap.xml/sitemap/Wilr-GoogleSitemaps-Tests-Model-TestDataObject/3</loc>
<lastmod>2023-02-13</lastmod>
</sitemap>
</sitemapindex>

View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='http://localhost/sitemap.xml/styleSheetIndex'?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://localhost/sitemap.xml/sitemap/GoogleSitemapRoute/1</loc>
</sitemap>
</sitemapindex>