SitemapPresenter
extends BasePresenter
in package
uses
SitemapBuilderTrait
Table of Contents
Constants
- FILE_INDEX : mixed = 'sitemap.xml'
- FILE_PAGES : mixed = 'sitemap-pages.xml'
- SITEMAP_BATCH_SIZE : mixed = 500
- Number of entities loaded by one query.
Properties
- $cronLockManager : CronLockManager
- $linkGenerator : LinkGenerator
- $operationRunner : OperationRunner
- Boundary of one cron action (see {@see self::tryCall()}).
- $settingContext : SettingContext
- $settingManager : SettingManager
- $sitemapLocalizationManager : LocalizationManager
- $sitemapRouterDispatcher : RouterDispatcher
- $sitemapSiteBaseUrl : SiteBaseUrl
- Website address from `site.baseUrl`.
- $debugMode : bool
- $productionMode : bool
- $pageManager : PageManager
Methods
- __construct() : mixed
- actionGenerate() : void
- Generates the XML sitemap of System pages with all language variants (hreflang).
- actionIndex() : void
- Generates the sitemap index linking all generated `sitemap-*.xml` files in the directory.
- releaseLock() : void
- Releases the lock at most once; called by both `shutdown()` and the PHP shutdown function.
- sendResponse() : void
- setConfigParameters() : void
- shutdown() : void
- startup() : void
- buildSitemapXml() : string
- Builds the sitemap XML from entities.
- generateUrl() : string|null
- isSameSiteRequest() : bool
- Tells whether the request comes from the own site (Nette SameSite cookie); protects GET actions with a side effect.
- 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.
- sendError() : never
- sendSkipped() : never
- Reports that the job did only part of the work and the next call finishes the rest (e.g. a reindex done in chunks).
- sendSuccess() : never
- shouldSaveSitemap() : bool
- tryCall() : bool
- Runs the `action<Action>()` method as one operation (transitional).
- warmupTranslator() : void
- Warms the translator cache from already loaded entities, avoiding the global `warmupCache(null)`.
- buildPageUrl() : string|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.
- loadPages() : array<int, Page>
- resolveCanonicalRoute() : Route|null
- resolveSitemapDir() : string
- resolveSitemapItemId() : int
Constants
FILE_INDEX
private
mixed
FILE_INDEX
= 'sitemap.xml'
FILE_PAGES
private
mixed
FILE_PAGES
= 'sitemap-pages.xml'
SITEMAP_BATCH_SIZE
Number of entities loaded by one query.
private
mixed
SITEMAP_BATCH_SIZE
= 500
The file (bucket) size is computed separately from the language count, see SitemapBucketWriter::bucketSizeFor().
Properties
$cronLockManager
public
CronLockManager
$cronLockManager
Attributes
- #[Inject]
$linkGenerator
public
LinkGenerator
$linkGenerator
Attributes
- #[Inject]
$operationRunner
Boundary of one cron action (see {@see self::tryCall()}).
public
OperationRunner
$operationRunner
Attributes
- #[Inject]
$settingContext
public
SettingContext
$settingContext
Attributes
- #[Inject]
$settingManager
public
SettingManager
$settingManager
Attributes
- #[Inject]
$sitemapLocalizationManager
public
LocalizationManager
$sitemapLocalizationManager
Attributes
- #[Inject]
$sitemapRouterDispatcher
public
RouterDispatcher
$sitemapRouterDispatcher
Attributes
- #[Inject]
$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]
$debugMode
protected
bool
$debugMode
= false
$productionMode
protected
bool
$productionMode
= false
$pageManager read-only
private
PageManager
$pageManager
Methods
__construct()
public
__construct(PageManager $pageManager) : mixed
Parameters
- $pageManager : PageManager
actionGenerate()
Generates the XML sitemap of System pages with all language variants (hreflang).
public
actionGenerate() : void
URL: /cron/system/sitemap/generate?token=xxx[&save=1]
Frequency: daily, at night.
Attributes
- #[CronJob]
- $frequency: 'daily:03:34'
- $outbound: false
- $query: ['save' => '1']
actionIndex()
Generates the sitemap index linking all generated `sitemap-*.xml` files in the directory.
public
actionIndex() : void
robots.txt then only needs to point here.
URL: /cron/system/sitemap/index?token=xxx[&save=1]
Frequency: daily, at night, after the other sitemaps.
Attributes
- #[CronJob]
- $frequency: 'daily:04:30'
- $outbound: false
- $query: ['save' => '1']
releaseLock()
Releases the lock at most once; called by both `shutdown()` and the PHP shutdown function.
public
releaseLock() : void
sendResponse()
public
sendResponse(Response $response) : void
Parameters
- $response : Response
setConfigParameters()
public
setConfigParameters([bool $debugMode = false ][, bool $productionMode = false ]) : void
Parameters
- $debugMode : bool = false
- $productionMode : bool = false
shutdown()
public
shutdown(Response $response) : void
Parameters
- $response : Response
startup()
public
startup() : void
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
Return values
stringgenerateUrl()
protected
generateUrl(string $destination, array<string, mixed> $params) : string|null
Parameters
- $destination : string
- $params : array<string, mixed>
Return values
string|nullisSameSiteRequest()
Tells whether the request comes from the own site (Nette SameSite cookie); protects GET actions with a side effect.
protected
isSameSiteRequest() : bool
getHttpRequest() returns the IRequest interface, while isSameSite() exists only on
Nette\Http\Request.
Return values
boolloadActiveLocalizations()
Loads active localizations including the linked language.
protected
loadActiveLocalizations() : array<int, Localization>
Return values
array<int, Localization>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=1it returns a JSON preview (how many buckets would be created) - with
?save=1it 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.xmlfiles (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
$limitentities 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
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)
sendError()
protected
sendError(string $message[, int $code = 500 ]) : never
Parameters
- $message : string
- $code : int = 500
Return values
neversendSkipped()
Reports that the job did only part of the work and the next call finishes the rest (e.g. a reindex done in chunks).
protected
sendSkipped(string $reason[, array<string, mixed> $data = [] ]) : never
The scheduler does not record skipped as a run, so the job stays due and is called
again on the next tick.
Parameters
- $reason : string
- $data : array<string, mixed> = []
Return values
neversendSuccess()
protected
sendSuccess([array<string|int, mixed> $data = [] ]) : never
Parameters
- $data : array<string|int, mixed> = []
Return values
nevershouldSaveSitemap()
protected
shouldSaveSitemap() : bool
Return values
booltryCall()
Runs the `action<Action>()` method as one operation (transitional).
protected
tryCall(string $method, array<string, mixed> $params) : bool
Cron has no internalAction*(): its actions call the Api managers directly, so without this in
scoped mode nothing would ever empty the unit of work of a cron run. The boundary is the action
only; startup() (settings, token, lock) stays outside like the startup of an Api request. A cron
action ends with AbortException from send*(), which is a regular end: the outcome comes from the
response (ok succeeded, skipped unknown — possibly a part of a batch was done, error failed).
Render and signal methods are not wrapped (cron presenters have none).
Parameters
- $method : string
- $params : array<string, mixed>
Return values
boolwarmupTranslator()
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>
buildPageUrl()
private
buildPageUrl(Page $page, string $localeKey, int $languageId) : string|null
Parameters
- $page : Page
- $localeKey : string
- $languageId : int
Return values
string|nullbuildUrlsByLocale()
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
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
neverloadPages()
private
loadPages() : array<int, Page>
Return values
array<int, Page>resolveCanonicalRoute()
private
resolveCanonicalRoute(Page $page) : Route|null
Parameters
- $page : Page
Return values
Route|nullresolveSitemapDir()
private
resolveSitemapDir() : string
Return values
stringresolveSitemapItemId()
private
static resolveSitemapItemId(object $item) : int
Parameters
- $item : object