userAgent) ->timeout(30) ->retry(2, 1000) ->withHeaders(['Range' => "bytes={$start}-{$end}"]) ->get(self::WARC_BASE_URL.$attributes['filename']); if (! $response->successful()) { return []; } $html = $this->extractHtmlFromWarcSegment($response->body()); return $html === null ? [] : $this->extractHtmlMeta($html); } /** * A WARC segment (gzip-compressed) contains WARC headers, then the raw HTTP * response (its own headers followed by the HTML body). Strip both header blocks. */ private function extractHtmlFromWarcSegment(string $gzipped): ?string { $decompressed = @gzdecode($gzipped); if ($decompressed === false) { return null; } [$warcHeaders, $rest] = array_pad(preg_split("/\r?\n\r?\n/", $decompressed, 2), 2, null); if ($rest === null) { return null; } [$httpHeaders, $html] = array_pad(preg_split("/\r?\n\r?\n/", $rest, 2), 2, null); return $html; } private function extractHtmlMeta(string $html): array { $meta = ['title' => null, 'description' => null, 'keywords' => null, 'content' => null]; $document = new DOMDocument; libxml_use_internal_errors(true); $document->loadHTML(''.$html, LIBXML_NOERROR | LIBXML_NOWARNING); libxml_clear_errors(); $xpath = new DOMXPath($document); $titleNode = $xpath->query('//title')->item(0); if ($titleNode) { $meta['title'] = trim(preg_replace('/\s+/', ' ', $titleNode->textContent)) ?: null; } foreach (['description', 'keywords'] as $name) { $node = $xpath->query("//meta[translate(@name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='{$name}']")->item(0); if ($node && $node->getAttribute('content') !== '') { $meta[$name] = trim(preg_replace('/\s+/', ' ', $node->getAttribute('content'))); } } $meta['content'] = $this->extractVisibleText($xpath); return $meta; } /** * Pull the page's visible body text (for later keyword/product search — e.g. "does * this site sell honey?"), stripped of script/style/noscript content and collapsed * whitespace, capped at MAX_CONTENT_LENGTH so a single page can't blow up storage. */ private function extractVisibleText(DOMXPath $xpath): ?string { foreach ($xpath->query('//script | //style | //noscript') as $node) { $node->parentNode?->removeChild($node); } $body = $xpath->query('//body')->item(0); if (! $body) { return null; } $text = trim(preg_replace('/\s+/', ' ', $body->textContent)); return $text === '' ? null : mb_substr($text, 0, self::MAX_CONTENT_LENGTH); } }