urls = $urls; $this->hash_key = $hash_key; } public function extract( string $xml, string $document_url ): SitemapExtraction { $bytes = strlen( $xml ); $document = $this->urls->evaluate_sitemap( $document_url ); if ( 'accepted' !== $document['state'] || ! is_string( $document['url'] ) ) { return $this->not_tested( $bytes, 'invalid_sitemap_url' ); } if ( $bytes > self::MAX_XML_BYTES ) { return $this->not_tested( $bytes, 'sitemap_budget_exceeded' ); } if ( false !== stripos( $xml, 'not_tested( $bytes, 'sitemap_doctype_rejected' ); } if ( ! class_exists( DOMDocument::class ) || ! class_exists( DOMXPath::class ) ) { return $this->not_tested( $bytes, 'dom_extension_unavailable' ); } $previous = libxml_use_internal_errors( true ); $dom = null; try { $dom = new DOMDocument(); $loaded = $dom->loadXML( $xml, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_COMPACT ); $xpath = $loaded ? new DOMXPath( $dom ) : null; } catch ( Throwable $exception ) { $xpath = null; } finally { libxml_clear_errors(); libxml_use_internal_errors( $previous ); } if ( ! $xpath instanceof DOMXPath || ! $dom instanceof DOMDocument ) { return $this->not_tested( $bytes, 'sitemap_parse_failed' ); } $root = $this->document_element( $dom ); if ( ! $root instanceof DOMElement ) { return $this->not_tested( $bytes, 'sitemap_parse_failed' ); } $type = strtolower( $this->local_name( $root ) ); if ( ! in_array( $type, array( 'urlset', 'sitemapindex' ), true ) ) { return $this->not_tested( $bytes, 'unsupported_sitemap_root' ); } $query = 'urlset' === $type ? '/*[local-name()="urlset"]/*[local-name()="url"]/*[local-name()="loc"]' : '/*[local-name()="sitemapindex"]/*[local-name()="sitemap"]/*[local-name()="loc"]'; $nodes = $xpath->query( $query ); if ( ! $nodes instanceof DOMNodeList ) { return $this->not_tested( $bytes, 'sitemap_query_failed' ); } $accepted = 0; $skipped = 0; $urls = array(); $index = 0; foreach ( $nodes as $node ) { if ( ++$index > self::MAX_LOCATIONS ) { break; } if ( ! $node instanceof DOMNode ) { ++$skipped; continue; } $candidate = trim( $this->node_text( $node ) ); $decision = 'urlset' === $type ? $this->urls->evaluate( $candidate ) : $this->urls->evaluate_sitemap( $candidate, $document['url'] ); if ( 'accepted' === $decision['state'] && is_string( $decision['url'] ) ) { ++$accepted; $urls[ $decision['url'] ] = true; } else { ++$skipped; } } $runtime_urls = array_keys( $urls ); $signals = array( 'state' => 'observed', 'type' => $type, 'byte_count' => $bytes, 'document_reference' => $this->reference( $document['url'] ), 'entry_count' => $nodes->length, 'accepted' => $accepted, 'unique_accepted' => count( $runtime_urls ), 'skipped' => $skipped, 'truncated' => $nodes->length > self::MAX_LOCATIONS, 'references' => array_map( fn ( string $url ): string => $this->reference( $url ), $runtime_urls ), 'limitations' => array( 'raw_content_not_retained', 'queryless_same_scope_only' ), ); return 'urlset' === $type ? new SitemapExtraction( $signals, $runtime_urls, array() ) : new SitemapExtraction( $signals, array(), $runtime_urls ); } private function reference( string $value ): string { return hash_hmac( 'sha256', $value, $this->hash_key ); } private function node_text( DOMNode $node ): string { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Native DOM API property. return $node->textContent; } private function local_name( DOMElement $element ): string { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Native DOM API property. return $element->localName; } private function document_element( DOMDocument $document ): ?DOMElement { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Native DOM API property. return $document->documentElement; } private function not_tested( int $bytes, string $reason ): SitemapExtraction { return new SitemapExtraction( array( 'state' => 'not_tested', 'byte_count' => $bytes, 'reason' => $reason, 'limitations' => array( 'raw_content_not_retained', 'sitemap_preconditions_not_met' ), ), array(), array() ); } }