Skip to content

Commit 30937ab

Browse files
committed
Add CrawlCache::writeFilesIter
1 parent eca9e3f commit 30937ab

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/CrawlCache.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,44 @@ function( $dir ) use ( $path ) {
148148
}
149149
}
150150

151+
/**
152+
* Write path contents to the crawled site dir,
153+
* returning an Iterator of the same paths.
154+
*
155+
* @param \Iterator $paths
156+
* @return \Iterator
157+
*/
158+
public static function writeFilesIter( \Iterator $paths ) : \Iterator {
159+
$cache_hits = 0;
160+
foreach ( $paths as $path ) {
161+
$body = $path['body'] ?? null;
162+
$is_cacheable = true;
163+
$status = $path['status'];
164+
165+
if ( $status === 404 ) {
166+
$is_cacheable = false;
167+
}
168+
elseif ( in_array( $status, WP2STATIC_REDIRECT_CODES ) ) {
169+
$is_cacheable = false;
170+
}
171+
172+
$content_hash = null;
173+
if ( $is_cacheable && $body ) {
174+
$content_hash = md5( $body );
175+
$path['content_hash'] = $content_hash;
176+
}
177+
178+
if ( $is_cacheable && $content_hash && CrawlCache::getUrl( $path['path'], $content_hash ) ) {
179+
$cache_hits++;
180+
} elseif ( $body ) {
181+
$static_path = StaticSite::transformPath( $path['path'] );
182+
StaticSite::add( $static_path, $body );
183+
}
184+
185+
yield $path;
186+
}
187+
}
188+
151189
public static function addUrl( string $url, string $page_hash, int $status,
152190
?string $redirect_to ) : void {
153191
global $wpdb;

0 commit comments

Comments
 (0)