Skip to content

Commit

Permalink
Merge branch 'feature/paginated-sitemaps/v3' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Jul 19, 2024
2 parents 1856564 + 956d380 commit e54f7d7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
41 changes: 33 additions & 8 deletions src/gql/resolvers/SitemapResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
use GraphQL\Type\Definition\ResolveInfo;
use nystudio107\seomatic\helpers\Gql as GqlHelper;
use nystudio107\seomatic\helpers\PluginTemplate;
use nystudio107\seomatic\helpers\Sitemap;
use nystudio107\seomatic\models\MetaBundle;
use nystudio107\seomatic\models\SitemapCustomTemplate;
use nystudio107\seomatic\models\SitemapIndexTemplate;
use nystudio107\seomatic\models\SitemapTemplate;
use nystudio107\seomatic\Seomatic;
use yii\web\NotFoundHttpException;

/**
Expand Down Expand Up @@ -55,15 +58,18 @@ public static function getSitemaps($source, array $arguments, $context, ResolveI
return [];
}

$sitemapList = [];
// If either of the source bundle arguments are present, get the sitemap
if (!empty($arguments['sourceBundleType']) || !empty($arguments['sourceBundleHandle'])) {
$filename = self::createFilenameFromComponents($site->groupId, $arguments['sourceBundleType'] ?? '', $arguments['sourceBundleHandle'] ?? '', $siteId);
$filenames = self::createSitemapFilenamesFromComponents($site->groupId, $arguments['sourceBundleType'] ?? '', $arguments['sourceBundleHandle'] ?? '', $siteId);

return [
self::getSitemapItemByFilename($filename),
];
foreach ($filenames as $filename) {
$sitemapList[] = self::getSitemapItemByFilename($filename);
}

return $sitemapList;
}
$sitemapList = [];

$sitemapIndexItems = [self::getSitemapIndexListEntry($siteId, $site->groupId)];

// Scrape each index for individual entries
Expand Down Expand Up @@ -151,7 +157,7 @@ protected static function getSitemapIndexListEntry($siteId, $groupId): array
*/
protected static function getSitemapItemByFilename($filename)
{
if (!preg_match('/sitemaps-(?P<groupId>\d+)-(?P<type>[\w\.*]+)-(?P<handle>[\w\.*]+)-(?P<siteId>\d+)/i', $filename, $matches)) {
if (!preg_match('/sitemaps-(?P<groupId>\d+)-(?P<type>[\w\.*]+)-(?P<handle>[\w\.*]+)-(?P<siteId>\d+)-sitemap(-p(?P<page>\d+))?/i', $filename, $matches)) {
return null;
}

Expand All @@ -173,8 +179,27 @@ protected static function getSitemapItemByFilename($filename)
* @param int $siteId
* @return string
*/
protected static function createFilenameFromComponents(int $groupId, string $bundleType, string $bundleHandle, int $siteId): string
protected static function createSitemapFilenamesFromComponents(int $groupId, string $bundleType, string $bundleHandle, int $siteId): array

Check failure on line 182 in src/gql/resolvers/SitemapResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan

PHPDoc tag @return with type string is incompatible with native type array.
{
return "sitemaps-$groupId-$bundleType-$bundleHandle-$siteId-sitemap.xml";
$metaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceHandle($bundleType, $bundleHandle, $siteId);
if (!$metaBundle) {
return [];
}

$pageSize = $metaBundle->metaSitemapVars->sitemapPageSize ?? null;
if (!$pageSize) {
return ["sitemaps-$groupId-$bundleType-$bundleHandle-$siteId-sitemap.xml"];
}

$seoElementClass = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType($metaBundle->sourceBundleType);
$totalElements = Sitemap::getTotalElementsInSitemap($seoElementClass, $metaBundle);

Check failure on line 195 in src/gql/resolvers/SitemapResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $seoElementClass of static method nystudio107\seomatic\helpers\Sitemap::getTotalElementsInSitemap() expects string, nystudio107\seomatic\base\SeoElementInterface|null given.
$pageCount = (!empty($pageSize) && $pageSize > 0) ? ceil($totalElements / $pageSize) : 1;

Check failure on line 196 in src/gql/resolvers/SitemapResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan

Variable $pageSize in empty() always exists and is not falsy.

$sitemapFilenames = [];
for ($page = 1; $page <= $pageCount; $page++) {
$sitemapFilenames[] = sprintf('sitemaps-%d-%s-%s-%d-sitemap-p%d.xml', $groupId, $bundleType, $bundleHandle, $siteId, $page);
}

return $sitemapFilenames;
}
}
16 changes: 15 additions & 1 deletion src/helpers/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,21 @@ protected static function assetFilesSitemapLink(Asset $asset, MetaBundle $metaBu
}
}

protected static function getElementListSitemap(array $elements)
/**
* Return the total number of elements in a sitemap, respecting metabundle settings.
*
* @param string $seoElementClass
* @param MetaBundle $metaBundle
* @return int|null
*/
public static function getTotalElementsInSitemap(string $seoElementClass, MetaBundle $metaBundle)
{
$totalElements = $seoElementClass::sitemapElementsQuery($metaBundle)->count();

if ($metaBundle->metaSitemapVars->sitemapLimit && ($totalElements > $metaBundle->metaSitemapVars->sitemapLimit)) {
$totalElements = $metaBundle->metaSitemapVars->sitemapLimit;
}

return $totalElements;
}
}
7 changes: 2 additions & 5 deletions src/models/SitemapIndexTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use nystudio107\seomatic\events\RegisterSitemapsEvent;
use nystudio107\seomatic\events\RegisterSitemapUrlsEvent;
use nystudio107\seomatic\helpers\MetaValue as MetaValueHelper;
use nystudio107\seomatic\helpers\Sitemap;
use nystudio107\seomatic\Seomatic;
use yii\base\Event;
use yii\caching\TagDependency;
Expand Down Expand Up @@ -189,11 +190,7 @@ public function render(array $params = []): string
$metaBundle->metaSitemapVars->sitemapLimit = null;
}

$totalElements = $seoElement::sitemapElementsQuery($metaBundle)->count();

if ($metaBundle->metaSitemapVars->sitemapLimit && ($totalElements > $metaBundle->metaSitemapVars->sitemapLimit)) {
$totalElements = $metaBundle->metaSitemapVars->sitemapLimit;
}
$totalElements = Sitemap::getTotalElementsInSitemap($seoElement, $metaBundle);

Check failure on line 193 in src/models/SitemapIndexTemplate.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $seoElementClass of static method nystudio107\seomatic\helpers\Sitemap::getTotalElementsInSitemap() expects string, nystudio107\seomatic\base\SeoElementInterface given.

$pageSize = $metaBundle->metaSitemapVars->sitemapPageSize;
$pageCount = (!empty($pageSize) && $pageSize > 0) ? ceil($totalElements / $pageSize) : 1;
Expand Down

0 comments on commit e54f7d7

Please sign in to comment.