diff --git a/controllers/ViewPhotoAlbum.php b/controllers/ViewPhotoAlbum.php index 1d2272a..a491005 100644 --- a/controllers/ViewPhotoAlbum.php +++ b/controllers/ViewPhotoAlbum.php @@ -289,10 +289,4 @@ class ViewPhotoAlbum extends HTMLController $description = !empty($tag->description) ? $tag->description : ''; return new AlbumHeaderBox($tag->tag, $description, $back_link, $back_link_title); } - - public function __destruct() - { - if (isset($this->iterator)) - $this->iterator->clean(); - } } diff --git a/controllers/ViewTimeline.php b/controllers/ViewTimeline.php index d5dd9d3..33d933c 100644 --- a/controllers/ViewTimeline.php +++ b/controllers/ViewTimeline.php @@ -54,10 +54,4 @@ class ViewTimeline extends HTMLController // Set the canonical url. $this->page->setCanonicalUrl(BASEURL . '/timeline/'); } - - public function __destruct() - { - if (isset($this->iterator)) - $this->iterator->clean(); - } } diff --git a/models/Asset.php b/models/Asset.php index e8cdedd..a4f8353 100644 --- a/models/Asset.php +++ b/models/Asset.php @@ -24,7 +24,7 @@ class Asset protected $tags; protected $thumbnails; - protected function __construct(array $data) + public function __construct(array $data) { foreach ($data as $attribute => $value) { diff --git a/models/AssetIterator.php b/models/AssetIterator.php index 5690fb6..9088719 100644 --- a/models/AssetIterator.php +++ b/models/AssetIterator.php @@ -1,42 +1,50 @@ db = Registry::get('db'); $this->direction = $direction; - $this->res_assets = $res_assets; - $this->res_meta = $res_meta; - $this->res_thumbs = $res_thumbs; $this->return_format = $return_format; + $this->rowCount = $stmt_assets->rowCount(); + + $this->assets_iterator = new CachedPDOIterator($stmt_assets); + $this->assets_iterator->rewind(); + + $this->meta_iterator = new CachedPDOIterator($stmt_meta); + $this->thumbs_iterator = new CachedPDOIterator($stmt_thumbs); } - public function next() + public static function all() { - $row = $this->db->fetchAssoc($this->res_assets); + return self::getByOptions(); + } - // No more rows? + public function current(): mixed + { + $row = $this->assets_iterator->current(); if (!$row) - return false; + return $row; - // Looks up metadata. + // Collect metadata $row['meta'] = []; - while ($meta = $this->db->fetchAssoc($this->res_meta)) + $this->meta_iterator->rewind(); + foreach ($this->meta_iterator as $meta) { if ($meta['id_asset'] != $row['id_asset']) continue; @@ -44,54 +52,23 @@ class AssetIterator extends Asset $row['meta'][$meta['variable']] = $meta['value']; } - // Reset internal pointer for next asset. - $this->db->data_seek($this->res_meta, 0); - - // Looks up thumbnails. + // Collect thumbnails $row['thumbnails'] = []; - while ($thumbs = $this->db->fetchAssoc($this->res_thumbs)) + $this->thumbs_iterator->rewind(); + foreach ($this->thumbs_iterator as $thumb) { - if ($thumbs['id_asset'] != $row['id_asset']) + if ($thumb['id_asset'] != $row['id_asset']) continue; - $row['thumbnails'][$thumbs['selector']] = $thumbs['filename']; + $row['thumbnails'][$thumb['selector']] = $thumb['filename']; } - // Reset internal pointer for next asset. - $this->db->data_seek($this->res_thumbs, 0); - if ($this->return_format === 'object') return new Asset($row); else return $row; } - public function reset() - { - $this->db->data_seek($this->res_assets, 0); - $this->db->data_seek($this->res_meta, 0); - $this->db->data_seek($this->res_thumbs, 0); - } - - public function clean() - { - if (!$this->res_assets) - return; - - $this->db->free($this->res_assets); - $this->res_assets = null; - } - - public function num() - { - return $this->db->rowCount($this->res_assets); - } - - public static function all() - { - return self::getByOptions(); - } - public static function getByOptions(array $options = [], $return_count = false, $return_format = 'object') { $params = [ @@ -210,13 +187,38 @@ class AssetIterator extends Asset return $iterator; } - public function isAscending() + public function key(): mixed + { + return $this->assets_iterator->key(); + } + + public function isAscending(): bool { return $this->direction === 'asc'; } - public function isDescending() + public function isDescending(): bool { return $this->direction === 'desc'; } + + public function next(): void + { + $this->assets_iterator->next(); + } + + public function num(): int + { + return $this->rowCount; + } + + public function rewind(): void + { + $this->assets_iterator->rewind(); + } + + public function valid(): bool + { + return $this->assets_iterator->valid(); + } } diff --git a/models/PhotoMosaic.php b/models/PhotoMosaic.php index 8326e1d..4859ef4 100644 --- a/models/PhotoMosaic.php +++ b/models/PhotoMosaic.php @@ -8,11 +8,11 @@ class PhotoMosaic { - private $descending; + private bool $descending; private AssetIterator $iterator; - private $layouts; - private $processedImages = 0; - private $queue = []; + private array $layouts; + private int $processedImages = 0; + private array $queue = []; const IMAGE_MASK_ALL = Image::TYPE_PORTRAIT | Image::TYPE_LANDSCAPE | Image::TYPE_PANORAMA; const NUM_DAYS_CUTOFF = 7; @@ -25,11 +25,6 @@ class PhotoMosaic $this->descending = $iterator->isDescending(); } - public function __destruct() - { - $this->iterator->clean(); - } - private function availableLayouts() { static $layouts = [ @@ -86,9 +81,14 @@ class PhotoMosaic } } - // Check whatever's next up! - while (($asset = $this->iterator->next()) && ($image = $asset->getImage())) + // Check whatever's up next! + // NB: not is not a `foreach` so as to not reset the iterator implicitly + while ($this->iterator->valid()) { + $asset = $this->iterator->current(); + $image = $asset->getImage(); + $this->iterator->next(); + // Give up on the recordset once dates are too far apart if (isset($refDate) && abs(self::daysApart($image->getDateCaptured(), $refDate)) > self::NUM_DAYS_CUTOFF) { diff --git a/templates/FeaturedThumbnailManager.php b/templates/FeaturedThumbnailManager.php index ffbe1c0..49e58de 100644 --- a/templates/FeaturedThumbnailManager.php +++ b/templates/FeaturedThumbnailManager.php @@ -8,12 +8,12 @@ class FeaturedThumbnailManager extends SubTemplate { - private $assets; + private $iterator; private $currentThumbnailId; - public function __construct(AssetIterator $assets, $currentThumbnailId) + public function __construct(AssetIterator $iterator, $currentThumbnailId) { - $this->assets = $assets; + $this->iterator = $iterator; $this->currentThumbnailId = $currentThumbnailId; } @@ -25,7 +25,7 @@ class FeaturedThumbnailManager extends SubTemplate