mirror of
https://github.com/wilr/silverstripe-googlesitemaps.git
synced 2024-10-22 11:05:48 +02:00
Merge pull request #94 from helpfulrobot/convert-to-psr-2
Converted to PSR-2
This commit is contained in:
commit
289ed77139
@ -37,428 +37,443 @@
|
||||
*
|
||||
* @package googlesitemaps
|
||||
*/
|
||||
class GoogleSitemap extends Object {
|
||||
class GoogleSitemap extends Object
|
||||
{
|
||||
|
||||
/**
|
||||
* List of {@link DataObject} class names to include. As well as the change
|
||||
* frequency and priority of each class.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $dataobjects = array();
|
||||
/**
|
||||
* List of {@link DataObject} class names to include. As well as the change
|
||||
* frequency and priority of each class.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $dataobjects = array();
|
||||
|
||||
/**
|
||||
* List of custom routes to include in the sitemap (such as controller
|
||||
* subclasses) as well as the change frequency and priority.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $routes = array();
|
||||
/**
|
||||
* List of custom routes to include in the sitemap (such as controller
|
||||
* subclasses) as well as the change frequency and priority.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $routes = array();
|
||||
|
||||
/**
|
||||
* Decorates the given DataObject with {@link GoogleSitemapDecorator}
|
||||
* and pushes the class name to the registered DataObjects.
|
||||
* Note that all registered DataObjects need the method AbsoluteLink().
|
||||
*
|
||||
* @param string $className name of DataObject to register
|
||||
* @param string $changeFreq how often is this DataObject updated?
|
||||
* Possible values:
|
||||
* always, hourly, daily, weekly, monthly, yearly, never
|
||||
* @param string $priority How important is this DataObject in comparison to other urls?
|
||||
* Possible values: 0.1, 0.2 ... , 0.9, 1.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function register_dataobject($className, $changeFreq = 'monthly', $priority = '0.6') {
|
||||
if (!self::is_registered($className)) {
|
||||
$className::add_extension('GoogleSitemapExtension');
|
||||
/**
|
||||
* Decorates the given DataObject with {@link GoogleSitemapDecorator}
|
||||
* and pushes the class name to the registered DataObjects.
|
||||
* Note that all registered DataObjects need the method AbsoluteLink().
|
||||
*
|
||||
* @param string $className name of DataObject to register
|
||||
* @param string $changeFreq how often is this DataObject updated?
|
||||
* Possible values:
|
||||
* always, hourly, daily, weekly, monthly, yearly, never
|
||||
* @param string $priority How important is this DataObject in comparison to other urls?
|
||||
* Possible values: 0.1, 0.2 ... , 0.9, 1.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function register_dataobject($className, $changeFreq = 'monthly', $priority = '0.6')
|
||||
{
|
||||
if (!self::is_registered($className)) {
|
||||
$className::add_extension('GoogleSitemapExtension');
|
||||
|
||||
self::$dataobjects[$className] = array(
|
||||
'frequency' => ($changeFreq) ? $changeFreq : 'monthly',
|
||||
'priority' => ($priority) ? $priority : '0.6'
|
||||
);
|
||||
}
|
||||
}
|
||||
self::$dataobjects[$className] = array(
|
||||
'frequency' => ($changeFreq) ? $changeFreq : 'monthly',
|
||||
'priority' => ($priority) ? $priority : '0.6'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers multiple dataobjects in a single line. See {@link register_dataobject}
|
||||
* for the heavy lifting
|
||||
*
|
||||
* @param array $dataobjects array of class names of DataObject to register
|
||||
* @param string $changeFreq how often is this DataObject updated?
|
||||
* Possible values:
|
||||
* always, hourly, daily, weekly, monthly, yearly, never
|
||||
* @param string $priority How important is this DataObject in comparison to other urls?
|
||||
* Possible values: 0.1, 0.2 ... , 0.9, 1.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function register_dataobjects($dataobjects, $changeFreq = 'monthly', $priority = '0.6') {
|
||||
foreach($dataobjects as $obj) {
|
||||
self::register_dataobject($obj, $changeFreq, $priority);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Registers multiple dataobjects in a single line. See {@link register_dataobject}
|
||||
* for the heavy lifting
|
||||
*
|
||||
* @param array $dataobjects array of class names of DataObject to register
|
||||
* @param string $changeFreq how often is this DataObject updated?
|
||||
* Possible values:
|
||||
* always, hourly, daily, weekly, monthly, yearly, never
|
||||
* @param string $priority How important is this DataObject in comparison to other urls?
|
||||
* Possible values: 0.1, 0.2 ... , 0.9, 1.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function register_dataobjects($dataobjects, $changeFreq = 'monthly', $priority = '0.6')
|
||||
{
|
||||
foreach ($dataobjects as $obj) {
|
||||
self::register_dataobject($obj, $changeFreq, $priority);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given class name is already registered or not.
|
||||
*
|
||||
* @param string $className Name of DataObject to check
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_registered($className) {
|
||||
return isset(self::$dataobjects[$className]);
|
||||
}
|
||||
/**
|
||||
* Checks whether the given class name is already registered or not.
|
||||
*
|
||||
* @param string $className Name of DataObject to check
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_registered($className)
|
||||
{
|
||||
return isset(self::$dataobjects[$className]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters a class from the sitemap. Mostly used for the test suite
|
||||
*
|
||||
* @param string
|
||||
*/
|
||||
public static function unregister_dataobject($className) {
|
||||
unset(self::$dataobjects[$className]);
|
||||
}
|
||||
/**
|
||||
* Unregisters a class from the sitemap. Mostly used for the test suite
|
||||
*
|
||||
* @param string
|
||||
*/
|
||||
public static function unregister_dataobject($className)
|
||||
{
|
||||
unset(self::$dataobjects[$className]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears registered {@link DataObjects}. Useful for unit tests.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function clear_registered_dataobjects() {
|
||||
self::$dataobjects = array();
|
||||
}
|
||||
/**
|
||||
* Clears registered {@link DataObjects}. Useful for unit tests.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function clear_registered_dataobjects()
|
||||
{
|
||||
self::$dataobjects = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a given route to the sitemap list
|
||||
*
|
||||
* @param string
|
||||
* @param string
|
||||
* @param string
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function register_route($route, $changeFreq = 'monthly', $priority = '0.6') {
|
||||
self::$routes = array_merge(self::$routes, array(
|
||||
$route => array(
|
||||
'frequency' => ($changeFreq) ? $changeFreq : 'monthly',
|
||||
'priority' => ($priority) ? $priority : '0.6'
|
||||
)
|
||||
));
|
||||
}
|
||||
/**
|
||||
* Register a given route to the sitemap list
|
||||
*
|
||||
* @param string
|
||||
* @param string
|
||||
* @param string
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function register_route($route, $changeFreq = 'monthly', $priority = '0.6')
|
||||
{
|
||||
self::$routes = array_merge(self::$routes, array(
|
||||
$route => array(
|
||||
'frequency' => ($changeFreq) ? $changeFreq : 'monthly',
|
||||
'priority' => ($priority) ? $priority : '0.6'
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a given list of relative urls. Will be merged with the current
|
||||
* registered routes. If you want to replace them, please call {@link clear_routes}
|
||||
*
|
||||
* @param array
|
||||
* @param string
|
||||
* @param string
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function register_routes($routes, $changeFreq = 'monthly', $priority = '0.6') {
|
||||
foreach($routes as $route) {
|
||||
self::register_route($route, $changeFreq, $priority);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Registers a given list of relative urls. Will be merged with the current
|
||||
* registered routes. If you want to replace them, please call {@link clear_routes}
|
||||
*
|
||||
* @param array
|
||||
* @param string
|
||||
* @param string
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function register_routes($routes, $changeFreq = 'monthly', $priority = '0.6')
|
||||
{
|
||||
foreach ($routes as $route) {
|
||||
self::register_route($route, $changeFreq, $priority);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears registered routes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function clear_registered_routes() {
|
||||
self::$routes = array();
|
||||
}
|
||||
/**
|
||||
* Clears registered routes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function clear_registered_routes()
|
||||
{
|
||||
self::$routes = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the list of data to include in the rendered sitemap. Links
|
||||
* can include pages from the website, dataobjects (such as forum posts)
|
||||
* as well as custom registered paths.
|
||||
*
|
||||
* @param string
|
||||
* @param int
|
||||
*
|
||||
* @return ArrayList
|
||||
*/
|
||||
public function getItems($class, $page = 1) {
|
||||
//normalise the class name
|
||||
try {
|
||||
$reflectionClass = new ReflectionClass($class);
|
||||
$class = $reflectionClass->getName();
|
||||
} catch (ReflectionException $e) {
|
||||
// this can happen when $class is GoogleSitemapRoute
|
||||
//we should try to carry on
|
||||
}
|
||||
/**
|
||||
* Constructs the list of data to include in the rendered sitemap. Links
|
||||
* can include pages from the website, dataobjects (such as forum posts)
|
||||
* as well as custom registered paths.
|
||||
*
|
||||
* @param string
|
||||
* @param int
|
||||
*
|
||||
* @return ArrayList
|
||||
*/
|
||||
public function getItems($class, $page = 1)
|
||||
{
|
||||
//normalise the class name
|
||||
try {
|
||||
$reflectionClass = new ReflectionClass($class);
|
||||
$class = $reflectionClass->getName();
|
||||
} catch (ReflectionException $e) {
|
||||
// this can happen when $class is GoogleSitemapRoute
|
||||
//we should try to carry on
|
||||
}
|
||||
|
||||
$output = new ArrayList();
|
||||
$count = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
|
||||
$filter = Config::inst()->get('GoogleSitemap', 'use_show_in_search');
|
||||
$output = new ArrayList();
|
||||
$count = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
|
||||
$filter = Config::inst()->get('GoogleSitemap', 'use_show_in_search');
|
||||
|
||||
// todo migrate to extension hook or DI point for other modules to
|
||||
// modify state filters
|
||||
if(class_exists('Translatable')) {
|
||||
Translatable::disable_locale_filter();
|
||||
}
|
||||
// todo migrate to extension hook or DI point for other modules to
|
||||
// modify state filters
|
||||
if (class_exists('Translatable')) {
|
||||
Translatable::disable_locale_filter();
|
||||
}
|
||||
|
||||
if($class == "SiteTree") {
|
||||
$filter = ($filter) ? "\"ShowInSearch\" = 1" : "";
|
||||
$instances = Versioned::get_by_stage('SiteTree', 'Live', $filter);
|
||||
}
|
||||
else if($class == "GoogleSitemapRoute") {
|
||||
$instances = array_slice(self::$routes, ($page - 1) * $count, $count);
|
||||
$output = new ArrayList();
|
||||
if ($class == "SiteTree") {
|
||||
$filter = ($filter) ? "\"ShowInSearch\" = 1" : "";
|
||||
$instances = Versioned::get_by_stage('SiteTree', 'Live', $filter);
|
||||
} elseif ($class == "GoogleSitemapRoute") {
|
||||
$instances = array_slice(self::$routes, ($page - 1) * $count, $count);
|
||||
$output = new ArrayList();
|
||||
|
||||
if($instances) {
|
||||
foreach($instances as $route => $config) {
|
||||
$output->push(new ArrayData(array(
|
||||
'AbsoluteLink' => Director::absoluteURL($route),
|
||||
'ChangeFrequency' => $config['frequency'],
|
||||
'GooglePriority' => $config['priority']
|
||||
)));
|
||||
}
|
||||
}
|
||||
if ($instances) {
|
||||
foreach ($instances as $route => $config) {
|
||||
$output->push(new ArrayData(array(
|
||||
'AbsoluteLink' => Director::absoluteURL($route),
|
||||
'ChangeFrequency' => $config['frequency'],
|
||||
'GooglePriority' => $config['priority']
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
else {
|
||||
$instances = new DataList($class);
|
||||
}
|
||||
return $output;
|
||||
} else {
|
||||
$instances = new DataList($class);
|
||||
}
|
||||
|
||||
$this->extend("alterDataList", $instances, $class);
|
||||
$this->extend("alterDataList", $instances, $class);
|
||||
|
||||
$instances = $instances->limit(
|
||||
$count,
|
||||
($page - 1) * $count
|
||||
);
|
||||
$instances = $instances->limit(
|
||||
$count,
|
||||
($page - 1) * $count
|
||||
);
|
||||
|
||||
if($instances) {
|
||||
foreach($instances as $obj) {
|
||||
if($obj->canIncludeInGoogleSitemap()) {
|
||||
$output->push($obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($instances) {
|
||||
foreach ($instances as $obj) {
|
||||
if ($obj->canIncludeInGoogleSitemap()) {
|
||||
$output->push($obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static interface to instance level ->getItems() for backward compatibility.
|
||||
*
|
||||
* @param string
|
||||
* @param int
|
||||
*
|
||||
* @return ArrayList
|
||||
* @deprecated Please create an instance and call ->getSitemaps() instead.
|
||||
*/
|
||||
public static function get_items($class, $page = 1) {
|
||||
return static::inst()->getItems($class, $page);
|
||||
}
|
||||
/**
|
||||
* Static interface to instance level ->getItems() for backward compatibility.
|
||||
*
|
||||
* @param string
|
||||
* @param int
|
||||
*
|
||||
* @return ArrayList
|
||||
* @deprecated Please create an instance and call ->getSitemaps() instead.
|
||||
*/
|
||||
public static function get_items($class, $page = 1)
|
||||
{
|
||||
return static::inst()->getItems($class, $page);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the string frequency of edits for a particular dataobject class.
|
||||
*
|
||||
* Frequency for {@link SiteTree} objects can be determined from the version
|
||||
* history.
|
||||
*
|
||||
* @param string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_frequency_for_class($class) {
|
||||
foreach(self::$dataobjects as $type => $config) {
|
||||
if($class == $type) {
|
||||
return $config['frequency'];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns the string frequency of edits for a particular dataobject class.
|
||||
*
|
||||
* Frequency for {@link SiteTree} objects can be determined from the version
|
||||
* history.
|
||||
*
|
||||
* @param string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_frequency_for_class($class)
|
||||
{
|
||||
foreach (self::$dataobjects as $type => $config) {
|
||||
if ($class == $type) {
|
||||
return $config['frequency'];
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default priority of edits for a particular dataobject class.
|
||||
*
|
||||
* @param string
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public static function get_priority_for_class($class) {
|
||||
foreach(self::$dataobjects as $type => $config) {
|
||||
if($class == $type) {
|
||||
return $config['priority'];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns the default priority of edits for a particular dataobject class.
|
||||
*
|
||||
* @param string
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public static function get_priority_for_class($class)
|
||||
{
|
||||
foreach (self::$dataobjects as $type => $config) {
|
||||
if ($class == $type) {
|
||||
return $config['priority'];
|
||||
}
|
||||
}
|
||||
|
||||
return 0.5;
|
||||
}
|
||||
return 0.5;
|
||||
}
|
||||
|
||||
/**
|
||||
* The google site map is broken down into multiple smaller files to
|
||||
* prevent overbearing a server. By default separate {@link DataObject}
|
||||
* records are keep in separate files and broken down into chunks.
|
||||
*
|
||||
* @return ArrayList
|
||||
*/
|
||||
public function getSitemaps() {
|
||||
$countPerFile = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
|
||||
$sitemaps = new ArrayList();
|
||||
$filter = Config::inst()->get('GoogleSitemap', 'use_show_in_search');
|
||||
/**
|
||||
* The google site map is broken down into multiple smaller files to
|
||||
* prevent overbearing a server. By default separate {@link DataObject}
|
||||
* records are keep in separate files and broken down into chunks.
|
||||
*
|
||||
* @return ArrayList
|
||||
*/
|
||||
public function getSitemaps()
|
||||
{
|
||||
$countPerFile = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
|
||||
$sitemaps = new ArrayList();
|
||||
$filter = Config::inst()->get('GoogleSitemap', 'use_show_in_search');
|
||||
|
||||
if(class_exists('SiteTree')) {
|
||||
// move to extension hook. At the moment moduleexists config hook
|
||||
// does not work.
|
||||
if(class_exists('Translatable')) {
|
||||
Translatable::disable_locale_filter();
|
||||
}
|
||||
if (class_exists('SiteTree')) {
|
||||
// move to extension hook. At the moment moduleexists config hook
|
||||
// does not work.
|
||||
if (class_exists('Translatable')) {
|
||||
Translatable::disable_locale_filter();
|
||||
}
|
||||
|
||||
$filter = ($filter) ? "\"ShowInSearch\" = 1" : "";
|
||||
$class = 'SiteTree';
|
||||
$instances = Versioned::get_by_stage($class, 'Live', $filter);
|
||||
$this->extend("alterDataList", $instances, $class);
|
||||
$count = $instances->count();
|
||||
$filter = ($filter) ? "\"ShowInSearch\" = 1" : "";
|
||||
$class = 'SiteTree';
|
||||
$instances = Versioned::get_by_stage($class, 'Live', $filter);
|
||||
$this->extend("alterDataList", $instances, $class);
|
||||
$count = $instances->count();
|
||||
|
||||
$neededForPage = ceil($count / $countPerFile);
|
||||
$neededForPage = ceil($count / $countPerFile);
|
||||
|
||||
for($i = 1; $i <= $neededForPage; $i++) {
|
||||
for ($i = 1; $i <= $neededForPage; $i++) {
|
||||
$lastEdited = $instances
|
||||
->limit($countPerFile, ($i - 1) * $countPerFile)
|
||||
->sort(array())
|
||||
->max('LastEdited');
|
||||
|
||||
$lastEdited = $instances
|
||||
->limit($countPerFile, ($i - 1) * $countPerFile)
|
||||
->sort(array())
|
||||
->max('LastEdited');
|
||||
$lastModified = ($lastEdited) ? date('Y-m-d', strtotime($lastEdited)) : date('Y-m-d');
|
||||
|
||||
$lastModified = ($lastEdited) ? date('Y-m-d', strtotime($lastEdited)) : date('Y-m-d');
|
||||
$sitemaps->push(new ArrayData(array(
|
||||
'ClassName' => 'SiteTree',
|
||||
'LastModified' => $lastModified,
|
||||
'Page' => $i
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
$sitemaps->push(new ArrayData(array(
|
||||
'ClassName' => 'SiteTree',
|
||||
'LastModified' => $lastModified,
|
||||
'Page' => $i
|
||||
)));
|
||||
}
|
||||
}
|
||||
if (count(self::$dataobjects) > 0) {
|
||||
foreach (self::$dataobjects as $class => $config) {
|
||||
$list = new DataList($class);
|
||||
$list = $list->sort('LastEdited ASC');
|
||||
$this->extend("alterDataList", $list, $class);
|
||||
$neededForClass = ceil($list->count() / $countPerFile);
|
||||
|
||||
if(count(self::$dataobjects) > 0) {
|
||||
foreach(self::$dataobjects as $class => $config) {
|
||||
$list = new DataList($class);
|
||||
$list = $list->sort('LastEdited ASC');
|
||||
$this->extend("alterDataList", $list, $class);
|
||||
$neededForClass = ceil($list->count() / $countPerFile);
|
||||
for ($i = 1; $i <= $neededForClass; $i++) {
|
||||
// determine the last modified date for this slice
|
||||
$sliced = $list
|
||||
->limit($countPerFile, ($i - 1) * $countPerFile)
|
||||
->last();
|
||||
|
||||
for($i = 1; $i <= $neededForClass; $i++) {
|
||||
// determine the last modified date for this slice
|
||||
$sliced = $list
|
||||
->limit($countPerFile, ($i - 1) * $countPerFile)
|
||||
->last();
|
||||
$lastModified = ($sliced) ? $sliced->dbObject('LastEdited')->Format('Y-m-d') : date('Y-m-d');
|
||||
|
||||
$lastModified = ($sliced) ? $sliced->dbObject('LastEdited')->Format('Y-m-d') : date('Y-m-d');
|
||||
$sitemaps->push(new ArrayData(array(
|
||||
'ClassName' => $class,
|
||||
'Page' => $i,
|
||||
'LastModified' => $lastModified
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$sitemaps->push(new ArrayData(array(
|
||||
'ClassName' => $class,
|
||||
'Page' => $i,
|
||||
'LastModified' => $lastModified
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count(self::$routes) > 0) {
|
||||
$needed = ceil(count(self::$routes) / $countPerFile);
|
||||
|
||||
if(count(self::$routes) > 0) {
|
||||
$needed = ceil(count(self::$routes) / $countPerFile);
|
||||
for ($i = 1; $i <= $needed; $i++) {
|
||||
$sitemaps->push(new ArrayData(array(
|
||||
'ClassName' => 'GoogleSitemapRoute',
|
||||
'Page' => $i
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
for($i = 1; $i <= $needed; $i++) {
|
||||
$sitemaps->push(new ArrayData(array(
|
||||
'ClassName' => 'GoogleSitemapRoute',
|
||||
'Page' => $i
|
||||
)));
|
||||
}
|
||||
}
|
||||
return $sitemaps;
|
||||
}
|
||||
|
||||
return $sitemaps;
|
||||
}
|
||||
/**
|
||||
* Static interface to instance level ->getSitemaps() for backward compatibility.
|
||||
*
|
||||
* @return ArrayList
|
||||
* @deprecated Please create an instance and call ->getSitemaps() instead.
|
||||
*/
|
||||
public static function get_sitemaps()
|
||||
{
|
||||
return static::inst()->getSitemaps();
|
||||
}
|
||||
|
||||
/**
|
||||
* Static interface to instance level ->getSitemaps() for backward compatibility.
|
||||
*
|
||||
* @return ArrayList
|
||||
* @deprecated Please create an instance and call ->getSitemaps() instead.
|
||||
*/
|
||||
public static function get_sitemaps() {
|
||||
return static::inst()->getSitemaps();
|
||||
}
|
||||
/**
|
||||
* Notifies Google 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 Google of the update.
|
||||
*
|
||||
* If the site is in development mode no ping will be sent regardless whether
|
||||
* the Google notification is enabled.
|
||||
*
|
||||
* @return string Response text
|
||||
*/
|
||||
public static function ping()
|
||||
{
|
||||
if (!self::enabled()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies Google 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 Google of the update.
|
||||
*
|
||||
* If the site is in development mode no ping will be sent regardless whether
|
||||
* the Google notification is enabled.
|
||||
*
|
||||
* @return string Response text
|
||||
*/
|
||||
public static function ping() {
|
||||
if(!self::enabled()) {
|
||||
return false;
|
||||
}
|
||||
// Don't ping if the site has disabled it, or if the site is in dev mode
|
||||
$active = Config::inst()->get('GoogleSitemap', 'google_notification_enabled');
|
||||
|
||||
// Don't ping if the site has disabled it, or if the site is in dev mode
|
||||
$active = Config::inst()->get('GoogleSitemap', 'google_notification_enabled');
|
||||
if (!$active || Director::isDev()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!$active || Director::isDev()) {
|
||||
return;
|
||||
}
|
||||
$location = urlencode(Controller::join_links(
|
||||
Director::absoluteBaseURL(),
|
||||
'sitemap.xml'
|
||||
));
|
||||
|
||||
$location = urlencode(Controller::join_links(
|
||||
Director::absoluteBaseURL(),
|
||||
'sitemap.xml'
|
||||
));
|
||||
$response = self::send_ping(
|
||||
"www.google.com", "/webmasters/sitemaps/ping", sprintf("sitemap=%s", $location)
|
||||
);
|
||||
|
||||
$response = self::send_ping(
|
||||
"www.google.com", "/webmasters/sitemaps/ping", sprintf("sitemap=%s", $location)
|
||||
);
|
||||
return $response;
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
/**
|
||||
* Send an HTTP request to the host.
|
||||
*
|
||||
* @return String Response text
|
||||
*/
|
||||
protected static function send_ping($host, $path, $query)
|
||||
{
|
||||
$socket = fsockopen($host, 80, $errno, $error);
|
||||
if (!$socket) {
|
||||
return $error;
|
||||
}
|
||||
if ($query) {
|
||||
$query = '?' . $query;
|
||||
}
|
||||
$request = "GET {$path}{$query} HTTP/1.1\r\nHost: $host\r\nConnection: Close\r\n\r\n";
|
||||
fwrite($socket, $request);
|
||||
$response = stream_get_contents($socket);
|
||||
|
||||
/**
|
||||
* Send an HTTP request to the host.
|
||||
*
|
||||
* @return String Response text
|
||||
*/
|
||||
protected static function send_ping($host, $path, $query) {
|
||||
$socket = fsockopen($host, 80, $errno, $error);
|
||||
if (!$socket) {
|
||||
return $error;
|
||||
}
|
||||
if ($query) {
|
||||
$query = '?' . $query;
|
||||
}
|
||||
$request = "GET {$path}{$query} HTTP/1.1\r\nHost: $host\r\nConnection: Close\r\n\r\n";
|
||||
fwrite($socket, $request);
|
||||
$response = stream_get_contents($socket);
|
||||
return $response;
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is GoogleSitemap enabled?
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function enabled() {
|
||||
return (Config::inst()->get('GoogleSitemap', 'enabled', Config::INHERITED));
|
||||
}
|
||||
/**
|
||||
* Is GoogleSitemap enabled?
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function enabled()
|
||||
{
|
||||
return (Config::inst()->get('GoogleSitemap', 'enabled', Config::INHERITED));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convenience method for manufacturing an instance for hew instance-level methods (and for easier type definition).
|
||||
*
|
||||
* @return GoogleSitemap
|
||||
*/
|
||||
public static function inst() {
|
||||
return GoogleSitemap::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for manufacturing an instance for hew instance-level methods (and for easier type definition).
|
||||
*
|
||||
* @return GoogleSitemap
|
||||
*/
|
||||
public static function inst()
|
||||
{
|
||||
return GoogleSitemap::create();
|
||||
}
|
||||
}
|
||||
|
@ -12,65 +12,68 @@
|
||||
*
|
||||
* @package googlesitemaps
|
||||
*/
|
||||
class GoogleSitemapController extends Controller {
|
||||
class GoogleSitemapController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $allowed_actions = array(
|
||||
'index',
|
||||
'sitemap'
|
||||
);
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $allowed_actions = array(
|
||||
'index',
|
||||
'sitemap'
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Default controller action for the sitemap.xml file. Renders a index
|
||||
* file containing a list of links to sub sitemaps containing the data.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function index($url) {
|
||||
if(GoogleSitemap::enabled()) {
|
||||
Config::inst()->update('SSViewer', 'set_source_file_comments', false);
|
||||
/**
|
||||
* Default controller action for the sitemap.xml file. Renders a index
|
||||
* file containing a list of links to sub sitemaps containing the data.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function index($url)
|
||||
{
|
||||
if (GoogleSitemap::enabled()) {
|
||||
Config::inst()->update('SSViewer', 'set_source_file_comments', false);
|
||||
|
||||
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
|
||||
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
|
||||
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
|
||||
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
|
||||
|
||||
$sitemaps = GoogleSitemap::inst()->getSitemaps();
|
||||
$this->extend('updateGoogleSitemaps', $sitemaps);
|
||||
$sitemaps = GoogleSitemap::inst()->getSitemaps();
|
||||
$this->extend('updateGoogleSitemaps', $sitemaps);
|
||||
|
||||
return array(
|
||||
'Sitemaps' => $sitemaps
|
||||
);
|
||||
} else {
|
||||
return new SS_HTTPResponse('Page not found', 404);
|
||||
}
|
||||
}
|
||||
return array(
|
||||
'Sitemaps' => $sitemaps
|
||||
);
|
||||
} else {
|
||||
return new SS_HTTPResponse('Page not found', 404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Specific controller action for displaying a particular list of links
|
||||
* for a class
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function sitemap() {
|
||||
$class = $this->request->param('ID');
|
||||
$page = $this->request->param('OtherID');
|
||||
/**
|
||||
* Specific controller action for displaying a particular list of links
|
||||
* for a class
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function sitemap()
|
||||
{
|
||||
$class = $this->request->param('ID');
|
||||
$page = $this->request->param('OtherID');
|
||||
|
||||
if(GoogleSitemap::enabled() && $class && $page) {
|
||||
Config::inst()->update('SSViewer', 'set_source_file_comments', false);
|
||||
if (GoogleSitemap::enabled() && $class && $page) {
|
||||
Config::inst()->update('SSViewer', 'set_source_file_comments', false);
|
||||
|
||||
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
|
||||
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
|
||||
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
|
||||
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
|
||||
|
||||
$items = GoogleSitemap::inst()->getItems($class, $page);
|
||||
$this->extend('updateGoogleSitemapItems', $items, $class, $page);
|
||||
$items = GoogleSitemap::inst()->getItems($class, $page);
|
||||
$this->extend('updateGoogleSitemapItems', $items, $class, $page);
|
||||
|
||||
return array(
|
||||
'Items' => $items
|
||||
);
|
||||
} else {
|
||||
return new SS_HTTPResponse('Page not found', 404);
|
||||
}
|
||||
}
|
||||
return array(
|
||||
'Items' => $items
|
||||
);
|
||||
} else {
|
||||
return new SS_HTTPResponse('Page not found', 404);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,111 +6,117 @@
|
||||
*
|
||||
* @package googlesitemaps
|
||||
*/
|
||||
class GoogleSitemapExtension extends DataExtension {
|
||||
class GoogleSitemapExtension extends DataExtension
|
||||
{
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function canIncludeInGoogleSitemap() {
|
||||
$can = true;
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function canIncludeInGoogleSitemap()
|
||||
{
|
||||
$can = true;
|
||||
|
||||
if($this->owner->hasMethod('AbsoluteLink')) {
|
||||
$hostHttp = parse_url(Director::protocolAndHost(), PHP_URL_HOST);
|
||||
$objHttp = parse_url($this->owner->AbsoluteLink(), PHP_URL_HOST);
|
||||
if ($this->owner->hasMethod('AbsoluteLink')) {
|
||||
$hostHttp = parse_url(Director::protocolAndHost(), PHP_URL_HOST);
|
||||
$objHttp = parse_url($this->owner->AbsoluteLink(), PHP_URL_HOST);
|
||||
|
||||
if($objHttp != $hostHttp) {
|
||||
$can = false;
|
||||
}
|
||||
}
|
||||
if ($objHttp != $hostHttp) {
|
||||
$can = false;
|
||||
}
|
||||
}
|
||||
|
||||
if($can) {
|
||||
$can = $this->owner->canView();
|
||||
}
|
||||
if ($can) {
|
||||
$can = $this->owner->canView();
|
||||
}
|
||||
|
||||
if($can) {
|
||||
$can = $this->owner->getGooglePriority();
|
||||
}
|
||||
if ($can) {
|
||||
$can = $this->owner->getGooglePriority();
|
||||
}
|
||||
|
||||
$this->owner->invokeWithExtensions('alterCanIncludeInGoogleSitemap', $can);
|
||||
$this->owner->invokeWithExtensions('alterCanIncludeInGoogleSitemap', $can);
|
||||
|
||||
return $can;
|
||||
}
|
||||
return $can;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function onAfterPublish() {
|
||||
GoogleSitemap::ping();
|
||||
}
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function onAfterPublish()
|
||||
{
|
||||
GoogleSitemap::ping();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function onAfterUnpublish() {
|
||||
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.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getGooglePriority() {
|
||||
$field = $this->owner->hasField('Priority');
|
||||
/**
|
||||
* The default value of the priority field depends on the depth of the page in
|
||||
* the site tree, so it must be calculated dynamically.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getGooglePriority()
|
||||
{
|
||||
$field = $this->owner->hasField('Priority');
|
||||
|
||||
if(isset($this->Priority) || ($field && $this->Priority = $this->owner->getField('Priority'))) {
|
||||
return ($this->Priority < 0) ? false : $this->Priority;
|
||||
}
|
||||
if (isset($this->Priority) || ($field && $this->Priority = $this->owner->getField('Priority'))) {
|
||||
return ($this->Priority < 0) ? false : $this->Priority;
|
||||
}
|
||||
|
||||
return GoogleSitemap::get_priority_for_class($this->owner->class);
|
||||
}
|
||||
return GoogleSitemap::get_priority_for_class($this->owner->class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pages change frequency calculated by pages age and number of
|
||||
* versions. Google expects always, hourly, daily, weekly, monthly, yearly
|
||||
* or never as values.
|
||||
*
|
||||
* @see http://support.google.com/webmasters/bin/answer.py?hl=en&answer=183668&topic=8476&ctx=topic
|
||||
*
|
||||
* @return SS_Datetime
|
||||
*/
|
||||
public function getChangeFrequency() {
|
||||
if($freq = GoogleSitemap::get_frequency_for_class($this->owner->class)) {
|
||||
return $freq;
|
||||
}
|
||||
/**
|
||||
* Returns a pages change frequency calculated by pages age and number of
|
||||
* versions. Google expects always, hourly, daily, weekly, monthly, yearly
|
||||
* or never as values.
|
||||
*
|
||||
* @see http://support.google.com/webmasters/bin/answer.py?hl=en&answer=183668&topic=8476&ctx=topic
|
||||
*
|
||||
* @return SS_Datetime
|
||||
*/
|
||||
public function getChangeFrequency()
|
||||
{
|
||||
if ($freq = GoogleSitemap::get_frequency_for_class($this->owner->class)) {
|
||||
return $freq;
|
||||
}
|
||||
|
||||
$date = date('Y-m-d H:i:s');
|
||||
$date = date('Y-m-d H:i:s');
|
||||
|
||||
$created = new SS_Datetime();
|
||||
$created->value = ($this->owner->Created) ? $this->owner->Created : $date;
|
||||
$created = new SS_Datetime();
|
||||
$created->value = ($this->owner->Created) ? $this->owner->Created : $date;
|
||||
|
||||
$now = new SS_Datetime();
|
||||
$now->value = $date;
|
||||
$now = new SS_Datetime();
|
||||
$now->value = $date;
|
||||
|
||||
$versions = ($this->owner->Version) ? $this->owner->Version : 1;
|
||||
$timediff = $now->format('U') - $created->format('U');
|
||||
$versions = ($this->owner->Version) ? $this->owner->Version : 1;
|
||||
$timediff = $now->format('U') - $created->format('U');
|
||||
|
||||
// Check how many revisions have been made over the lifetime of the
|
||||
// Page for a rough estimate of it's changing frequency.
|
||||
$period = $timediff / ($versions + 1);
|
||||
// Check how many revisions have been made over the lifetime of the
|
||||
// Page for a rough estimate of it's changing frequency.
|
||||
$period = $timediff / ($versions + 1);
|
||||
|
||||
if ($period > 60 * 60 * 24 * 365) {
|
||||
$freq = 'yearly';
|
||||
} elseif ($period > 60 * 60 * 24 * 30) {
|
||||
$freq = 'monthly';
|
||||
} elseif ($period > 60 * 60 * 24 * 7) {
|
||||
$freq = 'weekly';
|
||||
} elseif ($period > 60 * 60 * 24) {
|
||||
$freq = 'daily';
|
||||
} elseif ($period > 60 * 60) {
|
||||
$freq = 'hourly';
|
||||
} else {
|
||||
$freq = 'always';
|
||||
}
|
||||
if ($period > 60 * 60 * 24 * 365) {
|
||||
$freq = 'yearly';
|
||||
} elseif ($period > 60 * 60 * 24 * 30) {
|
||||
$freq = 'monthly';
|
||||
} elseif ($period > 60 * 60 * 24 * 7) {
|
||||
$freq = 'weekly';
|
||||
} elseif ($period > 60 * 60 * 24) {
|
||||
$freq = 'daily';
|
||||
} elseif ($period > 60 * 60) {
|
||||
$freq = 'hourly';
|
||||
} else {
|
||||
$freq = 'always';
|
||||
}
|
||||
|
||||
return $freq;
|
||||
}
|
||||
return $freq;
|
||||
}
|
||||
}
|
||||
|
@ -3,111 +3,123 @@
|
||||
/**
|
||||
* @package googlesitemaps
|
||||
*/
|
||||
class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension {
|
||||
class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $db = array(
|
||||
"Priority" => "Varchar(5)"
|
||||
);
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $db = array(
|
||||
"Priority" => "Varchar(5)"
|
||||
);
|
||||
|
||||
/**
|
||||
* @param FieldList
|
||||
*/
|
||||
public function updateSettingsFields(&$fields) {
|
||||
$prorities = array(
|
||||
'-1' => _t('GoogleSitemaps.PRIORITYNOTINDEXED', "Not indexed"),
|
||||
'1.0' => '1 - ' . _t('GoogleSitemaps.PRIORITYMOSTIMPORTANT', "Most important"),
|
||||
'0.9' => '2',
|
||||
'0.8' => '3',
|
||||
'0.7' => '4',
|
||||
'0.6' => '5',
|
||||
'0.5' => '6',
|
||||
'0.4' => '7',
|
||||
'0.3' => '8',
|
||||
'0.2' => '9',
|
||||
'0.1' => '10 - ' . _t('GoogleSitemaps.PRIORITYLEASTIMPORTANT', "Least important")
|
||||
);
|
||||
/**
|
||||
* @param FieldList
|
||||
*/
|
||||
public function updateSettingsFields(&$fields)
|
||||
{
|
||||
$prorities = array(
|
||||
'-1' => _t('GoogleSitemaps.PRIORITYNOTINDEXED', "Not indexed"),
|
||||
'1.0' => '1 - ' . _t('GoogleSitemaps.PRIORITYMOSTIMPORTANT', "Most important"),
|
||||
'0.9' => '2',
|
||||
'0.8' => '3',
|
||||
'0.7' => '4',
|
||||
'0.6' => '5',
|
||||
'0.5' => '6',
|
||||
'0.4' => '7',
|
||||
'0.3' => '8',
|
||||
'0.2' => '9',
|
||||
'0.1' => '10 - ' . _t('GoogleSitemaps.PRIORITYLEASTIMPORTANT', "Least important")
|
||||
);
|
||||
|
||||
$tabset = $fields->findOrMakeTab('Root.Settings');
|
||||
$tabset = $fields->findOrMakeTab('Root.Settings');
|
||||
|
||||
$message = "<p>";
|
||||
$message .= sprintf(_t('GoogleSitemaps.METANOTEPRIORITY', "Manually specify a Google Sitemaps priority for this page (%s)"),
|
||||
'<a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=71936#prioritize" target="_blank">?</a>'
|
||||
);
|
||||
$message .= "</p>";
|
||||
$message = "<p>";
|
||||
$message .= sprintf(_t('GoogleSitemaps.METANOTEPRIORITY', "Manually specify a Google Sitemaps priority for this page (%s)"),
|
||||
'<a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=71936#prioritize" target="_blank">?</a>'
|
||||
);
|
||||
$message .= "</p>";
|
||||
|
||||
$tabset->push(new Tab('GoogleSitemap', _t('GoogleSitemaps.TABGOOGLESITEMAP', 'Google Sitemap'),
|
||||
new LiteralField("GoogleSitemapIntro", $message),
|
||||
$priority = new DropdownField("Priority", $this->owner->fieldLabel('Priority'), $prorities, $this->owner->Priority)
|
||||
));
|
||||
$tabset->push(new Tab('GoogleSitemap', _t('GoogleSitemaps.TABGOOGLESITEMAP', 'Google Sitemap'),
|
||||
new LiteralField("GoogleSitemapIntro", $message),
|
||||
$priority = new DropdownField("Priority", $this->owner->fieldLabel('Priority'), $prorities, $this->owner->Priority)
|
||||
));
|
||||
|
||||
$priority->setEmptyString(_t('GoogleSitemaps.PRIORITYAUTOSET', 'Auto-set based on page depth'));
|
||||
}
|
||||
$priority->setEmptyString(_t('GoogleSitemaps.PRIORITYAUTOSET', 'Auto-set based on page depth'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FieldList
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updateFieldLabels(&$labels) {
|
||||
parent::updateFieldLabels($labels);
|
||||
/**
|
||||
* @param FieldList
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function updateFieldLabels(&$labels)
|
||||
{
|
||||
parent::updateFieldLabels($labels);
|
||||
|
||||
$labels['Priority'] = _t('GoogleSitemaps.METAPAGEPRIO', "Page Priority");
|
||||
}
|
||||
$labels['Priority'] = _t('GoogleSitemaps.METAPAGEPRIO', "Page Priority");
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that all parent pages of this page (if any) are published
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasPublishedParent() {
|
||||
/**
|
||||
* Ensure that all parent pages of this page (if any) are published
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasPublishedParent()
|
||||
{
|
||||
|
||||
// Skip root pages
|
||||
if(empty($this->owner->ParentID)) return true;
|
||||
// Skip root pages
|
||||
if (empty($this->owner->ParentID)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Ensure direct parent exists
|
||||
$parent = $this->owner->Parent();
|
||||
if(empty($parent) || !$parent->exists()) return false;
|
||||
// Ensure direct parent exists
|
||||
$parent = $this->owner->Parent();
|
||||
if (empty($parent) || !$parent->exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check ancestry
|
||||
return $parent->hasPublishedParent();
|
||||
}
|
||||
// Check ancestry
|
||||
return $parent->hasPublishedParent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function canIncludeInGoogleSitemap() {
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function canIncludeInGoogleSitemap()
|
||||
{
|
||||
|
||||
// Check that parent page is published
|
||||
if(!$this->owner->hasPublishedParent()) return false;
|
||||
// Check that parent page is published
|
||||
if (!$this->owner->hasPublishedParent()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = parent::canIncludeInGoogleSitemap();
|
||||
$result = ($this->owner instanceof ErrorPage) ? false : $result;
|
||||
$result = parent::canIncludeInGoogleSitemap();
|
||||
$result = ($this->owner instanceof ErrorPage) ? false : $result;
|
||||
|
||||
return $result;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getGooglePriority() {
|
||||
setlocale(LC_ALL, "en_US.UTF8");
|
||||
$priority = $this->owner->getField('Priority');
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getGooglePriority()
|
||||
{
|
||||
setlocale(LC_ALL, "en_US.UTF8");
|
||||
$priority = $this->owner->getField('Priority');
|
||||
|
||||
if(!$priority) {
|
||||
$parentStack = $this->owner->parentStack();
|
||||
$numParents = is_array($parentStack) ? count($parentStack) - 1 : 0;
|
||||
if (!$priority) {
|
||||
$parentStack = $this->owner->parentStack();
|
||||
$numParents = is_array($parentStack) ? count($parentStack) - 1 : 0;
|
||||
|
||||
$num = max(0.1, 1.0 - ($numParents / 10));
|
||||
$result = str_replace(",", ".", $num);
|
||||
$num = max(0.1, 1.0 - ($numParents / 10));
|
||||
$result = str_replace(",", ".", $num);
|
||||
|
||||
return $result;
|
||||
} else if ($priority == -1) {
|
||||
return false;
|
||||
} else {
|
||||
return (is_numeric($priority) && $priority <= 1.0) ? $priority : 0.5;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
} elseif ($priority == -1) {
|
||||
return false;
|
||||
} else {
|
||||
return (is_numeric($priority) && $priority <= 1.0) ? $priority : 0.5;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,350 +6,375 @@
|
||||
* @package googlesitemaps
|
||||
* @subpackage tests
|
||||
*/
|
||||
class GoogleSitemapTest extends FunctionalTest {
|
||||
class GoogleSitemapTest extends FunctionalTest
|
||||
{
|
||||
|
||||
public static $fixture_file = 'googlesitemaps/tests/GoogleSitemapTest.yml';
|
||||
public static $fixture_file = 'googlesitemaps/tests/GoogleSitemapTest.yml';
|
||||
|
||||
protected $extraDataObjects = array(
|
||||
'GoogleSitemapTest_DataObject',
|
||||
'GoogleSitemapTest_OtherDataObject',
|
||||
'GoogleSitemapTest_UnviewableDataObject'
|
||||
);
|
||||
protected $extraDataObjects = array(
|
||||
'GoogleSitemapTest_DataObject',
|
||||
'GoogleSitemapTest_OtherDataObject',
|
||||
'GoogleSitemapTest_UnviewableDataObject'
|
||||
);
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
if(class_exists('Page')) {
|
||||
$this->loadFixture('googlesitemaps/tests/GoogleSitemapPageTest.yml');
|
||||
}
|
||||
if (class_exists('Page')) {
|
||||
$this->loadFixture('googlesitemaps/tests/GoogleSitemapPageTest.yml');
|
||||
}
|
||||
|
||||
GoogleSitemap::clear_registered_dataobjects();
|
||||
GoogleSitemap::clear_registered_routes();
|
||||
}
|
||||
GoogleSitemap::clear_registered_dataobjects();
|
||||
GoogleSitemap::clear_registered_routes();
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
parent::tearDown();
|
||||
public function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
GoogleSitemap::clear_registered_dataobjects();
|
||||
GoogleSitemap::clear_registered_routes();
|
||||
}
|
||||
GoogleSitemap::clear_registered_dataobjects();
|
||||
GoogleSitemap::clear_registered_routes();
|
||||
}
|
||||
|
||||
public function testIndexFileWithCustomRoute() {
|
||||
GoogleSitemap::register_route('/test/');
|
||||
public function testIndexFileWithCustomRoute()
|
||||
{
|
||||
GoogleSitemap::register_route('/test/');
|
||||
|
||||
$response = $this->get('sitemap.xml');
|
||||
$body = $response->getBody();
|
||||
$response = $this->get('sitemap.xml');
|
||||
$body = $response->getBody();
|
||||
|
||||
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapRoute/1") ."</loc>";
|
||||
$this->assertEquals(1, substr_count($body, $expected) , '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() {
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject", '');
|
||||
public function testGetItems()
|
||||
{
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject", '');
|
||||
|
||||
$items = GoogleSitemap::get_items('GoogleSitemapTest_DataObject', 1);
|
||||
$this->assertEquals(2, $items->count());
|
||||
$items = GoogleSitemap::get_items('GoogleSitemapTest_DataObject', 1);
|
||||
$this->assertEquals(2, $items->count());
|
||||
|
||||
$this->assertDOSEquals(array(
|
||||
array("Priority" => "0.2"),
|
||||
array("Priority" => "0.4")
|
||||
), $items);
|
||||
$this->assertDOSEquals(array(
|
||||
array("Priority" => "0.2"),
|
||||
array("Priority" => "0.4")
|
||||
), $items);
|
||||
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_OtherDataObject");
|
||||
$this->assertEquals(1, GoogleSitemap::get_items('GoogleSitemapTest_OtherDataObject', 1)->count());
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_OtherDataObject");
|
||||
$this->assertEquals(1, GoogleSitemap::get_items('GoogleSitemapTest_OtherDataObject', 1)->count());
|
||||
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_UnviewableDataObject");
|
||||
$this->assertEquals(0, GoogleSitemap::get_items('GoogleSitemapTest_UnviewableDataObject', 1)->count());
|
||||
}
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_UnviewableDataObject");
|
||||
$this->assertEquals(0, GoogleSitemap::get_items('GoogleSitemapTest_UnviewableDataObject', 1)->count());
|
||||
}
|
||||
|
||||
public function testGetItemsWithCustomRoutes() {
|
||||
GoogleSitemap::register_routes(array(
|
||||
'/test-route/',
|
||||
'/someother-route/',
|
||||
'/fake-sitemap-route/'
|
||||
));
|
||||
public function testGetItemsWithCustomRoutes()
|
||||
{
|
||||
GoogleSitemap::register_routes(array(
|
||||
'/test-route/',
|
||||
'/someother-route/',
|
||||
'/fake-sitemap-route/'
|
||||
));
|
||||
|
||||
$items = GoogleSitemap::get_items('GoogleSitemapRoute', 1);
|
||||
$this->assertEquals(3, $items->count());
|
||||
}
|
||||
$items = GoogleSitemap::get_items('GoogleSitemapRoute', 1);
|
||||
$this->assertEquals(3, $items->count());
|
||||
}
|
||||
|
||||
public function testAccessingSitemapRootXMLFile() {
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_OtherDataObject");
|
||||
public function testAccessingSitemapRootXMLFile()
|
||||
{
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_OtherDataObject");
|
||||
|
||||
$response = $this->get('sitemap.xml');
|
||||
$body = $response->getBody();
|
||||
$response = $this->get('sitemap.xml');
|
||||
$body = $response->getBody();
|
||||
|
||||
// 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/GoogleSitemapTest_DataObject/1") ."</loc>";
|
||||
$this->assertEquals(1, substr_count($body, $expected) , 'A link to GoogleSitemapTest_DataObject exists');
|
||||
// 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/GoogleSitemapTest_DataObject/1") ."</loc>";
|
||||
$this->assertEquals(1, substr_count($body, $expected), 'A link to GoogleSitemapTest_DataObject exists');
|
||||
|
||||
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_OtherDataObject/1") ."</loc>";
|
||||
$this->assertEquals(1, substr_count($body, $expected) , 'A link to GoogleSitemapTest_OtherDataObject exists');
|
||||
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_OtherDataObject/1") ."</loc>";
|
||||
$this->assertEquals(1, substr_count($body, $expected), 'A link to GoogleSitemapTest_OtherDataObject exists');
|
||||
|
||||
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_UnviewableDataObject/2") ."</loc>";
|
||||
$this->assertEquals(0, substr_count($body, $expected) , 'A link to a GoogleSitemapTest_UnviewableDataObject does not exist');
|
||||
}
|
||||
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_UnviewableDataObject/2") ."</loc>";
|
||||
$this->assertEquals(0, substr_count($body, $expected), 'A link to a GoogleSitemapTest_UnviewableDataObject does not exist');
|
||||
}
|
||||
|
||||
public function testLastModifiedDateOnRootXML() {
|
||||
Config::inst()->update('GoogleSitemap', 'enabled', true);
|
||||
public function testLastModifiedDateOnRootXML()
|
||||
{
|
||||
Config::inst()->update('GoogleSitemap', 'enabled', true);
|
||||
|
||||
if(!class_exists('Page')) {
|
||||
$this->markTestIncomplete('No cms module installed, page related test skipped');
|
||||
}
|
||||
if (!class_exists('Page')) {
|
||||
$this->markTestIncomplete('No cms module installed, page related test skipped');
|
||||
}
|
||||
|
||||
$page = $this->objFromFixture('Page', 'Page1');
|
||||
$page->publish('Stage', 'Live');
|
||||
$page->flushCache();
|
||||
$page = $this->objFromFixture('Page', 'Page1');
|
||||
$page->publish('Stage', 'Live');
|
||||
$page->flushCache();
|
||||
|
||||
$page2 = $this->objFromFixture('Page', 'Page2');
|
||||
$page2->publish('Stage', 'Live');
|
||||
$page2->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\"='".$page2->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();
|
||||
$response = $this->get('sitemap.xml');
|
||||
$body = $response->getBody();
|
||||
|
||||
$expected = '<lastmod>2014-03-14</lastmod>';
|
||||
$expected = '<lastmod>2014-03-14</lastmod>';
|
||||
|
||||
$this->assertEquals(1, substr_count($body, $expected) , 'The last mod date should use most recent LastEdited date');
|
||||
}
|
||||
$this->assertEquals(1, substr_count($body, $expected), 'The last mod date should use most recent LastEdited date');
|
||||
}
|
||||
|
||||
public function testIndexFilePaginatedSitemapFiles() {
|
||||
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
|
||||
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1);
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
|
||||
public function testIndexFilePaginatedSitemapFiles()
|
||||
{
|
||||
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
|
||||
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1);
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
|
||||
|
||||
$response = $this->get('sitemap.xml');
|
||||
$body = $response->getBody();
|
||||
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1") ."</loc>";
|
||||
$this->assertEquals(1, substr_count($body, $expected) , 'A link to the first page of GoogleSitemapTest_DataObject exists');
|
||||
$response = $this->get('sitemap.xml');
|
||||
$body = $response->getBody();
|
||||
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1") ."</loc>";
|
||||
$this->assertEquals(1, substr_count($body, $expected), 'A link to the first page of GoogleSitemapTest_DataObject exists');
|
||||
|
||||
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_DataObject/2") ."</loc>";
|
||||
$this->assertEquals(1, substr_count($body, $expected) , 'A link to the second page GoogleSitemapTest_DataObject exists');
|
||||
$expected = "<loc>". Director::absoluteURL("sitemap.xml/sitemap/GoogleSitemapTest_DataObject/2") ."</loc>";
|
||||
$this->assertEquals(1, substr_count($body, $expected), 'A link to the second page GoogleSitemapTest_DataObject exists');
|
||||
|
||||
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original);
|
||||
}
|
||||
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original);
|
||||
}
|
||||
|
||||
public function testRegisterRoutesIncludesAllRoutes() {
|
||||
GoogleSitemap::register_route('/test/');
|
||||
GoogleSitemap::register_routes(array(
|
||||
'/test/', // duplication should be replaced
|
||||
'/unittests/',
|
||||
'/anotherlink/'
|
||||
), 'weekly');
|
||||
public function testRegisterRoutesIncludesAllRoutes()
|
||||
{
|
||||
GoogleSitemap::register_route('/test/');
|
||||
GoogleSitemap::register_routes(array(
|
||||
'/test/', // duplication should be replaced
|
||||
'/unittests/',
|
||||
'/anotherlink/'
|
||||
), 'weekly');
|
||||
|
||||
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapRoute/1');
|
||||
$body = $response->getBody();
|
||||
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapRoute/1');
|
||||
$body = $response->getBody();
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap');
|
||||
$this->assertEquals(3, substr_count($body, "<loc>"));
|
||||
}
|
||||
$this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap');
|
||||
$this->assertEquals(3, substr_count($body, "<loc>"));
|
||||
}
|
||||
|
||||
public function testAccessingNestedSiteMap() {
|
||||
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
|
||||
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1);
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
|
||||
public function testAccessingNestedSiteMap()
|
||||
{
|
||||
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
|
||||
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1);
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
|
||||
|
||||
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1');
|
||||
$body = $response->getBody();
|
||||
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1');
|
||||
$body = $response->getBody();
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap');
|
||||
$this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap');
|
||||
|
||||
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original);
|
||||
}
|
||||
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original);
|
||||
}
|
||||
|
||||
public function testAccessingNestedSiteMapCaseInsensitive() {
|
||||
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
|
||||
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1);
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
|
||||
public function testAccessingNestedSiteMapCaseInsensitive()
|
||||
{
|
||||
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
|
||||
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1);
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
|
||||
|
||||
$response = $this->get('sitemap.xml/sitemap/googlesitemaptest_dataobject/1');
|
||||
$body = $response->getBody();
|
||||
$response = $this->get('sitemap.xml/sitemap/googlesitemaptest_dataobject/1');
|
||||
$body = $response->getBody();
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap');
|
||||
$this->assertEquals(200, $response->getStatusCode(), 'successful loaded nested sitemap');
|
||||
|
||||
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original);
|
||||
}
|
||||
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original);
|
||||
}
|
||||
|
||||
public function testGetItemsWithPages() {
|
||||
if(!class_exists('Page')) {
|
||||
$this->markTestIncomplete('No cms module installed, page related test skipped');
|
||||
}
|
||||
public function testGetItemsWithPages()
|
||||
{
|
||||
if (!class_exists('Page')) {
|
||||
$this->markTestIncomplete('No cms module installed, page related test skipped');
|
||||
}
|
||||
|
||||
$page = $this->objFromFixture('Page', 'Page1');
|
||||
$page->publish('Stage', 'Live');
|
||||
$page->flushCache();
|
||||
$page = $this->objFromFixture('Page', 'Page1');
|
||||
$page->publish('Stage', 'Live');
|
||||
$page->flushCache();
|
||||
|
||||
$page2 = $this->objFromFixture('Page', 'Page2');
|
||||
$page2->publish('Stage', 'Live');
|
||||
$page2->flushCache();
|
||||
$page2 = $this->objFromFixture('Page', 'Page2');
|
||||
$page2->publish('Stage', 'Live');
|
||||
$page2->flushCache();
|
||||
|
||||
$this->assertDOSContains(array(
|
||||
array('Title' => 'Testpage1'),
|
||||
array('Title' => 'Testpage2')
|
||||
), GoogleSitemap::get_items('SiteTree'), "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->publish('Stage', 'Live');
|
||||
|
||||
$this->session()->inst_set('loggedInAs', null);
|
||||
|
||||
$this->assertDOSEquals(array(
|
||||
array('Title' => 'Testpage1')
|
||||
), GoogleSitemap::get_items('SiteTree'), "There should be only 1 page, other is logged in only");
|
||||
}
|
||||
|
||||
public function testAccess() {
|
||||
Config::inst()->update('GoogleSitemap', 'enabled', true);
|
||||
|
||||
$response = $this->get('sitemap.xml');
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode(), 'Sitemap returns a 200 success when enabled');
|
||||
$this->assertEquals('application/xml; charset="utf-8"', $response->getHeader('Content-Type'));
|
||||
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
|
||||
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1');
|
||||
$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()->remove('GoogleSitemap', 'enabled');
|
||||
Config::inst()->update('GoogleSitemap', 'enabled', false);
|
||||
|
||||
$response = $this->get('sitemap.xml');
|
||||
$this->assertEquals(404, $response->getStatusCode(), 'Sitemap index returns a 404 when disabled');
|
||||
|
||||
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1');
|
||||
$this->assertEquals(404, $response->getStatusCode(), 'Sitemap file returns a 404 when disabled');
|
||||
}
|
||||
|
||||
public function testDecoratorAddsFields() {
|
||||
if(!class_exists("Page")) {
|
||||
$this->markTestIncomplete('No cms module installed, page related test skipped');
|
||||
}
|
||||
|
||||
$page = $this->objFromFixture('Page', 'Page1');
|
||||
|
||||
$fields = $page->getSettingsFields();
|
||||
$tab = $fields->fieldByName('Root')->fieldByName('Settings')->fieldByName('GoogleSitemap');
|
||||
|
||||
$this->assertInstanceOf('Tab', $tab);
|
||||
$this->assertInstanceOf('DropdownField', $tab->fieldByName('Priority'));
|
||||
$this->assertInstanceOf('LiteralField', $tab->fieldByName('GoogleSitemapIntro'));
|
||||
}
|
||||
|
||||
public function testGetPriority() {
|
||||
if(!class_exists("Page")) {
|
||||
$this->markTestIncomplete('No cms module installed, page related test skipped');
|
||||
}
|
||||
|
||||
$page = $this->objFromFixture('Page', 'Page1');
|
||||
|
||||
// invalid field doesn't break google
|
||||
$page->Priority = 'foo';
|
||||
$this->assertEquals(0.5, $page->getGooglePriority());
|
||||
|
||||
// custom value (set as string as db field is varchar)
|
||||
$page->Priority = '0.2';
|
||||
$this->assertEquals(0.2, $page->getGooglePriority());
|
||||
|
||||
// -1 indicates that we should not index this
|
||||
$page->Priority = -1;
|
||||
$this->assertFalse($page->getGooglePriority());
|
||||
}
|
||||
|
||||
public function testUnpublishedPage() {
|
||||
|
||||
if(!class_exists('SiteTree')) {
|
||||
$this->markTestSkipped('Test skipped; CMS module required for testUnpublishedPage');
|
||||
}
|
||||
|
||||
$orphanedPage = new SiteTree();
|
||||
$orphanedPage->ParentID = 999999; // missing parent id
|
||||
$orphanedPage->write();
|
||||
$orphanedPage->publish("Stage", "Live");
|
||||
|
||||
$rootPage = new SiteTree();
|
||||
$rootPage->ParentID = 0;
|
||||
$rootPage->write();
|
||||
$rootPage->publish("Stage", "Live");
|
||||
|
||||
$oldMode = Versioned::get_reading_mode();
|
||||
Versioned::reading_stage('Live');
|
||||
|
||||
try {
|
||||
$this->assertEmpty($orphanedPage->hasPublishedParent());
|
||||
$this->assertEmpty($orphanedPage->canIncludeInGoogleSitemap());
|
||||
$this->assertNotEmpty($rootPage->hasPublishedParent());
|
||||
$this->assertNotEmpty($rootPage->canIncludeInGoogleSitemap());
|
||||
} catch(Exception $ex) {
|
||||
Versioned::set_reading_mode($oldMode);
|
||||
throw $ex;
|
||||
} // finally {
|
||||
Versioned::set_reading_mode($oldMode);
|
||||
// }
|
||||
}
|
||||
$this->assertDOSContains(array(
|
||||
array('Title' => 'Testpage1'),
|
||||
array('Title' => 'Testpage2')
|
||||
), GoogleSitemap::get_items('SiteTree'), "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->publish('Stage', 'Live');
|
||||
|
||||
$this->session()->inst_set('loggedInAs', null);
|
||||
|
||||
$this->assertDOSEquals(array(
|
||||
array('Title' => 'Testpage1')
|
||||
), GoogleSitemap::get_items('SiteTree'), "There should be only 1 page, other is logged in only");
|
||||
}
|
||||
|
||||
public function testAccess()
|
||||
{
|
||||
Config::inst()->update('GoogleSitemap', 'enabled', true);
|
||||
|
||||
$response = $this->get('sitemap.xml');
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode(), 'Sitemap returns a 200 success when enabled');
|
||||
$this->assertEquals('application/xml; charset="utf-8"', $response->getHeader('Content-Type'));
|
||||
|
||||
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
|
||||
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1');
|
||||
$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()->remove('GoogleSitemap', 'enabled');
|
||||
Config::inst()->update('GoogleSitemap', 'enabled', false);
|
||||
|
||||
$response = $this->get('sitemap.xml');
|
||||
$this->assertEquals(404, $response->getStatusCode(), 'Sitemap index returns a 404 when disabled');
|
||||
|
||||
$response = $this->get('sitemap.xml/sitemap/GoogleSitemapTest_DataObject/1');
|
||||
$this->assertEquals(404, $response->getStatusCode(), 'Sitemap file returns a 404 when disabled');
|
||||
}
|
||||
|
||||
public function testDecoratorAddsFields()
|
||||
{
|
||||
if (!class_exists("Page")) {
|
||||
$this->markTestIncomplete('No cms module installed, page related test skipped');
|
||||
}
|
||||
|
||||
$page = $this->objFromFixture('Page', 'Page1');
|
||||
|
||||
$fields = $page->getSettingsFields();
|
||||
$tab = $fields->fieldByName('Root')->fieldByName('Settings')->fieldByName('GoogleSitemap');
|
||||
|
||||
$this->assertInstanceOf('Tab', $tab);
|
||||
$this->assertInstanceOf('DropdownField', $tab->fieldByName('Priority'));
|
||||
$this->assertInstanceOf('LiteralField', $tab->fieldByName('GoogleSitemapIntro'));
|
||||
}
|
||||
|
||||
public function testGetPriority()
|
||||
{
|
||||
if (!class_exists("Page")) {
|
||||
$this->markTestIncomplete('No cms module installed, page related test skipped');
|
||||
}
|
||||
|
||||
$page = $this->objFromFixture('Page', 'Page1');
|
||||
|
||||
// invalid field doesn't break google
|
||||
$page->Priority = 'foo';
|
||||
$this->assertEquals(0.5, $page->getGooglePriority());
|
||||
|
||||
// custom value (set as string as db field is varchar)
|
||||
$page->Priority = '0.2';
|
||||
$this->assertEquals(0.2, $page->getGooglePriority());
|
||||
|
||||
// -1 indicates that we should not index this
|
||||
$page->Priority = -1;
|
||||
$this->assertFalse($page->getGooglePriority());
|
||||
}
|
||||
|
||||
public function testUnpublishedPage()
|
||||
{
|
||||
if (!class_exists('SiteTree')) {
|
||||
$this->markTestSkipped('Test skipped; CMS module required for testUnpublishedPage');
|
||||
}
|
||||
|
||||
$orphanedPage = new SiteTree();
|
||||
$orphanedPage->ParentID = 999999; // missing parent id
|
||||
$orphanedPage->write();
|
||||
$orphanedPage->publish("Stage", "Live");
|
||||
|
||||
$rootPage = new SiteTree();
|
||||
$rootPage->ParentID = 0;
|
||||
$rootPage->write();
|
||||
$rootPage->publish("Stage", "Live");
|
||||
|
||||
$oldMode = Versioned::get_reading_mode();
|
||||
Versioned::reading_stage('Live');
|
||||
|
||||
try {
|
||||
$this->assertEmpty($orphanedPage->hasPublishedParent());
|
||||
$this->assertEmpty($orphanedPage->canIncludeInGoogleSitemap());
|
||||
$this->assertNotEmpty($rootPage->hasPublishedParent());
|
||||
$this->assertNotEmpty($rootPage->canIncludeInGoogleSitemap());
|
||||
} catch (Exception $ex) {
|
||||
Versioned::set_reading_mode($oldMode);
|
||||
throw $ex;
|
||||
} // finally {
|
||||
Versioned::set_reading_mode($oldMode);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @package googlesitemaps
|
||||
* @subpackage tests
|
||||
*/
|
||||
class GoogleSitemapTest_DataObject extends DataObject implements TestOnly {
|
||||
class GoogleSitemapTest_DataObject extends DataObject implements TestOnly
|
||||
{
|
||||
|
||||
public static $db = array(
|
||||
'Priority' => 'Varchar(10)'
|
||||
);
|
||||
public static $db = array(
|
||||
'Priority' => 'Varchar(10)'
|
||||
);
|
||||
|
||||
public function canView($member = null) {
|
||||
return true;
|
||||
}
|
||||
public function canView($member = null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function AbsoluteLink() {
|
||||
return Director::absoluteBaseURL();
|
||||
}
|
||||
public function AbsoluteLink()
|
||||
{
|
||||
return Director::absoluteBaseURL();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @package googlesitemaps
|
||||
* @subpackage tests
|
||||
*/
|
||||
class GoogleSitemapTest_OtherDataObject extends DataObject implements TestOnly {
|
||||
class GoogleSitemapTest_OtherDataObject extends DataObject implements TestOnly
|
||||
{
|
||||
|
||||
public static $db = array(
|
||||
'Priority' => 'Varchar(10)'
|
||||
);
|
||||
public static $db = array(
|
||||
'Priority' => 'Varchar(10)'
|
||||
);
|
||||
|
||||
public function canView($member = null) {
|
||||
return true;
|
||||
}
|
||||
public function canView($member = null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function AbsoluteLink() {
|
||||
return Director::absoluteBaseURL();
|
||||
}
|
||||
public function AbsoluteLink()
|
||||
{
|
||||
return Director::absoluteBaseURL();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @package googlesitemaps
|
||||
* @subpackage tests
|
||||
*/
|
||||
class GoogleSitemapTest_UnviewableDataObject extends DataObject implements TestOnly {
|
||||
class GoogleSitemapTest_UnviewableDataObject extends DataObject implements TestOnly
|
||||
{
|
||||
|
||||
public static $db = array(
|
||||
'Priority' => 'Varchar(10)'
|
||||
);
|
||||
public static $db = array(
|
||||
'Priority' => 'Varchar(10)'
|
||||
);
|
||||
|
||||
public function canView($member = null) {
|
||||
return false;
|
||||
}
|
||||
public function canView($member = null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function AbsoluteLink() {
|
||||
return Director::absoluteBaseURL();
|
||||
}
|
||||
public function AbsoluteLink()
|
||||
{
|
||||
return Director::absoluteBaseURL();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user