googlesitemap.xml is now able to show any dataobject shown in a detail view in the frontend

This commit is contained in:
Roland Lehmann 2011-06-07 20:19:04 +02:00
parent c62538a860
commit 4ca8402508

View File

@ -1,5 +1,4 @@
<?php <?php
/** /**
* Initial implementation of Sitemap support. * Initial implementation of Sitemap support.
* GoogleSitemap should handle requests to 'sitemap.xml' * GoogleSitemap should handle requests to 'sitemap.xml'
@ -11,7 +10,7 @@
* sitemap whenever the GoogleBot visits your website. * sitemap whenever the GoogleBot visits your website.
* *
* Enabling notification of Google after every publish (in your _config.php): * Enabling notification of Google after every publish (in your _config.php):
* <example> * <example
* GoogleSitemap::enable_google_notificaton(); * GoogleSitemap::enable_google_notificaton();
* </example> * </example>
* *
@ -19,7 +18,6 @@
* *
* @package googlesitemaps * @package googlesitemaps
*/ */
class GoogleSitemap extends Controller { class GoogleSitemap extends Controller {
/** /**
@ -41,8 +39,70 @@ class GoogleSitemap extends Controller {
* @var boolean * @var boolean
*/ */
protected static $use_show_in_search = true; protected static $use_show_in_search = true;
public function Items() { /**
* List of DataObjects to show in sitemap.xml
*
* @var array
*/
public static $google_sitemap_dataobjects = array();
/**
* List of DataObjects change frequency
*
* @var array
*/
public static $google_sitemap_dataobjects_changefreq = array();
/**
* Decorates the given DataObject with 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
*/
public static function registerDataObject($className, $changeFreq = 'monthly') {
if (!self::isRegistered($className)) {
Object::add_extension($className, 'GoogleSitemapDecorator');
self::$google_sitemap_dataobjects[] = $className;
self::$google_sitemap_dataobjects_changefreq[] = $changeFreq;
}
}
/**
* Checks whether the given class name is already registered or not.
*
* @param string $className Name of DataObject to check
*
* @return bool
*/
public static function isRegistered($className) {
return in_array($className, self::$google_sitemap_dataobjects);
}
/**
* Adds DataObjects to the existing DataObjectSet with pages from the
* site tree
*
* @param DataObjectSet $newPages
*
* @return void
*/
protected function addRegisteredDataObjects($newPages) {
foreach (self::$google_sitemap_dataobjects as $index => $className) {
$dataObjectSet = DataObject::get($className);
foreach ($dataObjectSet as $dataObject) {
if ($dataObject->canView() && (!isset($dataObject->Priority) || $dataObject->Priority > 0)) {
$dataObject->ChangeFreq = self::$google_sitemap_dataobjects_changefreq[$index];
$newPages->push($dataObject);
}
}
}
}
public function Items() {
$filter = ''; $filter = '';
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; $bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`";
@ -62,12 +122,14 @@ class GoogleSitemap extends Controller {
// If the page has been set to 0 priority, we set a flag so it won't be included // If the page has been set to 0 priority, we set a flag so it won't be included
if($page->canView() && (!isset($page->Priority) || $page->Priority > 0)) { if($page->canView() && (!isset($page->Priority) || $page->Priority > 0)) {
// The one field that isn't easy to deal with in the template is
$created = $page->dbObject('Created'); // Change frequency, so we set that here.
$properties = $page->toMap();
$created = new SS_Datetime();
$created->value = $properties['Created'];
$now = new SS_Datetime(); $now = new SS_Datetime();
$now->value = date('Y-m-d H:i:s'); $now->value = date('Y-m-d H:i:s');
$versions = $page->Version; $versions = $properties['Version'];
$timediff = $now->format('U') - $created->format('U'); $timediff = $now->format('U') - $created->format('U');
// Check how many revisions have been made over the lifetime of the // Check how many revisions have been made over the lifetime of the
@ -93,6 +155,7 @@ class GoogleSitemap extends Controller {
} }
} }
} }
$this->addRegisteredDataObjects($newPages);
return $newPages; return $newPages;
} }
} }
@ -156,4 +219,4 @@ class GoogleSitemap extends Controller {
public static function disable() { public static function disable() {
self::$enabled = false; self::$enabled = false;
} }
} }