Skip to content

Commit

Permalink
Guard agaist exceeding max parameter limit
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Mar 17, 2024
1 parent 82ba49d commit 4c0edf6
Showing 1 changed file with 77 additions and 55 deletions.
132 changes: 77 additions & 55 deletions src/helpers/SiteUriHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,22 @@ public static function getCachedSiteUris(array $cacheIds): array

$siteUriModels = [];

/** @var array $siteUris */
$siteUris = CacheRecord::find()
->select(['siteId', 'uri'])
->where(['id' => $cacheIds])
->orderBy([
'siteId' => SORT_ASC,
'uri' => SORT_ASC,
])
->asArray()
->all();
$cacheIdChunks = self::getChunkedQueryParams($cacheIds);
foreach ($cacheIdChunks as $cacheIds) {
/** @var array $siteUris */
$siteUris = CacheRecord::find()
->select(['siteId', 'uri'])
->where(['id' => $cacheIds])
->orderBy([
'siteId' => SORT_ASC,
'uri' => SORT_ASC,
])
->asArray()
->all();

foreach ($siteUris as $siteUri) {
$siteUriModels[] = new SiteUriModel($siteUri);
foreach ($siteUris as $siteUri) {
$siteUriModels[] = new SiteUriModel($siteUri);
}
}

return $siteUriModels;
Expand All @@ -311,20 +314,23 @@ public static function getElementSiteUris(array $elementIds): array

$siteUriModels = [];

// Get the site URIs of the elements themselves
/** @var array $siteUris */
$siteUris = Element_SiteSettings::find()
->select(['siteId', 'uri'])
->where(['elementId' => $elementIds])
->andWhere([
'not',
['uri' => null],
])
->asArray()
->all();
$elementIdChunks = self::getChunkedQueryParams($elementIds);
foreach ($elementIdChunks as $elementIds) {
// Get the site URIs of the elements themselves
/** @var array $siteUris */
$siteUris = Element_SiteSettings::find()
->select(['siteId', 'uri'])
->where(['elementId' => $elementIds])
->andWhere([
'not',
['uri' => null],
])
->asArray()
->all();

foreach ($siteUris as $siteUri) {
$siteUriModels[] = new SiteUriModel($siteUri);
foreach ($siteUris as $siteUri) {
$siteUriModels[] = new SiteUriModel($siteUri);
}
}

return $siteUriModels;
Expand All @@ -345,34 +351,38 @@ public static function getAssetSiteUris(array $elementIds): array
}

$urls = [];
$assets = Asset::find()
->id($elementIds)
->all();

foreach ($assets as $asset) {
$url = $asset->getUrl();
$urls[] = $url;

// Get all existing image transform URLs
if ($asset->kind === Asset::KIND_IMAGE) {
$indexes = (new Query())
->select([
'filename',
'transformString',
])
->from([Table::IMAGETRANSFORMINDEX])
->where([
'assetId' => $asset->id,
'fileExists' => true,
])
->all();

foreach ($indexes as $index) {
$urls[] = str_replace(
$asset->getFilename(),
$index['transformString'] . '/' . $asset->getFilename(),
$url,
);
$elementIdChunks = self::getChunkedQueryParams($elementIds);
foreach ($elementIdChunks as $elementIds) {
$assets = Asset::find()
->id($elementIds)
->all();

foreach ($assets as $asset) {
$url = $asset->getUrl();
$urls[] = $url;

// Get all existing image transform URLs
if ($asset->kind === Asset::KIND_IMAGE) {
$indexes = (new Query())
->select([
'filename',
'transformString',
])
->from([Table::IMAGETRANSFORMINDEX])
->where([
'assetId' => $asset->id,
'fileExists' => true,
])
->all();

foreach ($indexes as $index) {
$urls[] = str_replace(
$asset->getFilename(),
$index['transformString'] . '/' . $asset->getFilename(),
$url,
);
}
}
}
}
Expand All @@ -394,9 +404,7 @@ public static function getCacheIdsFromSiteUris(array $siteUris): array

$cacheIdSets = [];

// Chunk the site URIs to avoid exceeding the maximum parameter limit.
// https://github.com/putyourlightson/craft-blitz/issues/639
$siteUriChunks = array_chunk($siteUris, 10000);
$siteUriChunks = self::getChunkedQueryParams($siteUris, 2);
foreach ($siteUriChunks as $siteUris) {
$condition = ['or'];

Expand Down Expand Up @@ -590,4 +598,18 @@ public static function getSiteUrisFlattenedToArrays(array $siteUris): array

return $flatennedSiteUris;
}

/**
* Returns a chunked array of query params, to avoid exceeding the maximum parameter limit.
* https://github.com/putyourlightson/craft-blitz/issues/639
*
* @since 4.14.0
*/
private static function getChunkedQueryParams(array $items, int $paramsPerItem = 1): array
{
// Divide 65000 by the number of parameters per item to ensure we never go over the hard maximum parameter limit of 65535.
$chunkLength = (int)ceil(65000 / $paramsPerItem);

return array_chunk($items, $chunkLength);
}
}

0 comments on commit 4c0edf6

Please sign in to comment.