CoolCMS API

SitemapBuilderTrait

Shared logic of the cron sitemap presenters.

Loads localizations, builds XML with hreflang alternates, writes to disk or responds directly, and wraps LinkGenerator.

Table of Contents

Constants

SITEMAP_BATCH_SIZE  : mixed = 500
Number of entities loaded by one query.

Properties

$linkGenerator  : LinkGenerator
$sitemapLocalizationManager  : LocalizationManager
$sitemapRouterDispatcher  : RouterDispatcher
$sitemapSiteBaseUrl  : SiteBaseUrl
Website address from `site.baseUrl`.

Methods

buildSitemapXml()  : string
Builds the sitemap XML from entities.
generateUrl()  : string|null
loadActiveLocalizations()  : array<int, Localization>
Loads active localizations including the linked language.
respondBucketedSitemap()  : void
Splits a large sitemap into N files by fixed ID ranges.
respondSitemap()  : void
Sends the XML response, or writes it to a file and sends a JSON success.
shouldSaveSitemap()  : bool
warmupTranslator()  : void
Warms the translator cache from already loaded entities, avoiding the global `warmupCache(null)`.
buildUrlsByLocale()  : array<string, string>
Returns the item URL for every active localization (localization key => URL); an empty result means the item does not belong in the sitemap.
failSitemapWrite()  : never
Reports a write failure: the detail (including the path) goes to the server log only, the response carries no absolute path.
resolveSitemapDir()  : string
resolveSitemapItemId()  : int

Constants

Properties

$sitemapSiteBaseUrl

Website address from `site.baseUrl`.

public SiteBaseUrl $sitemapSiteBaseUrl

A sitemap holds published addresses, so it must not depend on the host cron called the site under. An empty setting falls back to the request address.

Attributes
#[Inject]

Methods

buildSitemapXml()

Builds the sitemap XML from entities.

protected buildSitemapXml(iterable<string|int, TItem> $items, callable(TItem $item, string $localeKey, int $languageId): Array $urlBuilder, callable(TItem $item): array{changefreq?: string, priority?: ?string, lastmod?: ?string} $metaBuilder, array<int, Localization> $localizations) : string

Calls $urlBuilder for every entity and localization, and $metaBuilder once per entity.

Parameters
$items : iterable<string|int, TItem>
$urlBuilder : callable(TItem $item, string $localeKey, int $languageId): Array
$metaBuilder : callable(TItem $item): array{changefreq?: string, priority?: ?string, lastmod?: ?string}
$localizations : array<int, Localization>
Tags
template
Return values
string

generateUrl()

protected generateUrl(string $destination, array<string, mixed> $params) : string|null
Parameters
$destination : string
$params : array<string, mixed>
Return values
string|null

respondBucketedSitemap()

Splits a large sitemap into N files by fixed ID ranges.

protected respondBucketedSitemap(string $filenamePrefix, callable(): int $maxIdResolver, callable(int $afterId, int $limit): array<int, TItem> $batchLoader, callable(TItem $item, string $localeKey, int $languageId): Array $urlBuilder, callable(TItem $item): array{changefreq?: string, priority?: ?string, lastmod?: ?string} $metaBuilder[, array<string, int|string> $statsExtra = [] ]) : void

The ID boundaries are stable: deleting one item does not move others between files.

  • without ?save=1 it returns a JSON preview (how many buckets would be created)
  • with ?save=1 it reads entities in keyset batches and writes each to the temp file of its bucket; only when all are written does it publish them atomically and delete surplus old {prefix}-N.xml files (StagedFileSet)

Only one batch (SITEMAP_BATCH_SIZE) is held in memory; the Api read of the next batch runs UnitOfWork::clear(), so nothing keeps the previous batch alive. The bucket size follows the language count and the protocol limits are enforced by SitemapBucketWriter.

Parameters
$filenamePrefix : string

e.g. "sitemap-bazaar-adverts" (without number and extension)

$maxIdResolver : callable(): int

returns the max ID of the active subset (0 = no data), preview only

$batchLoader : callable(int $afterId, int $limit): array<int, TItem>

loads at most $limit entities with ID > $afterId, ascending by ID

$urlBuilder : callable(TItem $item, string $localeKey, int $languageId): Array
$metaBuilder : callable(TItem $item): array{changefreq?: string, priority?: ?string, lastmod?: ?string}
$statsExtra : array<string, int|string> = []

extra statistics for the JSON response

Tags
template

respondSitemap()

Sends the XML response, or writes it to a file and sends a JSON success.

protected respondSitemap(string $xml[, string|null $filename = null ][, array<string, int|string> $stats = [] ]) : void
Parameters
$xml : string
$filename : string|null = null
$stats : array<string, int|string> = []

extra statistics for the JSON response (e.g. items, languages)

warmupTranslator()

Warms the translator cache from already loaded entities, avoiding the global `warmupCache(null)`.

protected warmupTranslator(string $translatorClass, array<int, object> $entities) : void
Parameters
$translatorClass : string
$entities : array<int, object>

buildUrlsByLocale()

Returns the item URL for every active localization (localization key => URL); an empty result means the item does not belong in the sitemap.

private buildUrlsByLocale(TItem $item, callable(TItem $item, string $localeKey, int $languageId): Array $urlBuilder, array<int, Localization> $localizations) : array<string, string>
Parameters
$item : TItem
$urlBuilder : callable(TItem $item, string $localeKey, int $languageId): Array
$localizations : array<int, Localization>
Tags
template
Return values
array<string, string>

failSitemapWrite()

Reports a write failure: the detail (including the path) goes to the server log only, the response carries no absolute path.

private failSitemapWrite(RuntimeException $e, string $fileName) : never
Parameters
$e : RuntimeException
$fileName : string
Return values
never

resolveSitemapItemId()

private static resolveSitemapItemId(object $item) : int
Parameters
$item : object
Return values
int
On this page

Search results