encapsulated calculation of a pages change frequency

This commit is contained in:
Roland Lehmann 2011-06-11 17:20:20 +02:00
parent a9019b9099
commit 74de83227a
2 changed files with 123 additions and 121 deletions

View File

@ -166,48 +166,7 @@ class GoogleSitemap extends Controller {
// If the page has been set to 0 priority, we set a flag so // If the page has been set to 0 priority, we set a flag so
// it won't be included // 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 $page->setChangeFrequency();
// Change frequency, so we set that here.
$date = date('Y-m-d H:i:s');
$prop = $page->toMap();
$created = new SS_Datetime();
$created->value = (isset($prop['Created'])) ? $prop['Created'] : $date;
$now = new SS_Datetime();
$now->value = $date;
$versions = (isset($prop['Version'])) ? $prop['Version'] : 1;
$timediff = $now->format('U') - $created->format('U');
// Check how many revisions have been made over the lifetime of the
// Page for a rough estimate of it's changing frequency.
$period = $timediff / ($versions + 1);
if($period > 60*60*24*365) {
// > 1 year
$page->ChangeFreq = 'yearly';
}
elseif($period > 60*60*24*30) {
$page->ChangeFreq = 'monthly';
}
elseif($period > 60*60*24*7) {
// > 1 week
$page->ChangeFreq = 'weekly';
}
elseif($period > 60*60*24) {
// > 1 day
$page->ChangeFreq = 'daily';
}
elseif($period > 60*60) {
// > 1 hour
$page->ChangeFreq = 'hourly';
} else {
// < 1 hour
$page->ChangeFreq = 'always';
}
$newPages->push($page); $newPages->push($page);
} }
} }

View File

@ -8,13 +8,11 @@
*/ */
class GoogleSitemapDecorator extends DataObjectDecorator { class GoogleSitemapDecorator extends DataObjectDecorator {
} }
/** /**
* @package googlesitemaps * @package googlesitemaps
*/ */
class GoogleSitemapSiteTreeDecorator extends SiteTreeDecorator { class GoogleSitemapSiteTreeDecorator extends SiteTreeDecorator {
function extraStatics() { function extraStatics() {
@ -51,10 +49,8 @@ class GoogleSitemapSiteTreeDecorator extends SiteTreeDecorator {
"<p>" . "<p>" .
sprintf( sprintf(
_t( _t(
'SiteTree.METANOTEPRIORITY', 'SiteTree.METANOTEPRIORITY', "Manually specify a Google Sitemaps priority for this page (%s)"
"Manually specify a Google Sitemaps priority for this page (%s)" ), '<a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=71936#prioritize" target="_blank">?</a>'
),
'<a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=71936#prioritize" target="_blank">?</a>'
) . ) .
"</p>" "</p>"
), ),
@ -92,4 +88,51 @@ class GoogleSitemapSiteTreeDecorator extends SiteTreeDecorator {
return $this->owner->getField('Priority'); return $this->owner->getField('Priority');
} }
} }
/**
* Set a pages change frequency calculated by pages age and number of versions.
* Google expects always, hourly, daily, weekly, monthly, yearly or never as values.
*
* @return void
*/
public function setChangeFrequency() {
// The one field that isn't easy to deal with in the template is
// Change frequency, so we set that here.
$date = date('Y-m-d H:i:s');
$prop = $this->owner->toMap();
$created = new SS_Datetime();
$created->value = (isset($prop['Created'])) ? $prop['Created'] : $date;
$now = new SS_Datetime();
$now->value = $date;
$versions = (isset($prop['Version'])) ? $prop['Version'] : 1;
$timediff = $now->format('U') - $created->format('U');
// Check how many revisions have been made over the lifetime of the
// Page for a rough estimate of it's changing frequency.
$period = $timediff / ($versions + 1);
if ($period > 60 * 60 * 24 * 365) {
// > 1 year
$this->owner->ChangeFreq = 'yearly';
} elseif ($period > 60 * 60 * 24 * 30) {
$this->owner->ChangeFreq = 'monthly';
} elseif ($period > 60 * 60 * 24 * 7) {
// > 1 week
$this->owner->ChangeFreq = 'weekly';
} elseif ($period > 60 * 60 * 24) {
// > 1 day
$this->owner->ChangeFreq = 'daily';
} elseif ($period > 60 * 60) {
// > 1 hour
$this->owner->ChangeFreq = 'hourly';
} else {
// < 1 hour
$this->owner->ChangeFreq = 'always';
}
}
} }