From e2a1dc52504fd3c7e67ed346c27155476c36a6d5 Mon Sep 17 00:00:00 2001 From: Patrick Nelson Date: Mon, 12 Oct 2015 16:59:42 -0400 Subject: [PATCH 1/2] FIX/NEW for #90 Allow ability to alter DataList and deprecate static interface for better extensibility. --- code/GoogleSitemap.php | 53 +++++++++++++++++--- code/controllers/GoogleSitemapController.php | 5 +- tests/GoogleSitemapTest.php | 2 + 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/code/GoogleSitemap.php b/code/GoogleSitemap.php index 8217dfa..97255fe 100644 --- a/code/GoogleSitemap.php +++ b/code/GoogleSitemap.php @@ -37,7 +37,7 @@ * * @package googlesitemaps */ -class GoogleSitemap { +class GoogleSitemap extends Object { /** * List of {@link DataObject} class names to include. As well as the change @@ -181,7 +181,7 @@ class GoogleSitemap { * * @return ArrayList */ - public static function get_items($class, $page = 1) { + public function getItems($class, $page = 1) { //normalise the class name try { $reflectionClass = new ReflectionClass($class); @@ -203,8 +203,8 @@ class GoogleSitemap { if($class == "SiteTree") { $filter = ($filter) ? "\"ShowInSearch\" = 1" : ""; - $instances = Versioned::get_by_stage('SiteTree', 'Live', $filter); + $this->extend("alterDataList", $instances, $class); } else if($class == "GoogleSitemapRoute") { $instances = array_slice(self::$routes, ($page - 1) * $count, $count); @@ -241,7 +241,21 @@ class GoogleSitemap { 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. * @@ -258,6 +272,8 @@ class GoogleSitemap { return $config['frequency']; } } + + return ''; } /** @@ -284,7 +300,7 @@ class GoogleSitemap { * * @return ArrayList */ - public static function get_sitemaps() { + public function getSitemaps() { $countPerFile = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap'); $sitemaps = new ArrayList(); $filter = Config::inst()->get('GoogleSitemap', 'use_show_in_search'); @@ -297,7 +313,9 @@ class GoogleSitemap { } $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(); $neededForPage = ceil($count / $countPerFile); @@ -357,6 +375,16 @@ class GoogleSitemap { 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 * by default, to enable, read the documentation provided in the docs folder. @@ -420,5 +448,16 @@ class GoogleSitemap { */ 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(); + } + } diff --git a/code/controllers/GoogleSitemapController.php b/code/controllers/GoogleSitemapController.php index b6168df..44e3879 100644 --- a/code/controllers/GoogleSitemapController.php +++ b/code/controllers/GoogleSitemapController.php @@ -22,6 +22,7 @@ class GoogleSitemapController extends Controller { 'sitemap' ); + /** * Default controller action for the sitemap.xml file. Renders a index * 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('X-Robots-Tag', 'noindex'); - $sitemaps = GoogleSitemap::get_sitemaps(); + $sitemaps = GoogleSitemap::inst()->getSitemaps(); $this->extend('updateGoogleSitemaps', $sitemaps); return array( @@ -62,7 +63,7 @@ class GoogleSitemapController extends Controller { $this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"'); $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); return array( diff --git a/tests/GoogleSitemapTest.php b/tests/GoogleSitemapTest.php index 808c7e9..ff04be1 100644 --- a/tests/GoogleSitemapTest.php +++ b/tests/GoogleSitemapTest.php @@ -1,6 +1,8 @@ getSitemaps() instead of ::get_sitemaps()). + * * @package googlesitemaps * @subpackage tests */ From a641ded9263541cca8f662e76a942d6fa50d6afb Mon Sep 17 00:00:00 2001 From: Patrick Nelson Date: Mon, 12 Oct 2015 17:23:01 -0400 Subject: [PATCH 2/2] FIX/NEW for #90 Extending alterDataList after both Versioned::get_by_stage() and also new DataList(). No list left behind! --- code/GoogleSitemap.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/code/GoogleSitemap.php b/code/GoogleSitemap.php index 97255fe..81b292d 100644 --- a/code/GoogleSitemap.php +++ b/code/GoogleSitemap.php @@ -204,7 +204,6 @@ class GoogleSitemap extends Object { if($class == "SiteTree") { $filter = ($filter) ? "\"ShowInSearch\" = 1" : ""; $instances = Versioned::get_by_stage('SiteTree', 'Live', $filter); - $this->extend("alterDataList", $instances, $class); } else if($class == "GoogleSitemapRoute") { $instances = array_slice(self::$routes, ($page - 1) * $count, $count); @@ -226,6 +225,8 @@ class GoogleSitemap extends Object { $instances = new DataList($class); } + $this->extend("alterDataList", $instances, $class); + $instances = $instances->limit( $count, ($page - 1) * $count @@ -341,7 +342,7 @@ class GoogleSitemap extends Object { 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++) {