NEW Include version number in meta generator tag (#2721)

This commit is contained in:
Steve Boyd 2022-03-09 14:48:02 +13:00 committed by GitHub
parent 07df04f3c4
commit 3b62336180
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 7 deletions

View File

@ -21,6 +21,7 @@ use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Manifest\ModuleResource;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Core\Manifest\VersionProvider;
use SilverStripe\Core\Resettable;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\CheckboxField;
@ -339,7 +340,16 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
* @config
* @var string
*/
private static $meta_generator = 'SilverStripe - https://www.silverstripe.org';
private static $meta_generator = 'Silverstripe CMS';
/**
* Whether to display the version portion of the meta generator tag
* Set to false if it's viewed as a concern.
*
* @config
* @var bool
*/
private static $show_meta_generator_version = true;
protected $_cache_statusFlags = null;
@ -395,6 +405,11 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
*/
protected $creatableChildrenCache;
/**
* @var VersionProvider
*/
private $versionProvider;
/**
* Fetches the {@link SiteTree} object that maps to a link.
*
@ -1401,13 +1416,13 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
'content' => $this->obj('Title')->forTemplate()
];
$generator = trim(Config::inst()->get(self::class, 'meta_generator'));
if (!empty($generator)) {
$generator = $this->getGenerator();
if ($generator) {
$tags['generator'] = [
'attributes' => [
'name' => 'generator',
'content' => $generator,
],
'content' => $generator
]
];
}
@ -1449,6 +1464,49 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
return $tags;
}
/**
* Create the value for the meta generator tag
* Will suffix on the major.minor version of a stable tag
*
* @return string
*/
private function getGenerator(): string
{
$generator = trim(Config::inst()->get(self::class, 'meta_generator'));
if ($generator === '') {
return '';
}
if (self::config()->get('show_meta_generator_version')) {
$version = $this->getVersionProvider()->getModuleVersion('silverstripe/framework');
// Only include stable version numbers so as not to clutter any aggregate reports
// with non-standard versions e.g. forks
if (preg_match('#^([0-9]+\.[0-9]+)\.[0-9]+$#', $version, $m)) {
$generator .= ' ' . $m[1];
}
}
return $generator;
}
/**
* @return VersionProvider
*/
public function getVersionProvider(): VersionProvider
{
if ($this->versionProvider === null) {
$this->versionProvider = VersionProvider::singleton();
}
return $this->versionProvider;
}
/**
* @param VersionProvider $versionProvider
*/
public function setVersionProvider(VersionProvider $versionProvider): void
{
$this->versionProvider = $versionProvider;
}
/**
* Return the title, description, keywords and language metatags.
*

View File

@ -21,7 +21,7 @@
"require": {
"silverstripe/admin": "^1.9@dev",
"silverstripe/campaign-admin": "^1.7@dev",
"silverstripe/framework": "^4.10",
"silverstripe/framework": "^4.11",
"silverstripe/reports": "^4.7@dev",
"silverstripe/siteconfig": "^4.7@dev",
"silverstripe/versioned": "^1.7@dev",

View File

@ -14,6 +14,7 @@ use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Manifest\VersionProvider;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\DataObject;
@ -1432,6 +1433,12 @@ class SiteTreeTest extends SapphireTest
$charset = Config::inst()->get(ContentNegotiator::class, 'encoding');
$mockVersionProvider = $this->getMockBuilder(VersionProvider::class)
->setMethods(['getModuleVersion'])
->getMock();
$mockVersionProvider->method('getModuleVersion')->willReturn('4.50.99');
$page->setVersionProvider($mockVersionProvider);
$expected = [
'title' => [
'tag' => 'title',
@ -1440,7 +1447,11 @@ class SiteTreeTest extends SapphireTest
'generator' => [
'attributes' => [
'name' => 'generator',
'content' => Config::inst()->get(SiteTree::class, 'meta_generator')
'content' => sprintf(
'%s %s',
Config::inst()->get(SiteTree::class, 'meta_generator'),
'4.50'
)
],
],
'contentType' => [
@ -1470,6 +1481,12 @@ class SiteTreeTest extends SapphireTest
];
$this->assertEquals($expected, $page->MetaComponents());
// test the meta generator tag version can be configured off
Config::modify()->set(SiteTree::class, 'show_meta_generator_version', false);
$content = $expected['generator']['attributes']['content'];
$expected['generator']['attributes']['content'] = str_replace(' 4.50', '', $content);
$this->assertEquals($expected, $page->MetaComponents());
}
/**