Merge pull request #91 from patricknelson/master

FIX/NEW for #90 Allow ability to alter DataList and deprecate static interface for better extensibility.
This commit is contained in:
Will Rossiter 2015-10-17 17:29:29 +13:00
commit dd41e39484
3 changed files with 53 additions and 10 deletions

View File

@ -37,7 +37,7 @@
* *
* @package googlesitemaps * @package googlesitemaps
*/ */
class GoogleSitemap { class GoogleSitemap extends Object {
/** /**
* List of {@link DataObject} class names to include. As well as the change * List of {@link DataObject} class names to include. As well as the change
@ -181,7 +181,7 @@ class GoogleSitemap {
* *
* @return ArrayList * @return ArrayList
*/ */
public static function get_items($class, $page = 1) { public function getItems($class, $page = 1) {
//normalise the class name //normalise the class name
try { try {
$reflectionClass = new ReflectionClass($class); $reflectionClass = new ReflectionClass($class);
@ -203,7 +203,6 @@ class GoogleSitemap {
if($class == "SiteTree") { if($class == "SiteTree") {
$filter = ($filter) ? "\"ShowInSearch\" = 1" : ""; $filter = ($filter) ? "\"ShowInSearch\" = 1" : "";
$instances = Versioned::get_by_stage('SiteTree', 'Live', $filter); $instances = Versioned::get_by_stage('SiteTree', 'Live', $filter);
} }
else if($class == "GoogleSitemapRoute") { else if($class == "GoogleSitemapRoute") {
@ -226,6 +225,8 @@ class GoogleSitemap {
$instances = new DataList($class); $instances = new DataList($class);
} }
$this->extend("alterDataList", $instances, $class);
$instances = $instances->limit( $instances = $instances->limit(
$count, $count,
($page - 1) * $count ($page - 1) * $count
@ -241,7 +242,21 @@ class GoogleSitemap {
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);
}
/** /**
* Returns the string frequency of edits for a particular dataobject class. * Returns the string frequency of edits for a particular dataobject class.
* *
@ -258,6 +273,8 @@ class GoogleSitemap {
return $config['frequency']; return $config['frequency'];
} }
} }
return '';
} }
/** /**
@ -284,7 +301,7 @@ class GoogleSitemap {
* *
* @return ArrayList * @return ArrayList
*/ */
public static function get_sitemaps() { public function getSitemaps() {
$countPerFile = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap'); $countPerFile = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
$sitemaps = new ArrayList(); $sitemaps = new ArrayList();
$filter = Config::inst()->get('GoogleSitemap', 'use_show_in_search'); $filter = Config::inst()->get('GoogleSitemap', 'use_show_in_search');
@ -297,7 +314,9 @@ class GoogleSitemap {
} }
$filter = ($filter) ? "\"ShowInSearch\" = 1" : ""; $filter = ($filter) ? "\"ShowInSearch\" = 1" : "";
$instances = Versioned::get_by_stage('SiteTree', 'Live', $filter); $class = 'SiteTree';
$instances = Versioned::get_by_stage($class, 'Live', $filter);
$this->extend("alterDataList", $instances, $class);
$count = $instances->count(); $count = $instances->count();
$neededForPage = ceil($count / $countPerFile); $neededForPage = ceil($count / $countPerFile);
@ -323,7 +342,7 @@ class GoogleSitemap {
foreach(self::$dataobjects as $class => $config) { foreach(self::$dataobjects as $class => $config) {
$list = new DataList($class); $list = new DataList($class);
$list = $list->sort('LastEdited ASC'); $list = $list->sort('LastEdited ASC');
$this->extend("alterDataList", $list, $class);
$neededForClass = ceil($list->count() / $countPerFile); $neededForClass = ceil($list->count() / $countPerFile);
for($i = 1; $i <= $neededForClass; $i++) { for($i = 1; $i <= $neededForClass; $i++) {
@ -357,6 +376,16 @@ class GoogleSitemap {
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();
}
/** /**
* Notifies Google about changes to your sitemap. This behavior is disabled * Notifies Google about changes to your sitemap. This behavior is disabled
* by default, to enable, read the documentation provided in the docs folder. * by default, to enable, read the documentation provided in the docs folder.
@ -420,5 +449,16 @@ class GoogleSitemap {
*/ */
public static function enabled() { public static function enabled() {
return (Config::inst()->get('GoogleSitemap', 'enabled', Config::INHERITED)); 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();
}
} }

View File

@ -22,6 +22,7 @@ class GoogleSitemapController extends Controller {
'sitemap' 'sitemap'
); );
/** /**
* Default controller action for the sitemap.xml file. Renders a index * Default controller action for the sitemap.xml file. Renders a index
* file containing a list of links to sub sitemaps containing the data. * file containing a list of links to sub sitemaps containing the data.
@ -35,7 +36,7 @@ class GoogleSitemapController extends Controller {
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"'); $this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex'); $this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
$sitemaps = GoogleSitemap::get_sitemaps(); $sitemaps = GoogleSitemap::inst()->getSitemaps();
$this->extend('updateGoogleSitemaps', $sitemaps); $this->extend('updateGoogleSitemaps', $sitemaps);
return array( return array(
@ -62,7 +63,7 @@ class GoogleSitemapController extends Controller {
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"'); $this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex'); $this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
$items = GoogleSitemap::get_items($class, $page); $items = GoogleSitemap::inst()->getItems($class, $page);
$this->extend('updateGoogleSitemapItems', $items, $class, $page); $this->extend('updateGoogleSitemapItems', $items, $class, $page);
return array( return array(

View File

@ -1,6 +1,8 @@
<?php <?php
/** /**
* TODO: Migrate to new instance level interface instead of using static methods for retrieval of site maps and items (i.e. ->getSitemaps() instead of ::get_sitemaps()).
*
* @package googlesitemaps * @package googlesitemaps
* @subpackage tests * @subpackage tests
*/ */