SitemapBucketWriter
in package
Splits a large sitemap into `{prefix}-{bucket}.xml` files by fixed id ranges.
Bucket is intdiv(id - 1, bucketSize), so id boundaries are stable and deleting one item does
not move the others between files.
- The bucket size derives from the language count (bucketSizeFor()), because every item
yields one
<url>per language. - Protocol limits (50 000 URLs, 50 MB) are guarded: when the next item would exceed them, the
writer continues in
{prefix}-{bucket}-{n}.xml(n = 1, 2, ...). - Items are written as they arrive; only the open file is held in memory.
Files are created through StagedFileSet::reserve(); the caller controls publishing (commit).
IMPORTANT: items must arrive in ascending id order, otherwise add() throws.
Table of Contents
Constants
- MAX_BYTES : mixed = 52428800
- Protocol limit of the uncompressed file size (50 MiB).
- MAX_URLS : mixed = 50000
- Sitemaps.org protocol limit of URLs per file.
- TARGET_URLS : mixed = 45000
- Target URL ceiling per file, kept below the protocol limit.
- BYTES_RESERVE : mixed = 5242880
- Size reserve: a new file starts when written bytes reach `MAX_BYTES` minus this reserve.
Properties
- $bucketSize : int
- $current : SitemapUrlsetWriter|null
- $currentBucket : int
- $currentFile : string
- $currentItems : int
- $currentPart : int
- $filenamePrefix : string
- $lastId : int
- $maxUrls : int
- $set : StagedFileSet
- $softMaxBytes : int
- $written : array<int, array{bucket: int, file: string, items: int, urls: int}>
Methods
- __construct() : mixed
- add() : void
- bucketSizeFor() : int
- Returns how many ids belong to one file so that URLs (items x languages) stay within {@see TARGET_URLS}.
- discard() : void
- Closes the open file after an error; temporary files are cleaned up by `StagedFileSet::abort()`.
- finish() : array<int, array{bucket: int, file: string, items: int, urls: int}>
- Finishes the last file and returns the written files in write order.
- stalePattern() : string
- Returns the glob pattern of one set's files: numbered buckets only.
- closeCurrent() : void
- isFull() : bool
- openFile() : void
Constants
MAX_BYTES
Protocol limit of the uncompressed file size (50 MiB).
public
mixed
MAX_BYTES
= 52428800
MAX_URLS
Sitemaps.org protocol limit of URLs per file.
public
mixed
MAX_URLS
= 50000
TARGET_URLS
Target URL ceiling per file, kept below the protocol limit.
public
mixed
TARGET_URLS
= 45000
BYTES_RESERVE
Size reserve: a new file starts when written bytes reach `MAX_BYTES` minus this reserve.
private
mixed
BYTES_RESERVE
= 5242880
Properties
$bucketSize read-only
private
int
$bucketSize
$current
private
SitemapUrlsetWriter|null
$current
= null
$currentBucket
private
int
$currentBucket
= -1
$currentFile
private
string
$currentFile
= ''
$currentItems
private
int
$currentItems
= 0
$currentPart
private
int
$currentPart
= 0
$filenamePrefix read-only
private
string
$filenamePrefix
$lastId
private
int
$lastId
= 0
$maxUrls read-only
private
int
$maxUrls
= self::MAX_URLS
$set read-only
private
StagedFileSet
$set
$softMaxBytes read-only
private
int
$softMaxBytes
= self::MAX_BYTES - self::BYTES_RESERVE
$written
private
array<int, array{bucket: int, file: string, items: int, urls: int}>
$written
= []
Methods
__construct()
public
__construct(StagedFileSet $set, string $filenamePrefix, int $bucketSize[, int $maxUrls = self::MAX_URLS ][, int $softMaxBytes = self::MAX_BYTES - self::BYTES_RESERVE ]) : mixed
Parameters
- $set : StagedFileSet
- $filenamePrefix : string
- $bucketSize : int
- $maxUrls : int = self::MAX_URLS
- $softMaxBytes : int = self::MAX_BYTES - self::BYTES_RESERVE
add()
public
add(int $id, array<string, string> $urlsByLocale, array{changefreq?: ?string, priority?: ?string, lastmod?: ?string} $meta) : void
Parameters
- $id : int
- $urlsByLocale : array<string, string>
- $meta : array{changefreq?: ?string, priority?: ?string, lastmod?: ?string}
Tags
bucketSizeFor()
Returns how many ids belong to one file so that URLs (items x languages) stay within {@see TARGET_URLS}.
public
static bucketSizeFor(int $languageCount) : int
Parameters
- $languageCount : int
Return values
intdiscard()
Closes the open file after an error; temporary files are cleaned up by `StagedFileSet::abort()`.
public
discard() : void
finish()
Finishes the last file and returns the written files in write order.
public
finish() : array<int, array{bucket: int, file: string, items: int, urls: int}>
Return values
array<int, array{bucket: int, file: string, items: int, urls: int}>stalePattern()
Returns the glob pattern of one set's files: numbered buckets only.
public
static stalePattern(string $filenamePrefix) : string
IMPORTANT: a looser pattern such as sitemap-blog-news-*.xml would also delete the unrelated
sitemap-blog-news-categories.xml.
Parameters
- $filenamePrefix : string
Return values
stringcloseCurrent()
private
closeCurrent() : void
isFull()
private
isFull(int $nextUrls) : bool
Parameters
- $nextUrls : int
Return values
boolopenFile()
private
openFile(int $bucket, int $part) : void
Parameters
- $bucket : int
- $part : int