Last active
October 25, 2020 15:28
-
-
Save BenMorel/19499bf5c918cd0ea0f0b3f27a5a8254 to your computer and use it in GitHub Desktop.
Adds "PECL <package name>" in front of versions in php.net docs changelogs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adds "PECL <package name>" in front of versions in php.net docs changelogs. | |
* Used for https://github.com/php/doc-en/pull/162 | |
*/ | |
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/reference')); | |
foreach ($files as $file) { | |
$file = (string) $file; | |
if (substr($file, -4) !== '.xml') { | |
continue; | |
} | |
if (preg_match('#/reference/([^/]+)/#', $file, $matches) !== 1) { | |
die("Incorrect file name $file\n"); | |
} | |
$name = $matches[1]; | |
$originalXml = $xml = file_get_contents($file); | |
preg_match_all('#<(?:section|refsect1) role="changelog">.+?</(?:section|refsect1)>#s', $xml, $matches); | |
$changelogSections = $matches[0]; | |
foreach ($changelogSections as $changelogSection) { | |
if (preg_match('#<tbody[^>]*>.+?</tbody>#s', $changelogSection, $matches) !== 1) { | |
die("Could not find tbody inside changelog in $file\n"); | |
} | |
$tbody = $matches[0]; | |
// locate the first <entry> inside each <row>, which contains the version number | |
$newTbody = preg_replace_callback('#(<row>\s+<entry>)([^<]+?)(</entry>)#s', function (array $matches) use ($file, $name) { | |
[$all, $start, $version, $end] = $matches; | |
if (preg_match('/^[a-z]+/i', $version) === 1) { | |
// versions starts with a string: PECL, ibm_db2, dbase, ... | |
// already properly identified | |
return $all; | |
} | |
if (preg_match('/^[0-9]+/', $version) !== 1) { | |
die("Unexpected character in version string $version in $file\n"); | |
} | |
$majorVersion = (int) $version[0]; | |
if ($majorVersion >= 4) { | |
// most likely a PHP version; we can't know for sure, | |
// but this script isn't meant to be exhaustive anyway | |
return $all; | |
} | |
return $start . 'PECL ' . $name . ' ' . $version . $end; | |
}, $tbody); | |
$newChangelogSection = str_replace($tbody, $newTbody, $changelogSection); | |
$xml = str_replace($changelogSection, $newChangelogSection, $xml); | |
} | |
if ($xml !== $originalXml) { | |
echo "Writing $file\n"; | |
file_put_contents($file, $xml); | |
} | |
} | |
echo "Done.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment