#!/usr/bin/env python3 """Generate a WordPress + PHP test matrix for GitHub Actions. WordPress versions (from wordpress.org API + release zip probing): - Latest release branch: min, middle, and max patch versions - Second-latest release branch: last two patch versions PHP versions (parsed from the WordPress PHP compatibility handbook table): - For each WordPress version, run lowest and highest supported PHP """ import json import re import urllib.error import urllib.request VERSIONS_API_URL = "https://api.wordpress.org/core/version-check/1.7/" RELEASE_URL = "https://wordpress.org/wordpress-{version}.tar.gz" HANDBOOK_URL = ( "https://make.wordpress.org/core/handbook/references/" "php-compatibility-and-wordpress-versions/" ) CHART_ID = "supported-version-chart" OLDER_SECTION_ID = "older-wordpress-versions" def branch_sort_key(branch: str): major, minor = branch.split(".") return int(major), int(minor) def version_tuple(v: str) -> tuple[int, ...]: return tuple(int(part) for part in v.split(".") if part.isdigit()) def major_minor(v: str) -> str: parts = v.split(".") return f"{parts[0]}.{parts[1]}" def fetch_offers(): with urllib.request.urlopen(VERSIONS_API_URL) as response: return json.load(response).get("offers", []) def latest_version_per_branch(offers) -> dict[str, str]: """API offers only include the current patch per branch.""" latest: dict[str, tuple[int, str]] = {} for offer in offers: version = offer.get("version", "") if not version or version.count(".") < 1: continue parts = version.split(".") branch = ".".join(parts[:2]) patch = int(parts[2]) if len(parts) > 2 and parts[2].isdigit() else 0 current = latest.get(branch) if current is None or patch > current[0]: latest[branch] = (patch, version) return {branch: version for branch, (_, version) in latest.items()} def release_exists(version: str) -> bool: request = urllib.request.Request( RELEASE_URL.format(version=version), method="HEAD", ) try: with urllib.request.urlopen(request, timeout=15) as response: return response.status == 200 except urllib.error.HTTPError as error: return error.code == 200 except urllib.error.URLError: return False def discover_versions_for_branch(branch: str, latest: str) -> list[str]: """Discover published .tar.gz releases from wordpress.org (API has latest only).""" parts = latest.split(".") if len(parts) == 2: return [latest] if release_exists(latest) else [] max_patch = int(parts[2]) versions = [] for patch in range(max_patch + 1): version = f"{branch}.{patch}" if release_exists(version): versions.append(version) return versions def select_min_middle_max(versions: list[str]) -> list[str]: """Pick three patch versions: earliest, middle, and latest.""" if not versions: return [] if len(versions) == 1: return [versions[0]] if len(versions) == 2: return [versions[0], versions[0], versions[1]] return [versions[0], versions[len(versions) // 2], versions[-1]] def select_last_two(versions: list[str]) -> list[str]: if len(versions) >= 2: return versions[-2:] return versions def strip_html(cell_html: str) -> str: text = re.sub(r"<[^>]+>", "", cell_html) return re.sub(r"\s+", " ", text).strip() def parse_php_column(header_html: str) -> str | None: text = strip_html(header_html) if not text or "WP / PHP" in text: return None match = re.match(r"^(\d+\.\d+)", text) return match.group(1) if match else None def parse_wp_row_label(cell_html: str) -> str | None: text = strip_html(cell_html) match = re.match(r"^(\d+\.\d+)", text) return match.group(1) if match else None def is_supported(cell_html: str) -> bool: """Only full support (Y). Beta (Y*) is excluded.""" text = strip_html(cell_html).upper() return text == "Y" def extract_primary_compatibility_table(html: str) -> str: """Match: #supported-version-chart -> next table in the page.""" anchor = f'id="{CHART_ID}"' start = html.find(anchor) if start == -1: raise ValueError(f'Could not find #{CHART_ID} on {HANDBOOK_URL}') end = html.find(f'id="{OLDER_SECTION_ID}"', start) section = html[start:end] if end != -1 else html[start:] table_start = section.find("