Merge pull request #94 from helpfulrobot/convert-to-psr-2

Converted to PSR-2
This commit is contained in:
Damian Mooyman 2015-12-18 10:02:57 +13:00
commit 289ed77139
5 changed files with 941 additions and 880 deletions

View File

@ -37,7 +37,8 @@
*
* @package googlesitemaps
*/
class GoogleSitemap extends Object {
class GoogleSitemap extends Object
{
/**
* List of {@link DataObject} class names to include. As well as the change
@ -69,7 +70,8 @@ class GoogleSitemap extends Object {
*
* @return void
*/
public static function register_dataobject($className, $changeFreq = 'monthly', $priority = '0.6') {
public static function register_dataobject($className, $changeFreq = 'monthly', $priority = '0.6')
{
if (!self::is_registered($className)) {
$className::add_extension('GoogleSitemapExtension');
@ -93,7 +95,8 @@ class GoogleSitemap extends Object {
*
* @return void
*/
public static function register_dataobjects($dataobjects, $changeFreq = 'monthly', $priority = '0.6') {
public static function register_dataobjects($dataobjects, $changeFreq = 'monthly', $priority = '0.6')
{
foreach ($dataobjects as $obj) {
self::register_dataobject($obj, $changeFreq, $priority);
}
@ -106,7 +109,8 @@ class GoogleSitemap extends Object {
*
* @return bool
*/
public static function is_registered($className) {
public static function is_registered($className)
{
return isset(self::$dataobjects[$className]);
}
@ -115,7 +119,8 @@ class GoogleSitemap extends Object {
*
* @param string
*/
public static function unregister_dataobject($className) {
public static function unregister_dataobject($className)
{
unset(self::$dataobjects[$className]);
}
@ -124,7 +129,8 @@ class GoogleSitemap extends Object {
*
* @return void
*/
public static function clear_registered_dataobjects() {
public static function clear_registered_dataobjects()
{
self::$dataobjects = array();
}
@ -137,7 +143,8 @@ class GoogleSitemap extends Object {
*
* @return void
*/
public static function register_route($route, $changeFreq = 'monthly', $priority = '0.6') {
public static function register_route($route, $changeFreq = 'monthly', $priority = '0.6')
{
self::$routes = array_merge(self::$routes, array(
$route => array(
'frequency' => ($changeFreq) ? $changeFreq : 'monthly',
@ -156,7 +163,8 @@ class GoogleSitemap extends Object {
*
* @return void
*/
public static function register_routes($routes, $changeFreq = 'monthly', $priority = '0.6') {
public static function register_routes($routes, $changeFreq = 'monthly', $priority = '0.6')
{
foreach ($routes as $route) {
self::register_route($route, $changeFreq, $priority);
}
@ -167,7 +175,8 @@ class GoogleSitemap extends Object {
*
* @return void
*/
public static function clear_registered_routes() {
public static function clear_registered_routes()
{
self::$routes = array();
}
@ -181,7 +190,8 @@ class GoogleSitemap extends Object {
*
* @return ArrayList
*/
public function getItems($class, $page = 1) {
public function getItems($class, $page = 1)
{
//normalise the class name
try {
$reflectionClass = new ReflectionClass($class);
@ -204,8 +214,7 @@ class GoogleSitemap extends Object {
if ($class == "SiteTree") {
$filter = ($filter) ? "\"ShowInSearch\" = 1" : "";
$instances = Versioned::get_by_stage('SiteTree', 'Live', $filter);
}
else if($class == "GoogleSitemapRoute") {
} elseif ($class == "GoogleSitemapRoute") {
$instances = array_slice(self::$routes, ($page - 1) * $count, $count);
$output = new ArrayList();
@ -220,8 +229,7 @@ class GoogleSitemap extends Object {
}
return $output;
}
else {
} else {
$instances = new DataList($class);
}
@ -252,7 +260,8 @@ class GoogleSitemap extends Object {
* @return ArrayList
* @deprecated Please create an instance and call ->getSitemaps() instead.
*/
public static function get_items($class, $page = 1) {
public static function get_items($class, $page = 1)
{
return static::inst()->getItems($class, $page);
}
@ -267,7 +276,8 @@ class GoogleSitemap extends Object {
*
* @return string
*/
public static function get_frequency_for_class($class) {
public static function get_frequency_for_class($class)
{
foreach (self::$dataobjects as $type => $config) {
if ($class == $type) {
return $config['frequency'];
@ -284,7 +294,8 @@ class GoogleSitemap extends Object {
*
* @return float
*/
public static function get_priority_for_class($class) {
public static function get_priority_for_class($class)
{
foreach (self::$dataobjects as $type => $config) {
if ($class == $type) {
return $config['priority'];
@ -301,7 +312,8 @@ class GoogleSitemap extends Object {
*
* @return ArrayList
*/
public function getSitemaps() {
public function getSitemaps()
{
$countPerFile = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
$sitemaps = new ArrayList();
$filter = Config::inst()->get('GoogleSitemap', 'use_show_in_search');
@ -322,7 +334,6 @@ class GoogleSitemap extends Object {
$neededForPage = ceil($count / $countPerFile);
for ($i = 1; $i <= $neededForPage; $i++) {
$lastEdited = $instances
->limit($countPerFile, ($i - 1) * $countPerFile)
->sort(array())
@ -382,7 +393,8 @@ class GoogleSitemap extends Object {
* @return ArrayList
* @deprecated Please create an instance and call ->getSitemaps() instead.
*/
public static function get_sitemaps() {
public static function get_sitemaps()
{
return static::inst()->getSitemaps();
}
@ -398,7 +410,8 @@ class GoogleSitemap extends Object {
*
* @return string Response text
*/
public static function ping() {
public static function ping()
{
if (!self::enabled()) {
return false;
}
@ -427,7 +440,8 @@ class GoogleSitemap extends Object {
*
* @return String Response text
*/
protected static function send_ping($host, $path, $query) {
protected static function send_ping($host, $path, $query)
{
$socket = fsockopen($host, 80, $errno, $error);
if (!$socket) {
return $error;
@ -447,7 +461,8 @@ class GoogleSitemap extends Object {
*
* @return boolean
*/
public static function enabled() {
public static function enabled()
{
return (Config::inst()->get('GoogleSitemap', 'enabled', Config::INHERITED));
}
@ -457,8 +472,8 @@ class GoogleSitemap extends Object {
*
* @return GoogleSitemap
*/
public static function inst() {
public static function inst()
{
return GoogleSitemap::create();
}
}

View File

@ -12,7 +12,8 @@
*
* @package googlesitemaps
*/
class GoogleSitemapController extends Controller {
class GoogleSitemapController extends Controller
{
/**
* @var array
@ -29,7 +30,8 @@ class GoogleSitemapController extends Controller {
*
* @return mixed
*/
public function index($url) {
public function index($url)
{
if (GoogleSitemap::enabled()) {
Config::inst()->update('SSViewer', 'set_source_file_comments', false);
@ -53,7 +55,8 @@ class GoogleSitemapController extends Controller {
*
* @return mixed
*/
public function sitemap() {
public function sitemap()
{
$class = $this->request->param('ID');
$page = $this->request->param('OtherID');

View File

@ -6,12 +6,14 @@
*
* @package googlesitemaps
*/
class GoogleSitemapExtension extends DataExtension {
class GoogleSitemapExtension extends DataExtension
{
/**
* @return boolean
*/
public function canIncludeInGoogleSitemap() {
public function canIncludeInGoogleSitemap()
{
$can = true;
if ($this->owner->hasMethod('AbsoluteLink')) {
@ -40,14 +42,16 @@ class GoogleSitemapExtension extends DataExtension {
/**
* @return void
*/
public function onAfterPublish() {
public function onAfterPublish()
{
GoogleSitemap::ping();
}
/**
* @return void
*/
public function onAfterUnpublish() {
public function onAfterUnpublish()
{
GoogleSitemap::ping();
}
@ -58,7 +62,8 @@ class GoogleSitemapExtension extends DataExtension {
*
* @return mixed
*/
public function getGooglePriority() {
public function getGooglePriority()
{
$field = $this->owner->hasField('Priority');
if (isset($this->Priority) || ($field && $this->Priority = $this->owner->getField('Priority'))) {
@ -77,7 +82,8 @@ class GoogleSitemapExtension extends DataExtension {
*
* @return SS_Datetime
*/
public function getChangeFrequency() {
public function getChangeFrequency()
{
if ($freq = GoogleSitemap::get_frequency_for_class($this->owner->class)) {
return $freq;
}

View File

@ -3,7 +3,8 @@
/**
* @package googlesitemaps
*/
class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension {
class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension
{
/**
* @var array
@ -15,7 +16,8 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension {
/**
* @param FieldList
*/
public function updateSettingsFields(&$fields) {
public function updateSettingsFields(&$fields)
{
$prorities = array(
'-1' => _t('GoogleSitemaps.PRIORITYNOTINDEXED', "Not indexed"),
'1.0' => '1 - ' . _t('GoogleSitemaps.PRIORITYMOSTIMPORTANT', "Most important"),
@ -51,7 +53,8 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension {
*
* @return void
*/
public function updateFieldLabels(&$labels) {
public function updateFieldLabels(&$labels)
{
parent::updateFieldLabels($labels);
$labels['Priority'] = _t('GoogleSitemaps.METAPAGEPRIO', "Page Priority");
@ -62,14 +65,19 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension {
*
* @return boolean
*/
public function hasPublishedParent() {
public function hasPublishedParent()
{
// Skip root pages
if(empty($this->owner->ParentID)) return true;
if (empty($this->owner->ParentID)) {
return true;
}
// Ensure direct parent exists
$parent = $this->owner->Parent();
if(empty($parent) || !$parent->exists()) return false;
if (empty($parent) || !$parent->exists()) {
return false;
}
// Check ancestry
return $parent->hasPublishedParent();
@ -78,10 +86,13 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension {
/**
* @return boolean
*/
public function canIncludeInGoogleSitemap() {
public function canIncludeInGoogleSitemap()
{
// Check that parent page is published
if(!$this->owner->hasPublishedParent()) return false;
if (!$this->owner->hasPublishedParent()) {
return false;
}
$result = parent::canIncludeInGoogleSitemap();
$result = ($this->owner instanceof ErrorPage) ? false : $result;
@ -92,7 +103,8 @@ class GoogleSitemapSiteTreeExtension extends GoogleSitemapExtension {
/**
* @return mixed
*/
public function getGooglePriority() {
public function getGooglePriority()
{
setlocale(LC_ALL, "en_US.UTF8");
$priority = $this->owner->getField('Priority');

View File

@ -6,7 +6,8 @@
* @package googlesitemaps
* @subpackage tests
*/
class GoogleSitemapTest extends FunctionalTest {
class GoogleSitemapTest extends FunctionalTest
{
public static $fixture_file = 'googlesitemaps/tests/GoogleSitemapTest.yml';
@ -16,7 +17,8 @@ class GoogleSitemapTest extends FunctionalTest {
'GoogleSitemapTest_UnviewableDataObject'
);
public function setUp() {
public function setUp()
{
parent::setUp();
if (class_exists('Page')) {
@ -27,14 +29,16 @@ class GoogleSitemapTest extends FunctionalTest {
GoogleSitemap::clear_registered_routes();
}
public function tearDown() {
public function tearDown()
{
parent::tearDown();
GoogleSitemap::clear_registered_dataobjects();
GoogleSitemap::clear_registered_routes();
}
public function testIndexFileWithCustomRoute() {
public function testIndexFileWithCustomRoute()
{
GoogleSitemap::register_route('/test/');
$response = $this->get('sitemap.xml');
@ -45,7 +49,8 @@ class GoogleSitemapTest extends FunctionalTest {
}
public function testGetItems() {
public function testGetItems()
{
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject", '');
$items = GoogleSitemap::get_items('GoogleSitemapTest_DataObject', 1);
@ -63,7 +68,8 @@ class GoogleSitemapTest extends FunctionalTest {
$this->assertEquals(0, GoogleSitemap::get_items('GoogleSitemapTest_UnviewableDataObject', 1)->count());
}
public function testGetItemsWithCustomRoutes() {
public function testGetItemsWithCustomRoutes()
{
GoogleSitemap::register_routes(array(
'/test-route/',
'/someother-route/',
@ -74,7 +80,8 @@ class GoogleSitemapTest extends FunctionalTest {
$this->assertEquals(3, $items->count());
}
public function testAccessingSitemapRootXMLFile() {
public function testAccessingSitemapRootXMLFile()
{
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
GoogleSitemap::register_dataobject("GoogleSitemapTest_OtherDataObject");
@ -93,7 +100,8 @@ class GoogleSitemapTest extends FunctionalTest {
$this->assertEquals(0, substr_count($body, $expected), 'A link to a GoogleSitemapTest_UnviewableDataObject does not exist');
}
public function testLastModifiedDateOnRootXML() {
public function testLastModifiedDateOnRootXML()
{
Config::inst()->update('GoogleSitemap', 'enabled', true);
if (!class_exists('Page')) {
@ -119,7 +127,8 @@ class GoogleSitemapTest extends FunctionalTest {
$this->assertEquals(1, substr_count($body, $expected), 'The last mod date should use most recent LastEdited date');
}
public function testIndexFilePaginatedSitemapFiles() {
public function testIndexFilePaginatedSitemapFiles()
{
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1);
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
@ -135,7 +144,8 @@ class GoogleSitemapTest extends FunctionalTest {
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original);
}
public function testRegisterRoutesIncludesAllRoutes() {
public function testRegisterRoutesIncludesAllRoutes()
{
GoogleSitemap::register_route('/test/');
GoogleSitemap::register_routes(array(
'/test/', // duplication should be replaced
@ -150,7 +160,8 @@ class GoogleSitemapTest extends FunctionalTest {
$this->assertEquals(3, substr_count($body, "<loc>"));
}
public function testAccessingNestedSiteMap() {
public function testAccessingNestedSiteMap()
{
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1);
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
@ -163,7 +174,8 @@ class GoogleSitemapTest extends FunctionalTest {
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original);
}
public function testAccessingNestedSiteMapCaseInsensitive() {
public function testAccessingNestedSiteMapCaseInsensitive()
{
$original = Config::inst()->get('GoogleSitemap', 'objects_per_sitemap');
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', 1);
GoogleSitemap::register_dataobject("GoogleSitemapTest_DataObject");
@ -176,7 +188,8 @@ class GoogleSitemapTest extends FunctionalTest {
Config::inst()->update('GoogleSitemap', 'objects_per_sitemap', $original);
}
public function testGetItemsWithPages() {
public function testGetItemsWithPages()
{
if (!class_exists('Page')) {
$this->markTestIncomplete('No cms module installed, page related test skipped');
}
@ -206,7 +219,8 @@ class GoogleSitemapTest extends FunctionalTest {
), GoogleSitemap::get_items('SiteTree'), "There should be only 1 page, other is logged in only");
}
public function testAccess() {
public function testAccess()
{
Config::inst()->update('GoogleSitemap', 'enabled', true);
$response = $this->get('sitemap.xml');
@ -229,7 +243,8 @@ class GoogleSitemapTest extends FunctionalTest {
$this->assertEquals(404, $response->getStatusCode(), 'Sitemap file returns a 404 when disabled');
}
public function testDecoratorAddsFields() {
public function testDecoratorAddsFields()
{
if (!class_exists("Page")) {
$this->markTestIncomplete('No cms module installed, page related test skipped');
}
@ -244,7 +259,8 @@ class GoogleSitemapTest extends FunctionalTest {
$this->assertInstanceOf('LiteralField', $tab->fieldByName('GoogleSitemapIntro'));
}
public function testGetPriority() {
public function testGetPriority()
{
if (!class_exists("Page")) {
$this->markTestIncomplete('No cms module installed, page related test skipped');
}
@ -264,8 +280,8 @@ class GoogleSitemapTest extends FunctionalTest {
$this->assertFalse($page->getGooglePriority());
}
public function testUnpublishedPage() {
public function testUnpublishedPage()
{
if (!class_exists('SiteTree')) {
$this->markTestSkipped('Test skipped; CMS module required for testUnpublishedPage');
}
@ -301,17 +317,20 @@ class GoogleSitemapTest extends FunctionalTest {
* @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 function canView($member = null) {
public function canView($member = null)
{
return true;
}
public function AbsoluteLink() {
public function AbsoluteLink()
{
return Director::absoluteBaseURL();
}
}
@ -320,17 +339,20 @@ class GoogleSitemapTest_DataObject extends DataObject implements TestOnly {
* @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 function canView($member = null) {
public function canView($member = null)
{
return true;
}
public function AbsoluteLink() {
public function AbsoluteLink()
{
return Director::absoluteBaseURL();
}
}
@ -339,17 +361,20 @@ class GoogleSitemapTest_OtherDataObject extends DataObject implements TestOnly {
* @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 function canView($member = null) {
public function canView($member = null)
{
return false;
}
public function AbsoluteLink() {
public function AbsoluteLink()
{
return Director::absoluteBaseURL();
}
}