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
*/
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,7 +203,6 @@ class GoogleSitemap {
if($class == "SiteTree") {
$filter = ($filter) ? "\"ShowInSearch\" = 1" : "";
$instances = Versioned::get_by_stage('SiteTree', 'Live', $filter);
}
else if($class == "GoogleSitemapRoute") {
@ -226,6 +225,8 @@ class GoogleSitemap {
$instances = new DataList($class);
}
$this->extend("alterDataList", $instances, $class);
$instances = $instances->limit(
$count,
($page - 1) * $count
@ -242,6 +243,20 @@ 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 +273,8 @@ class GoogleSitemap {
return $config['frequency'];
}
}
return '';
}
/**
@ -284,7 +301,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 +314,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);
@ -323,7 +342,7 @@ class GoogleSitemap {
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++) {
@ -357,6 +376,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.
@ -421,4 +450,15 @@ 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();
}
}

View File

@ -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(

View File

@ -1,6 +1,8 @@
<?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
* @subpackage tests
*/